-
Notifications
You must be signed in to change notification settings - Fork 7
/
enuts.h
2893 lines (2061 loc) · 117 KB
/
enuts.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
/**
* @file enuts.h
* @author Milinda Fernando (milinda@cs.utah.edu)
* @brief Spatially adaptive non uniform time stepping framework.
* @version 0.1
* @date 2019-07-12
* @copyright Copyright (c) 2019, School of Computing, University of Utah.
*
*/
#pragma once
#include "mesh.h"
#include <functional>
#include "block.h"
#include "point.h"
#include <assert.h>
#include "ts.h"
#include "ets.h"
#include "meshUtils.h"
#include <bitset>
#include "subSM.h"
#include "oct2vtk.h"
#include <iostream>
#include "blkAsync.h"
#include "waveletAMR.h"
#include "mathMeshUtils.h"
#define __LTS_BLK_SYNC_TIME_COMP_TOL__ 1e-6
namespace ts
{
/**@brief: Meta-data structure to compute the correct weight for the LTS wpart. */
struct WeightedPartitionData
{
unsigned int lmin;
unsigned int lmax;
unsigned int (*tfac) (unsigned int blev, unsigned int lmin, unsigned int lmax);
};
/**@brief: wpart meta data, (only should be used in the LTS class. )*/
static WeightedPartitionData wpart_meta;
/**
* @brief : simple structure to support storing of a single time step (explicit methods)
* note that the stages are numbered from 1 to m_uiNumStages.
* @tparam T : vector type
*/
template <typename T>
struct BlockTimeStep
{
public:
/**@brief: ets stages
* stage 0 : input vector
* stage 1 : ets stage 1
* .
* .
* stage p : ets stage p
* stage p+1 : output vector
*/
std::vector< BlockAsyncVector<T> > _vec;
/**@brief: rk stage*/
unsigned int _rks = ETS_STAGE_DEFAULT;
/**@brief: block time*/
unsigned int _time = LOOK_UP_TABLE_DEFAULT;
/**
* @brief allocate the Block time step vector.
* @param numVec : number of vectors per block.
* @param blkid : block id
* @param sz : sizes of the each dimension
* @param dof : degrees of freedoms.
*/
void alloc_vec(unsigned int numVec, unsigned int blkid ,const unsigned int *sz , unsigned int dof=1)
{
_vec.resize(numVec);
for(unsigned int k=0; k < _vec.size(); k++)
_vec[k].createVec(blkid,sz, false, BLK_ASYNC_VEC_MODE::BLK_UNZIP, dof);
}
/**
* @brief deallocate the vectors.
*/
void dealloc_vec()
{
for(unsigned int k=0; k < _vec.size(); k++)
_vec[k].destroyVec();
_vec.clear();
}
};
/**
* @brief Basic: class for performing non-uniform time stepping. (explicit time stepping)
* In order to perform non uniform time stepping the octree to block decomposition
* needed to be completed. We assume that the blocks are setup in the mesh class.
*
* Note: The stages are numbered from 1 to m_uiNumStages.
*/
#ifdef __PROFILE_ENUTS__
enum ENUTSPROFILE {ENUTS_EVOLVE=0, BLK_SYNC, NUTS_CORRECTION, ENUTS_BLK_UNZIP, ENUTS_BLK_ZIP, ENUTS_LAST};
#endif
template<typename T, typename Ctx>
class ExplicitNUTS : public ETS<T,Ctx>
{
using ETS<T,Ctx>::m_uiAppCtx;
using ETS<T,Ctx>::m_uiNumStages;
using ETS<T,Ctx>::m_uiEVar;
using ETS<T,Ctx>::m_uiStVec;
using ETS<T,Ctx>::m_uiEVecTmp;
using ETS<T,Ctx>::m_uiTimeInfo;
using ETS<T,Ctx>::m_uiAij;
using ETS<T,Ctx>::m_uiBi;
using ETS<T,Ctx>::m_uiCi;
using ETS<T,Ctx>::m_uiType;
using ETS<T,Ctx>::m_uiIsInternalAlloc;
#ifdef __PROFILE_ENUTS__
public:
std::vector<profiler_t> m_uiPt = std::vector<profiler_t>(static_cast<int>(ENUTSPROFILE::ENUTS_LAST));
const char *ENUTSPROFILE_NAMES[static_cast<int>(ENUTSPROFILE::ENUTS_LAST)] = {"evolve","blk_sync", "nuts_corr", "blk_unzip", "blk_zip" };
void init_pt()
{
for(unsigned int i=0; i < m_uiPt.size(); i++)
m_uiPt[i].start();
m_uiAppCtx->init_pt();
}
void reset_pt()
{
for(unsigned int i=0; i < m_uiPt.size(); i++)
m_uiPt[i].snapreset();
m_uiAppCtx->reset_pt();
}
void dump_pt(std::ostream& outfile)
{
const ot::Mesh* m_uiMesh = m_uiAppCtx->get_mesh();
if(!(m_uiMesh->isActive()))
return;
int rank = m_uiMesh->getMPIRank();
int npes = m_uiMesh->getMPICommSize();
MPI_Comm comm = m_uiMesh->getMPICommunicator();
const unsigned int currentStep = m_uiAppCtx->get_ts_info()._m_uiStep;
double t_stat;
double t_stat_g[3];
if(!rank)
{
//writes the header
if(currentStep<=1)
outfile<<"step_nuts\t act_npes\t glb_npes\t maxdepth\t numOcts\t dof_cg\t dof_uz\t"<<\
"gele_min\t gele_mean\t gele_max\t"\
"lele_min\t lele_mean\t lele_max\t"\
"lnodes_min\t lnodes_mean\t lnodes_max\t"\
"remsh_igt_min\t remesh_igt_mean\t remesh_igt_max\t"\
"evolve_min\t evolve_mean\t evolve_max\t"\
"blk_sync_min\t blk_sync_mean\t blk_sync_max\t"\
"nuts_corr_min\t nuts_corr_mean\t nuts_corr_max\t"\
"blk_unzip_min\t blk_unzip_mean\t blk_unzip_max\t"\
"rhs_blk_min\t rhs_blk_mean\t rhs_blk_max\t"\
"blk_zip_min\t blk_zip_mean\t blk_zip_max\t"<<std::endl;
}
if(!rank) outfile<<currentStep<<"\t ";
if(!rank) outfile<<m_uiMesh->getMPICommSize()<<"\t ";
if(!rank) outfile<<m_uiMesh->getMPICommSizeGlobal()<<"\t ";
if(!rank) outfile<<m_uiMaxDepth<<"\t ";
DendroIntL localSz=m_uiMesh->getNumLocalMeshElements();
DendroIntL globalSz;
par::Mpi_Reduce(&localSz,&globalSz,1,MPI_SUM,0,comm);
if(!rank)outfile<<globalSz<<"\t ";
localSz=m_uiMesh->getNumLocalMeshNodes();
par::Mpi_Reduce(&localSz,&globalSz,1,MPI_SUM,0,comm);
if(!rank)outfile<<globalSz<<"\t ";
localSz=m_uiMesh->getDegOfFreedomUnZip();
par::Mpi_Reduce(&localSz,&globalSz,1,MPI_SUM,0,comm);
if(!rank)outfile<<globalSz<<"\t ";
DendroIntL ghostElements=m_uiMesh->getNumPreGhostElements()+m_uiMesh->getNumPostGhostElements();
DendroIntL localElements=m_uiMesh->getNumLocalMeshElements();
t_stat=ghostElements;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
t_stat=localElements;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
DendroIntL ghostNodes=m_uiMesh->getNumPreMeshNodes()+m_uiMesh->getNumPostMeshNodes();
DendroIntL localNodes=m_uiMesh->getNumLocalMeshNodes();
t_stat=localNodes;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
t_stat= m_uiAppCtx->m_uiCtxpt[CTXPROFILE::REMESH].snap;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
t_stat=m_uiPt[ENUTSPROFILE::ENUTS_EVOLVE].snap;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
t_stat=m_uiPt[ENUTSPROFILE::BLK_SYNC].snap;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
t_stat=m_uiPt[ENUTSPROFILE::NUTS_CORRECTION].snap;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
t_stat=m_uiPt[ENUTSPROFILE::ENUTS_BLK_UNZIP].snap;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
t_stat=m_uiAppCtx->m_uiCtxpt[CTXPROFILE::RHS_BLK].snap;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
t_stat=m_uiPt[ENUTSPROFILE::ENUTS_BLK_ZIP].snap;
min_mean_max(&t_stat,t_stat_g,comm);
if(!rank) outfile<<t_stat_g[0]<<"\t "<<t_stat_g[1]<<"\t "<<t_stat_g[2]<<"\t ";
if(!rank) outfile<<std::endl;
}
#endif
protected:
/**@brief: minimum level of the grid. */
unsigned int m_uiLevMin;
/**@brief: max level of the grid. */
unsigned int m_uiLevMax;
/**@brief: element to block map. */
std::vector< unsigned int > m_uiE2B;
/**@brief: unzip vector for m_uiEVar*/
DVec m_uiEvarUzip;
/**@brief: DG vector for m_uiEVar*/
DVec m_uiEvarDG;
/**@brief: temp vector of the DG scheme*/
DVec m_uiDGTmp0;
/**@brief: List of block async vector. */
std::vector<ts::BlockTimeStep<T>> m_uiBVec;
/**@brief : keep track of the element time. */
std::vector<unsigned int> m_uiEleTime;
/**@brief : keep track of the element DT (int) to get the double dt, m_uiDT*DT[e] */
std::vector<unsigned int> m_uiEleDT;
/**@brief min and max time of blocks for a given level. */
std::vector<unsigned int> m_uiBlkTimeLevMinMax;
/**@brief: explicit timer interpolation operators for ENUTS */
ENUTSOp* m_uiECOp = NULL;
/**@brief: store the partial step id for the latest evolution. */
unsigned int m_uiPt;
/**@brief: store the active block IDs evolved by the partial blocks (unzip independent block ids)*/
std::vector<unsigned int> m_uiIndependentActiveBlkIDs;
/**@brief: store the active block IDs evolved by the partial blocks (unzip dependent block ids)*/
std::vector<unsigned int> m_uiDependentActiveBlkIDs;
/**@brief: The union of block independent and dependent block ids. */
std::vector<unsigned int> m_uiActiveBlkIDs;
/**@brief : if true weighted partition is used*/
bool m_uiUseWeightedPart;
private:
/**@brief: allocates the data stuctures and initialize with the current mesh stores in the class. */
void init_data_structures();
/**@brief: freee the allocated data structures. */
void free_data_structures();
private:
/**
* @brief Allocates internal variables for the time stepper.
* @return int
*/
int allocate_internal_vars();
/**@brief: Deallocate internal variables*/
int deallocate_internal_vars();
/**@bried: synchronize the block vectors*/
void sync_blk_timestep(unsigned int blk, unsigned int rk_s, bool isIGTSync=false);
/**@brief: update the element timestep*/
void update_ele_timestep();
public:
/**@brief: constructor
* Assumptions: Note that blocks result in from octree to block decomposition can be not 2:1 balanced.
* But to perform NUTS, blocks should be 2:1 balanced.
*/
ExplicitNUTS(Ctx* ctx, bool useWpart =true);
/**@brief: default destructor */
~ExplicitNUTS();
/**@brief: build all the required data structures for the non uniform TS*/
void init();
/**@brief : evolve the variables to the next coarsest time step (i.e. loop over the block sequence. ) Evolution in the sense of the NUTS*/
void evolve();
/**
* @brief : Perform parital step for the evolution.
* @param[in] pt: partial step id.
*/
void partial_evolve(unsigned int pt);
void gts_evolve(unsigned int pt);
/**@brief: compute the evolution with remesh triggered at partial steps. */
void evolve_with_remesh(unsigned int remesh_freq=5);
/**@brief: returns a constant pointer to the sub scatter maps. */
//inline ot::SubScatterMap* const get_sub_scatter_maps() const {return m_uiSubSM;}
/**@brief: perform remesh for the nuts class. */
void remesh(unsigned int grain_sz = DENDRO_DEFAULT_GRAIN_SZ, double ld_tol = DENDRO_DEFAULT_LB_TOL, unsigned int sf_k = DENDRO_DEFAULT_SF_K);
/**
* @brief compute the wavelets for the LTS vectors.
*
* @param varIds : variable ids considered for remeshing.
* @param numvars : number of variables.
* @param amr_coarsen_fac : AMR coarsening factor.
* @return true : if needed remeshing.
* @return false
*/
bool isRemeshEvars(const unsigned int * varIds, unsigned int numvars, std::function<double(double,double,double,double*)>wavelet_tol, double amr_coarsen_fac=0.1);
/**@brief: update all the internal data strucutures, with a new mesh pointer. */
int sync_with_mesh();
/**@brief returns the dt min value */
T get_dt_min() const { return m_uiTimeInfo._m_uiTh * m_uiAppCtx->getBlkTimestepFac(m_uiLevMax,m_uiLevMin,m_uiLevMax); }
/**@brief returns the dt max value */
T get_dt_max() const { return m_uiTimeInfo._m_uiTh * m_uiAppCtx->getBlkTimestepFac(m_uiLevMin,m_uiLevMin,m_uiLevMax); }
/**@brief: prints the load balance statistis*/
void dump_load_statistics(std::ostream & sout);
/**@brief: computes the estimated speedup for a given mesh. */
void dump_est_speedup(std::ostream & sout, bool verbose=false);
/**@brief : perform blk time step vector to vec CG zip operation.
* @param blkVec : pointer to the block vector.
* @param vecCG : allocated CG vector.
* @param s: blkVec.__vec[s] access parameter.
*/
void blk_vec_to_zipCG(const ts::BlockTimeStep<T>* blkVec, DVec& vecCG,unsigned int s=0);
/**
* @brief perform zip operation (DG) to get the vecDG from block vector.
* @param blkVec : blk vector in
* @param vecDG : allocated DG vector.
* @param s: blkVec.__vec[s] access parameter.
*/
void blk_vec_to_zipDG(const ts::BlockTimeStep<T>* blkVec, DVec& vecDG,unsigned int s=0);
/**
* @brief copy the block vector, to unzip vector,
* @param blkVec : array of block async vector
* @param vecUzip : unzip vector
* @param s : s index of the block async vector
*/
void blk_vec_to_unzipDG(const ts::BlockTimeStep<T>* blkVec, DVec& vecUzip,unsigned int s=0);
/**
* @brief copy DG vector to the block vector.
*
* @param vecDG DG vector
* @param blkVec array of block async vector,
* @param s : s index, of the block async vector.
*/
void vecDG_to_blk_vec(DVec& vecDG, ts::BlockTimeStep<T>* blkVec, unsigned int s=0);
/**@brief: compute weight for a wpart. */
static unsigned int getOctWeight(const ot::TreeNode* pNode)
{
const unsigned int finest_t = wpart_meta.tfac(wpart_meta.lmax,wpart_meta.lmin,wpart_meta.lmax);//m_uiAppCtx->getBlkTimestepFac(m_uiLevMax,m_uiLevMin,m_uiLevMax); // finest time level.
const unsigned int coarset_t = wpart_meta.tfac(wpart_meta.lmin,wpart_meta.lmin,wpart_meta.lmax);//m_uiAppCtx->getBlkTimestepFac(m_uiLevMin,m_uiLevMin,m_uiLevMax); // coarset time level.
const unsigned int weight = ((coarset_t-finest_t)/(wpart_meta.tfac(pNode->getLevel(),wpart_meta.lmin,wpart_meta.lmax)));//*nPe;
return std::max(1u,weight);
}
/**@brief: update wpart meta data from current variables. */
void update_wpart_metadata()
{
wpart_meta.lmin = m_uiLevMin;
wpart_meta.lmax = m_uiLevMax;
wpart_meta.tfac = &(Ctx::getBlkTimestepFac);
return;
}
};
template<typename T, typename Ctx>
ExplicitNUTS<T,Ctx>::ExplicitNUTS(Ctx* ctx, bool useWpart) : ETS<T,Ctx>(ctx)
{
m_uiUseWeightedPart = useWpart;
if(m_uiUseWeightedPart)
{
ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
pMesh->computeMinMaxLevel(m_uiLevMin,m_uiLevMax);
update_wpart_metadata();
if(!m_uiAppCtx->is_wpart_func_set())
m_uiAppCtx->set_wpart_function(getOctWeight);
ot::Mesh* newMesh = m_uiAppCtx->remesh();
m_uiAppCtx->grid_transfer(newMesh);
std::swap(pMesh,newMesh);
delete newMesh;
m_uiAppCtx->set_mesh(pMesh);
}
this->init_data_structures();
}
template<typename T, typename Ctx>
ExplicitNUTS<T,Ctx>::~ExplicitNUTS()
{
this->deallocate_internal_vars();
this->free_data_structures();
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::dump_est_speedup(std::ostream & sout, bool verbose)
{
const ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
if(pMesh->isActive())
{
const unsigned int rank = pMesh->getMPIRank();
const unsigned int npes = pMesh->getMPICommSize();
MPI_Comm comm = pMesh->getMPICommunicator();
const unsigned int finest_t = m_uiAppCtx->getBlkTimestepFac(m_uiLevMax,m_uiLevMin,m_uiLevMax); // finest time level.
const unsigned int coarset_t = m_uiAppCtx->getBlkTimestepFac(m_uiLevMin,m_uiLevMin,m_uiLevMax); // coarset time level.
double red_stat[3]; // for mpi reduction stats.
const std::vector<ot::Block>& blkList = pMesh->getLocalBlockList();
const ot::TreeNode* pNodes = pMesh->getAllElements().data();
// initialize the block async vectors
for(unsigned int blk =0; blk < blkList.size(); blk++)
{
m_uiBVec[blk]._time = 0;
m_uiBVec[blk]._rks = 0;
}
double localSz = 0;
for(unsigned int blk =0; blk < blkList.size(); blk++)
{
double nnx = (blkList[blk].getAllocationSzX() -2*blkList[blk].get1DPadWidth() );
//localSz += (nnx*nnx*nnx); //(blkList[blk].getLocalElementEnd() - blkList[blk].getLocalElementBegin());
localSz += (blkList[blk].getLocalElementEnd() - blkList[blk].getLocalElementBegin());
}
double globalESz;
par::Mpi_Reduce(&localSz,&globalESz,1,MPI_SUM,0, comm);
double globalBlkSz;
localSz = pMesh->getLocalBlockList().size();
par::Mpi_Reduce(&localSz,&globalBlkSz,1,MPI_SUM,0, comm);
std::vector<DendroIntL> octByLev;
std::vector<DendroIntL> octByLev_g;
octByLev.resize(m_uiMaxDepth+1,0);
octByLev_g.resize(m_uiMaxDepth+1,0);
for(unsigned int ele=pMesh->getElementLocalBegin(); ele < pMesh->getElementLocalEnd(); ele ++)
octByLev[pNodes[ele].getLevel()]++;
par::Mpi_Reduce(octByLev.data(),octByLev_g.data(),m_uiMaxDepth,MPI_SUM,0,comm);
if(!rank)
{
sout<<"level\t num_elements\t\n"<<std::endl;
for(int l = octByLev_g.size()-1; l >=0; l--)
sout<<l<<"\t"<<octByLev_g[l]<<std::endl;
}
if(verbose && !rank)
sout<<"partial_step\t active_elements\t all_elements\n"<<std::endl;
double totalW=0;
for(unsigned int pt=0; pt< coarset_t; pt ++)
{
double numBlkEvolved = 0;
for(unsigned int blk =0; blk < blkList.size(); blk++)
{
const unsigned int BLK_T = m_uiBVec[blk]._time;
const unsigned int NN = m_uiBVec[blk]._vec[0].getSz();
const unsigned int bLev = pNodes[blkList[blk].getLocalElementBegin()].getLevel();
const unsigned int BLK_DT = m_uiAppCtx->getBlkTimestepFac(bLev,m_uiLevMin,m_uiLevMax);
if( pt% BLK_DT !=0 )
continue;
double nnx = (blkList[blk].getAllocationSzX() -2*blkList[blk].get1DPadWidth() );
//std::cout<<"pt: "<<pt<<" blk id : "<<blk<<" blklevel : "<<bLev<<" is now at btime : "<<BLK_T<<" dt: "<<BLK_DT<<std::endl;
//numBlkEvolved += (nnx*nnx*nnx); //(blkList[blk].getLocalElementEnd() - blkList[blk].getLocalElementBegin());
numBlkEvolved += (blkList[blk].getLocalElementEnd() - blkList[blk].getLocalElementBegin());
m_uiBVec[blk]._time += BLK_DT;
m_uiBVec[blk]._rks=0;
}
par::Mpi_Reduce(&numBlkEvolved,red_stat,1,MPI_MIN,0,comm);
par::Mpi_Reduce(&numBlkEvolved,red_stat+1,1,MPI_SUM,0,comm);
par::Mpi_Reduce(&numBlkEvolved,red_stat+2,1,MPI_MAX,0,comm);
totalW += red_stat[1];
if(verbose && !rank)
sout<<pt<<"\t"<<red_stat[1]<<"\t"<<globalESz<<"\t"<<(red_stat[1]/globalESz)<<std::endl;
// std::cout<<" partial step : "<<pt<< " number of dof evolved (min, sum, max): ("<<red_stat[0]<<",\t "<<red_stat[1]<<",\t "<<red_stat[2]<<") out of "<<globalESz<<std::endl;
// compute the time step vector and increment time.
// for(unsigned int blk =0; blk < blkList.size(); blk++)
// {
// const unsigned int BLK_T = m_uiBVec[blk]._time;
// const unsigned int NN = m_uiBVec[blk]._vec[0].getSz();
// const unsigned int bLev = pNodes[blkList[blk].getLocalElementBegin()].getLevel();
// const unsigned int BLK_DT = m_uiAppCtx->getBlkTimestepFac(bLev,m_uiLevMin,m_uiLevMax);
// if( pt% BLK_DT !=0 )
// continue;
// m_uiBVec[blk]._time += BLK_DT;
// m_uiBVec[blk]._rks=0;
// }
}
if(!rank)
{
sout<<"number of LTS partial timesteps "<<coarset_t<<std::endl;
sout<<"LTS Work : "<<totalW<<" GTS Work: "<<globalESz*coarset_t<<std::endl;
sout<<" ets. speedup : "<<(globalESz*coarset_t/(double)(totalW))<<std::endl;
}
}
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::dump_load_statistics(std::ostream & sout)
{
const ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
if(pMesh->isActive())
{
double local_weight=0;
const ot::TreeNode* pNodes = pMesh->getAllElements().data();
for(unsigned int ele = pMesh->getElementLocalBegin(); ele < pMesh->getElementLocalEnd(); ele++)
local_weight+=getOctWeight(&pNodes[ele]);
double ld_stat[3];
MPI_Comm aComm =pMesh->getMPICommunicator();
par::Mpi_Reduce(&local_weight,ld_stat+0,1,MPI_MIN,0,aComm);
par::Mpi_Reduce(&local_weight,ld_stat+1,1,MPI_SUM,0,aComm);
ld_stat[1]=ld_stat[1]/(double)pMesh->getMPICommSize();
par::Mpi_Reduce(&local_weight,ld_stat+2,1,MPI_MAX,0,aComm);
if(!pMesh->getMPIRank())
std::cout<<YLW<<"\t LD Bal: (min,mean,max): "<<ld_stat[0]<<"|\t"<<ld_stat[1]<<"|\t"<<ld_stat[2]<<NRM<<std::endl;
}
return ;
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::init_data_structures()
{
// identify dependent and independent blocks.
const ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
const bool isActive = pMesh->isActive();
pMesh->computeMinMaxLevel(m_uiLevMin,m_uiLevMax);
assert( (m_uiLevMin > 0 ) && (m_uiLevMax <= m_uiMaxDepth) );
update_wpart_metadata();
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::free_data_structures()
{
return ;
}
template<typename T, typename Ctx>
int ExplicitNUTS<T,Ctx>::allocate_internal_vars()
{
assert(m_uiNumStages>0);
const unsigned int DOF = m_uiEVar.get_dof();
if(m_uiIsInternalAlloc)
return 0; // no need to allocated again if the internal vars are allocated.
m_uiStVec.resize(m_uiNumStages);
for(unsigned int i=0; i < m_uiNumStages; i++)
m_uiStVec[i].create_vector(m_uiAppCtx->get_mesh(), ot::DVEC_TYPE::OCT_LOCAL_NODES, ot::DVEC_LOC::HOST,m_uiEVar.get_dof(),true);
m_uiEVecTmp[0].create_vector(m_uiAppCtx->get_mesh(), ot::DVEC_TYPE::OCT_SHARED_NODES, ot::DVEC_LOC::HOST,m_uiEVar.get_dof(),true);
m_uiEVecTmp[1].create_vector(m_uiAppCtx->get_mesh(), ot::DVEC_TYPE::OCT_SHARED_NODES, ot::DVEC_LOC::HOST,m_uiEVar.get_dof(),true);
// allocate tmp DG vectors
m_uiDGTmp0.create_vector(m_uiAppCtx->get_mesh(), ot::DVEC_TYPE::OCT_LOCAL_NODES, ot::DVEC_LOC::HOST,m_uiEVar.get_dof(),true);
m_uiEvarUzip.create_vector(m_uiAppCtx->get_mesh(), ot::DVEC_TYPE::OCT_LOCAL_WITH_PADDING, ot::DVEC_LOC::HOST,m_uiEVar.get_dof(),true);
m_uiEvarDG.create_vector(m_uiAppCtx->get_mesh(), ot::DVEC_TYPE::OCT_LOCAL_NODES, ot::DVEC_LOC::HOST,m_uiEVar.get_dof(),true);
const ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
if(pMesh->isActive())
{
m_uiEleTime.resize(pMesh->getAllElements().size(),0);
m_uiEleDT.resize(pMesh->getAllElements().size(),0);
const std::vector<ot::Block>& blkList = pMesh->getLocalBlockList();
m_uiBVec.resize(blkList.size());
for(unsigned int blk =0; blk < blkList.size(); blk++)
{
const unsigned int sz[3] = { blkList[blk].getAllocationSzX(), blkList[blk].getAllocationSzY(), blkList[blk].getAllocationSzZ()};
m_uiBVec[blk].alloc_vec(m_uiNumStages+2, blk, sz, DOF);
}
}
m_uiECOp = new ENUTSOp(m_uiType);
m_uiIsInternalAlloc=true;
m_uiBlkTimeLevMinMax.resize(2*m_uiMaxDepth,0);
m_uiActiveBlkIDs.reserve(pMesh->getLocalBlockList().size());
m_uiDependentActiveBlkIDs.reserve(pMesh->getLocalBlockList().size());
m_uiIndependentActiveBlkIDs.reserve(pMesh->getLocalBlockList().size());
return 0;
}
template<typename T, typename Ctx>
int ExplicitNUTS<T,Ctx>::deallocate_internal_vars()
{
if(!m_uiIsInternalAlloc)
return 0;
for(unsigned int i=0; i < m_uiNumStages; i++)
{
this->m_uiStVec[i].destroy_vector();
}
this->m_uiStVec.clear();
this->m_uiEVecTmp[0].destroy_vector();
this->m_uiEVecTmp[1].destroy_vector();
//deallocate DG tmp.
m_uiDGTmp0.destroy_vector();
m_uiEvarUzip.destroy_vector();
m_uiEvarDG.destroy_vector();
m_uiEleTime.clear();
m_uiEleDT.clear();
for(unsigned int k=0; k < m_uiBVec.size(); k++)
{
for(unsigned int j=0; j < m_uiBVec[k]._vec.size(); j++)
m_uiBVec[k]._vec[j].destroyVec();
m_uiBVec[k]._vec.clear();
}
m_uiBVec.clear();
// deallocate m_uiStVec, which is based on the number of stages
for (unsigned int i = 0; i < m_uiNumStages; i++) {
m_uiStVec[i].destroy_vector();
}
m_uiStVec.clear();
m_uiBlkTimeLevMinMax.clear();
m_uiActiveBlkIDs.clear();
m_uiIndependentActiveBlkIDs.clear();
m_uiDependentActiveBlkIDs.clear();
delete m_uiECOp;
m_uiIsInternalAlloc = false;
return 0;
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::blk_vec_to_zipCG(const ts::BlockTimeStep<T>* blkVec, DVec& vecCG, unsigned int s)
{
ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
const std::vector<ot::Block>& blkList = pMesh->getLocalBlockList();
const unsigned int DOF = vecCG.get_dof();
for(unsigned int blk =0; blk < blkList.size(); blk++)
blkVec[blk]._vec[s].zip(pMesh, vecCG.get_vec_ptr(), DOF);
return;
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::blk_vec_to_zipDG(const ts::BlockTimeStep<T>* blkVec, DVec& vecDG, unsigned int s)
{
ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
const std::vector<ot::Block>& blkList = pMesh->getLocalBlockList();
const unsigned int DOF = vecDG.get_dof();
for(unsigned int blk =0; blk < blkList.size(); blk++)
blkVec[blk]._vec[s].zipDG(pMesh, vecDG.get_vec_ptr(), DOF);
return;
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::blk_vec_to_unzipDG(const ts::BlockTimeStep<T>* blkVec, DVec& vecUzip,unsigned int s)
{
ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
const std::vector<ot::Block>& blkList = pMesh->getLocalBlockList();
const unsigned int DOF = vecUzip.get_dof();
for(unsigned int v=0; v < DOF; v++)
{
T * d_ptr = vecUzip.get_vec_ptr() + v * pMesh->getDegOfFreedomUnZip();
for(unsigned int blk=0; blk < blkList.size(); blk++)
{
const unsigned int offset = blkList[blk].getOffset();
const unsigned int lx = blkList[blk].getAllocationSzX();
const unsigned int ly = blkList[blk].getAllocationSzY();
const unsigned int lz = blkList[blk].getAllocationSzZ();
const T * bvec = blkVec[blk]._vec[s].data() + (v*lx*ly*lz);
std::memcpy(d_ptr+offset, bvec , sizeof(T)*(lx*ly*lz));
}
}
return;
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::vecDG_to_blk_vec(DVec& vecDG, ts::BlockTimeStep<T>* blkVec, unsigned int s)
{
ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
const std::vector<ot::Block>& blkList = pMesh->getLocalBlockList();
const unsigned int DOF = vecDG.get_dof();
for(unsigned int blk =0; blk < blkList.size(); blk++)
{
blkVec[blk]._vec[s].copyFromVecDG(pMesh,vecDG.get_vec_ptr(),DOF);
blkVec[blk]._vec[s].mark_unsynced();
}
}
template<typename T, typename Ctx>
int ExplicitNUTS<T,Ctx>::sync_with_mesh()
{
if(m_uiAppCtx -> is_ets_synced())
return 0;
this->deallocate_internal_vars();
this->free_data_structures();
m_uiEVar = m_uiAppCtx->get_evolution_vars();
this->init_data_structures();
this->allocate_internal_vars();
m_uiAppCtx->set_ets_synced(true);
return 0;
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::init()
{
m_uiAppCtx->initialize();
m_uiTimeInfo = m_uiAppCtx->get_ts_info();
allocate_internal_vars();
m_uiAppCtx->compute_lts_ts_offset();
if(m_uiUseWeightedPart)
{
ot::Mesh* pMesh = m_uiAppCtx->get_mesh();
pMesh->computeMinMaxLevel(m_uiLevMin,m_uiLevMax);
update_wpart_metadata();
if(!m_uiAppCtx->is_wpart_func_set())
m_uiAppCtx->set_wpart_function(getOctWeight);
ot::Mesh* newMesh = m_uiAppCtx->remesh();
{
DendroIntL oldElements=pMesh->getNumLocalMeshElements();
DendroIntL newElements=newMesh->getNumLocalMeshElements();
DendroIntL oldElements_g, newElements_g;
par::Mpi_Reduce(&oldElements,&oldElements_g,1,MPI_SUM,0,pMesh->getMPIGlobalCommunicator());
par::Mpi_Reduce(&newElements,&newElements_g,1,MPI_SUM,0,newMesh->getMPIGlobalCommunicator());
if(!(pMesh->getMPIRankGlobal()))
std::cout<<GRN<<"[LTS Init(Wpart)]: \told mesh: "<<oldElements_g<<"\tnew mesh:"<<newElements_g<<NRM<<std::endl;
}
DVec eVar = m_uiAppCtx->get_evolution_vars();
unsigned int DOF= eVar.get_dof();
pMesh->readFromGhostBegin(eVar.get_vec_ptr(),DOF);
pMesh->readFromGhostEnd(eVar.get_vec_ptr(),DOF);
m_uiAppCtx->grid_transfer(newMesh);
std::swap(pMesh,newMesh);
delete newMesh;
m_uiAppCtx->set_mesh(pMesh);
}
//Ctx initialize might have changed the mesh i.e. converge untill mesh adapted to the initial data.
m_uiAppCtx->set_ets_synced(false);
this->sync_with_mesh();
}
template<typename T, typename Ctx>
void ExplicitNUTS<T,Ctx>::sync_blk_timestep(unsigned int blk, unsigned int rk_s,bool isIGTSync)
{
#ifdef __PROFILE_ENUTS__
m_uiPt[ENUTSPROFILE::BLK_SYNC].start();
#endif
ot::Mesh* pMesh = (ot::Mesh*)m_uiAppCtx->get_mesh();
if((!(pMesh->isActive())) || m_uiBVec[blk]._vec[rk_s].isSynced() )
return;
const ot::TreeNode* pNodes = pMesh->getAllElements().data();
const ot::Block* blkList = pMesh->getLocalBlockList().data();
const unsigned int regLevel = blkList[blk].getRegularGridLev();
const ot::TreeNode blkNode = blkList[blk].getBlockNode();
const unsigned int PW = blkList[blk].get1DPadWidth();
const unsigned int eOrder = pMesh->getElementOrder();
const unsigned int nPe = pMesh->getNumNodesPerElement();
const unsigned int * etVec = m_uiEleTime.data();
const unsigned int * dtELev = m_uiEleDT.data();
MPI_Comm comm = pMesh->getMPICommunicator();
const unsigned int lx = blkList[blk].getAllocationSzX();
const unsigned int ly = blkList[blk].getAllocationSzY();
const unsigned int lz = blkList[blk].getAllocationSzZ();
const unsigned int offset = blkList[blk].getOffset();
const unsigned int dgSz = pMesh->getAllElements().size() * pMesh->getNumNodesPerElement();
const unsigned int cgSz = pMesh->getDegOfFreedom();
const unsigned int unSz = pMesh->getDegOfFreedomUnZip();
const unsigned int* e2n = pMesh->getE2NMapping().data();
const unsigned int* e2e = pMesh->getE2EMapping().data();
const unsigned int bLev = pNodes[blkList[blk].getLocalElementBegin()].getLevel();
const unsigned int bTime = etVec[blkList[blk].getLocalElementBegin()];
const unsigned int bDT = dtELev[blkList[blk].getLocalElementBegin()];
T dt;
unsigned int tl = 0;
unsigned int dtl = 0; // dt time level.
unsigned int lookUp;
const unsigned int cSz[3] = { eOrder+1, eOrder+1, eOrder+1 };
unsigned int fchild[4];
unsigned int echild[2];
const unsigned int dof = m_uiEVar.get_dof();
std::vector<T> cVec;
cVec.resize(dof*nPe*(m_uiNumStages+1));
T* cVin[(m_uiNumStages+1)*dof]; // correction vec. pointer in,
T* cVout[(m_uiNumStages+1)*dof]; // correction vec. pointer out,