From e68dae12c5406a5d7c182cbba2effbe87e64e36d Mon Sep 17 00:00:00 2001 From: "Mark I. Richardson" Date: Sat, 7 Nov 2026 00:29:25 -0700 Subject: [PATCH] Use MPI types that match REAL and INTEGER in wrf_dm_maxval and wrf_dm_minval wrf_dm_maxval_real and wrf_dm_minval_real reduced their (value, rank) pair with MPI_2REAL whatever the size of REAL, so with -r8 the MAXLOC and MINLOC compared the wrong bits and returned one rank's value instead of the domain maximum or minimum. The pair type now follows RWORDSIZE, as getrealmpitype does for single values. The index pair is INTEGER, so all four routines now broadcast it as MPI_INTEGER: the real variants sent it as MPI_REAL (right only because both are 4 bytes) and the doubleprecision variants as MPI_DOUBLE_PRECISION (16 bytes from an 8-byte buffer). A default build does the same as before. --- external/RSL_LITE/module_dm.F | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/external/RSL_LITE/module_dm.F b/external/RSL_LITE/module_dm.F index 578eccc9d9..afed1236f7 100644 --- a/external/RSL_LITE/module_dm.F +++ b/external/RSL_LITE/module_dm.F @@ -1239,6 +1239,28 @@ INTEGER function getrealmpitype() RETURN END FUNCTION getrealmpitype +! internal, used below: the MPI pair type (value, rank) matching a REAL, +! for the MAXLOC and MINLOC reductions, which need a pair type + INTEGER function getreal2mpitype() +#ifndef STUBMPI + IMPLICIT NONE + INTEGER rtypesize, dtypesize, ierr + CALL mpi_type_size ( MPI_REAL, rtypesize, ierr ) + CALL mpi_type_size ( MPI_DOUBLE_PRECISION, dtypesize, ierr ) + IF ( RWORDSIZE .EQ. rtypesize ) THEN + getreal2mpitype = MPI_2REAL + ELSE IF ( RWORDSIZE .EQ. dtypesize ) THEN + getreal2mpitype = MPI_2DOUBLE_PRECISION + ELSE + CALL wrf_error_fatal ( 'RWORDSIZE or DWORDSIZE does not match any MPI type' ) + END IF +#else +! required dummy initialization for function that is never called + getreal2mpitype = 1 +#endif + RETURN + END FUNCTION getreal2mpitype + INTEGER FUNCTION wrf_dm_max_int ( inval ) IMPLICIT NONE #ifndef STUBMPI @@ -1419,11 +1441,11 @@ SUBROUTINE wrf_dm_maxval_real ( val, idex, jdex ) inreduce=(/ val, real(mytask) /) bcast=(/ idex,jdex /) CALL wrf_get_dm_communicator(comm) - call MPI_Allreduce(inreduce,outreduce,1,MPI_2REAL,& + call MPI_Allreduce(inreduce,outreduce,1,getreal2mpitype(),& MPI_MAXLOC,comm,i) mrank=outreduce(2) val=outreduce(1) - call MPI_Bcast(bcast,2,MPI_REAL,mrank,comm,i) + call MPI_Bcast(bcast,2,MPI_INTEGER,mrank,comm,i) idex=bcast(1) jdex=bcast(2) # else @@ -1444,11 +1466,11 @@ SUBROUTINE wrf_dm_minval_real ( val, idex, jdex ) inreduce=(/ val, real(mytask) /) bcast=(/ idex,jdex /) CALL wrf_get_dm_communicator(comm) - call MPI_Allreduce(inreduce,outreduce,1,MPI_2REAL,& + call MPI_Allreduce(inreduce,outreduce,1,getreal2mpitype(),& MPI_MINLOC,comm,i) mrank=outreduce(2) val=outreduce(1) - call MPI_Bcast(bcast,2,MPI_REAL,mrank,comm,i) + call MPI_Bcast(bcast,2,MPI_INTEGER,mrank,comm,i) idex=bcast(1) jdex=bcast(2) # else @@ -1474,7 +1496,7 @@ SUBROUTINE wrf_dm_maxval_doubleprecision ( val, idex, jdex ) MPI_MAXLOC,comm,i) mrank=outreduce(2) val=outreduce(1) - call MPI_Bcast(bcast,2,MPI_DOUBLE_PRECISION,mrank,comm,i) + call MPI_Bcast(bcast,2,MPI_INTEGER,mrank,comm,i) idex=bcast(1) jdex=bcast(2) # else @@ -1499,7 +1521,7 @@ SUBROUTINE wrf_dm_minval_doubleprecision ( val, idex, jdex ) MPI_MINLOC,comm,i) mrank=outreduce(2) val=outreduce(1) - call MPI_Bcast(bcast,2,MPI_DOUBLE_PRECISION,mrank,comm,i) + call MPI_Bcast(bcast,2,MPI_INTEGER,mrank,comm,i) idex=bcast(1) jdex=bcast(2) # else