-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
mpi_many_msgs.c
94 lines (82 loc) · 2.42 KB
/
mpi_many_msgs.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
/* File: mpi_many_msgs.c
* Purpose: Compare the time needed to send n messages consisting of
* a single double each with the time needed to send one message
* consisting of n doubles.
*
* Compile: mpicc -g -Wall -O2 -o mpi_many_msgs mpi_many_msgs.c
* Run: mpiexec -n 2 mpi_many_msgs <number of doubles>
*
* Input: none
* Output: Elapsed time for n messages of size 1 double and elapsed
* time for 1 message of n doubles
*
* IPP: Section 3.5 (pp. 116 and ff.)
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int my_rank;
int comm_sz;
MPI_Comm comm;
void Get_arg(int argc, char* argv[], int* n_p);
int main(int argc, char* argv[]) {
double* x;
int i, n;
MPI_Status status;
double start, finish;
MPI_Init(&argc, &argv);
comm = MPI_COMM_WORLD;
MPI_Comm_size(comm, &comm_sz);
MPI_Comm_rank(comm, &my_rank);
Get_arg(argc, argv, &n);
x = malloc(n*sizeof(double));
if (my_rank == 0)
for (i = 0; i < n; i++) x[i] = i;
else /* my_rank == 1 */
for (i = 0; i < n; i++) x[i] = -1;
MPI_Barrier(comm);
start = MPI_Wtime();
if (my_rank == 0)
for (i = 0; i < n; i++)
MPI_Send(&x[i], 1, MPI_DOUBLE, 1, 0, comm);
else /* my_rank == 1 */
for (i = 0; i < n; i++)
MPI_Recv(&x[i], 1, MPI_DOUBLE, 0, 0, comm, &status);
finish = MPI_Wtime();
printf("Proc %d > First comm took %e seconds\n", my_rank,
finish-start);
fflush(stdout);
MPI_Barrier(comm);
start = MPI_Wtime();
if (my_rank == 0)
MPI_Send(x, n, MPI_DOUBLE, 1, 0, comm);
else /* my_rank == 1 */
MPI_Recv(x, n, MPI_DOUBLE, 0, 0, comm, &status);
finish = MPI_Wtime();
printf("Proc %d > Second comm took %e seconds\n", my_rank,
finish-start);
fflush(stdout);
free(x);
MPI_Finalize();
return 0;
} /* main */
/*-------------------------------------------------------------------*/
void Get_arg(
int argc /* in */,
char* argv[] /* out */,
int* n_p /* out */) {
if (my_rank == 0) {
if (argc != 2 || comm_sz != 2) {
fprintf(stderr, "usage: mpiexec -n 2 %s <number of doubles>\n",
argv[0]);
*n_p = 0;
} else {
*n_p = strtol(argv[1], NULL, 10);
}
}
MPI_Bcast(n_p, 1, MPI_INT, 0, comm);
if (*n_p <= 0 || comm_sz != 2) {
MPI_Finalize();
exit(0);
}
} /* Get_arg */