Skip to content

Conversation

dssgabriel
Copy link
Collaborator

@dssgabriel dssgabriel commented Oct 2, 2025

This PR closes #173 by changing the implementation of Req<Mpi>::wait_any to actually complete at least one request (and calls any registered callbacks on that request) before returning.

Also includes:

  • Changed wait to take KokkosComm::Req<Mpi> by reference to avoid a potentially costly deep copy of the postWaits_ callback vector
  • Added an overload on wait taking an rvalue-reference so that writing the following remains valid:
    KokkosComm::wait(KokkosComm::send(handle, view, peer));
  • Modernized wait_all and wait_any interfaces to use std::span instead of std::vector& references

@dssgabriel dssgabriel added C-enhancement Category: an enhancement or bug fix A-mpi Area: KokkosComm MPI backend implementation labels Oct 2, 2025
Comment on lines 92 to 100
while (completed != 0) {
for (Req<Mpi> &req : reqs) {
int flag;
MPI_Test(&(req.mpi_request()), &flag, MPI_STATUS_IGNORE);
if (flag) {
completed++;
wait(req);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is semantically correct now but I am not really fond of the active wait. Can't we build a MPI_REQUEST array and use MPI_Waitany?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I think we should do the same for wait_all in that case 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we go about calling the registered callbacks on the requests that have completed, though?

Copy link
Collaborator

@cwpearson cwpearson Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MPI_Waitany provides the index of the thing that completed, so I think you'd just have to go back and do the callbacks of our associated request.

Closes kokkos#173: change the implementation of `Req<Mpi>::wait_any` to
actually complete at least one request (and calls any registered
callbacks on that request) before returning.
@dssgabriel
Copy link
Collaborator Author

I changed the implementation to match the MPI_Waitany semantics, i.e., complete one request and exit, as opposed to what I had before, which was more similar to MPI_Waitsome (which may complete multiple requests before returning).

I think we can merge this as it is, and I'll do a follow-up PR that updates our wait_all and wait_any implementation to extract arrays of MPI_Requests and directly call the MPI functions.

for (Req<Mpi> &req : reqs) {
int flag;
MPI_Test(&(req.mpi_request()), &flag, MPI_STATUS_IGNORE);
if (flag) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for MPI_REQUEST_NULL?
If not, we can end-up with an infinite loop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take a look at what the standard says and fix this accordingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A call to MPI_TEST returns flag = true if the operation identified by request is complete. In such a case, the status object is set to contain information on the completed operation; if the communication object was created by a nonblocking send or receive, then it is deallocated and the request handle is set to MPI_REQUEST_NULL. The call returns flag = false, otherwise. In this case, the value of the status object is undefined. MPI_TEST is a local operation.

I am not an MPI expert but my understanding is that flag = false if the input is MPI_REQUEST_NULL as it cannot be completed.

Co-authored-by: Cédric Chevalier <cedric.chevalier019@proton.me>
@cwpearson cwpearson added the SNL-CI-APPROVAL Required to run SNL CI on non-SNL contributions label Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mpi Area: KokkosComm MPI backend implementation C-enhancement Category: an enhancement or bug fix SNL-CI-APPROVAL Required to run SNL CI on non-SNL contributions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mpi: wait_any may return without completing at least one request
3 participants