Skip to content

Remove C-Wrapper for MPI_Init #16

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 21, 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
21 changes: 12 additions & 9 deletions src/mpi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,21 @@ module mpi

subroutine MPI_Init_proc(ierr)
use mpi_c_bindings, only: c_mpi_init
use iso_c_binding, only : c_int
use iso_c_binding, only : c_int, c_ptr, c_null_ptr
integer, optional, intent(out) :: ierr
integer :: local_ierr
integer(c_int) :: local_ierr
integer(c_int) :: argc
type(c_ptr) :: argv = c_null_ptr
argc = 0
! Call C MPI_Init directly with argc=0, argv=NULL
local_ierr = c_mpi_init(argc, argv)

if (present(ierr)) then
call c_mpi_init(ierr)
else
call c_mpi_init(local_ierr)
if (local_ierr /= 0) then
print *, "MPI_Init failed with error code: ", local_ierr
end if
ierr = int(local_ierr)
else if (local_ierr /= 0) then
print *, "MPI_Init failed with error code: ", local_ierr
end if
end subroutine
end subroutine MPI_Init_proc

subroutine MPI_Init_thread_proc(required, provided, ierr)
use mpi_c_bindings, only : c_mpi_init_thread
Expand Down
13 changes: 9 additions & 4 deletions src/mpi_c_bindings.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ module mpi_c_bindings
implicit none

interface
subroutine c_mpi_init(ierr) bind(C, name="mpi_init_wrapper")
use iso_c_binding, only: c_int
integer(c_int), intent(out) :: ierr
end subroutine c_mpi_init
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
!> as 'intent(inout)'? Though, currently LFortran
!> errors with this
integer(c_int) :: argc
type(c_ptr) :: argv
integer(c_int) :: c_mpi_init
end function c_mpi_init

subroutine c_mpi_init_thread(required, provided, ierr) bind(C, name="mpi_init_thread_wrapper")
use iso_c_binding, only: c_int
Expand Down
10 changes: 5 additions & 5 deletions src/mpi_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

#define MPI_STATUS_SIZE 5

void mpi_init_wrapper(int *ierr) {
int argc = 0;
char **argv = NULL;
*ierr = MPI_Init(&argc, &argv);
}
// void mpi_init_wrapper(int *ierr) {
// int argc = 0;
// char **argv = NULL;
// *ierr = MPI_Init(&argc, &argv);
// }

void mpi_init_thread_wrapper(int *required, int *provided, int *ierr) {
int argc = 0;
Expand Down
36 changes: 36 additions & 0 deletions tests/init_1.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
program test_mpi_init_rank_size
use mpi
implicit none
integer :: ierr, rank, size

! Initialize MPI
call MPI_Init(ierr)
if (ierr /= MPI_SUCCESS) then
print *, "MPI_Init failed with error code: ", ierr
stop 1
end if

! Get rank and size
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
if (ierr /= MPI_SUCCESS) then
print *, "MPI_Comm_rank failed with error code: ", ierr
call MPI_Finalize(ierr)
stop 1
end if

call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)
if (ierr /= MPI_SUCCESS) then
print *, "MPI_Comm_size failed with error code: ", ierr
call MPI_Finalize(ierr)
stop 1
end if

print *, "Hello from rank ", rank, " of ", size

! Finalize MPI
call MPI_Finalize(ierr)
if (ierr /= MPI_SUCCESS) then
print *, "MPI_Finalize failed with error code: ", ierr
stop 1
end if
end program test_mpi_init_rank_size