-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultigrid3d.cpp
1913 lines (1464 loc) · 65.8 KB
/
multigrid3d.cpp
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
#include <unistd.h>
#include <iostream>
#include <cstddef>
#include <iomanip>
#include <cassert>
#include <vector>
#include <cstdio>
#include <utility>
#include <math.h>
#include "allreduce.h"
#include "minimonitoring.h"
/* TODOs
- add clean version of the code:
- without asserts
- with simple loops, no optimization for contiguous lines, etc.
*/
MiniMon minimon;
using std::cout;
using std::setfill;
using std::setw;
using std::cerr;
using std::endl;
using std::setw;
using std::vector;
using TeamSpecT = dash::TeamSpec<3>;
using MatrixT = dash::NArray<double,3>;
using PatternT = typename MatrixT::pattern_type;
using StencilT = dash::halo::StencilPoint<3>;
using StencilSpecT = dash::halo::StencilSpec<StencilT,26>;
using CycleSpecT = dash::halo::GlobalBoundarySpec<3>;
using HaloT = dash::halo::HaloMatrixWrapper<MatrixT>;
using StencilOpT = dash::halo::StencilOperator<double,PatternT,StencilSpecT>;
/* for the smoothing operation, only the 6-point stencil is needed.
However, the prolongation operation also needs the */
constexpr StencilSpecT stencil_spec(
StencilT(0.5, -1, 0, 0), StencilT(0.5, 1, 0, 0),
StencilT(0.5, 0,-1, 0), StencilT(0.5, 0, 1, 0),
StencilT(0.5, 0, 0,-1), StencilT(0.5, 0, 0, 1),
StencilT(0.25, -1,-1, 0), StencilT( 0.25, 1, 1, 0),
StencilT(0.25, -1, 0,-1), StencilT( 0.25, 1, 0, 1),
StencilT(0.25, 0,-1,-1), StencilT( 0.25, 0, 1, 1),
StencilT(0.25, -1, 1, 0), StencilT( 0.25, 1,-1, 0),
StencilT(0.25, -1, 0, 1), StencilT( 0.25, 1, 0,-1),
StencilT(0.25, 0,-1, 1), StencilT( 0.25, 0, 1,-1),
StencilT(0.125, -1,-1,-1), StencilT( 0.125, 1,-1,-1),
StencilT(0.125, -1,-1, 1), StencilT( 0.125, 1,-1, 1),
StencilT(0.125, -1, 1,-1), StencilT( 0.125, 1, 1,-1),
StencilT(0.125, -1, 1, 1), StencilT( 0.125, 1, 1, 1));
constexpr CycleSpecT cycle_spec(
dash::halo::BoundaryProp::CUSTOM,
dash::halo::BoundaryProp::CUSTOM,
dash::halo::BoundaryProp::CUSTOM );
struct Level {
public:
using SizeSpecT = dash::SizeSpec<3>;
using DistSpecT = dash::DistributionSpec<3>;
/* now with double-buffering. src_grid and src_halo should only be read,
newgrid should only be written. dst_grid and dst_halo are only there to keep the other ones
before both are swapped in swap() */
public:
MatrixT* src_grid;
MatrixT* dst_grid;
MatrixT* rhs_grid; /* right hand side, doesn't need a halo */
HaloT* src_halo;
HaloT* dst_halo;
StencilOpT* src_op;
StencilOpT* dst_op;
/* this are the values of the 7 non-zero matrix values -- only 4 different values, though,
because the matrix is symmetric */
double acenter, ax, ay, az;
/* this is the factor for the right hand side, which is 0 at the finest grid. */
double ff;
/* Diagonal element of matrix M, which is the inverse of the diagonal of matrix A.
This factor multiplies the defect in $ f - Au $. */
double m;
/* sz, sy, sx are the dimensions in meters of the grid excluding the boundary regions */
double sz, sy, sx;
/* the maximum time step according to the stability condition for the
time simulation mode */
double dt;
/*
lz, ly, lx are the dimensions in meters of the grid including the boundary regions,
nz, ny, nx are th number of inner grid points per dimension, excluding the boundary regions,
therefore, lz,ly,lx are discretized into (nz+2)*(ny+2)*(nx+2) grid points
*/
Level( double lz, double ly, double lx,
size_t nz, size_t ny, size_t nx,
dash::Team& team, TeamSpecT teamspec ) :
_grid_1( SizeSpecT( nz, ny, nx ), DistSpecT( dash::BLOCKED, dash::BLOCKED, dash::BLOCKED ), team, teamspec ),
_grid_2( SizeSpecT( nz, ny, nx ), DistSpecT( dash::BLOCKED, dash::BLOCKED, dash::BLOCKED ), team, teamspec ),
_rhs_grid( SizeSpecT( nz, ny, nx ), DistSpecT( dash::BLOCKED, dash::BLOCKED, dash::BLOCKED ), team, teamspec ),
_halo_grid_1( _grid_1, cycle_spec, stencil_spec ),
_halo_grid_2( _grid_2, cycle_spec, stencil_spec ),
_stencil_op_1(_halo_grid_1.stencil_operator(stencil_spec)),
_stencil_op_2(_halo_grid_2.stencil_operator(stencil_spec)),
src_grid(&_grid_1), dst_grid(&_grid_2), rhs_grid(&_rhs_grid),
src_halo(&_halo_grid_1), dst_halo(&_halo_grid_2),
src_op(&_stencil_op_1),dst_op(&_stencil_op_2) {
assert( 1 < nz );
assert( 1 < ny );
assert( 1 < nx );
sz= lz;
sy= ly;
sx= lx;
double hz= lz/(nz+1);
double hy= ly/(ny+1);
double hx= lx/(nx+1);
/* This is the original setting for the linear system. */
/* stability condition: r <= 1/2 with r= dt/h^2 ==> dt <= 1/2*h^2
dtheta= ru*u_plus + ru*u_minus - 2*ru*u_center with ru=dt/hu^2 <= 1/2 */
double hmin= std::min( hz, std::min( hy, hx ) );
dt= 0.5*hmin*hmin;
ax= -1.0/hx/hx;
ay= -1.0/hy/hy;
az= -1.0/hz/hz;
acenter= -2.0*(ax+ay+az);
m= 1.0 / acenter;
ff= 1.0; /* factor for right-hand-side */
for ( uint32_t a= 0; a < team.size(); a++ ) {
if ( a == dash::myid() ) {
if ( 0 == a ) {
cout << "Level " <<
"dim. " << lz << "m×" << ly << "m×" << lz << "m " <<
"in grid of " << nz << "×" << ny << "×" << nx <<
" h_= " << hz << "," << hy << "," << hx <<
" with team of " << team.size() <<
" ⇒ a_= " << acenter << "," << ax << "," << ay << "," << az <<
" , m= " << m << " , ff= " << ff <<endl;
}
}
team.barrier();
}
}
/***
Alternative version of the constructor that takes the parent Level as the first argument.
From this, it can get the original physical dimensions lz, ly, lx and the original
grid distances hy, hy, hx.
nz, ny, nx are th number of inner grid points per dimension, excluding the boundary regions
*/
Level( const Level& parent,
size_t nz, size_t ny, size_t nx,
dash::Team& team, TeamSpecT teamspec ) :
_grid_1( SizeSpecT( nz, ny, nx ), DistSpecT( dash::BLOCKED, dash::BLOCKED, dash::BLOCKED ), team, teamspec ),
_grid_2( SizeSpecT( nz, ny, nx ), DistSpecT( dash::BLOCKED, dash::BLOCKED, dash::BLOCKED ), team, teamspec ),
_rhs_grid( SizeSpecT( nz, ny, nx ), DistSpecT( dash::BLOCKED, dash::BLOCKED, dash::BLOCKED ), team, teamspec ),
_halo_grid_1( _grid_1, cycle_spec, stencil_spec ),
_halo_grid_2( _grid_2, cycle_spec, stencil_spec ),
_stencil_op_1(_halo_grid_1.stencil_operator(stencil_spec)),
_stencil_op_2(_halo_grid_2.stencil_operator(stencil_spec)),
src_grid(&_grid_1), dst_grid(&_grid_2), rhs_grid(&_rhs_grid),
src_halo(&_halo_grid_1), dst_halo(&_halo_grid_2),
src_op(&_stencil_op_1),dst_op(&_stencil_op_2) {
assert( 1 < nz );
assert( 1 < ny );
assert( 1 < nx );
sz= parent.sz;
sy= parent.sy;
sx= parent.sx;
ax= parent.ax;
ay= parent.ay;
az= parent.az;
acenter= parent.acenter;
ff= parent.ff;
m= parent.m;
dt= parent.dt;
for ( uint32_t a= 0; a < team.size(); a++ ) {
if ( a == dash::myid() ) {
if ( 0 == a ) {
cout << "Level with a parent level " <<
"in grid of " << nz << "×" << ny << "×" << nx <<
" with team of " << team.size() <<
" ⇒ a_= " << acenter << "," << ax << "," << ay << "," << az <<
" , m= " << m << " , ff= " << ff << endl;
}
}
team.barrier();
}
}
Level() = delete;
/** swap grid and halos for the double buffering scheme */
void swap() {
std::swap( src_halo, dst_halo );
std::swap( src_grid, dst_grid );
std::swap( src_op, dst_op );
}
double max_dt() const {
/* stability condition: r <= 1/2 with r= dt/h^2 ==> dt <= 1/2*h^2
dtheta= ru*u_plus + ru*u_minus - 2*ru*u_center with ru=dt/hu^2 <= 1/2 */
cout << " dt= " << dt << endl;
return dt;
}
private:
MatrixT _grid_1;
MatrixT _grid_2;
HaloT _halo_grid_1;
HaloT _halo_grid_2;
MatrixT _rhs_grid;
StencilOpT _stencil_op_1;
StencilOpT _stencil_op_2;
};
void initgrid( Level& level ) {
/* not strictly necessary but it also avoids NAN values */
dash::fill( level.src_grid->begin(), level.src_grid->end(), 0.0 );
dash::fill( level.dst_grid->begin(), level.dst_grid->end(), 0.0 );
dash::fill( level.rhs_grid->begin(), level.rhs_grid->end(), 0.0 );
level.src_grid->barrier();
}
/* apply boundary value settings, where the top and bottom planes have a
hot circle in the middle and everything else is cold */
void initboundary( Level& level ) {
using index_t = dash::default_index_t;
double gd= level.src_grid->extent(0);
double gh= level.src_grid->extent(1);
double gw= level.src_grid->extent(2);
/* This way of setting boundaries uses subsampling on the top and bottom
planes to determine the border values. This is another logical way that
may be convenient sometimes. It guarantees that the boundary values on all
the levels match.
All other sides are constant at 0.0 degrees. The top an bottom circles are
hot with 10.0 degrees. */
auto lambda= [gd,gh,gw]( const auto& coords ) {
index_t z= coords[0];
index_t y= coords[1];
index_t x= coords[2];
double ret= 1.0;
/* for simplicity make every side uniform */
if ( -1 == z || gd == z ) {
/* radius differs on top and bottom plane */
//double r= ( -1 == z ) ? 0.4 : 0.3;
double r= 0.4;
double r2= r*r;
double lowvalue= 2.0;
double highvalue= 9.0;
double midx= 0.5;
double midy= 0.5;
/* At entry (x/gw,y/gh) we sample the
rectangle [ x/gw,(x+1)/gw ) x [ y/gw, (y+1)/gh ) with m² points. */
int32_t m= 3;
int32_t m2= m*m;
double sum= 0.0;
double weight= 0.0;
for ( double iy= -m+1; iy < m; iy++ ) {
for ( double ix= -m+1; ix < m; ix++ ) {
double sx= (x+ix/m)/(gw-1);
double sy= (y+iy/m)/(gh-1);
double d2= (sx-midx)*(sx-midx) + (sy-midy)*(sy-midy);
sum += ( d2 <= r2 ) ? highvalue : lowvalue;
weight += 1.0;
}
}
ret = sum / weight;
}
return ret;
};
level.src_halo->set_custom_halos( lambda );
level.dst_halo->set_custom_halos( lambda );
}
/* sets all boundary values to 0, that is what is neede on the coarser grids */
void initboundary_zero( Level& level ) {
using index_t = dash::default_index_t;
auto lambda= []( const auto& coords ) { return 0.0; };
level.src_halo->set_custom_halos( lambda );
level.dst_halo->set_custom_halos( lambda );
}
/* check some grid values for 3d mirror symmetry. This should hold for
appropriate boundary conditions and a correct solver.
Here we use global accesses for simplicity. */
bool check_symmetry( MatrixT& grid, double eps ) {
if ( 0 == dash::myid() ) {
size_t w= grid.extent(2);
size_t h= grid.extent(1);
size_t d= grid.extent(0);
size_t m= std::min( std::min( w, h ), d ) /2;
/* x-y-z diagonals */
for ( size_t t= 0; t < m; ++t ) {
double first= grid[d/2+t][h/2+t][w/2+t];
if ( std::fabs( first - grid[d/2+t][h/2+t][w/2-t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2+t][h/2-t][w/2+t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2+t][h/2-t][w/2-t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2-t][h/2+t][w/2+t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2-t][h/2+t][w/2-t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2-t][h/2-t][w/2+t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2-t][h/2-t][w/2-t] ) > eps ) return false;
}
/* x-y diagonals */
for ( size_t t= 0; t < m; ++t ) {
double first= grid[d/2][h/2+t][w/2+t];
if ( std::fabs( first - grid[d/2][h/2+t][w/2-t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2][h/2-t][w/2+t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2][h/2-t][w/2-t] ) > eps ) return false;
}
/* y-z diagonals */
for ( size_t t= 0; t < m; ++t ) {
double first= grid[d/2+t][h/2+t][w/2];
if ( std::fabs( first - grid[d/2+t][h/2+t][w/2] ) > eps ) return false;
if ( std::fabs( first - grid[d/2+t][h/2-t][w/2] ) > eps ) return false;
if ( std::fabs( first - grid[d/2-t][h/2+t][w/2] ) > eps ) return false;
if ( std::fabs( first - grid[d/2-t][h/2-t][w/2] ) > eps ) return false;
}
/* x-z diagonals */
for ( size_t t= 0; t < m; ++t ) {
double first= grid[d/2+t][h/2][w/2+t];
if ( std::fabs( first - grid[d/2+t][h/2][w/2-t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2-t][h/2][w/2+t] ) > eps ) return false;
if ( std::fabs( first - grid[d/2-t][h/2][w/2-t] ) > eps ) return false;
}
}
return true;
}
void scaledownboundary( Level& fine, Level& coarse ) {
assert( coarse.src_grid->extent(2)*2 == fine.src_grid->extent(2) );
assert( coarse.src_grid->extent(1)*2 == fine.src_grid->extent(1) );
assert( coarse.src_grid->extent(0)*2 == fine.src_grid->extent(0) );
size_t dmax= coarse.src_grid->extent(0);
size_t hmax= coarse.src_grid->extent(1);
//size_t wmax= coarse.src_grid->extent(2);
auto finehalo= fine.src_halo;
auto lambda= [&finehalo,&dmax,&hmax]( const auto& coord ) {
auto coordf= coord;
for( auto& c : coordf ) {
if ( c > 0 ) c *= 2;
}
if ( -1 == coord[0] || dmax == coord[0] ) {
/* z plane */
return 0.25 * (
*finehalo->halo_element_at_global( { coordf[0], coordf[1]+0, coordf[2]+0 } ) +
*finehalo->halo_element_at_global( { coordf[0], coordf[1]+0, coordf[2]+1 } ) +
*finehalo->halo_element_at_global( { coordf[0], coordf[1]+1, coordf[2]+0 } ) +
*finehalo->halo_element_at_global( { coordf[0], coordf[1]+1, coordf[2]+1 } ) );
} else if ( -1 == coord[1] || hmax == coord[1] ) {
/* y plane */
return 0.25 * (
*finehalo->halo_element_at_global( { coordf[0]+0, coordf[1], coordf[2]+0 } ) +
*finehalo->halo_element_at_global( { coordf[0]+0, coordf[1], coordf[2]+1 } ) +
*finehalo->halo_element_at_global( { coordf[0]+1, coordf[1], coordf[2]+0 } ) +
*finehalo->halo_element_at_global( { coordf[0]+1, coordf[1], coordf[2]+1 } ) );
} else /* if ( -1 == coord[2] || wmax == coord[2] ) */ {
/* x plane */
return 0.25 * (
*finehalo->halo_element_at_global( { coordf[0]+0, coordf[1]+0, coordf[2] } ) +
*finehalo->halo_element_at_global( { coordf[0]+0, coordf[1]+1, coordf[2] } ) +
*finehalo->halo_element_at_global( { coordf[0]+1, coordf[1]+0, coordf[2] } ) +
*finehalo->halo_element_at_global( { coordf[0]+1, coordf[1]+1, coordf[2] } ) );
}
};
coarse.src_halo->set_custom_halos( lambda );
coarse.dst_halo->set_custom_halos( lambda );
}
void scaledown( Level& fine, Level& coarse ) {
using signed_size_t = typename std::make_signed<size_t>::type;
auto& finegrid= *fine.src_grid;
auto& fine_rhs_grid= *fine.rhs_grid;
auto& coarsegrid= *coarse.src_grid;
auto& coarse_rhs_grid= *coarse.rhs_grid;
auto& finehalo = *fine.src_halo;
// stencil points for scale down with coefficients
dash::halo::StencilSpec<StencilT,6> stencil_spec(
StencilT(-fine.az, -1, 0, 0), StencilT(-fine.az, 1, 0, 0),
StencilT(-fine.ay, 0,-1, 0), StencilT(-fine.ay, 0, 1, 0),
StencilT(-fine.ax, 0, 0,-1), StencilT(-fine.ax, 0, 0, 1)
);
// scaledown
minimon.start();
assert( (coarsegrid.extent(2)+1) * 2 == finegrid.extent(2)+1 );
assert( (coarsegrid.extent(1)+1) * 2 == finegrid.extent(1)+1 );
assert( (coarsegrid.extent(0)+1) * 2 == finegrid.extent(0)+1 );
const auto& extentc= coarsegrid.local.extents();
const auto& cornerc= coarsegrid.pattern().global( {0,0,0} );
const auto& extentf= finegrid.local.extents();
const auto& cornerf= finegrid.pattern().global( {0,0,0} );
assert( cornerc[0] * 2 == cornerf[0] );
assert( cornerc[1] * 2 == cornerf[1] );
assert( cornerc[2] * 2 == cornerf[2] );
assert( 0 == cornerc[0] %2 );
assert( 0 == cornerc[1] %2 );
assert( 0 == cornerc[2] %2 );
assert( extentc[0] * 2 == extentf[0] || extentc[0] * 2 +1 == extentf[0] );
assert( extentc[1] * 2 == extentf[1] || extentc[1] * 2 +1 == extentf[1] );
assert( extentc[2] * 2 == extentf[2] || extentc[2] * 2 +1 == extentf[2] );
/* Here we $ r= f - Au $ on the fine grid and 'straigth injection' to the
rhs of the coarser grid in one. Therefore, we don't need a halo of the fine
grid, because the stencil neighbor points on the fine grid are always there
for a coarse grid point.
According to the text book (Introduction to Algebraic Multigrid -- Course notes
of an algebraic multigrid course at univertisty of Heidelberg in Wintersemester
1998/99, Version 1.1 by Christian Wagner http://www.mgnet.org/mgnet/papers/Wagner/amgV11.pdf)
there should by an extra factor 1/2^3 for the coarse value. But this doesn't seem to work,
factor 4.0 works much better. */
double extra_factor= 4.0;
/* 1) start async halo exchange for fine grid*/
finehalo.update_async();
// iterates over all inner elements and calculates value for coarse rhs grid
auto stencil_op_fine = fine.src_halo->stencil_operator(stencil_spec);
for ( signed_size_t z= 1; z < extentc[0] - 1 ; z++ ) {
for ( signed_size_t y= 1; y < extentc[1] - 1 ; y++ ) {
for ( signed_size_t x= 1; x < extentc[2] - 1 ; x++ ) {
coarse_rhs_grid.local[z][y][x] = extra_factor * (
fine.ff * fine_rhs_grid.local[2*z+1][2*y+1][2*x+1] +
stencil_op_fine.inner.get_value_at({2*z+1,2*y+1,2*x+1}, -fine.acenter));
}
}
}
/* 3) set coarse grid to 0.0 */
dash::fill( coarsegrid.begin(), coarsegrid.end(), 0.0 );
/* 4) wait for async halo exchange. Technically, we need only the back halos in every
dimension and only for the front unit per dimension. However, we do the halo update
collectvely to keep it managable. */
finehalo.wait();
auto& stencil_op_coarse = *coarse.src_op;
auto* coarse_rhs_begin = coarse_rhs_grid.lbegin();
// update all boundary elements for coarse rhs grid
// coarse grid halo wrapper used to get coordinates for coarse rhs grid
// elements
auto bend = stencil_op_coarse.boundary.end();
for( auto it = stencil_op_coarse.boundary.begin(); it != bend; ++it ) {
const auto& coords = it.coords();
// coarse coords to fine grid coords
decltype(coords) coords_fine = {2*coords[0] + 1, 2*coords[1] + 1, 2*coords[2] + 1};
// updates value for coarse rhs grid
coarse_rhs_begin[it.lpos()] = extra_factor * (
fine.ff * fine_rhs_grid.local[coords_fine[0]][coords_fine[1]][coords_fine[2]] +
// default operation std::plus used for stencil point and center values
stencil_op_fine.boundary.get_value_at(coords_fine, -fine.acenter));
}
minimon.stop( "scaledown", finegrid.team().size(), finegrid.local_size() );
}
/* this version uses a correct prolongation from the coarser grid of (2^n)^3 to (2^(n+1))^3
elements. Note that it is 2^n elements per dimension instead of 2^n -1!
This version loops over the coarse grid */
//void scaleup_loop_coarse( Level& coarse, Level& fine ) {
void scaleup( Level& coarse, Level& fine ) {
using signed_size_t = typename std::make_signed<size_t>::type;
MatrixT& coarsegrid= *coarse.src_grid;
MatrixT& finegrid= *fine.src_grid;
// scaleup
minimon.start();
assert( (coarsegrid.extent(2)+1) * 2 == finegrid.extent(2)+1 );
assert( (coarsegrid.extent(1)+1) * 2 == finegrid.extent(1)+1 );
assert( (coarsegrid.extent(0)+1) * 2 == finegrid.extent(0)+1 );
const auto& extentc= coarsegrid.pattern().local_extents();
const auto& cornerc= coarsegrid.pattern().global( {0,0,0} );
const auto& extentf= finegrid.pattern().local_extents();
const auto& cornerf= finegrid.pattern().global( {0,0,0} );
assert( cornerc[0] * 2 == cornerf[0] );
assert( cornerc[1] * 2 == cornerf[1] );
assert( cornerc[2] * 2 == cornerf[2] );
assert( 0 == cornerc[0] %2 );
assert( 0 == cornerc[1] %2 );
assert( 0 == cornerc[2] %2 );
assert( extentc[0] * 2 == extentf[0] || extentc[0] * 2 +1 == extentf[0] );
assert( extentc[1] * 2 == extentf[1] || extentc[1] * 2 +1 == extentf[1] );
assert( extentc[2] * 2 == extentf[2] || extentc[2] * 2 +1 == extentf[2] );
/* if last element in coarse grid per dimension has no 2*i+2 element in
the local fine grid, then handle it as a separate loop using halo.
sub[i] is always 0 or 1 */
std::array< size_t, 3 > sub;
for ( uint32_t i= 0; i < 3; ++i ) {
sub[i]= ( extentc[i] * 2 == extentf[i] ) ? 1 : 0;
}
/* start async halo exchange for coarse grid*/
coarse.src_halo->update_async();
/* second loop over the coarse grid and add the contributions to the
fine grid elements */
/* this is the iterator-ized version of the code */
auto& stencil_op_fine = *fine.src_op;
// set inner elements
for ( signed_size_t z= 1; z < extentc[0] - 1; z++ ) {
for ( signed_size_t y= 1; y < extentc[1] - 1; y++ ) {
for ( signed_size_t x= 1; x < extentc[2] - 1; x++ ) {
stencil_op_fine.inner.set_values_at({2*z+1, 2*y+1,2*x+1},
coarsegrid.local[z][y][x], 1.0,std::plus<double>());
}
}
}
// set values for boundary elements, halo elements are excluded
auto bend = coarse.src_op->boundary.end();
for (auto it = coarse.src_op->boundary.begin(); it != bend; ++it ) {
const auto& coords = it.coords();
stencil_op_fine.boundary.set_values_at( {2*coords[0]+1, 2*coords[1]+1,
2*coords[2]+1}, *it, 1.0, std::plus<double>());
}
/* wait for async halo exchange */
coarse.src_halo->wait();
/* do the remaining updates with contributions from the coarse halo
for 6 planes, 12 edges, and 8 corners */
const auto& halo_block = coarse.src_halo->halo_block();
const auto& view = halo_block.view();
const auto& specs = stencil_op_fine.stencil_spec().specs();
// iterates over all halo regions to find and get all halo regions before
// center -> only needed for element update
for(const auto& region : halo_block.halo_regions()) {
// region filter -> custom halo regions and regions behind center are
// excluded
if(region.is_custom_region() ||
(region.spec()[0] == 2 && sub[0]) ||
(region.spec()[1] == 2 && sub[1]) ||
(region.spec()[2] == 2 && sub[2])) {
continue;
}
// iterates over all region elements und updates all elements in fine
// grid, except halo elements
auto region_end = region.end();
for(auto it = region.begin(); it != region_end; ++it) {
auto coords = it.gcoords();
// pointer to halo element
double* halo_element = coarse.src_halo->halo_element_at_global(coords);
// if halo element == nullptr no halo element exists for the given
// coordinates -> continue with next element
if(halo_element == nullptr)
continue;
// convert global coordinate to local and fine grid coordinate
for(auto d = 0; d < 3; d++) {
coords[d] -= view.offset(d); // to local
if(coords[d] < 0 )
continue;
coords[d] = coords[d] * 2 + 1; // to fine grid
}
// iterates over all stencil points
for(auto i = 0; i < specs.size(); ++i) {
// returns pair -> first stencil_point adjusted coords, second check for halo
auto coords_stencilp = specs[i].stencil_coords_check_abort(coords,
stencil_op_fine.view_local());
/*
* Checks if stencil point points to a local memory element.
* if its points to a halo element continue with next stencil point
*/
if(coords_stencilp.second)
continue;
// set new value for stencil point element
auto offset = stencil_op_fine.get_offset(coords_stencilp.first);
finegrid.lbegin()[offset] += specs[i].coefficient() * *halo_element;
}
}
}
/* how to calculate the number of flops here: for every element there are 2 flop (one add, one mul),
then calculate the number of finegrid points that receive a contribution from a coarse grid point with
coefficient 1.0, 0.5, 0.25, an 0.125 separately. Consider the case where a unit is last in the distributions
in any dimension, which is marked with 'sub[.]==1'. In those cases change '(extentc[.]-1)' --> '(extentc[.]-1+sub[.])'
Then sum them up and simplify. */
minimon.stop( "scaleup", coarsegrid.team().size() /* param */, coarsegrid.local_size() /* elem */,
(2*extentc[0]-1+sub[0])*(2*extentc[1]-1+sub[1])*(2*extentc[2]-1+sub[2])*2 /* flops */ );
}
void transfertofewer( Level& source /* with larger team*/, Level& dest /* with smaller team */ ) {
/* should only be called by the smaller team */
assert( 0 == dest.src_grid->team().position() );
cout << "unit " << dash::myid() << " transfertofewer" << endl;
/* we need to find the coordinates that the local unit needs to receive
from several other units that are not in this team */
/* we can safely assume that the source blocks are copied entirely */
std::array< long int, 3 > corner= dest.src_grid->pattern().global( {0,0,0} );
std::array< long unsigned int, 3 > sizes= dest.src_grid->pattern().local_extents();
/*
cout << " start coord: " <<
corner[0] << ", " << corner[1] << ", " << corner[2] << endl;
cout << " extents: " <<
sizes[0] << ", " << sizes[1] << ", " << sizes[2] << endl;
cout << " dest local dist " << dest.src_grid->lend() - dest.src_grid->lbegin() << endl;
cout << " dest global dist " << dest.src_grid->end() - dest.src_grid->begin() << endl;
cout << " src local dist " << source.src_grid->lend() - source.src_grid->lbegin() << endl;
cout << " src global dist " << source.src_grid->end() - source.src_grid->begin() << endl;
*/
/* Can I do this any cleverer than loops over the n-1 non-contiguous
dimensions and then a dash::copy for the 1 contiguous dimension? */
/*
for ( uint32_t z= 0; z < sizes[0]; z++ ) {
for ( uint32_t y= 0; y < sizes[1]; y++ ) {
for ( uint32_t x= 0; x < sizes[2]; x++ ) {
(*dest.src_grid)(z,y,x)= (*source.src_grid)(z,y,x);
}
}
}
*/
for ( uint32_t z= 0; z < sizes[0]; z++ ) {
for ( uint32_t y= 0; y < sizes[1]; y++ ) {
size_t offset= ((corner[0]+z)*sizes[1]+y)*sizes[2];
std::copy( source.src_grid->begin() + offset, source.src_grid->begin() + offset + sizes[2],
&dest.src_grid->local[z][y][0] );
std::copy( source.rhs_grid->begin() + offset, source.rhs_grid->begin() + offset + sizes[2],
&dest.rhs_grid->local[z][y][0] );
//dash::copy( start, start + sizes[2], &dest.src_grid->local[z][y][0] );
//dash::copy( source.grid.begin()+40, source.grid.begin()+48, buf );
}
}
}
void transfertomore( Level& source /* with smaller team*/, Level& dest /* with larger team */ ) {
/* should only be called by the smaller team */
assert( 0 == source.src_grid->team().position() );
cout << "unit " << dash::myid() << " transfertomore" << endl;
/* we need to find the coordinates that the local unit needs to receive
from several other units that are not in this team */
/* we can safely assume that the source blocks are copied entirely */
std::array< long int, 3 > corner= source.src_grid->pattern().global( {0,0,0} );
std::array< long unsigned int, 3 > sizes= source.src_grid->pattern().local_extents();
/*
cout << " start coord: " <<
corner[0] << ", " << corner[1] << ", " << corner[2] << endl;
cout << " extents: " <<
sizes[0] << ", " << sizes[1] << ", " << sizes[2] << endl;
cout << " dest local dist " << source.src_grid->lend() - source.src_grid->lbegin() << endl;
cout << " dest global dist " << source.src_grid->end() - source.src_grid->begin() << endl;
cout << " src local dist " << dest.src_grid->lend() - dest.src_grid->lbegin() << endl;
cout << " src global dist " << dest.src_grid->end() - dest.src_grid->begin() << endl;
*/
/* stupid but functional version for the case with only one unit in the smaller team, very slow individual accesses */
/*
for ( uint32_t z= 0; z < sizes[0]; z++ ) {
for ( uint32_t y= 0; y < sizes[1]; y++ ) {
for ( uint32_t x= 0; x < sizes[2]; x++ ) {
(*dest.src_grid)(z,y,x)= (*source.src_grid)(z,y,x);
}
}
}
*/
for ( uint32_t z= 0; z < sizes[0]; z++ ) {
for ( uint32_t y= 0; y < sizes[1]; y++ ) {
size_t offset= ((corner[0]+z)*sizes[1]+y)*sizes[2];
std::copy( &source.src_grid->local[z][y][0], &source.src_grid->local[z][y][0] + sizes[2],
dest.src_grid->begin() + offset );
std::copy( &source.rhs_grid->local[z][y][0], &source.rhs_grid->local[z][y][0] + sizes[2],
dest.rhs_grid->begin() + offset );
//dash::copy( start, start + sizes[2], &dest.src_grid->local[z][y][0] );
//dash::copy( source.grid.begin()+40, source.grid.begin()+48, buf );
}
}
//std::copy( source.src_grid->begin(), source.src_grid->end(), dest.src_grid->begin() );
}
/**
Smoothen the given level from oldgrid+src_halo to newgrid. Call Level::swap() at the end.
The parallel global residual is returned as a return parameter, but only
if it is not NULL because then the expensive parallel reduction is just avoided.
*/
double smoothen( Level& level, Allreduce& res, double coeff= 1.0 ) {
SCOREP_USER_FUNC()
uint32_t par= level.src_grid->team().size();
// smoothen
minimon.start();
level.src_grid->barrier();
size_t ld= level.src_grid->local.extent(0);
size_t lh= level.src_grid->local.extent(1);
size_t lw= level.src_grid->local.extent(2);
double localres= 0.0;
double ax= level.ax;
double ay= level.ay;
double az= level.az;
double ac= level.acenter;
double ff= level.ff;
double m= level.m;
const double c= coeff;
// async halo update
level.src_halo->update_async();
// smoothen_inner
minimon.start();
// update inner
/* the start value for both, the y loop and the x loop is 1 because either there is
a border area next to the halo -- then the first column or row is covered below in
the border update -- or there is an outside border -- then the first column or row
contains the boundary values. */
#if 1
auto p_rhs= level.rhs_grid->lbegin();
level.src_op->inner.update(level.dst_grid->lbegin(),
[&](auto* center, auto* center_dst, auto offset, const auto& offsets) {
double dtheta= m * (
ff * p_rhs[offset] -
ax * ( center[offsets[4]] + center[offsets[5]] ) -
ay * ( center[offsets[2]] + center[offsets[3]] ) -
az * ( center[offsets[0]] + center[offsets[1]] ) -
ac * *center );
localres= std::max( localres, std::fabs( dtheta ) );
*center_dst = *center + c * dtheta;
});
#else
auto next_layer_off = lw * lh;
auto core_offset = lw * (lh + 1) + 1;
for ( size_t z= 1; z < ld-1; z++ ) {
for ( size_t y= 1; y < lh-1; y++ ) {
/* this should eventually be done with Alpaka or Kokkos to look
much nicer but still be fast */
const double* __restrict p_core= level.src_grid->lbegin() + core_offset;
const double* __restrict p_east= p_core + 1;
const double* __restrict p_west= p_core - 1;
const double* __restrict p_north= p_core + lw;
const double* __restrict p_south= p_core - lw;
const double* __restrict p_up= p_core + next_layer_off;
const double* __restrict p_down= p_core - next_layer_off;
const double* __restrict p_rhs= level.rhs_grid->lbegin() + core_offset;
double* __restrict p_new= level.dst_grid->lbegin() + core_offset;
for ( size_t x= 1; x < lw-1; x++ ) {
/*
stability condition: r <= 1/2 with r= dt/h^2 ==> dt <= 1/2*h^2
dtheta= ru*u_plus + ru*u_minus - 2*ru*u_center with ru=dt/hu^2 <= 1/2
*/
double dtheta= m * (
ff * *p_rhs -
ax * ( *p_east + *p_west ) -
ay * ( *p_north + *p_south ) -
az * ( *p_up + *p_down ) -
ac * *p_core );
*p_new= *p_core + c * dtheta;
localres= std::max( localres, std::fabs( dtheta ) );
p_core++;
p_east++;
p_west++;
p_north++;
p_south++;
p_up++;
p_down++;
p_rhs++;
p_new++;
}
core_offset += lw;
}
core_offset += 2 * lw;
}
#endif
minimon.stop( "smoothen_inner", par, /* elements */ (ld-2)*(lh-2)*(lw-2), /* flops */ 16*(ld-2)*(lh-2)*(lw-2), /*loads*/ 7*(ld-2)*(lh-2)*(lw-2), /* stores */ (ld-2)*(lh-2)*(lw-2) );
// smoothen_wait
minimon.start();
// wait for async halo update
level.src_halo->wait();
minimon.stop( "smoothen_wait", par, /* elements */ ld*lh*lw );
// smoothen_collect
minimon.start();
/* unit 0 (of any active team) waits until all local residuals from all
other active units are in */
res.collect_and_spread( level.src_grid->team() );
minimon.stop( "smoothen_collect", par );
// smoothen_outer
minimon.start();
/// begin pointer of local block, needed because halo border iterator is read-only
auto grid_local_begin= level.dst_grid->lbegin();
auto rhs_grid_local_begin= level.rhs_grid->lbegin();
auto bend = level.src_op->boundary.end();
// update border area
for( auto it = level.src_op->boundary.begin(); it != bend; ++it ) {
double dtheta= m * (
ff * rhs_grid_local_begin[ it.lpos() ] -
ax * ( it.value_at(4) + it.value_at(5) ) -
ay * ( it.value_at(2) + it.value_at(3) ) -
az * ( it.value_at(0) + it.value_at(1) ) -
ac * *it );
grid_local_begin[ it.lpos() ]= *it + c * dtheta;
localres= std::max( localres, std::fabs( dtheta ) );
}
minimon.stop( "smoothen_outer", par, /* elements */ 2*(ld*lh+lh*lw+lw*ld),
/* flops */ 16*(ld*lh+lh*lw+lw*ld), /*loads*/ 7*(ld*lh+lh*lw+lw*ld), /* stores */ (ld*lh+lh*lw+lw*ld) );
// smoothen_wait_res
minimon.start();
res.wait( level.src_grid->team() );
/* global residual from former iteration */
double oldres= res.get();
res.set( &localres, level.src_grid->team() );
minimon.stop( "smoothen_wait_res", par );
level.swap();
minimon.stop( "smoothen", par, /* elements */ ld*lh*lw,
/* flops */ 16*ld*lh*lw, /*loads*/ 7*ld*lh*lw, /* stores */ ld*lh*lw );
return oldres;
}
//#define DETAILOUTPUT 1
template<typename Iterator>
void recursive_cycle( Iterator it, Iterator itend,
uint32_t beta, uint32_t gamma, double epsilon, Allreduce& res ) {
SCOREP_USER_FUNC()
Iterator itnext( it );
++itnext;
/* reached end of recursion? */
if ( itend == itnext ) {
/* smoothen completely */
uint32_t j= 0;
res.reset( (*it)->src_grid->team() );
while ( res.get() > epsilon ) {
/* need global residual for iteration count */
smoothen( **it, res );
j++;
}
if ( 0 == dash::myid() ) {
cout << "smoothing " <<
(*it)->src_grid->extent(2) << "×" <<