-
Notifications
You must be signed in to change notification settings - Fork 2
/
05-mpi-soln.c
119 lines (101 loc) · 3.13 KB
/
05-mpi-soln.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "timing.h"
#include <stdio.h>
#include <stdlib.h>
// FIXME kill
#include <mpi.h>
int main(int argc, char **argv)
{
int rank_count, my_rank, worker_count;
// FIXME kill
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &rank_count);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
worker_count = rank_count-1;
if (argc != 3)
{
fprintf(stderr, "need two arguments!\n");
abort();
}
const long n = atol(argv[1]);
const int ntrips = atoi(argv[2]);
// FIXME kill
if (n % worker_count != 0)
{
fprintf(stderr, "size not divisible\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
long divided_n = n / worker_count;
printf("rank %d/%d reporting for duty\n", my_rank, rank_count);
const int tag = 0;
if (my_rank == 0)
{
printf("doing %d trips...\n", ntrips);
double *x = (double *) malloc(sizeof(double) * n);
if (!x) { perror("alloc x"); MPI_Abort(MPI_COMM_WORLD, 1); }
double *y = (double *) malloc(sizeof(double) * n);
if (!y) { perror("alloc y"); MPI_Abort(MPI_COMM_WORLD, 1); }
double *z = (double *) malloc(sizeof(double) * n);
if (!z) { perror("alloc z"); MPI_Abort(MPI_COMM_WORLD, 1); }
for (int i = 0; i < n; ++i)
{
x[i] = i;
y[i] = 2*i;
}
timestamp_type time1, time2;
get_timestamp(&time1);
for (int i = 0; i < worker_count; ++i)
{
printf("before send %d\n", i);
MPI_Send(x + i*divided_n, divided_n, MPI_DOUBLE, i+1, tag, MPI_COMM_WORLD);
MPI_Send(y + i*divided_n, divided_n, MPI_DOUBLE, i+1, tag, MPI_COMM_WORLD);
printf("after send %d\n", i);
}
for (int i = 0; i < worker_count; ++i)
{
printf("before recv %d\n", i);
MPI_Recv(z + i*divided_n, divided_n, MPI_DOUBLE, i+1, tag, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("after recv %d\n", i);
}
get_timestamp(&time2);
double elapsed = timestamp_diff_in_seconds(time1,time2)/ntrips;
printf("%f GB/s\n",
3*n*sizeof(double)/1e9/elapsed);
printf("%f GFlops/s\n",
n/1e9/elapsed);
for (int i = 0; i < n; ++i)
{
if (z[i] != x[i] + y[i])
{
printf("bad %d\n", i);
MPI_Abort(MPI_COMM_WORLD, 1);
}
}
}
else
{
double *xbuf = (double *) malloc(sizeof(double) * divided_n);
if (!xbuf) { perror("alloc xbuf"); MPI_Abort(MPI_COMM_WORLD, 1); }
double *ybuf = (double *) malloc(sizeof(double) * divided_n);
if (!ybuf) { perror("alloc ybuf"); MPI_Abort(MPI_COMM_WORLD, 1); }
double *zbuf = (double *) malloc(sizeof(double) * divided_n);
if (!zbuf) { perror("alloc zbuf"); MPI_Abort(MPI_COMM_WORLD, 1); }
MPI_Recv(xbuf, divided_n, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
MPI_Recv(ybuf, divided_n, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("start rank %d\n", my_rank);
for (int trip = 0; trip < ntrips; ++trip)
{
for (int i = 0; i < divided_n; ++i)
{
zbuf[i] = xbuf[i] + ybuf[i];
}
}
printf("done rank %d\n", my_rank);
MPI_Send(zbuf, divided_n, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD);
printf("done rank %d\n", my_rank);
}
MPI_Finalize();
return 0;
}