-
Notifications
You must be signed in to change notification settings - Fork 182
/
time-chain.c
101 lines (85 loc) · 2.77 KB
/
time-chain.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char *argv[])
{
int i, rank, ntasks;
int size = 10000000;
int *message;
int *receiveBuffer;
MPI_Status status, statuses[2];
MPI_Request requests[2];
double t0, t1, tmax = 0, time = 0;
int source, destination;
int count;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
/* Allocate message buffers */
message = malloc(sizeof(int) * size);
receiveBuffer = malloc(sizeof(int) * size);
/* Initialize message */
for (i = 0; i < size; i++) {
message[i] = rank;
}
/* Set source and destination ranks */
if (rank < ntasks - 1) {
destination = rank + 1;
} else {
destination = MPI_PROC_NULL;
}
if (rank > 0) {
source = rank - 1;
} else {
source = MPI_PROC_NULL;
}
MPI_Barrier(MPI_COMM_WORLD);
t0 = MPI_Wtime();
/* Send+Recv */
MPI_Recv(receiveBuffer, size, MPI_INT, source, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
MPI_Send(message, size, MPI_INT, destination, rank + 1,
MPI_COMM_WORLD);
t1 = MPI_Wtime() - t0;
MPI_Reduce(&t1, &time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&t1, &tmax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
if (rank == 0) {
time = time / ntasks;
printf(" Send+Recv: avg %6.3f s / max %6.3f s\n", time, tmax);
fflush(stdout);
}
MPI_Barrier(MPI_COMM_WORLD);
t0 = MPI_Wtime();
/* Sendrecv */
MPI_Sendrecv(message, size, MPI_INT, destination, rank + 1,
receiveBuffer, size, MPI_INT, source, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
t1 = MPI_Wtime() - t0;
MPI_Reduce(&t1, &time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&t1, &tmax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
if (rank == 0) {
time = time / ntasks;
printf(" Sendrecv: avg %6.3f s / max %6.3f s\n", time, tmax);
fflush(stdout);
}
MPI_Barrier(MPI_COMM_WORLD);
t0 = MPI_Wtime();
/* Isend+Irecv */
MPI_Irecv(receiveBuffer, size, MPI_INT, source, MPI_ANY_TAG,
MPI_COMM_WORLD, &requests[0]);
MPI_Isend(message, size, MPI_INT, destination, rank + 1,
MPI_COMM_WORLD, &requests[1]);
MPI_Waitall(2, requests, statuses);
t1 = MPI_Wtime() - t0;
MPI_Reduce(&t1, &time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&t1, &tmax, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
if (rank == 0) {
time = time / ntasks;
printf("Isend+Irecv: avg %6.3f s / max %6.3f s\n\n", time, tmax);
fflush(stdout);
}
free(message);
free(receiveBuffer);
MPI_Finalize();
return 0;
}