-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcol_matrix_lat_indexed.c
278 lines (240 loc) · 8.08 KB
/
col_matrix_lat_indexed.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <mpi.h>
#define TAG_INDEXED 999
#if 0
#define MAX_ELEM 10
#define NB_WARMUP 0
#define NB_LOOPS 1
#else
#define MAX_ELEM 4096
#define NB_WARMUP 1000
#define NB_LOOPS 10000
#endif
#define OPT_INT 0
#define OPT_DOUBLE 1
/*
* code that sends a matrix column
* and measures the time spent
*
* The part that is sent is represented below:
*
* V
[00][00] ................. [00][CC] .................. [00][99]
[01][00] ................. [01][CC] .................. [01][99]
[02][00] ................. [02][CC] .................. [02][99]
...
[97][00] ................. [97][CC] .................. [97][99]
[98][00] ................. [98][CC] .................. [98][99]
[99][00] ................. [99][CC] .................. [99][99]
*/
double snd_matrix_double[MAX_ELEM][MAX_ELEM];
double rcv_matrix_double[MAX_ELEM][MAX_ELEM];
double ref_rcv_matrix_double[MAX_ELEM][MAX_ELEM];
int snd_matrix_int[MAX_ELEM][MAX_ELEM];
int rcv_matrix_int[MAX_ELEM][MAX_ELEM];
int ref_rcv_matrix_int[MAX_ELEM][MAX_ELEM];
void init_matrices(int opt, int col, int blocklen)
{
int i, j;
/*
* Init the matrices
*/
if (opt == OPT_DOUBLE) {
for (i = 0; i < MAX_ELEM; i++) {
for (j = 0; j < MAX_ELEM; j++) {
snd_matrix_double[i][j] = 1.0 * i * MAX_ELEM + j;
rcv_matrix_double[i][j] = -1.0;
ref_rcv_matrix_double[i][j] = -1.0;
}
}
for (i = 0; i < MAX_ELEM; i++) {
for( j = 0; j < blocklen; j++ ) {
snd_matrix_double[i][col+j] = 1.0 * i * MAX_ELEM + col + j;
ref_rcv_matrix_double[i][col+j] = snd_matrix_double[i][col+j];
}
}
} else {
for (i = 0; i < MAX_ELEM; i++) {
for (j = 0; j < MAX_ELEM; j++) {
snd_matrix_int[i][j] = i * MAX_ELEM + j;
rcv_matrix_int[i][j] = -1;
ref_rcv_matrix_int[i][j] = -1;
}
}
for (i = 0; i < MAX_ELEM; i++) {
for( j = 0; j < blocklen; j++ ) {
snd_matrix_int[i][col+j] = i * MAX_ELEM + col + j;
ref_rcv_matrix_int[i][col+j] = snd_matrix_int[i][col+j];
}
}
}
}
void warmup(int my_rank, int col, int blocklen, MPI_Datatype dtt, int opt)
{
MPI_Status status;
int i, j, data_dump = 0;
int errors;
void *ptr_send, *ptr_recv;
if (!NB_WARMUP) return;
if (opt == OPT_DOUBLE) {
ptr_send = snd_matrix_double;
ptr_recv = rcv_matrix_double;
} else {
ptr_send = snd_matrix_int;
ptr_recv = rcv_matrix_int;
}
if (!my_rank) {
for (i = 0; i < NB_WARMUP; i++) {
MPI_Send(ptr_send, 1, dtt, 1, TAG_INDEXED, MPI_COMM_WORLD);
MPI_Recv(ptr_recv, 1, dtt, 1, TAG_INDEXED, MPI_COMM_WORLD, &status);
}
} else {
for (i = 0; i < NB_WARMUP; i++) {
MPI_Recv(ptr_recv, 1, dtt, 0, TAG_INDEXED, MPI_COMM_WORLD, &status);
MPI_Send(ptr_send, 1, dtt, 0, TAG_INDEXED, MPI_COMM_WORLD);
}
/* Check for errors */
errors = 0;
for (i = 0; i < MAX_ELEM; i++) {
for (j = 0; j < MAX_ELEM; j++) {
if (opt == OPT_DOUBLE) {
if (rcv_matrix_double[i][j] != ref_rcv_matrix_double[i][j]) {
if( !data_dump ) {
ompi_datatype_dump(dtt);
data_dump = 1;
}
printf("!!!!!!!!! rcv_matrix[%d][%d] : expected %f GOT %f\n", i, j, ref_rcv_matrix_double[i][j], rcv_matrix_double[i][j]);
errors++;
}
} else {
if (rcv_matrix_int[i][j] != ref_rcv_matrix_int[i][j]) {
if( !data_dump ) {
ompi_datatype_dump(dtt);
data_dump = 1;
}
printf("!!!!!!!!! rcv_matrix[%d][%d] : expected %d GOT %d\n", i, j, ref_rcv_matrix_int[i][j], rcv_matrix_int[i][j]);
}
}
}
}
if (errors) {
printf("FOUND %d errors", errors);
printf(" )-:\n");
}
#if 0
else { printf("w000t!!!\n"); }
#endif
}
}
int main(int argc, char **argv)
{
double t_beg, t_end;
double latency;
int disps[MAX_ELEM], blklens[MAX_ELEM];
int i, col, opt, blocklen = 1, blocklen_pos = 3;
int size, rank;
char *type;
void *ptr_send, *ptr_recv;
MPI_Datatype newtype;
MPI_Status status;
MPI_Init(&argc, &argv);
//sleep(20);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (argc < 3) {
if (!rank) {
printf("Syntax: %s <column number> {double|int} [blocklen=1]\n", argv[0]);
}
MPI_Finalize();
exit(1);
}
col = atoi(argv[1]);
if (col < 0 || col >= MAX_ELEM) {
if (!rank) {
printf("%s: <column number> should be >= 0 and < %d\n", argv[0], MAX_ELEM);
}
MPI_Finalize();
exit(1);
}
type = argv[2];
if (strcmp(type, "double")) {
if (strcmp(type, "int")) {
if (!rank) {
printf("Syntax: %s <column number> {double|int} [blocklen=1]\n", argv[0]);
}
MPI_Finalize();
exit(1);
} else {
opt = OPT_INT;
ptr_send = snd_matrix_int;
ptr_recv = rcv_matrix_int;
}
} else {
opt = OPT_DOUBLE;
ptr_send = snd_matrix_double;
ptr_recv = rcv_matrix_double;
}
if (rank == 0) {
fprintf(stdout, "(%s) LATENCY MATRIX %d x %d %ss (usecs)\n",
argv[0], MAX_ELEM, MAX_ELEM,
(opt == OPT_INT) ? "int" : "double");
fflush(stdout);
}
while( argc > blocklen_pos ) {
blocklen = atoi(argv[blocklen_pos]);
if( (blocklen < 1) || ((col + blocklen) > MAX_ELEM) ) {
if(!rank) {
printf("When col = %d blocklen must be [1 .. %d[\n", col, MAX_ELEM-col);
MPI_Abort(MPI_COMM_WORLD, -1);
exit(-1);
}
}
blocklen_pos++;
/* Compute start and size of each row.
* Actually size is constant since we are transferring a column. */
for (i = 0; i < MAX_ELEM; i++) {
disps[i] = i * MAX_ELEM + col;
blklens[i] = blocklen;
}
/*
* count = # of blocks
* array_of_blocklen = # of elems of oldtype in each block
* array_of_displacements = displacement of each block (in # of elems) wrt beginning
*/
MPI_Type_indexed(MAX_ELEM, blklens, disps,
(opt == OPT_INT) ? MPI_INT : MPI_DOUBLE, &newtype);
MPI_Type_commit(&newtype);
if (size != 2) { /* do the check later after creating the datatype */
if (!rank) {
printf("shoulf be run with 2 ranks!!!!\n");
}
MPI_Finalize();
exit(1);
}
init_matrices(opt, col, blocklen);
warmup(rank, col, blocklen, newtype, opt);
t_beg = MPI_Wtime();
if (rank == 0) {
for (i = 0; i < NB_LOOPS; i++) {
MPI_Send(ptr_send, 1, newtype, 1, TAG_INDEXED, MPI_COMM_WORLD);
MPI_Recv(ptr_recv, 1, newtype, 1, TAG_INDEXED, MPI_COMM_WORLD, &status);
}
} else {
for (i = 0; i < NB_LOOPS; i++) {
MPI_Recv(ptr_recv, 1, newtype, 0, TAG_INDEXED, MPI_COMM_WORLD, &status);
MPI_Send(ptr_send, 1, newtype, 0, TAG_INDEXED, MPI_COMM_WORLD);
}
}
t_end = MPI_Wtime();
if (rank == 0) {
latency = (t_end - t_beg) * 1e6 / (2.0 * NB_LOOPS);
fprintf(stdout, "col=%d blocklen=%d -----------------> %f usecs\n", col, blocklen, latency);
fflush(stdout);
}
MPI_Type_free(&newtype);
}
MPI_Finalize();
return 0;
}