-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb2r_halo_sync.cu
executable file
·510 lines (460 loc) · 22.7 KB
/
b2r_halo_sync.cu
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//
// b2r_halo_sync.cu
// pack and unpack ghost zone data
//
// Created by Zhengchun Liu on 3/15/16.
// Copyright © 2016 research. All rights reserved.
//
#ifndef b2r_halo_sync_h
#define b2r_halo_sync_h
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <cuda_runtime.h>
#include "b2r_config.h"
#define CUDA_BLOCK_SIZE 32
using namespace std;
#define cudaErrchk(ans) cudaAssert((ans), __FILE__, __LINE__)
inline void cudaAssert(cudaError_t code, string file, int line){
if (code != cudaSuccess){
cerr << "CUDA Error: " << cudaGetErrorString(code) << "; file: " << file << ", line:" << line << endl;
exit(-1);
}
}
/*
***************************************************************************************************
* Global Variables *
***************************************************************************************************
*/
extern int B2R_B_X, B2R_B_Y, B2R_B_Z;
extern int B2R_BLOCK_SIZE_X, B2R_BLOCK_SIZE_Y, B2R_BLOCK_SIZE_Z;
extern int B2R_R;
extern int BLOCK_DIM_X, BLOCK_DIM_Y, BLOCK_DIM_Z;
__constant__ int d_B2R_B_X, d_B2R_B_Y, d_B2R_B_Z;
__constant__ int d_B2R_BLOCK_SIZE_X, d_B2R_BLOCK_SIZE_Y, d_B2R_BLOCK_SIZE_Z;
__constant__ int d_B2R_R;
// send/receive buffer, could be reused for different dimension
char * h_dp_pad_send_buf[2], * h_dp_pad_recv_buf[2];
CELL_DT ** d_pad_send_buf;
CELL_DT ** d_pad_recv_buf;
char * h_pad_send_buf[2];
char * h_pad_recv_buf[2];
extern void b2r_sync_wait(int sent_cnt, int recv_cnt);
extern int b2r_send_pad_x(int tstep, char **pad_send_buf);
extern int b2r_recv_pad_x(int tstep, char **pad_recv_buf);
extern int b2r_send_pad_y(int tstep, char **pad_send_buf);
extern int b2r_recv_pad_y(int tstep, char **pad_recv_buf);
extern int b2r_send_pad_z(int tstep, char **pad_send_buf);
extern int b2r_recv_pad_z(int tstep, char **pad_recv_buf);
extern char b2r_halo_update[6];
/*
***************************************************************************************************
* func name: b2r_sync_init
* description: check the avaliability of MPICH_RDMA_ENABLED_CUDA
allocate device memory for send/receive buffer
parameters :
* none
* return: none
***************************************************************************************************
*/
void b2r_sync_init()
{
// Ensure that RDMA ENABLED CUDA is set correctly
int direct = getenv("MPICH_RDMA_ENABLED_CUDA")==NULL?0:atoi(getenv ("MPICH_RDMA_ENABLED_CUDA"));
if(direct != 1){
cout << "MPICH_RDMA_ENABLED_CUDA not enabled!" << endl;
exit (-1);
}
const int Rd = B2R_R * B2R_D;
int buffer_size = sizeof(CELL_DT) * max(max(Rd*B2R_B_Y*B2R_B_Z, B2R_BLOCK_SIZE_X*Rd*B2R_B_Z),
B2R_BLOCK_SIZE_X*B2R_BLOCK_SIZE_Y*Rd);
cudaErrchk( cudaMalloc((void**)&(h_dp_pad_send_buf[0]), buffer_size) );
cudaErrchk( cudaMalloc((void**)&(h_dp_pad_send_buf[1]), buffer_size) );
cudaErrchk( cudaMalloc((void**)&(h_dp_pad_recv_buf[0]), buffer_size) );
cudaErrchk( cudaMalloc((void**)&(h_dp_pad_recv_buf[1]), buffer_size) );
cudaErrchk( cudaMalloc((void**)&(d_pad_send_buf), 2*sizeof(CELL_DT *)) );
cudaErrchk( cudaMalloc((void**)&(d_pad_recv_buf), 2*sizeof(CELL_DT *)) );
cudaErrchk( cudaMemcpy((void *)d_pad_send_buf, (void *)h_dp_pad_send_buf, 2*sizeof(CELL_DT *), cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpy((void *)d_pad_recv_buf, (void *)h_dp_pad_recv_buf, 2*sizeof(CELL_DT *), cudaMemcpyHostToDevice) );
// sending buffer
h_pad_send_buf[0] = new char[buffer_size](); // direction 1(to: left/up/infront)
h_pad_send_buf[1] = new char[buffer_size](); // direction 2(to: right/down/behind)
// receiving buffer
h_pad_recv_buf[0] = new char[buffer_size](); // direction 1(from: left/up/infront)
h_pad_recv_buf[1] = new char[buffer_size](); // direction 2(from: right/down/behind)
cudaErrchk( cudaMemcpyToSymbol(d_B2R_B_X, &B2R_B_X, sizeof(int), 0, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpyToSymbol(d_B2R_B_Y, &B2R_B_Y, sizeof(int), 0, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpyToSymbol(d_B2R_B_Z, &B2R_B_Z, sizeof(int), 0, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpyToSymbol(d_B2R_BLOCK_SIZE_X, &B2R_BLOCK_SIZE_X, sizeof(int), 0, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpyToSymbol(d_B2R_BLOCK_SIZE_Y, &B2R_BLOCK_SIZE_Y, sizeof(int), 0, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpyToSymbol(d_B2R_BLOCK_SIZE_Z, &B2R_BLOCK_SIZE_Z, sizeof(int), 0, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpyToSymbol(d_B2R_R, &B2R_R, sizeof(int), 0, cudaMemcpyHostToDevice) );
}
/*
***************************************************************************************************
* func name: b2r_sync_finalize
* description:
parameters :
* none
* return: none
***************************************************************************************************
*/
void b2r_sync_finalize(){
for(int i=0; i<2; i++){
cudaFree( h_dp_pad_recv_buf[i] );
cudaFree( h_dp_pad_recv_buf[i] );
}
cudaFree(d_pad_send_buf);
cudaFree(d_pad_recv_buf);
}
/*
***************************************************************************************************
* func name: b2r_sync_flag
* description: check the avaliability of synchronizing halo subparts
results will be wrote back to global variable
b2r_halo_update, idex definition refer to SYNC_M_CNT
parameters :
* rank number of MPI process
* return: none
***************************************************************************************************
*/
void b2r_sync_flag(int rank)
{
int block_id_x = (rank % (BLOCK_DIM_X * BLOCK_DIM_Y)) % BLOCK_DIM_X;
int block_id_y = (rank % (BLOCK_DIM_X * BLOCK_DIM_Y)) / BLOCK_DIM_X;
int block_id_z = (rank / (BLOCK_DIM_X * BLOCK_DIM_Y));
memset(b2r_halo_update, 0, sizeof(char) * sizeof(b2r_halo_update));
// left B x R*D
if(block_id_x >= 1)
{
b2r_halo_update[0] = 1;
}
// right B x R*D
if(block_id_x < BLOCK_DIM_X-1)
{
b2r_halo_update[1] = 1;
}
// up
if(block_id_y >= 1)
{
b2r_halo_update[2] = 1;
}
// down
if(block_id_y < BLOCK_DIM_Y-1)
{
b2r_halo_update[3] = 1;
}
#if _ENV_3D_
// in front
if(block_id_z >= 1)
{
b2r_halo_update[4] = 1;
}
// behind
if(block_id_z < BLOCK_DIM_Z-1)
{
b2r_halo_update[5] = 1;
}
#endif
}
/*
***************************************************************************************************
* func name: sub_matrix_pack_x
* description: pack sub cube in X direction to one dimensional array / buffer for sending(sync)
to other process / block
* parameters :
* none
* return: none
***************************************************************************************************
*/
__global__ void sub_matrix_pack_x(CELL_DT *d_grid, CELL_DT **pad_send_buf, bool neg, bool pos)
{
const int Rd = d_B2R_R * B2R_D;
// distance between 2D slice (in elements)
const int stride = d_B2R_BLOCK_SIZE_X * d_B2R_BLOCK_SIZE_Y;
const int stride_vd = d_B2R_B_Z * d_B2R_B_Y;
int ix = blockIdx.x*blockDim.x + threadIdx.x;
int iy = blockIdx.y*blockDim.y + threadIdx.y;
if(ix >= d_B2R_B_Z || iy >= d_B2R_B_Y){
return;
}
int gy = iy + Rd;
int gz = ix + Rd;
// left R*D * d_B2R_B_Y * d_B2R_B_Z
for (int gx = Rd; neg && gx < 2*Rd; ++gx){
pad_send_buf[0][(gx-Rd)*stride_vd + iy*d_B2R_B_Z+ix] = d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx];
}
// right R*D * d_B2R_B_Y * d_B2R_B_Z
for (int gx = d_B2R_B_X; pos && gx < d_B2R_B_X+Rd; ++gx){
pad_send_buf[1][(gx-d_B2R_B_X)*stride_vd + iy*d_B2R_B_Z+ix] = d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx];
}
}
/*
***************************************************************************************************
* func name: sub_matrix_pack_y
* description: pack sub cube in Y direction to one dimensional array / buffer for sending(sync)
to other process / block
* parameters :
* none
* return: none
***************************************************************************************************
*/
__global__ void sub_matrix_pack_y(CELL_DT *d_grid, CELL_DT **pad_send_buf, bool neg, bool pos)
{
const int Rd = d_B2R_R * B2R_D;
// distance between 2D slice (in elements)
const int stride = d_B2R_BLOCK_SIZE_X * d_B2R_BLOCK_SIZE_Y;
const int stride_vd = d_B2R_BLOCK_SIZE_X * d_B2R_B_Z;
int ix = blockIdx.x*blockDim.x + threadIdx.x;
int iy = blockIdx.y*blockDim.y + threadIdx.y;
if(ix >= d_B2R_BLOCK_SIZE_X || iy >= d_B2R_B_Z){
return;
}
int gx = ix;
int gz = iy + Rd;
// up B2R_BLOCK_SIZE_X * R*D * d_B2R_B_Z
for (int gy = Rd; neg && gy < 2*Rd; ++gy){
pad_send_buf[0][(gy-Rd)*stride_vd + iy*d_B2R_BLOCK_SIZE_X+ix] = d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx];
}
// down B2R_BLOCK_SIZE_X * R*D * d_B2R_B_Z
for (int gy = d_B2R_B_Y; pos && gy < d_B2R_B_Y+Rd; ++gy){
pad_send_buf[1][(gy-d_B2R_B_Y)*stride_vd + iy*d_B2R_BLOCK_SIZE_X+ix] = d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx];
}
}
/*
***************************************************************************************************
* func name: sub_matrix_pack_z
* description: pack sub cube in Z direction to one dimensional array / buffer for sending(sync)
to other process / block
* parameters :
* none
* return: none
***************************************************************************************************
*/
__global__ void sub_matrix_pack_z(CELL_DT *d_grid, CELL_DT **pad_send_buf, bool neg, bool pos)
{
const int Rd = d_B2R_R * B2R_D;
// distance between 2D slice (in elements)
const int stride = d_B2R_BLOCK_SIZE_X * d_B2R_BLOCK_SIZE_Y;
int ix = blockIdx.x*blockDim.x + threadIdx.x;
int iy = blockIdx.y*blockDim.y + threadIdx.y;
if(ix >= d_B2R_BLOCK_SIZE_X || iy >= d_B2R_BLOCK_SIZE_Y){
return;
}
int gx = ix;
int gy = iy;
// infront B2R_BLOCK_SIZE_X * R*D * B2R_BLOCK_SIZE_Z
for (int gz = Rd; neg && gz < 2*Rd; ++gz){
pad_send_buf[0][(gz-Rd)*stride + iy*d_B2R_BLOCK_SIZE_X+ix] = d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx];
}
// behind B2R_BLOCK_SIZE_X * R*D * B2R_BLOCK_SIZE_Z
for (int gz = d_B2R_B_Z; pos && gz < d_B2R_B_Z+Rd; ++gz){
pad_send_buf[1][(gz-d_B2R_B_Z)*stride + iy*d_B2R_BLOCK_SIZE_X+ix] = d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx];
}
}
/*
***************************************************************************************************
* func name: sub_matrix_unpack_x
* description: unpack received data (from left and right) to ghost region
* parameters :
* none
* return: none
***************************************************************************************************
*/
__global__ void sub_matrix_unpack_x(CELL_DT *d_grid, CELL_DT **pad_recv_buf, bool neg, bool pos)
{
const int Rd = d_B2R_R * B2R_D;
// distance between 2D slice (in elements)
const int stride = d_B2R_BLOCK_SIZE_X * d_B2R_BLOCK_SIZE_Y;
const int stride_vd = d_B2R_B_Z * d_B2R_B_Y;
int ix = blockIdx.x*blockDim.x + threadIdx.x;
int iy = blockIdx.y*blockDim.y + threadIdx.y;
if(ix >= d_B2R_B_Z || iy >= d_B2R_B_Y){
return;
}
int gy = iy + Rd;
int gz = ix + Rd;
// from left R*D * d_B2R_B_Y * d_B2R_B_Z
for (int gx = 0; neg && gx < Rd; ++gx){
d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx] = pad_recv_buf[0][gx*stride_vd + iy*d_B2R_B_Z+ix];
}
// from right R*D * d_B2R_B_Y * d_B2R_B_Z
for (int gx = d_B2R_B_X+Rd; pos && gx < d_B2R_BLOCK_SIZE_X; ++gx){
d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx] = pad_recv_buf[1][(gx-d_B2R_B_X-Rd)*stride_vd + iy*d_B2R_B_Z+ix];
}
}
/*
***************************************************************************************************
* func name: sub_matrix_unpack_y
* description: unpack received data (from up and down) to ghost region
* parameters :
* none
* return: none
***************************************************************************************************
*/
__global__ void sub_matrix_unpack_y(CELL_DT *d_grid, CELL_DT **pad_recv_buf, bool neg, bool pos)
{
const int Rd = d_B2R_R * B2R_D;
// distance between 2D slice (in elements)
const int stride = d_B2R_BLOCK_SIZE_X * d_B2R_BLOCK_SIZE_Y;
const int stride_vd = d_B2R_BLOCK_SIZE_X * d_B2R_B_Z;
int ix = blockIdx.x*blockDim.x + threadIdx.x;
int iy = blockIdx.y*blockDim.y + threadIdx.y;
if(ix >= d_B2R_BLOCK_SIZE_X || iy >= d_B2R_B_Z){
return;
}
int gx = ix;
int gz = iy + Rd;
// from up B2R_BLOCK_SIZE_X * R*D * d_B2R_B_Z
for (int gy = 0; neg && gy < Rd; ++gy){
d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx] = pad_recv_buf[0][gy*stride_vd + iy*d_B2R_BLOCK_SIZE_X+ix];
}
// from down B2R_BLOCK_SIZE_X * R*D * d_B2R_B_Z
for (int gy = d_B2R_B_Y+Rd; pos && gy < d_B2R_BLOCK_SIZE_Y; ++gy){
d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx] = pad_recv_buf[1][(gy-d_B2R_B_Y-Rd)*stride_vd + iy*d_B2R_BLOCK_SIZE_X+ix];
}
}
/*
***************************************************************************************************
* func name: sub_matrix_unpack_z
* description: unpack received data (from infront and behind) to ghost region
* parameters :
* none
* return: none
***************************************************************************************************
*/
__global__ void sub_matrix_unpack_z(CELL_DT *d_grid, CELL_DT **pad_recv_buf, bool neg, bool pos)
{
const int Rd = d_B2R_R * B2R_D;
// distance between 2D slice (in elements)
const int stride = d_B2R_BLOCK_SIZE_X * d_B2R_BLOCK_SIZE_Y;
int ix = blockIdx.x*blockDim.x + threadIdx.x;
int iy = blockIdx.y*blockDim.y + threadIdx.y;
if(ix >= d_B2R_BLOCK_SIZE_X || iy >= d_B2R_BLOCK_SIZE_Y){
return;
}
int gx = ix;
int gy = iy;
// from infront B2R_BLOCK_SIZE_X * R*D * B2R_BLOCK_SIZE_Z
for (int gz = 0; neg && gz < Rd; ++gz){
d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx] = pad_recv_buf[0][gz*stride + iy*d_B2R_BLOCK_SIZE_X+ix];
}
// from behind B2R_BLOCK_SIZE_X * R*D * B2R_BLOCK_SIZE_Z
for (int gz = d_B2R_B_Z+Rd; pos && gz < d_B2R_BLOCK_SIZE_Z; ++gz){
d_grid[stride*gz + gy*d_B2R_BLOCK_SIZE_X + gx] = pad_recv_buf[1][(gz-d_B2R_B_Z-Rd)*stride + iy*d_B2R_BLOCK_SIZE_X+ix];
}
}
/*
***************************************************************************************************
* func name: boundary_updating_direct
* description: update subdomain boundaries via MPI
* parameters :
* none
* return: none
***************************************************************************************************
*/
void boundary_updating_direct(int time_step, CELL_DT *d_grid)
{
int send_req_cnt, recv_req_cnt;
// updating ghost zone in X direction
// Launch configuration:
// cout << "about to sync for " << time_step << endl;
dim3 dimBlock_x(CUDA_BLOCK_SIZE, CUDA_BLOCK_SIZE, 1);
dim3 dimGrid_x(ceil((float)B2R_B_Z/CUDA_BLOCK_SIZE), ceil((float)B2R_B_Y/CUDA_BLOCK_SIZE), 1);
// invoke cuda threads to copy corresponding block data to halo sync buffer
sub_matrix_pack_x<<<dimGrid_x, dimBlock_x>>>(d_grid, d_pad_send_buf, b2r_halo_update[0]==1, b2r_halo_update[1]==1);
cudaErrchk( cudaDeviceSynchronize() );
send_req_cnt = b2r_send_pad_x(time_step, h_dp_pad_send_buf); // send halos via non-block MPI
recv_req_cnt = b2r_recv_pad_x(time_step, h_dp_pad_recv_buf); // receive halos via non-block MPI
b2r_sync_wait(send_req_cnt, recv_req_cnt);
// invoke cuda threads to copy received block data to halos
sub_matrix_unpack_x<<<dimGrid_x, dimBlock_x>>>(d_grid, d_pad_recv_buf, b2r_halo_update[0]==1, b2r_halo_update[1]==1);
cudaErrchk( cudaDeviceSynchronize() );
// updating ghost zone in Y direction
dim3 dimBlock_y(CUDA_BLOCK_SIZE, CUDA_BLOCK_SIZE, 1);
dim3 dimGrid_y(ceil((float)B2R_BLOCK_SIZE_X/CUDA_BLOCK_SIZE), ceil((float)B2R_B_Y/CUDA_BLOCK_SIZE), 1);
sub_matrix_pack_y<<<dimGrid_y, dimBlock_y>>>(d_grid, d_pad_send_buf, b2r_halo_update[2]==1, b2r_halo_update[3]==1);
cudaErrchk( cudaDeviceSynchronize() );
send_req_cnt = b2r_send_pad_y(time_step, h_dp_pad_send_buf); // send halos via non-block MPI
recv_req_cnt = b2r_recv_pad_y(time_step, h_dp_pad_recv_buf); // receive halos via non-block MPI
b2r_sync_wait(send_req_cnt, recv_req_cnt);
sub_matrix_unpack_y<<<dimGrid_y, dimBlock_y>>>(d_grid, d_pad_recv_buf, b2r_halo_update[2]==1, b2r_halo_update[3]==1);
cudaErrchk( cudaDeviceSynchronize() );
// updating ghost zone in Z direction
#if _ENV_3D_
dim3 dimBlock_z(CUDA_BLOCK_SIZE, CUDA_BLOCK_SIZE, 1);
dim3 dimGrid_z(ceil((float)B2R_BLOCK_SIZE_X/CUDA_BLOCK_SIZE), ceil((float)B2R_BLOCK_SIZE_Y/CUDA_BLOCK_SIZE), 1);
sub_matrix_pack_z<<<dimGrid_z, dimBlock_z>>>(d_grid, d_pad_send_buf, b2r_halo_update[4]==1, b2r_halo_update[5]==1);
cudaErrchk( cudaDeviceSynchronize() );
send_req_cnt = b2r_send_pad_z(time_step, h_dp_pad_send_buf); // send halos via non-block MPI
recv_req_cnt = b2r_recv_pad_z(time_step, h_dp_pad_recv_buf); // receive halos via non-block MPI
b2r_sync_wait(send_req_cnt, recv_req_cnt);
sub_matrix_unpack_z<<<dimGrid_z, dimBlock_z>>>(d_grid, d_pad_recv_buf, b2r_halo_update[4]==1, b2r_halo_update[5]==1);
cudaErrchk( cudaDeviceSynchronize() );
#endif
}
/*
***************************************************************************************************
* func name: boundary_updating
* description: update subdomain boundaries via MPI
* parameters :
* none
* return: none
***************************************************************************************************
*/
void boundary_updating(int time_step, CELL_DT *d_grid)
{
int send_req_cnt, recv_req_cnt;
const int Rd = B2R_R * B2R_D;
int buffer_size = sizeof(CELL_DT) * max(max(Rd*B2R_B_Y*B2R_B_Z, B2R_BLOCK_SIZE_X*Rd*B2R_B_Z),
B2R_BLOCK_SIZE_X*B2R_BLOCK_SIZE_Y*Rd);
// updating ghost zone in X direction
// Launch configuration:
// cout << "about to sync for " << time_step << endl;
dim3 dimBlock_x(CUDA_BLOCK_SIZE, CUDA_BLOCK_SIZE, 1);
dim3 dimGrid_x(ceil((float)B2R_B_Z/CUDA_BLOCK_SIZE), ceil((float)B2R_B_Y/CUDA_BLOCK_SIZE), 1);
// invoke cuda threads to copy corresponding block data to halo sync buffer
sub_matrix_pack_x<<<dimGrid_x, dimBlock_x>>>(d_grid, d_pad_send_buf, b2r_halo_update[0]==1, b2r_halo_update[1]==1);
cudaErrchk( cudaDeviceSynchronize() );
cudaErrchk( cudaMemcpy((void*) (h_pad_send_buf[0]), (void*) (h_dp_pad_send_buf[0]), buffer_size, cudaMemcpyDeviceToHost) );
cudaErrchk( cudaMemcpy((void*) (h_pad_send_buf[1]), (void*) (h_dp_pad_send_buf[1]), buffer_size, cudaMemcpyDeviceToHost) );
send_req_cnt = b2r_send_pad_x(time_step, h_pad_send_buf); // send halos via non-block MPI
recv_req_cnt = b2r_recv_pad_x(time_step, h_pad_recv_buf); // receive halos via non-block MPI
b2r_sync_wait(send_req_cnt, recv_req_cnt);
cudaErrchk( cudaMemcpy((void*) (h_dp_pad_recv_buf[0]), (void*) (h_pad_recv_buf[0]), buffer_size, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpy((void*) (h_dp_pad_recv_buf[1]), (void*) (h_pad_recv_buf[1]), buffer_size, cudaMemcpyHostToDevice) );
// invoke cuda threads to copy received block data to halos
sub_matrix_unpack_x<<<dimGrid_x, dimBlock_x>>>(d_grid, d_pad_recv_buf, b2r_halo_update[0]==1, b2r_halo_update[1]==1);
cudaErrchk( cudaDeviceSynchronize() );
// updating ghost zone in Y direction
dim3 dimBlock_y(CUDA_BLOCK_SIZE, CUDA_BLOCK_SIZE, 1);
dim3 dimGrid_y(ceil((float)B2R_BLOCK_SIZE_X/CUDA_BLOCK_SIZE), ceil((float)B2R_B_Y/CUDA_BLOCK_SIZE), 1);
sub_matrix_pack_y<<<dimGrid_y, dimBlock_y>>>(d_grid, d_pad_send_buf, b2r_halo_update[2]==1, b2r_halo_update[3]==1);
cudaErrchk( cudaDeviceSynchronize() );
cudaErrchk( cudaMemcpy((void*) (h_pad_send_buf[0]), (void*) (h_dp_pad_send_buf[0]), buffer_size, cudaMemcpyDeviceToHost) );
cudaErrchk( cudaMemcpy((void*) (h_pad_send_buf[1]), (void*) (h_dp_pad_send_buf[1]), buffer_size, cudaMemcpyDeviceToHost) );
send_req_cnt = b2r_send_pad_y(time_step, h_pad_send_buf); // send halos via non-block MPI
recv_req_cnt = b2r_recv_pad_y(time_step, h_pad_recv_buf); // receive halos via non-block MPI
b2r_sync_wait(send_req_cnt, recv_req_cnt);
cudaErrchk( cudaMemcpy((void*) (h_dp_pad_recv_buf[0]), (void*) (h_pad_recv_buf[0]), buffer_size, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpy((void*) (h_dp_pad_recv_buf[1]), (void*) (h_pad_recv_buf[1]), buffer_size, cudaMemcpyHostToDevice) );
sub_matrix_unpack_y<<<dimGrid_y, dimBlock_y>>>(d_grid, d_pad_recv_buf, b2r_halo_update[2]==1, b2r_halo_update[3]==1);
cudaErrchk( cudaDeviceSynchronize() );
// updating ghost zone in Z direction
#if _ENV_3D_
dim3 dimBlock_z(CUDA_BLOCK_SIZE, CUDA_BLOCK_SIZE, 1);
dim3 dimGrid_z(ceil((float)B2R_BLOCK_SIZE_X/CUDA_BLOCK_SIZE), ceil((float)B2R_BLOCK_SIZE_Y/CUDA_BLOCK_SIZE), 1);
sub_matrix_pack_z<<<dimGrid_z, dimBlock_z>>>(d_grid, d_pad_send_buf, b2r_halo_update[4]==1, b2r_halo_update[5]==1);
cudaErrchk( cudaDeviceSynchronize() );
cudaErrchk( cudaMemcpy((void*) (h_pad_send_buf[0]), (void*) (h_dp_pad_send_buf[0]), buffer_size, cudaMemcpyDeviceToHost) );
cudaErrchk( cudaMemcpy((void*) (h_pad_send_buf[1]), (void*) (h_dp_pad_send_buf[1]), buffer_size, cudaMemcpyDeviceToHost) );
send_req_cnt = b2r_send_pad_z(time_step, h_pad_send_buf); // send halos via non-block MPI
recv_req_cnt = b2r_recv_pad_z(time_step, h_pad_recv_buf); // receive halos via non-block MPI
b2r_sync_wait(send_req_cnt, recv_req_cnt);
cudaErrchk( cudaMemcpy((void*) (h_dp_pad_recv_buf[0]), (void*) (h_pad_recv_buf[0]), buffer_size, cudaMemcpyHostToDevice) );
cudaErrchk( cudaMemcpy((void*) (h_dp_pad_recv_buf[1]), (void*) (h_pad_recv_buf[1]), buffer_size, cudaMemcpyHostToDevice) );
sub_matrix_unpack_z<<<dimGrid_z, dimBlock_z>>>(d_grid, d_pad_recv_buf, b2r_halo_update[4]==1, b2r_halo_update[5]==1);
cudaErrchk( cudaDeviceSynchronize() );
#endif
}
#endif