/*
!===============================================================================
!
! bgl_cart_comm_create.c - read Blue Gene torus topology
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2010 Hinnerk Stueben
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! BQCD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
*/

#ifndef NONMPI
# include <mpi.h>
#endif

#ifdef IBM
# include <mpix.h>

#include <common/bgp_personality.h>
#include <common/bgp_personality_inlines.h>

int bgl_cart_comm_create(int *size, int *ndims, int *dims, int *periods, int *coords)
{
  MPI_Comm comm;

  if (MPIX_Cart_comm_create(&comm) != MPI_SUCCESS)  return 1;

  MPI_Comm_size(comm, size);
  MPI_Cartdim_get(comm, ndims);
  MPI_Cart_get(comm, 4, dims, periods, coords);

  return 0;
}

// from neighb.c by Thomas 
void get_grid_internalat3(int* g)
{
  _BGP_Personality_t personality;
  unsigned p_grid[4];
  Kernel_GetPersonality(&personality, sizeof(personality));

  switch(personality.Kernel_Config.ProcessConfig)
    {
    case _BGP_PERS_PROCESSCONFIG_SMP:
      p_grid[3] = 1;
      break;
    case _BGP_PERS_PROCESSCONFIG_2x2:
      p_grid[3] = 2;
      break;
    case _BGP_PERS_PROCESSCONFIG_VNM:
      p_grid[3] = 4;
      break;
    }
  p_grid[0] = personality.Network_Config.Xnodes;
  p_grid[1] = personality.Network_Config.Ynodes;
  p_grid[2] = personality.Network_Config.Znodes;
  for (unsigned i=0; i<4; ++i)
    g[i] = p_grid[i];

}
#else
  int bgl_cart_comm_create(int *size, int *ndims, int *dims, int *periods, int *coords) { return 1; }
  int bgl_cart_comm_create_(int *size, int *ndims, int *dims, int *periods, int *coords) { return 1; }
  int bgl_cart_comm_create__(int *size, int *ndims, int *dims, int *periods, int *coords) { return 1; }
  void get_grid_internalat3(int* g){}
  void get_grid_internalat3_(int* g){}
  void get_grid_internalat3__(int* g){}
#endif
