Skip to content

Commit cd1d4bd

Browse files
authored
Add New MPI_OP-> MPI_MAX (#125)
* Add Test for MPI_Max * Add new MPI_Op-> MPI_MAX
1 parent b54a662 commit cd1d4bd

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/mpi.f90

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module mpi
1616
integer, parameter :: MPI_COMM_WORLD = -1000
1717
real(8), parameter :: MPI_IN_PLACE = -1002
1818
integer, parameter :: MPI_SUM = -2300
19+
integer, parameter :: MPI_MAX = -2301
1920
integer, parameter :: MPI_INFO_NULL = -2000
2021
integer, parameter :: MPI_STATUS_SIZE = 5
2122
integer :: MPI_STATUS_IGNORE = 0
@@ -132,10 +133,12 @@ module mpi
132133
contains
133134

134135
integer(kind=MPI_HANDLE_KIND) function handle_mpi_op_f2c(op_f) result(c_op)
135-
use mpi_c_bindings, only: c_mpi_op_f2c, c_mpi_sum
136+
use mpi_c_bindings, only: c_mpi_op_f2c, c_mpi_sum, c_mpi_max
136137
integer, intent(in) :: op_f
137138
if (op_f == MPI_SUM) then
138139
c_op = c_mpi_sum
140+
else if (op_f == MPI_MAX) then
141+
c_op = c_MPI_MAX
139142
else
140143
c_op = c_mpi_op_f2c(op_f)
141144
end if

src/mpi_c_bindings.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module mpi_c_bindings
1717
integer(kind=MPI_HANDLE_KIND), bind(C, name="c_MPI_INT") :: c_mpi_int
1818
integer(kind=MPI_HANDLE_KIND), bind(C, name="c_MPI_COMM_WORLD") :: c_mpi_comm_world
1919
integer(kind=MPI_HANDLE_KIND), bind(C, name="c_MPI_SUM") :: c_mpi_sum
20+
integer(kind=MPI_HANDLE_KIND), bind(C, name="c_MPI_MAX") :: c_mpi_max
2021

2122
interface
2223

src/mpi_constants.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ MPI_Datatype c_MPI_INT = MPI_INT;
1515
void* c_MPI_IN_PLACE = MPI_IN_PLACE;
1616

1717
MPI_Op c_MPI_SUM = MPI_SUM;
18+
19+
MPI_Op c_MPI_MAX = MPI_MAX;

tests/reduce_max_op_01.f90

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
program reduce_max_1
2+
use mpi
3+
implicit none
4+
integer :: ierr, rank, size, root
5+
integer :: sendbuf, recvbuf
6+
logical :: error
7+
8+
! Initialize MPI
9+
call MPI_Init(ierr)
10+
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
11+
call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)
12+
13+
! Root process
14+
root = 0
15+
16+
! Each process sends its rank + 100 as input
17+
sendbuf = rank + 100
18+
19+
! Perform reduction with MPI_MAX
20+
call MPI_Reduce(sendbuf, recvbuf, 1, MPI_INTEGER, MPI_MAX, root, MPI_COMM_WORLD, ierr)
21+
22+
! Verify result on root
23+
error = .false.
24+
if (rank == root) then
25+
if (recvbuf /= size - 1 + 100) then
26+
print *, "Error: Expected max ", size - 1 + 100, ", got ", recvbuf
27+
error = .true.
28+
else
29+
print *, "MPI_Reduce with MPI_MAX test passed: max = ", recvbuf
30+
end if
31+
end if
32+
33+
! Clean up
34+
call MPI_Finalize(ierr)
35+
36+
if (error) stop 1
37+
end program reduce_max_1

0 commit comments

Comments
 (0)