-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi.c
482 lines (459 loc) · 22 KB
/
mpi.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* Copyright (c) 2016, Los Alamos National Security, LLC
* All rights reserved.
* Copyright 2016. Los Alamos National Security, LLC. This software was produced under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL), which is operated by Los Alamos National Security, LLC for the U.S. Department of Energy. The U.S. Government has rights to use, reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to produce derivative works, such modified software should be clearly marked, so as not to confuse it with the version available from LANL.
* Additionally, redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. Neither the name of Los Alamos National Security, LLC, Los Alamos National Laboratory, LANL, the U.S. Government, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdint.h>
#include <immintrin.h>
#include <math.h>
#include <omp.h>
#include <mpi.h>
#define WORD (256/8)
#define OUT_GHOST 7
#define X_OUT_GHOST (((OUT_GHOST - 1)/WORD + 1) * WORD)
#define Y_OUT_GHOST OUT_GHOST
#define IN_GHOST (OUT_GHOST + 1)
#define X_IN_GHOST ((OUT_GHOST/WORD + 1) * WORD)
#define Y_IN_GHOST IN_GHOST
#define X_IN_GHOST_WORDS (X_IN_GHOST/WORD)
//Here are the tags we will use to distinguish where the data is coming from
//and going to. Notice that the top left corner is sent to the bottom right
//corner of the top left neighbor.
#define TOP_LEFT_SEND 0
#define BOTTOM_RIGHT_RECV 0
#define TOP_SEND 1
#define BOTTOM_RECV 1
#define TOP_RIGHT_SEND 2
#define BOTTOM_LEFT_RECV 2
#define RIGHT_SEND 3
#define LEFT_RECV 3
#define BOTTOM_RIGHT_SEND 4
#define TOP_LEFT_RECV 4
#define BOTTOM_SEND 5
#define TOP_RECV 5
#define BOTTOM_LEFT_SEND 6
#define TOP_RIGHT_RECV 6
#define LEFT_SEND 7
#define RIGHT_RECV 7
void *aligned_malloc(int size) {
char *mem = malloc(sizeof(void*) + size + WORD - 1);
void **ptr = (void**)(((uintptr_t)(mem + sizeof(void*) + WORD - 1)) & ~((uintptr_t)(WORD - 1)));
ptr[-1] = mem;
return ptr;
}
void aligned_free(void *ptr) {
free(((void**)ptr)[-1]);
}
unsigned *life (const unsigned height,
const unsigned width,
const unsigned * const initial,
const unsigned iters) {
int rank;
int size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
//We will be arranging our processes in a grid. We are assuming that the
//problem width is divisible by the square root of the number of processors
//times the width of a word, and that the height is divisible by the
//square root of the number of processes, and the number of processes
//is a perfect square
const unsigned side = (int)sqrt((double)size);
const unsigned my_height = height/side;
const unsigned my_width = width/side;
const unsigned my_y = (rank / side);
const unsigned my_x = (rank % side);
const unsigned my_padded_height = my_height + 2 * Y_IN_GHOST;
const unsigned my_padded_width = my_width + 2 * X_IN_GHOST;
const unsigned my_width_words = my_width/WORD;
const unsigned my_padded_width_words = my_padded_width/WORD;
uint8_t *universe = (uint8_t*)aligned_malloc(my_padded_height * my_padded_width);
uint8_t *new = (uint8_t*)aligned_malloc(my_padded_height * my_padded_width);
const __m256i ones = _mm256_set_epi8(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1);
const __m256i twos = _mm256_slli_epi32(ones, 1);
const __m256i threes = _mm256_or_si256(ones, twos);
//We start by sending the data to all the processes. The data is first
//partitioned into a grid of rectangles (one for each processor).
//Here we first break up the initial data into rectangles.
uint8_t *scatter_buffer_send;
uint8_t *scatter_buffer_recv = (uint8_t*)aligned_malloc(my_height * my_width);
if (rank == 0) {
scatter_buffer_send = (uint8_t*)aligned_malloc(height * width);
for (unsigned their_y = 0; their_y < side; their_y++) {
for (unsigned their_x = 0; their_x < side; their_x++) {
for (unsigned y = 0; y < my_height; y++) {
for (unsigned x = 0; x < my_width; x++) {
scatter_buffer_send[(their_y * side + their_x) * (my_width * my_height) + y * my_width + x] =
initial[(their_y * my_height + y) * width + their_x * my_width + x];
}
}
}
}
}
MPI_Scatter((const void*)scatter_buffer_send,
my_height * my_width,
MPI_UNSIGNED_CHAR,
(void*)scatter_buffer_recv,
my_height * my_width,
MPI_UNSIGNED_CHAR,
0,
MPI_COMM_WORLD);
//Now that the data has been scattered, we copy our personal rectangle into
//our local universe.
for (unsigned y = Y_IN_GHOST; y < Y_IN_GHOST + my_height; y++) {
for (unsigned x = X_IN_GHOST; x < X_IN_GHOST + my_width; x++) {
universe[(y * my_padded_width) + x] = scatter_buffer_recv[(y - Y_IN_GHOST) * my_width + x - X_IN_GHOST];
}
}
//There's a bunch of send buffers aren't there?
__m256i *ghost_buffer_top_left_send = (__m256i*)aligned_malloc(X_IN_GHOST * Y_IN_GHOST);
__m256i *ghost_buffer_top_send = (__m256i*)aligned_malloc( my_width * Y_IN_GHOST);
__m256i *ghost_buffer_top_right_send = (__m256i*)aligned_malloc(X_IN_GHOST * Y_IN_GHOST);
__m256i *ghost_buffer_right_send = (__m256i*)aligned_malloc(X_IN_GHOST * my_height );
__m256i *ghost_buffer_bottom_right_send = (__m256i*)aligned_malloc(X_IN_GHOST * Y_IN_GHOST);
__m256i *ghost_buffer_bottom_send = (__m256i*)aligned_malloc( my_width * Y_IN_GHOST);
__m256i *ghost_buffer_bottom_left_send = (__m256i*)aligned_malloc(X_IN_GHOST * Y_IN_GHOST);
__m256i *ghost_buffer_left_send = (__m256i*)aligned_malloc(X_IN_GHOST * my_height );
__m256i *ghost_buffer_bottom_right_recv = (__m256i*)aligned_malloc(X_IN_GHOST * Y_IN_GHOST);
__m256i *ghost_buffer_bottom_recv = (__m256i*)aligned_malloc( my_width * Y_IN_GHOST);
__m256i *ghost_buffer_bottom_left_recv = (__m256i*)aligned_malloc(X_IN_GHOST * Y_IN_GHOST);
__m256i *ghost_buffer_left_recv = (__m256i*)aligned_malloc(X_IN_GHOST * my_height );
__m256i *ghost_buffer_top_left_recv = (__m256i*)aligned_malloc(X_IN_GHOST * Y_IN_GHOST);
__m256i *ghost_buffer_top_recv = (__m256i*)aligned_malloc( my_width * Y_IN_GHOST);
__m256i *ghost_buffer_top_right_recv = (__m256i*)aligned_malloc(X_IN_GHOST * Y_IN_GHOST);
__m256i *ghost_buffer_right_recv = (__m256i*)aligned_malloc(X_IN_GHOST * my_height );
MPI_Request top_left_req;
MPI_Request top_req;
MPI_Request top_right_req;
MPI_Request right_req;
MPI_Request bottom_right_req;
MPI_Request bottom_req;
MPI_Request bottom_left_req;
MPI_Request left_req;
for (unsigned i = 0; i < iters; i+= IN_GHOST) {
__m256i *universe_words = (__m256i*)universe;
//Here are all of the sends to our neighbors in every cardinal direction.
//The sends are nonblocking, so that we can move right on to the next send
//without waiting for our neighbors to receive.
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(ghost_buffer_top_left_send + y * X_IN_GHOST_WORDS + x,
_mm256_load_si256(universe_words + (Y_IN_GHOST + y) * my_padded_width_words + X_IN_GHOST_WORDS + x));
}
}
MPI_Isend(ghost_buffer_top_left_send,
X_IN_GHOST * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + side - 1) % side) * side + ((my_x + side - 1) % side),
TOP_LEFT_SEND,
MPI_COMM_WORLD,
&top_left_req);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < my_width_words; x++) {
_mm256_store_si256(ghost_buffer_top_send + y * my_width_words + x,
_mm256_load_si256(universe_words + (Y_IN_GHOST + y) * my_padded_width_words + X_IN_GHOST_WORDS + x));
}
}
MPI_Isend(ghost_buffer_top_send,
my_width * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + side - 1) % side) * side + my_x,
TOP_SEND,
MPI_COMM_WORLD,
&top_req);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(ghost_buffer_top_right_send + y * X_IN_GHOST_WORDS + x,
_mm256_load_si256(universe_words + (y + Y_IN_GHOST) * my_padded_width_words + x + my_width_words));
}
}
MPI_Isend(ghost_buffer_top_right_send,
X_IN_GHOST * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + side - 1) % side) * side + ((my_x + 1) % side),
TOP_RIGHT_SEND,
MPI_COMM_WORLD,
&top_right_req);
for (unsigned y = 0; y < my_height; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(ghost_buffer_right_send + y * X_IN_GHOST_WORDS + x,
_mm256_load_si256(universe_words + (y + Y_IN_GHOST) * my_padded_width_words + x + my_width_words));
}
}
MPI_Isend(ghost_buffer_right_send,
X_IN_GHOST * my_height,
MPI_UNSIGNED_CHAR,
my_y * side + ((my_x + 1) % side),
RIGHT_SEND,
MPI_COMM_WORLD,
&right_req);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(ghost_buffer_bottom_right_send + y * X_IN_GHOST_WORDS + x,
_mm256_load_si256(universe_words + (y + my_height) * my_padded_width_words + x + my_width_words));
}
}
MPI_Isend(ghost_buffer_bottom_right_send,
X_IN_GHOST * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + 1) % side) * side + ((my_x + 1) % side),
BOTTOM_RIGHT_SEND,
MPI_COMM_WORLD,
&bottom_right_req);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < my_width_words; x++) {
_mm256_store_si256(ghost_buffer_bottom_send + y * my_width_words + x,
_mm256_load_si256(universe_words + (y + my_height) * my_padded_width_words + x + X_IN_GHOST_WORDS));
}
}
MPI_Isend(ghost_buffer_bottom_send,
my_width * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + 1) % side) * side + my_x,
BOTTOM_SEND,
MPI_COMM_WORLD,
&bottom_req);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(ghost_buffer_bottom_left_send + y * X_IN_GHOST_WORDS + x,
_mm256_load_si256(universe_words + (y + my_height) * my_padded_width_words + x + X_IN_GHOST_WORDS));
}
}
MPI_Isend(ghost_buffer_bottom_left_send,
X_IN_GHOST * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + 1) % side) * side + ((my_x + side - 1) % side),
BOTTOM_LEFT_SEND,
MPI_COMM_WORLD,
&bottom_left_req);
for (unsigned y = 0; y < my_height; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(ghost_buffer_left_send + y * X_IN_GHOST_WORDS + x,
_mm256_load_si256(universe_words + (y + Y_IN_GHOST) * my_padded_width_words + x + X_IN_GHOST_WORDS));
}
}
MPI_Isend(ghost_buffer_left_send,
X_IN_GHOST * my_height,
MPI_UNSIGNED_CHAR,
my_y * side + ((my_x + side - 1) % side),
LEFT_SEND,
MPI_COMM_WORLD,
&left_req);
//Now we receive ghost zones from all of our neighbors. Since we need to
//process our received data immediately, the received data is blocking.
MPI_Recv((void*)ghost_buffer_bottom_right_recv,
X_IN_GHOST * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + 1) % side) * side + ((my_x + 1) % side),
BOTTOM_RIGHT_RECV,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(universe_words + (y + Y_IN_GHOST + my_height) * my_padded_width_words + x + X_IN_GHOST_WORDS + my_width_words,
_mm256_load_si256(ghost_buffer_bottom_right_recv + y * X_IN_GHOST_WORDS + x));
}
}
MPI_Recv((void*)ghost_buffer_bottom_recv,
my_width * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + 1) % side) * side + my_x,
BOTTOM_RECV,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < my_width_words; x++) {
_mm256_store_si256(universe_words + (y + Y_IN_GHOST + my_height) * my_padded_width_words + x + X_IN_GHOST_WORDS,
_mm256_load_si256(ghost_buffer_bottom_recv + y * my_width_words + x));
}
}
MPI_Recv((void*)ghost_buffer_bottom_left_recv,
X_IN_GHOST * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + 1) % side) * side + ((my_x + side - 1) % side),
BOTTOM_LEFT_RECV,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(universe_words + (y + Y_IN_GHOST + my_height) * my_padded_width_words + x,
_mm256_load_si256(ghost_buffer_bottom_left_recv + y * X_IN_GHOST_WORDS + x));
}
}
MPI_Recv((void*)ghost_buffer_left_recv,
X_IN_GHOST * my_height,
MPI_UNSIGNED_CHAR,
my_y * side + ((my_x + side - 1) % side),
LEFT_RECV,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
for (unsigned y = 0; y < my_height; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(universe_words + (y + Y_IN_GHOST) * my_padded_width_words + x,
_mm256_load_si256(ghost_buffer_left_recv + y * X_IN_GHOST_WORDS + x));
}
}
MPI_Recv((void*)ghost_buffer_top_left_recv,
X_IN_GHOST * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + side - 1) % side) * side + ((my_x + side - 1) % side),
TOP_LEFT_RECV,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(universe_words + y * my_padded_width_words + x,
_mm256_load_si256(ghost_buffer_top_left_recv + y * X_IN_GHOST_WORDS + x));
}
}
MPI_Recv((void*)ghost_buffer_top_recv,
my_width * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + side - 1) % side) * side + my_x,
TOP_RECV,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < my_width_words; x++) {
_mm256_store_si256(universe_words + y * my_padded_width_words + x + X_IN_GHOST_WORDS,
_mm256_load_si256(ghost_buffer_top_recv + y * my_width_words + x));
}
}
MPI_Recv((void*)ghost_buffer_top_right_recv,
X_IN_GHOST * Y_IN_GHOST,
MPI_UNSIGNED_CHAR,
((my_y + side - 1) % side) * side + ((my_x + 1) % side),
TOP_RIGHT_RECV,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
for (unsigned y = 0; y < Y_IN_GHOST; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(universe_words + y * my_padded_width_words + x + X_IN_GHOST_WORDS + my_width_words,
_mm256_load_si256(ghost_buffer_top_right_recv + y * X_IN_GHOST_WORDS + x));
}
}
MPI_Recv((void*)ghost_buffer_right_recv,
X_IN_GHOST * my_height,
MPI_UNSIGNED_CHAR,
my_y * side + ((my_x + 1) % side),
RIGHT_RECV,
MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
for (unsigned y = 0; y < my_height; y++) {
for (unsigned x = 0; x < X_IN_GHOST_WORDS; x++) {
_mm256_store_si256(universe_words + (y + Y_IN_GHOST) * my_padded_width_words + x + X_IN_GHOST_WORDS + my_width_words,
_mm256_load_si256(ghost_buffer_right_recv + y * X_IN_GHOST_WORDS + x));
}
}
//The inner loop is the same.
#pragma omp parallel
{
uint8_t *my_universe = universe;
uint8_t *my_new = new;
for (unsigned j = 0; j < IN_GHOST & j + i < iters; j++) {
#pragma omp for
for (unsigned y = (Y_IN_GHOST - Y_OUT_GHOST); y < my_height + Y_IN_GHOST + Y_OUT_GHOST; y++) {
for (unsigned x = (X_IN_GHOST - X_OUT_GHOST); x + WORD <= my_width + X_IN_GHOST + X_OUT_GHOST; x += WORD) {
__m256i n;
__m256i alive;
uint8_t *u = my_universe + (y - 1) * my_padded_width + x - 1;
n = _mm256_lddqu_si256((__m256i*)u);
n = _mm256_add_epi8(_mm256_load_si256((__m256i*)(u + 1)), n);
n = _mm256_add_epi8(_mm256_lddqu_si256((__m256i*)(u + 2)), n);
u += my_padded_width;
n = _mm256_add_epi8(_mm256_lddqu_si256((__m256i*)u), n);
alive = _mm256_load_si256((__m256i*)(u + 1));
n = _mm256_add_epi8(_mm256_lddqu_si256((__m256i*)(u + 2)), n);
u += my_padded_width;
n = _mm256_add_epi8(_mm256_lddqu_si256((__m256i*)u), n);
n = _mm256_add_epi8(_mm256_load_si256((__m256i*)(u + 1)), n);
n = _mm256_add_epi8(_mm256_lddqu_si256((__m256i*)(u + 2)), n);
_mm256_stream_si256((__m256i*)(my_new + y * my_padded_width + x),
_mm256_or_si256(
_mm256_and_si256(ones, _mm256_cmpeq_epi8(n, threes)),
_mm256_and_si256(alive, _mm256_cmpeq_epi8(n, twos))));
}
}
uint8_t *tmp = my_universe;
my_universe = my_new;
my_new = tmp;
}
#pragma omp single
{
universe = my_universe;
new = my_new;
}
}
//Before we start another iteration and start sending again, let's make sure
//that everyone has received our messages.
MPI_Wait(&top_left_req, MPI_STATUS_IGNORE);
MPI_Wait(&top_req, MPI_STATUS_IGNORE);
MPI_Wait(&top_right_req, MPI_STATUS_IGNORE);
MPI_Wait(&right_req, MPI_STATUS_IGNORE);
MPI_Wait(&bottom_right_req, MPI_STATUS_IGNORE);
MPI_Wait(&bottom_req, MPI_STATUS_IGNORE);
MPI_Wait(&bottom_left_req, MPI_STATUS_IGNORE);
MPI_Wait(&left_req, MPI_STATUS_IGNORE);
}
unsigned *out = NULL;
//This part is very similar to the Scatter. We now have all of the final
//configurations, and need to send them to the master process so that we
//can return a matrix.
for (unsigned y = Y_IN_GHOST; y < Y_IN_GHOST + my_height; y++) {
for (unsigned x = X_IN_GHOST; x < X_IN_GHOST + my_width; x++) {
scatter_buffer_recv[(y - Y_IN_GHOST) * my_width + x - X_IN_GHOST] = universe[y * my_padded_width + x];
}
}
MPI_Gather((const void*)scatter_buffer_recv,
my_height * my_width,
MPI_UNSIGNED_CHAR,
(void*)scatter_buffer_send,
my_height * my_width,
MPI_UNSIGNED_CHAR,
0,
MPI_COMM_WORLD);
if (rank == 0) {
out = (unsigned*)malloc(sizeof(unsigned) * height * width);
for (unsigned their_y = 0; their_y < side; their_y++) {
for (unsigned their_x = 0; their_x < side; their_x++) {
for (unsigned y = 0; y < my_height; y++) {
for (unsigned x = 0; x < my_width; x++) {
out[(their_y * my_height + y) * width + their_x * my_width + x] =
scatter_buffer_send[(their_y * side + their_x) * (my_width * my_height) + y * my_width + x];
}
}
}
}
}
aligned_free(new);
aligned_free(universe);
aligned_free(ghost_buffer_top_left_send);
aligned_free(ghost_buffer_top_send);
aligned_free(ghost_buffer_top_right_send);
aligned_free(ghost_buffer_right_send);
aligned_free(ghost_buffer_bottom_right_send);
aligned_free(ghost_buffer_bottom_send);
aligned_free(ghost_buffer_bottom_left_send);
aligned_free(ghost_buffer_left_send);
aligned_free(ghost_buffer_top_left_recv);
aligned_free(ghost_buffer_top_recv);
aligned_free(ghost_buffer_top_right_recv);
aligned_free(ghost_buffer_right_recv);
aligned_free(ghost_buffer_bottom_right_recv);
aligned_free(ghost_buffer_bottom_recv);
aligned_free(ghost_buffer_bottom_left_recv);
aligned_free(ghost_buffer_left_recv);
if (rank == 0) {
aligned_free(scatter_buffer_send);
}
aligned_free(scatter_buffer_recv);
return out;
}