I need to make a program that uses MPI non-blocking send and receive calls to communication with a partner task. The partner task is task rank + numberOfTask/2 and vice versa. The message being sent to the partner is the senders rank.
I've tried doing this:
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define MASTER 0
int main (int argc, char *argv[]) {
int i, numtasks, taskid, len, partner, message;
char hostname[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Request reqsTx[numtasks], reqsRx[numtasks];
MPI_Status stats[numtasks];
/* need an even number of tasks */
if (numtasks % 2 != 0) {
if (taskid == MASTER)
printf("Quitting. Need an even number of tasks: numtasks=%d\n", numtasks);
} else {
if (taskid == MASTER)
printf("MASTER: Number of MPI tasks is: %d\n", numtasks);
MPI_Get_processor_name(hostname, &len);
printf ("Hello from task %d\n", taskid);
/* determine partner and then send to partner */
if (taskid < numtasks/2) {
partner = numtasks/2 + taskid;
MPI_Isend(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsTx[taskid]);
} else if (taskid >= numtasks/2) {
partner = taskid - numtasks/2;
MPI_Isend(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsTx[taskid]);
}
printf ("Send %d completed\n", taskid);
MPI_Waitall(numtasks, reqsTx, stats);
printf("Done waiting for transmit!");
/* determine partner and then receive from partner */
if (taskid < numtasks/2) {
MPI_Irecv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsRx[taskid]);
} else if (taskid >= numtasks/2) {
MPI_Irecv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &reqsRx[taskid]);
}
printf ("Receive %d completed\n", taskid);
MPI_Waitall(numtasks, reqsRx, stats);
printf("Done waiting for receive!");
/* print partner info and exit*/
printf("Task %d is partner with %d\n", taskid, message);
}
MPI_Finalize();
}
But the first MPI_Waitall(numtasks, reqsTx, stats); fails and the program terminates. Anyone knows what I'm doing wrong here?
Best regards