Skip to content

Add New MPI_OP-> MPI_MAX #125

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 2 commits into from
May 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
5 changes: 4 additions & 1 deletion src/mpi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module mpi
integer, parameter :: MPI_COMM_WORLD = -1000
real(8), parameter :: MPI_IN_PLACE = -1002
integer, parameter :: MPI_SUM = -2300
integer, parameter :: MPI_MAX = -2301
integer, parameter :: MPI_INFO_NULL = -2000
integer, parameter :: MPI_STATUS_SIZE = 5
integer :: MPI_STATUS_IGNORE = 0
Expand Down Expand Up @@ -123,10 +124,12 @@ module mpi
contains

integer(kind=MPI_HANDLE_KIND) function handle_mpi_op_f2c(op_f) result(c_op)
use mpi_c_bindings, only: c_mpi_op_f2c, c_mpi_sum
use mpi_c_bindings, only: c_mpi_op_f2c, c_mpi_sum, c_mpi_max
integer, intent(in) :: op_f
if (op_f == MPI_SUM) then
c_op = c_mpi_sum
else if (op_f == MPI_MAX) then
c_op = c_MPI_MAX
else
c_op = c_mpi_op_f2c(op_f)
end if
Expand Down
1 change: 1 addition & 0 deletions src/mpi_c_bindings.f90
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module mpi_c_bindings
integer(kind=MPI_HANDLE_KIND), bind(C, name="c_MPI_INT") :: c_mpi_int
integer(kind=MPI_HANDLE_KIND), bind(C, name="c_MPI_COMM_WORLD") :: c_mpi_comm_world
integer(kind=MPI_HANDLE_KIND), bind(C, name="c_MPI_SUM") :: c_mpi_sum
integer(kind=MPI_HANDLE_KIND), bind(C, name="c_MPI_MAX") :: c_mpi_max

interface

Expand Down
2 changes: 2 additions & 0 deletions src/mpi_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ MPI_Datatype c_MPI_INT = MPI_INT;
void* c_MPI_IN_PLACE = MPI_IN_PLACE;

MPI_Op c_MPI_SUM = MPI_SUM;

MPI_Op c_MPI_MAX = MPI_MAX;
37 changes: 37 additions & 0 deletions tests/reduce_max_op_01.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
program reduce_max_1
use mpi
implicit none
integer :: ierr, rank, size, root
integer :: sendbuf, recvbuf
logical :: error

! Initialize MPI
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)

! Root process
root = 0

! Each process sends its rank + 100 as input
sendbuf = rank + 100

! Perform reduction with MPI_MAX
call MPI_Reduce(sendbuf, recvbuf, 1, MPI_INTEGER, MPI_MAX, root, MPI_COMM_WORLD, ierr)

! Verify result on root
error = .false.
if (rank == root) then
if (recvbuf /= size - 1 + 100) then
print *, "Error: Expected max ", size - 1 + 100, ", got ", recvbuf
error = .true.
else
print *, "MPI_Reduce with MPI_MAX test passed: max = ", recvbuf
end if
end if

! Clean up
call MPI_Finalize(ierr)

if (error) stop 1
end program reduce_max_1