Skip to content

remove C MPI wrapper function for MPI_Cart_create #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/mpi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,20 @@ subroutine MPI_Ssend_proc(buf, count, datatype, dest, tag, comm, ierror)
call c_mpi_ssend(buf, count, datatype, dest, tag, comm, ierror)
end subroutine

subroutine MPI_Cart_create_proc(comm, ndims, dims, periods, reorder, newcomm, ierror)
use mpi_c_bindings, only: c_mpi_cart_create
use iso_c_binding, only: c_int
subroutine MPI_Cart_create_proc(comm_old, ndims, dims, periods, reorder, comm_cart, ierror)
use iso_c_binding, only: c_int, c_ptr
use mpi_c_bindings, only: c_mpi_cart_create, c_mpi_comm_f2c, c_mpi_comm_c2f
integer, intent(in) :: ndims, dims(ndims)
logical, intent(in) :: periods(ndims), reorder
integer, intent(in) :: comm
integer, intent(out) :: newcomm
integer, intent(in) :: comm_old
integer, intent(out) :: comm_cart
integer, optional, intent(out) :: ierror
integer(c_int) :: ndims_c, reorder_c, dims_c(ndims), periods_c(ndims)
type(c_ptr) :: c_comm_old
type(c_ptr) :: c_comm_cart
integer(c_int) :: local_ierr

c_comm_old = c_mpi_comm_f2c(comm_old)
ndims_c = ndims
if (reorder) then
reorder_c = 1
Expand All @@ -421,7 +426,16 @@ subroutine MPI_Cart_create_proc(comm, ndims, dims, periods, reorder, newcomm, ie
elsewhere
periods_c = 0
end where
call c_mpi_cart_create(comm, ndims, dims_c, periods_c, reorder_c, newcomm, ierror)
local_ierr = c_mpi_cart_create(c_comm_old, ndims, dims_c, periods_c, reorder_c, c_comm_cart)
comm_cart = c_mpi_comm_c2f(c_comm_cart)

if (present(ierror)) then
ierror = local_ierr
else
if (local_ierr /= MPI_SUCCESS) then
print *, "MPI_Cart_create failed with error code: ", local_ierr
end if
end if
end subroutine

subroutine MPI_Cart_coords_proc(comm, rank, maxdims, coords, ierror)
Expand Down
18 changes: 13 additions & 5 deletions src/mpi_c_bindings.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ function c_mpi_comm_f2c(comm_f) bind(C, name="get_c_comm_from_fortran")
type(c_ptr) :: c_mpi_comm_f2c ! MPI_Comm as pointer
end function c_mpi_comm_f2c

function c_mpi_comm_c2f(comm_c) bind(C, name="MPI_Comm_c2f")
use iso_c_binding, only: c_int, c_ptr
type(c_ptr), value :: comm_c
integer :: c_mpi_comm_c2f
end function

function c_mpi_init(argc, argv) bind(C, name="MPI_Init")
use iso_c_binding, only : c_int, c_ptr
!> TODO: is the intent need to be explicitly specified
Expand Down Expand Up @@ -188,12 +194,14 @@ subroutine c_mpi_ssend(buf, count, datatype, dest, tag, comm, ierror) bind(C, na
integer(c_int), optional, intent(out) :: ierror
end subroutine

subroutine c_mpi_cart_create(comm, ndims, dims, periods, reorder, newcomm, ierror) bind(C, name="mpi_cart_create_wrapper")
use iso_c_binding, only: c_int
integer(c_int), intent(in) :: comm, ndims, reorder
function c_mpi_cart_create(comm_old, ndims, dims, periods, reorder, comm_cart) bind(C, name="MPI_Cart_create")
use iso_c_binding, only: c_int, c_ptr
type(c_ptr), value :: comm_old
integer(c_int), value :: ndims, reorder
integer(c_int), intent(in) :: dims(*), periods(*)
integer(c_int), intent(out) :: newcomm, ierror
end subroutine
type(c_ptr), intent(out) :: comm_cart
integer(c_int) :: c_mpi_cart_create
end function

function c_mpi_cart_coords(comm, rank, maxdims, coords) bind(C, name="MPI_Cart_coords")
use iso_c_binding, only: c_int, c_ptr
Expand Down
7 changes: 0 additions & 7 deletions src/mpi_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,6 @@ void mpi_ssend_wrapper(double *buf, int *count, int *datatype_f, int *dest,
*ierror = MPI_Ssend(buf, *count, datatype, *dest, *tag, comm);
}

void mpi_cart_create_wrapper(int * comm_f, int * ndims, int * dims, int * periods, int * reorder, int * newcomm_f, int * ierror){
MPI_Comm newcomm = MPI_COMM_NULL;
MPI_Comm comm = get_c_comm_from_fortran(*comm_f);
*ierror = MPI_Cart_create(comm, *ndims, dims, periods, *reorder, &newcomm);
*newcomm_f = MPI_Comm_c2f(newcomm);
}

void mpi_cart_shift_wrapper(int * comm_f, int * dir, int * disp, int * rank_source, int * rank_dest, int * ierror)
{
MPI_Comm comm = get_c_comm_from_fortran(*comm_f);
Expand Down