In MPI it is possible to run an asynchronous message passing routine (e.g. receive, with MPI_Irecv
). Is it possible to attach a callback function to be executed as soon as the request is complete? For example to process the data received.
This is an example of what I am looking for:
#include "mpi.h"
#include <stdio.h>
void mycallback(void* data){
(int*)data += 1; // add one to the received data
}
int main(int argc, char *argv[]){
int myid, numprocs, left, right;
int buffer[10], buffer2[10];
MPI_Request request;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
right = (myid + 1) % numprocs;
left = myid - 1;
if (left < 0)
left = numprocs - 1;
MPI_Irecv(buffer, 10, MPI_INT, left, 123, MPI_COMM_WORLD, &request);
// Attach_Callback(request, &mycallback); //somewhere after this point recv is completed an f is executed
MPI_Send(buffer2, 10, MPI_INT, right, 123, MPI_COMM_WORLD);
MPI_Wait(&request, &status); //the recv and the callback must have been called at this point
MPI_Finalize();
return 0;
}
I found that there is a MPI_Grequest_start
and MPI_Grequest_complete
function but they seem to be for something else as the request created is not related to a particular message passing.
Perhaps I have to implement a Grequest (generalized request) where the callback consists in a MPI_Recv
(not MPI_Irecv
). Is that the idea?
mycallback
after the MPI_Wait? the Send will only start once the Recv has finished (but request will be marked complete as soon as that's done) ... You may have to change the MPI_Send toMPI_Isend
– HeterogeneousMPI_Wait
while could have been executed before using the MPI thread itself. – LorentzMPI_Irecv + "tasks after receive"
? I don't quite understand in which of the three function I should put the completion code. – Lorentz