-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathskeleton.cpp
72 lines (53 loc) · 1.67 KB
/
skeleton.cpp
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
63
64
65
66
67
68
69
70
71
72
#include <cstdio>
#include <vector>
#include <mpi.h>
void print_ordered(double t);
int main(int argc, char *argv[])
{
int i, myid, ntasks;
constexpr int size = 10000000;
MPI_Status status;
double t0, t1;
int source, destination;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
// Initialize message
std::vector<int> message(size, myid);
std::vector<int> receiveBuffer(size);
// TODO: create a cartesian communicator
// and determine the source and destination ranks
// with the help of MPI_Cart_shift
// end TODO
// Start measuring the time spent in communication
MPI_Barrier(MPI_COMM_WORLD);
t0 = MPI_Wtime();
// TODO: Send messages
printf("Sender: %d. Sent elements: %d. Tag: %d. Receiver: %d\n",
myid, size, myid + 1, destination);
// TODO: Receive messages
printf("Receiver: %d. first element %d.\n",
myid, receiveBuffer[0]);
// Finalize measuring the time and print it out
t1 = MPI_Wtime();
MPI_Barrier(MPI_COMM_WORLD);
fflush(stdout);
print_ordered(t1 - t0);
MPI_Finalize();
return 0;
}
void print_ordered(double t)
{
int i, rank, ntasks;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
if (rank == 0) {
printf("Time elapsed in rank %2d: %6.3f\n", rank, t);
for (i = 1; i < ntasks; i++) {
MPI_Recv(&t, 1, MPI_DOUBLE, i, 11, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Time elapsed in rank %2d: %6.3f\n", i, t);
}
} else {
MPI_Send(&t, 1, MPI_DOUBLE, 0, 11, MPI_COMM_WORLD);
}
}