-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.h
1523 lines (1223 loc) · 52.5 KB
/
grid.h
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef GRID_H_
#define GRID_H_
#include "parallel.h"
#include "vec_types.h"
#include "bnd.h"
#include "zdf-cpp.h"
#include <iostream>
namespace source {
enum tag { lower = 0, upper = 1 };
}
namespace dest {
enum tag { upper = 0, lower = 1 };
}
/**
* @brief
*
*/
template <class T>
class grid {
protected:
/// @brief Local number of tiles
uint2 ntiles;
/// @brief Offset of local tiles in global tile grid
uint2 tile_start;
/// @brief Consider local boundaries periodic
int2 periodic;
/// @brief Local grid size
uint2 gnx;
/// @brief Buffers for sending messages
pair< Message<T>* > msg_send;
/*
struct {
Message<T> * lower;
Message<T> * upper;
} msg_send;
*/
/// @brief Buffers for receiving messages
/*
struct {
Message<T> * lower;
Message<T> * upper;
} msg_recv;
*/
pair< Message<T>* > msg_recv;
/**
* @brief Set the local node information. This information will (may) be
* different for each parallel node
*
* @note Global periodic information is taken from the parallel partition
*
*/
void initialize( ) {
int2 coords = part.get_coords();
ntiles.x = global_ntiles.x / part.dims.x;
tile_start.x = coords.x * ntiles.x;
if ( (unsigned) coords.x < global_ntiles.x % part.dims.x ) {
ntiles.x += 1;
tile_start.x += coords.x;
} else {
tile_start.x += global_ntiles.x % part.dims.x;
}
gnx.x = ntiles.x * nx.x;
periodic.x = part.periodic.x && (part.dims.x == 1);
ntiles.y = global_ntiles.y / part.dims.y;
tile_start.y = coords.y * ntiles.y;
if ( (unsigned) coords.y < global_ntiles.y % part.dims.y ) {
ntiles.y += 1;
tile_start.y += coords.y;
} else {
tile_start.y += global_ntiles.y % part.dims.y;
}
gnx.y = ntiles.y * nx.y;
periodic.y = part.periodic.y && (part.dims.y == 1);
// std::cout << "[" << part.get_rank() << "] - offset: " << tile_start << ", ntiles: " << ntiles << '\n';
// Allocate main data buffer
d_buffer = memory::malloc<T>( buffer_size() );
// Get maximum message size
int max_msg_size = max(
( gnx.y + gc.y.lower + gc.y.upper ) * max( gc.x.lower, gc.x.upper ),
max( gc.y.lower, gc.y.upper ) * ( gnx.x + gc.x.lower + gc.x.upper )
);
// Allocate message buffers
msg_recv.lower = new Message<T>( max_msg_size, part.get_comm() );
msg_recv.upper = new Message<T>( max_msg_size, part.get_comm() );
msg_send.lower = new Message<T>( max_msg_size, part.get_comm() );
msg_send.upper = new Message<T>( max_msg_size, part.get_comm() );
}
private:
/**
* @brief Validate grid / parallel parameters. The execution will stop if
* errors are found.
*
*/
void validate_parameters() {
// Grid parameters
if ( global_ntiles.x == 0 || global_ntiles.y == 0 ) {
std::cerr << "Invalid number of tiles " << global_ntiles << '\n';
part.abort(1);
}
if ( nx.x == 0 || nx.y == 0 ) {
std::cerr << "Invalid tiles size" << nx << '\n';
part.abort(1);
}
// Parallel partition
if ( (unsigned) part.dims.x > global_ntiles.x ) {
std::cerr << "Number of parallel nodes along x (" ;
std::cerr << part.dims.x << ") is larger than number of tiles along x(";
std::cerr << global_ntiles.x << '\n';
part.abort(1);
}
if ( (unsigned) part.dims.y > global_ntiles.y ) {
std::cerr << "Number of parallel nodes along y (" ;
std::cerr << part.dims.y << ") is larger than number of tiles along y(";
std::cerr << global_ntiles.y << '\n';
part.abort(1);
}
}
public:
/// @brief Parallel partition
Partition & part;
/// @brief Data buffer
T * d_buffer;
/// @brief Global number of tiles
const uint2 global_ntiles;
/// @brief Tile grid size
const uint2 nx;
/// @brief Tile guard cells
const bnd<unsigned int> gc;
/// @brief Tile grize including guard cells
const uint2 ext_nx;
/// @brief Offset in cells between lower tile corner and position (0,0)
const unsigned int offset;
/// @brief Tile volume (may be larger than product of cells for alignment)
const unsigned int tile_vol;
/// @brief Object name
std::string name;
/**
* @brief Construct a new grid object
*
* @param global_ntiles Global number of tiles
* @param nx Individual tile size
* @param gc Number of guard cells
* @param part Parallel partition
*/
grid( uint2 const global_ntiles, uint2 const nx, bnd<unsigned int> const gc, Partition & part ):
part( part ),
d_buffer( nullptr ),
global_ntiles( global_ntiles ),
nx( nx ),
gc(gc),
ext_nx( make_uint2( gc.x.lower + nx.x + gc.x.upper,
gc.y.lower + nx.y + gc.y.upper )),
offset( gc.y.lower * ext_nx.x + gc.x.lower ),
tile_vol( roundup4( ext_nx.x * ext_nx.y ) ),
name( "grid" )
{
// Validate parameters
validate_parameters();
// Set local information (ntiles, tile_start and local_periodic)
initialize();
};
/**
* @brief Construct a new grid object
*
* @note: The number of guard cells is set to 0
*
* @param global_ntiles Global number of tiles
* @param nx Individual tile size
* @param part Parallel partition
*/
grid( uint2 const global_ntiles, uint2 const nx, Partition & part ):
part( part ),
d_buffer( nullptr ),
global_ntiles( global_ntiles ),
nx( nx ),
gc( 0 ),
ext_nx( make_uint2( nx.x, nx.y )),
offset( 0 ),
tile_vol( roundup4( nx.x * nx.y )),
name( "grid" )
{
// Validate parameters
validate_parameters();
// Set local information (ntiles, tile_start and periodic)
initialize();
};
/**
* @brief Get the local number of tiles
*
* @return int2
*/
uint2 get_ntiles() { return ntiles; };
uint2 get_tile_start() { return tile_start; };
/**
* @brief grid destructor
*
*/
~grid(){
delete msg_recv.lower;
delete msg_recv.upper;
delete msg_send.lower;
delete msg_send.upper;
memory::free( d_buffer );
};
/**
* @brief Stream extraction
*
* @param os
* @param obj
* @return std::ostream&
*/
friend std::ostream& operator<<(std::ostream& os, const grid<T>& obj) {
os << obj.name << "{";
os << "(" << obj.ntiles.x << " x " << obj.ntiles.y << " tiles)";
os << ", (" << obj.nx.x << " x " << obj.nx.y << " points/tile)";
os << "}";
return os;
}
/**
* @brief Buffer size
*
* @return total size of data buffers
*/
const std::size_t buffer_size() {
return (static_cast <std::size_t> (tile_vol)) * ( ntiles.x * ntiles.y ) ;
};
/**
* @brief zero device data on a grid grid
*
* @return int Returns 0 on success, -1 on error
*/
int zero() {
memory::zero( d_buffer, buffer_size() );
return 0;
};
/**
* @brief Sets data to a constant value
*
* @param val Value
*/
void set( T const & val ){
const size_t size = buffer_size( );
for( size_t i = 0; i < size; i++ ) d_buffer[i] = val;
};
/**
* @brief Scalar assigment
*
* @param val Value
* @return T Returns the same value
*/
T operator=( T const val ) {
set( val );
return val;
}
grid<T>& operator+=(const grid<T>& rhs) {
for( unsigned i = 0; i < buffer_size(); ++i ) d_buffer[i] += rhs.d_buffer[i];
return *this;
}
/**
* @brief Adds another grid object on top of local object
*
* @param rhs Other object to add
* @return grid& Reference to local object
*/
void add( const grid<T> &rhs ) {
size_t const size = buffer_size( );
for( size_t i = 0; i < size; i++ ) d_buffer[i] += rhs.d_buffer[i];
};
/**
* @brief Gather local grid into a contiguous grid
*
* Used mostly for diagnostic output
*
* @param out Output buffer
* @return unsigned int Total number of cells
*/
unsigned int gather( T * const __restrict__ out ) {
// Loop over tiles
for( unsigned ty = 0; ty < ntiles.y; ty ++ ) {
for( unsigned tx = 0; tx < ntiles.x; tx ++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol + offset;
auto * const __restrict__ tile_data = & d_buffer[ tile_off ];
// Loop inside tile
for( unsigned iy = 0; iy < nx.y; iy ++ ) {
for( unsigned ix = 0; ix < nx.x; ix ++ ) {
auto const gix = tile_idx.x * nx.x + ix;
auto const giy = tile_idx.y * nx.y + iy;
auto const out_idx = giy * gnx.x + gix;
out[ out_idx ] = tile_data[ iy * ext_nx.x + ix ];
}
}
}
}
return gnx.x * gnx.y;
}
#ifdef _OPENMP
/**
* @brief Copies edge values to X neighboring guard cells
*
*/
void local_copy_to_gc_x() {
// Loop over tiles
#pragma omp parallel for
for( unsigned tid = 0; tid < ntiles.y * ntiles.x; tid ++ ) {
const auto tile_idx = make_int2( tid % ntiles.x, tid / ntiles.x );
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
{ // Copy from lower neighbour
int neighbor_tx = tile_idx.x;
neighbor_tx -= 1;
if ( periodic.x && neighbor_tx < 0 ) neighbor_tx += ntiles.x;
if ( neighbor_tx >= 0 ) {
auto * __restrict__ x_lower = d_buffer + (tile_idx.y * ntiles.x + neighbor_tx) * tile_vol;
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.lower; i++ ) {
local[ i + j * ext_nx.x ] = x_lower[ nx.x + i + j * ext_nx.x ];
}
}
}
}
{ // Copy from upper neighbour
int neighbor_tx = tile_idx.x;
neighbor_tx += 1;
if ( periodic.x && neighbor_tx >= static_cast<int>(ntiles.x) ) neighbor_tx -= ntiles.x;
if ( neighbor_tx < static_cast<int>(ntiles.x) ) {
auto * __restrict__ x_upper = d_buffer + (tile_idx.y * ntiles.x + neighbor_tx) * tile_vol;
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.upper; i++ ) {
local[ gc.x.lower + nx.x + i + j * ext_nx.x ] = x_upper[ gc.x.lower + i + j * ext_nx.x ];
}
}
}
}
}
}
/**
* @brief Copies edge values to Y neighboring guard cells
*
*/
void local_copy_to_gc_y() {
#pragma omp parallel for
for( unsigned tid = 0; tid < ntiles.y * ntiles.x; tid ++ ) {
const auto tile_idx = make_int2( tid % ntiles.x, tid / ntiles.x );
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = d_buffer + tile_off;
{ // Copy from lower neighbour
int neighbor_ty = tile_idx.y;
neighbor_ty -= 1;
if ( periodic.y && neighbor_ty < 0 ) neighbor_ty += ntiles.y;
if ( neighbor_ty >= 0 ) {
auto * __restrict__ y_lower = d_buffer + (neighbor_ty * ntiles.x + tile_idx.x) * tile_vol;
for( unsigned j = 0; j < gc.y.lower; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ i + j * ext_nx.x ] = y_lower[ i + ( nx.y + j ) * ext_nx.x ];
}
}
}
}
{ // Copy from upper neighbour
int neighbor_ty = tile_idx.y;
neighbor_ty += 1;
if ( periodic.y && neighbor_ty >= static_cast<int>(ntiles.y) ) neighbor_ty -= ntiles.y;
if ( neighbor_ty < static_cast<int>(ntiles.y) ) {
auto * __restrict__ y_upper = d_buffer + (neighbor_ty * ntiles.x + tile_idx.x) * tile_vol;
for( unsigned j = 0; j < gc.y.upper; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ i + ( gc.y.lower + nx.y + j ) * ext_nx.x ] = y_upper[ i + ( gc.y.lower + j ) * ext_nx.x ];
}
}
}
}
}
}
#else
/**
* @brief Copies edge values to X neighboring guard cells
*
*/
void local_copy_to_gc_x() {
// Loop over tiles
for( unsigned ty = 0; ty < ntiles.y; ty ++ ) {
for( unsigned tx = 0; tx < ntiles.x; tx ++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
{ // Copy from lower neighbour
int neighbor_tx = tile_idx.x;
neighbor_tx -= 1;
if ( periodic.x && neighbor_tx < 0 ) neighbor_tx += ntiles.x;
if ( neighbor_tx >= 0 ) {
auto * __restrict__ x_lower = d_buffer + (tile_idx.y * ntiles.x + neighbor_tx) * tile_vol;
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.lower; i++ ) {
local[ i + j * ext_nx.x ] = x_lower[ nx.x + i + j * ext_nx.x ];
}
}
}
}
{ // Copy from upper neighbour
int neighbor_tx = tile_idx.x;
neighbor_tx += 1;
if ( periodic.x && neighbor_tx >= static_cast<int>(ntiles.x) ) neighbor_tx -= ntiles.x;
if ( neighbor_tx < static_cast<int>(ntiles.x) ) {
auto * __restrict__ x_upper = d_buffer + (tile_idx.y * ntiles.x + neighbor_tx) * tile_vol;
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.upper; i++ ) {
local[ gc.x.lower + nx.x + i + j * ext_nx.x ] = x_upper[ gc.x.lower + i + j * ext_nx.x ];
}
}
}
}
}
}
}
/**
* @brief Copies edge values to Y neighboring guard cells
*
*/
void local_copy_to_gc_y() {
// Loop over tiles
for( unsigned ty = 0; ty < ntiles.y; ty ++ ) {
for( unsigned tx = 0; tx < ntiles.x; tx ++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = d_buffer + tile_off;
{ // Copy from lower neighbour
int neighbor_ty = tile_idx.y;
neighbor_ty -= 1;
if ( periodic.y && neighbor_ty < 0 ) neighbor_ty += ntiles.y;
if ( neighbor_ty >= 0 ) {
auto * __restrict__ y_lower = d_buffer + (neighbor_ty * ntiles.x + tile_idx.x) * tile_vol;
for( unsigned j = 0; j < gc.y.lower; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ i + j * ext_nx.x ] = y_lower[ i + ( nx.y + j ) * ext_nx.x ];
}
}
}
}
{ // Copy from upper neighbour
int neighbor_ty = tile_idx.y;
neighbor_ty += 1;
if ( periodic.y && neighbor_ty >= static_cast<int>(ntiles.y) ) neighbor_ty -= ntiles.y;
if ( neighbor_ty < static_cast<int>(ntiles.y) ) {
auto * __restrict__ y_upper = d_buffer + (neighbor_ty * ntiles.x + tile_idx.x) * tile_vol;
for( unsigned j = 0; j < gc.y.upper; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ i + ( gc.y.lower + nx.y + j ) * ext_nx.x ] = y_upper[ i + ( gc.y.lower + j ) * ext_nx.x ];
}
}
}
}
}
}
}
#endif
/**
* @brief Copies x values to neighboring guard cells, including cells on
* other parallel nodes
*
*/
void copy_to_gc_x() {
// Get x neighbors
int lnode = part.get_neighbor(-1, 0 );
int unode = part.get_neighbor(+1, 0 );
// Post message receives
if ( lnode >= 0 ) msg_recv.lower->irecv( lnode, source::lower );
if ( unode >= 0 ) msg_recv.upper->irecv( unode, source::upper );
// Send message - lower neighbor
if ( lnode >= 0 ) {
unsigned int tx = 0;
for( unsigned ty = 0; ty < ntiles.y; ty++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
T __restrict__ * msg = & msg_send.lower-> buffer [ ty * nx.y * gc.x.upper ];
auto lbound = (ty > 0 ) ? gc.y.lower : 0;
auto ubound = gc.y.lower + nx.y + ((ty < ntiles.y - 1 ) ? 0 : gc.y.upper);
for( unsigned j = lbound; j < ubound; j++ ) {
for( unsigned i = 0; i < gc.x.upper; i++ ) {
msg[ j * gc.x.upper + i ] = local[ j * ext_nx.x + gc.x.lower + i ];
}
}
}
int msg_size = gc.x.upper * ( gc.y.lower + gnx.y + gc.y.upper );
msg_send.lower->isend( msg_size, lnode, dest::lower );
}
// Send message - upper neighbor
if ( unode >= 0 ) {
unsigned int tx = ntiles.x - 1;
for( unsigned ty = 0; ty < ntiles.y; ty++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
T __restrict__ * msg = & msg_send.upper-> buffer[ ty * nx.y * gc.x.lower ];
auto lbound = (ty > 0 ) ? gc.y.lower : 0;
auto ubound = gc.y.lower + nx.y + ((ty < ntiles.y - 1 ) ? 0 : gc.y.upper);
for( unsigned j = lbound; j < ubound; j++ ) {
for( unsigned i = 0; i < gc.x.lower; i++ ) {
msg[ j * gc.x.lower + i ] = local[ j * ext_nx.x + nx.x + i ];
}
}
}
int msg_size = gc.x.lower * ( gc.y.lower + gnx.y + gc.y.upper );
msg_send.upper->isend( msg_size, unode, dest::upper );
}
// Process local tiles
local_copy_to_gc_x();
// Receive message - lower neighbor
if ( lnode >= 0 ) {
msg_recv.lower-> wait();
unsigned int tx = 0;
for( unsigned ty = 0; ty < ntiles.y; ty++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
T __restrict__ * msg = & msg_recv.lower-> buffer[ ty * nx.y * gc.x.lower ];
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.lower; i++ ) {
local[ j * ext_nx.x + i ] = msg[ j * gc.x.lower + i ];
}
}
}
}
// Receive message - upper neighbor
if ( unode >= 0 ) {
msg_recv.upper-> wait();
unsigned int tx = ntiles.x-1;
for( unsigned ty = 0; ty < ntiles.y; ty++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
T __restrict__ * msg = & msg_recv.upper -> buffer[ ty * nx.y * gc.x.upper ];
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.upper; i++ ) {
local[ j * ext_nx.x + gc.x.lower + nx.x + i ] = msg[ j * gc.x.upper + i ];
}
}
}
}
// Wait for send messages to complete
if ( lnode >= 0 ) msg_send.lower->wait( );
if ( unode >= 0 ) msg_send.upper->wait( );
}
/**
* @brief Copies y values to neighboring guard cells, including cells on
* other parallel nodes
*
*/
void copy_to_gc_y() {
// Get y neighbors
int lnode = part.get_neighbor(0, -1);
int unode = part.get_neighbor(0, +1);
// Post message receives
if ( lnode >= 0 ) msg_recv.lower->irecv( lnode, source::lower );
if ( unode >= 0 ) msg_recv.upper->irecv( unode, source::upper );
/// @brief Message buffer y stride
const int msg_ystride = gc.x.lower + gnx.x + gc.x.upper;
// Post message sends
if ( lnode >= 0 ) {
unsigned int ty = 0;
for( unsigned tx = 0; tx < ntiles.x; tx++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
T __restrict__ * msg = & msg_send.lower-> buffer[ tx * nx.x ];
auto lbound = (tx > 0 ) ? gc.x.lower : 0;
auto ubound = gc.x.lower + nx.x + ((tx < ntiles.x - 1 ) ? 0 : gc.x.upper);
for( unsigned j = 0; j < gc.y.upper; j++ ) {
for( unsigned i = lbound; i < ubound; i++ ) {
msg[ j * msg_ystride + i ] = local[ ( gc.y.lower + j ) * ext_nx.x + i ];
}
}
}
int msg_size = ( gc.x.lower + gnx.x + gc.x.upper ) * gc.y.upper;
msg_send.lower->isend( msg_size, lnode, dest::lower );
}
if ( unode >= 0 ) {
unsigned int ty = ntiles.y-1;
for( unsigned tx = 0; tx < ntiles.x; tx++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
T __restrict__ * msg = & msg_send.upper -> buffer[ tx * nx.x ];
auto lbound = (tx > 0) ? gc.x.lower : 0;
auto ubound = gc.x.lower + nx.x + ((tx < ntiles.x - 1 ) ? 0 : gc.x.upper);
for( unsigned j = 0; j < gc.y.lower; j++ ) {
for( unsigned i = lbound; i < ubound; i++ ) {
msg[ j * msg_ystride + i ] = local[ ( nx.y + j ) * ext_nx.x + i ];
}
}
}
int msg_size = ( gc.x.lower + gnx.x + gc.x.upper ) * gc.y.lower;;
msg_send.upper -> isend( msg_size, unode, dest::upper );
}
// Process local tiles
local_copy_to_gc_y();
// Wait for receive messages to complete and copy data
if ( lnode >= 0 ) {
msg_recv.lower-> wait();
unsigned int ty = 0;
for( unsigned tx = 0; tx < ntiles.x; tx++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
T __restrict__ * msg = & msg_recv.lower-> buffer[ tx * nx.x ];
for( unsigned j = 0; j < gc.y.lower; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ j * ext_nx.x + i ] = msg[ j * msg_ystride + i ];
}
}
}
}
if ( unode >= 0 ) {
msg_recv.upper-> wait();
unsigned int ty = ntiles.y - 1;
for( unsigned tx = 0; tx < ntiles.x; tx++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = & d_buffer[ tile_off ];
T __restrict__ * msg = & msg_recv.upper-> buffer[ tx * nx.x ];
for( unsigned j = 0; j < gc.y.upper; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ ( gc.y.lower + nx.y + j ) * ext_nx.x + i ] = msg[ j * msg_ystride + i ];
}
}
}
}
// Wait for send messages to complete
if ( lnode >= 0 ) msg_send.lower->wait( );
if ( unode >= 0 ) msg_send.upper->wait( );
}
/**
* @brief Copies edge values to neighboring guard cells, including other
* parallel nodes
*
*/
void copy_to_gc() {
// Copy along x direction
copy_to_gc_x();
// Copy along y direction
copy_to_gc_y();
};
#ifdef _OPENMP
/**
* @brief Adds values from neighboring x guard cells to local data
*
*/
void local_add_from_gc_x() {
// Add along x direction
// Loop over tiles
#pragma omp parallel for
for( unsigned tid = 0; tid < ntiles.y * ntiles.x; tid ++ ) {
const auto tile_idx = make_int2( tid % ntiles.x, tid / ntiles.x );
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = d_buffer + tile_off;
{ // Add from lower neighbour
int neighbor_tx = tile_idx.x;
neighbor_tx -= 1;
if ( periodic.x && neighbor_tx < 0 ) neighbor_tx += ntiles.x;
if ( neighbor_tx >= 0 ) {
T * __restrict__ x_lower = d_buffer + (tile_idx.y * ntiles.x + neighbor_tx) * tile_vol;
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.upper; i++ ) {
local[ gc.x.lower + i + j * ext_nx.x ] += x_lower[ gc.x.lower + nx.x + i + j * ext_nx.x ];
}
}
}
}
{ // Add from upper neighbour
int neighbor_tx = tile_idx.x;
neighbor_tx += 1;
if ( periodic.x && neighbor_tx >= static_cast<int>(ntiles.x) ) neighbor_tx -= ntiles.x;
if ( neighbor_tx < static_cast<int>(ntiles.x) ) {
auto * __restrict__ x_upper = d_buffer + (tile_idx.y * ntiles.x + neighbor_tx) * tile_vol;
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.lower; i++ ) {
local[ nx.x + i + j * ext_nx.x ] += x_upper[ i + j * ext_nx.x ];
}
}
}
}
}
}
/**
* @brief Adds values from neighboring y guard cells to local data
*
*/
void local_add_from_gc_y(){
// Add along y direction
// Loop over tiles
#pragma omp parallel for
for( unsigned tid = 0; tid < ntiles.y * ntiles.x; tid ++ ) {
const auto tile_idx = make_int2( tid % ntiles.x, tid / ntiles.x );
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = d_buffer + tile_off;
{ // Add from lower neighbour
int neighbor_ty = tile_idx.y;
neighbor_ty -= 1;
if ( periodic.y && neighbor_ty < 0 ) neighbor_ty += ntiles.y;
if ( neighbor_ty >= 0 ) {
auto * __restrict__ y_lower = d_buffer + (neighbor_ty * ntiles.x + tile_idx.x) * tile_vol;
for( unsigned j = 0; j < gc.y.upper; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ i + ( gc.y.lower + j ) * ext_nx.x ] += y_lower[ i + ( gc.y.lower + nx.y + j ) * ext_nx.x ];
}
}
}
}
{ // Add from upper neighbour
int neighbor_ty = tile_idx.y;
neighbor_ty += 1;
if ( periodic.y && neighbor_ty >= static_cast<int>(ntiles.y) ) neighbor_ty -= ntiles.y;
if ( neighbor_ty < static_cast<int>(ntiles.y) ) {
auto * __restrict__ y_upper = d_buffer + (neighbor_ty * ntiles.x + tile_idx.x) * tile_vol;
for( unsigned j = 0; j < gc.y.lower; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ i + ( nx.y + j ) * ext_nx.x ] += y_upper[ i + j * ext_nx.x ];
}
}
}
}
}
};
#else
/**
* @brief Adds values from neighboring x guard cells to local data
*
*/
void local_add_from_gc_x() {
// Loop over tiles
for( unsigned ty = 0; ty < ntiles.y; ty ++ ) {
for( unsigned tx = 0; tx < ntiles.x; tx ++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = d_buffer + tile_off;
{ // Add from lower neighbour
int neighbor_tx = tile_idx.x;
neighbor_tx -= 1;
if ( periodic.x && neighbor_tx < 0 ) neighbor_tx += ntiles.x;
if ( neighbor_tx >= 0 ) {
T * __restrict__ x_lower = d_buffer + (tile_idx.y * ntiles.x + neighbor_tx) * tile_vol;
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.upper; i++ ) {
local[ gc.x.lower + i + j * ext_nx.x ] += x_lower[ gc.x.lower + nx.x + i + j * ext_nx.x ];
}
}
}
}
{ // Add from upper neighbour
int neighbor_tx = tile_idx.x;
neighbor_tx += 1;
if ( periodic.x && neighbor_tx >= static_cast<int>(ntiles.x) ) neighbor_tx -= ntiles.x;
if ( neighbor_tx < static_cast<int>(ntiles.x) ) {
auto * __restrict__ x_upper = d_buffer + (tile_idx.y * ntiles.x + neighbor_tx) * tile_vol;
for( unsigned j = 0; j < ext_nx.y; j++ ) {
for( unsigned i = 0; i < gc.x.lower; i++ ) {
local[ nx.x + i + j * ext_nx.x ] += x_upper[ i + j * ext_nx.x ];
}
}
}
}
}
}
}
/**
* @brief Adds values from neighboring x guard cells to local data
*
*/
void local_add_from_gc_y() {
for( unsigned ty = 0; ty < ntiles.y; ty ++ ) {
for( unsigned tx = 0; tx < ntiles.x; tx ++ ) {
const auto tile_idx = make_uint2( tx, ty );
const auto tid = tile_idx.y * ntiles.x + tile_idx.x;
const auto tile_off = tid * tile_vol;
auto * __restrict__ local = d_buffer + tile_off;
{ // Add from lower neighbour
int neighbor_ty = tile_idx.y;
neighbor_ty -= 1;
if ( periodic.y && neighbor_ty < 0 ) neighbor_ty += ntiles.y;
if ( neighbor_ty >= 0 ) {
auto * __restrict__ y_lower = d_buffer + (neighbor_ty * ntiles.x + tile_idx.x) * tile_vol;
for( unsigned j = 0; j < gc.y.upper; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ i + ( gc.y.lower + j ) * ext_nx.x ] += y_lower[ i + ( gc.y.lower + nx.y + j ) * ext_nx.x ];
}
}
}
}
{ // Add from upper neighbour
int neighbor_ty = tile_idx.y;
neighbor_ty += 1;
if ( periodic.y && neighbor_ty >= static_cast<int>(ntiles.y) ) neighbor_ty -= ntiles.y;
if ( neighbor_ty < static_cast<int>(ntiles.y) ) {
auto * __restrict__ y_upper = d_buffer + (neighbor_ty * ntiles.x + tile_idx.x) * tile_vol;
for( unsigned j = 0; j < gc.y.lower; j++ ) {
for( unsigned i = 0; i < ext_nx.x; i++ ) {
local[ i + ( nx.y + j ) * ext_nx.x ] += y_upper[ i + j * ext_nx.x ];