Skip to content

Commit 619af6a

Browse files
committed
FEAT: Implment Wrappers of MPI_COMM_FREE
1 parent b0512bf commit 619af6a

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/mpi.f90

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ module mpi
7171
module procedure MPI_Comm_dup_proc
7272
end interface MPI_Comm_dup
7373

74+
interface MPi_Comm_free
75+
module procedure MPI_Comm_free_proc
76+
end interface MPI_Comm_free
77+
7478
interface MPI_Bcast
7579
module procedure MPI_Bcast_int_scalar
7680
module procedure MPI_Bcast_real_2D
@@ -435,7 +439,29 @@ subroutine MPI_Comm_dup_proc(comm, newcomm, ierror)
435439
print *, "MPI_Comm_dup failed with error code: ", local_ierr
436440
end if
437441
end if
438-
end subroutine
442+
end subroutine MPI_Comm_dup_proc
443+
444+
subroutine MPI_Comm_free_proc(comm, ierror)
445+
use mpi_c_bindings, only: c_mpi_comm_free, c_mpi_comm_f2c
446+
integer, intent(inout) :: comm
447+
integer, optional, intent(out) :: ierror
448+
integer(kind=MPI_HANDLE_KIND) :: c_comm
449+
integer :: local_ierr
450+
451+
c_comm = handle_mpi_comm_f2c(comm)
452+
local_ierr = c_mpi_comm_free(c_comm)
453+
comm = handle_mpi_comm_c2f(c_comm)
454+
455+
if (present(ierror)) then
456+
ierror = local_ierr
457+
else
458+
if (local_ierr /= MPI_SUCCESS) then
459+
print *, "MPI_Comm_free failed with error code: ", local_ierr
460+
end if
461+
end if
462+
463+
comm = MPI_COMM_NULL ! Set to null after freeing
464+
end subroutine MPI_Comm_free_proc
439465

440466
subroutine MPI_Bcast_int_scalar(buffer, count, datatype, root, comm, ierror)
441467
use mpi_c_bindings, only: c_mpi_bcast

src/mpi_c_bindings.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,5 +356,11 @@ function c_mpi_comm_create(comm, group, newcomm) bind(C, name="MPI_Comm_create")
356356
integer(c_int) :: c_mpi_comm_create
357357
end function c_mpi_comm_create
358358

359+
function c_mpi_comm_free(comm) bind(C, name="MPI_Comm_free")
360+
use iso_c_binding, only: c_ptr, c_int
361+
integer(kind=MPI_HANDLE_KIND), intent(inout) :: comm
362+
integer(c_int) :: c_mpi_comm_free
363+
end function c_mpi_comm_free
364+
359365
end interface
360366
end module mpi_c_bindings

tests/comm_free_1.f90

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
program mre_comm_free
2+
use mpi
3+
implicit none
4+
5+
integer :: ierr, rank, size
6+
integer :: dup_comm, dup_rank, dup_size
7+
8+
call MPI_INIT(ierr)
9+
if (ierr /= MPI_SUCCESS) error stop "MPI_INIT failed"
10+
11+
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
12+
call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
13+
14+
! Create a duplicate communicator (all processes participate)
15+
call MPI_COMM_DUP(MPI_COMM_WORLD, dup_comm, ierr)
16+
if (ierr /= MPI_SUCCESS) error stop "MPI_COMM_DUP failed"
17+
18+
! Use the new communicator
19+
call MPI_COMM_RANK(dup_comm, dup_rank, ierr)
20+
call MPI_COMM_SIZE(dup_comm, dup_size, ierr)
21+
22+
print *, 'Original Rank:', rank, ' -> Duplicate Rank:', dup_rank, &
23+
' / Size:', dup_size
24+
25+
! Free the duplicated communicator
26+
call MPI_COMM_FREE(dup_comm, ierr)
27+
if (ierr /= MPI_SUCCESS) error stop "MPI_COMM_FREE failed"
28+
29+
print *, 'Rank', rank, 'successfully freed dup_comm.'
30+
31+
call MPI_FINALIZE(ierr)
32+
end program mre_comm_free

0 commit comments

Comments
 (0)