-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmpi_isendrecv.cpp
More file actions
48 lines (38 loc) · 1.2 KB
/
mpi_isendrecv.cpp
File metadata and controls
48 lines (38 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <faabric/mpi/mpi.h>
#include <stdio.h>
namespace tests::mpi {
int iSendRecv()
{
MPI_Init(NULL, NULL);
int rank;
int worldSize;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
// Send and receive messages asynchronously in a ring
int right = (rank + 1) % worldSize;
int maxRank = worldSize - 1;
int left = rank > 0 ? rank - 1 : maxRank;
// Asynchronously receive from the left
int recvValue = -1;
MPI_Request recvRequest;
MPI_Irecv(&recvValue, 1, MPI_INT, left, 0, MPI_COMM_WORLD, &recvRequest);
// Asynchronously send to the right
int sendValue = rank;
MPI_Request sendRequest;
MPI_Isend(&sendValue, 1, MPI_INT, right, 0, MPI_COMM_WORLD, &sendRequest);
// Wait for both
MPI_Wait(&recvRequest, MPI_STATUS_IGNORE);
MPI_Wait(&sendRequest, MPI_STATUS_IGNORE);
// Check the received value is as expected
if (recvValue != left) {
printf("Rank %i - async not working properly (got %i expected %i)\n",
rank,
recvValue,
left);
return 1;
}
printf("Rank %i - async working properly\n", rank);
MPI_Finalize();
return 0;
}
}