在Fortran中能否向MPIO子例程传递NULL参数?MPI标准示例疑问
Great questions! Let's break this down clearly, since Fortran's MPI bindings handle "NULL-like" arguments a bit differently from C.
1. Can you pass NULL-like arguments to MPIO subroutines in Fortran?
Fortran doesn’t have a direct equivalent to C’s NULL pointer, but MPI’s Fortran bindings provide specific constants and rules to achieve the same effect when arguments are irrelevant to a process:
- For buffer arguments: When a buffer isn’t needed (e.g., non-root processes in gather operations), you can pass
MPI_BOTTOM—this is an MPI-defined address constant that acts like C’sNULLfor buffer parameters. - For datatype arguments: Use
MPI_DATATYPE_NULLexactly like you would in C when the datatype parameter is meaningless for the current process. - Critical note: You can only use these values for parameters explicitly marked as optional or process-dependent in the MPI standard. Always check the subroutine’s spec before using these "empty" values.
2. Can Fortran replicate the MPI_Gatherv call from MPI 3.1 Example 4.23?
Absolutely! The C example uses NULL for arguments that only matter to the root process. Fortran can mirror this behavior perfectly with MPI’s special constants:
For non-root processes, the equivalent Fortran call would be:
call MPI_GATHERV(lbuf, position, MPI_PACKED, MPI_BOTTOM, 0, 0, MPI_DATATYPE_NULL, root, comm, ierr)
Let’s break down the substitutions for the C NULL arguments:
MPI_BOTTOMreplaces theNULLreceive buffer (non-root processes don’t receive data, so this parameter is irrelevant).- We pass
0for the receive count and displacement arguments—MPI allows this when these values don’t apply to the process. MPI_DATATYPE_NULLmatches the C version’sMPI_DATATYPE_NULLfor the receive datatype parameter.
Just like in the C example, the root process must still provide valid values for all receive-side arguments—only non-root processes can use these "empty" placeholders.
内容的提问来源于stack exchange,提问作者Ray

