Skip to content

Remove C-wrapper for MPI_Barrier #37

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 3 commits 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
20 changes: 17 additions & 3 deletions src/mpi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,25 @@ function MPI_Wtime_proc() result(time)
end function

subroutine MPI_Barrier_proc(comm, ierror)
use mpi_c_bindings, only: c_mpi_barrier
use mpi_c_bindings, only: c_mpi_barrier, c_mpi_comm_f2c
use iso_c_binding, only: c_int, c_ptr
integer, intent(in) :: comm
integer, intent(out), optional :: ierror
call c_mpi_barrier(comm, ierror)
end subroutine
type(c_ptr) :: c_comm
integer(c_int) :: local_ierr

! Convert Fortran handle to C handle
c_comm = c_mpi_comm_f2c(comm)
local_ierr = c_mpi_barrier(c_comm)

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

subroutine MPI_Comm_rank_proc(comm, rank, ierror)
use mpi_c_bindings, only: c_mpi_comm_rank
Expand Down
17 changes: 11 additions & 6 deletions src/mpi_c_bindings.f90
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,17 @@ function c_mpi_wtime() result(time) bind(C, name="MPI_Wtime")
use iso_c_binding, only: c_double
real(c_double) :: time
end function

subroutine c_mpi_barrier(comm, ierror) bind(C, name="mpi_barrier_wrapper")
use iso_c_binding, only: c_int
integer(c_int), intent(in) :: comm
integer(c_int), intent(out), optional :: ierror
end subroutine
function c_mpi_comm_f2c(comm_f) bind(C, name="get_c_comm_from_fortran")
use iso_c_binding, only: c_int, c_ptr
integer(c_int), value :: comm_f
type(c_ptr) :: c_mpi_comm_f2c ! MPI_Comm as pointer
end function c_mpi_comm_f2c

function c_mpi_barrier(comm) bind(C, name="MPI_Barrier")
use iso_c_binding, only: c_ptr, c_int
type(c_ptr), value :: comm ! MPI_Comm as pointer
integer(c_int) :: c_mpi_barrier
end function c_mpi_barrier

subroutine c_mpi_comm_rank(comm, rank, ierror) bind(C, name="mpi_comm_rank_wrapper")
use iso_c_binding, only: c_int
Expand Down
8 changes: 4 additions & 4 deletions src/mpi_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ void mpi_allreduce_wrapper_int(const int *sendbuf, int *recvbuf, int *count,
}
}

void mpi_barrier_wrapper(int *comm_f, int *ierror) {
MPI_Comm comm = get_c_comm_from_fortran(*comm_f);
*ierror = MPI_Barrier(comm);
}
// void mpi_barrier_wrapper(int *comm_f, int *ierror) {
// MPI_Comm comm = MPI_Comm_f2c(*comm_f);
// *ierror = MPI_Barrier(comm);
// }

void mpi_comm_rank_wrapper(int *comm_f, int *rank, int *ierror) {
MPI_Comm comm = get_c_comm_from_fortran(*comm_f);
Expand Down