-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpingpong.cpp
More file actions
63 lines (50 loc) · 1.56 KB
/
pingpong.cpp
File metadata and controls
63 lines (50 loc) · 1.56 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <cstdlib>
#include <mpi.h>
#include <iostream>
double time_pingpong(int proc0, int proc1, long Nrepeat, long Nsize, MPI_Comm comm) {
int rank;
MPI_Comm_rank(comm, &rank);
char* msg = (char*) malloc(Nsize);
for (long i = 0; i < Nsize; i++) msg[i] = 42;
MPI_Barrier(comm);
double tt = MPI_Wtime();
for (long repeat = 0; repeat < Nrepeat; repeat++) {
MPI_Status status;
if (repeat % 2 == 0) { // even iterations
if (rank == proc0)
MPI_Send(msg, Nsize, MPI_CHAR, proc1, repeat, comm);
else if (rank == proc1)
MPI_Recv(msg, Nsize, MPI_CHAR, proc0, repeat, comm, &status);
}
else { // odd iterations
if (rank == proc0)
MPI_Recv(msg, Nsize, MPI_CHAR, proc1, repeat, comm, &status);
else if (rank == proc1)
MPI_Send(msg, Nsize, MPI_CHAR, proc0, repeat, comm);
}
}
tt = MPI_Wtime() - tt;
free(msg);
return tt;
}
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
if (argc < 3) {
printf("Usage: mpirun ./pingpong <process-rank0> <process-rank1>\n");
abort();
}
int proc0 = atoi(argv[1]);
int proc1 = atoi(argv[2]);
int rank;
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Comm_rank(comm, &rank);
long Nrepeat = 1000;
double tt = time_pingpong(proc0, proc1, Nrepeat, 1, comm);
if (!rank) printf("pingpong latency: %e ms\n", tt/Nrepeat * 1000);
Nrepeat = 10000;
long Nsize = 1000000;
tt = time_pingpong(proc0, proc1, Nrepeat, Nsize, comm);
if (!rank) printf("pingpong bandwidth: %e GB/s\n", (Nsize*Nrepeat)/tt/1e9);
MPI_Finalize();
}