You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MPICH Fortran test greqf uses extrastate wrong #7922
which implies that the callee must not modify it. Instead, MPICH captures this argument by reference and modifies it in free_fn. I think that the Fortran subroutine is a transcription of the C function, and that extrastate would usually be a pointer in both cases.
Was the INTENT(IN) attribute from the signature of MPI_Grequest_start? It is a input argument for MPI_Grequest_start, but not for the callbacks. Otherwise, what's the point of extra_state if nobody can update it?
I think that the Fortran subroutine is a transcription of the C function,
You'll discover that was not historically true. Fortran comes before C and the MPI standard was originally designed to work with both Fortran and C natively.
I think that extra_state is a pointer to the state (same as in C). While the state can be modified, the address in memory of the state remains fixed. That explains why extra_state is MPI_Aint, an address.
I think that extra_state is a pointer to the state (same as in C).
I see where you come from. It is a bit ambiguous.
With C, void * can be cast to anything. The actual type is determined by the user code. extra_state passes through MPI as a context. MPI implementation is guaranteed not change extra_state, as the INTENT(IN) signifies. I think that is all what the MPI standard says. How the user code uses extra_state is up to the user. Even in C, if user want to cast an int to void * is a valid usage.
The greqf.f test in MPICH 5.0.1 (directory test/mpi/f77/pt2pt/) contains the line
extrastate = extrastate - 1
This is similar, IMO, as in c that we cast an int to void *, passed to MPI, and the later modify the int value. It will likely cause issue if the modification happens while MPI is holding the context as it will be invalid context if the reference to "context" got changed in-between. However, one can argue that the free callback marks the end of MPI holding this context. Since MPI will no longer hold this context, the user code can change it with no consequence what-so-ever.
I am implementing Fortran MPI bindings mpif based on the new MPI ABI. I am running the MPICH Fortran test suite on these bindings, and this test is failing. The reason is that mpif captures extra_state by value, not by reference, so the change that happens in the test case never makes it back to the caller. Internally, mpif converts extra_state from MPI_Aint to void* and then forwards the call to underlying MPI implementation (e.g. MPICH).
How do you handle the callback? Do you let the underlying MPI directly call the callback as a C function or do you intercept with a proxy and call the Fortran callback in turn? I think you have to do that latter since there is no direct API tell MPI what is the language of the callbacks. If you do the latter, then you should call the user callback by Fortran convention, which is to pass extra_state by reference, right?
Yes, there is a trampoline, and extra_state is passed by reference:
struct grequest_box {
mpif_fortran_procedure query_fn;
mpif_fortran_procedure free_fn;
mpif_fortran_procedure cancel_fn;
// The registered value, copied rather than aliased; see the header
MPI_Aint extra_state;
};
int mpif_grequest_free_trampoline(void *extra_state) {
struct grequest_box *const box = extra_state;
MPI_Fint f_ierror = MPI_SUCCESS;
((fortran_grequest_free_fn)box->free_fn)(&box->extra_state, &f_ierror);
// The last callback this request will make, so the box goes with it
free(box);
return f_ierror;
}
I think you need store the reference of extra_state in box rather than value. Fortran user functions rely on the pass-by-ref convention. The address to your box field is meaningless to Fortran users.
Yes, if I want to use your interpretation of the MPI standard, then I need to capture the address of extra_state (I assume that is what MPICH is doing). However, I did interpret the standard differently when writing this function (and when opening this issue).
Activity
hzhou commentedon Aug 5, 2026
Was the
INTENT(IN)attribute from the signature ofMPI_Grequest_start? It is a input argument forMPI_Grequest_start, but not for the callbacks. Otherwise, what's the point ofextra_stateif nobody can update it?You'll discover that was not historically true. Fortran comes before C and the MPI standard was originally designed to work with both Fortran and C natively.
eschnett commentedon Aug 5, 2026
I think that
extra_stateis a pointer to the state (same as in C). While the state can be modified, the address in memory of the state remains fixed. That explains whyextra_stateisMPI_Aint, an address.hzhou commentedon Aug 5, 2026
I see where you come from. It is a bit ambiguous.
With C,
void *can be cast to anything. The actual type is determined by the user code.extra_statepasses through MPI as a context. MPI implementation is guaranteed not changeextra_state, as theINTENT(IN)signifies. I think that is all what the MPI standard says. How the user code usesextra_stateis up to the user. Even in C, if user want to cast aninttovoid *is a valid usage.This is similar, IMO, as in c that we cast an
inttovoid *, passed to MPI, and the later modify theintvalue. It will likely cause issue if the modification happens while MPI is holding the context as it will be invalid context if the reference to "context" got changed in-between. However, one can argue that the free callback marks the end of MPI holding this context. Since MPI will no longer hold this context, the user code can change it with no consequence what-so-ever.What actual issue are you seeing ?
eschnett commentedon Aug 6, 2026
I am implementing Fortran MPI bindings mpif based on the new MPI ABI. I am running the MPICH Fortran test suite on these bindings, and this test is failing. The reason is that mpif captures
extra_stateby value, not by reference, so the change that happens in the test case never makes it back to the caller. Internally, mpif convertsextra_statefromMPI_Ainttovoid*and then forwards the call to underlying MPI implementation (e.g. MPICH).hzhou commentedon Aug 6, 2026
How do you handle the callback? Do you let the underlying MPI directly call the callback as a C function or do you intercept with a proxy and call the Fortran callback in turn? I think you have to do that latter since there is no direct API tell MPI what is the language of the callbacks. If you do the latter, then you should call the user callback by Fortran convention, which is to pass
extra_stateby reference, right?eschnett commentedon Aug 6, 2026
Yes, there is a trampoline, and
extra_stateis passed by reference:hzhou commentedon Aug 6, 2026
I think you need store the reference of
extra_stateinboxrather than value. Fortran user functions rely on the pass-by-ref convention. The address to your box field is meaningless to Fortran users.eschnett commentedon Aug 6, 2026
Yes, if I want to use your interpretation of the MPI standard, then I need to capture the address of
extra_state(I assume that is what MPICH is doing). However, I did interpret the standard differently when writing this function (and when opening this issue).