-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlulesh.cpp
More file actions
6606 lines (5691 loc) · 221 KB
/
lulesh.cpp
File metadata and controls
6606 lines (5691 loc) · 221 KB
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
/*
Copyright (c) 2010.
Lawrence Livermore National Security, LLC.
Produced at the Lawrence Livermore National Laboratory.
LLNL-CODE-461231
All rights reserved.
This file is part of LULESH, Version 1.0.
Please also read this link -- http://www.opensource.org/licenses/index.php
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the disclaimer below.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the disclaimer (as noted below)
in the documentation and/or other materials provided with the
distribution.
* Neither the name of the LLNS/LLNL nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC,
THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Additional BSD Notice
1. This notice is required to be provided under our contract with the U.S.
Department of Energy (DOE). This work was produced at Lawrence Livermore
National Laboratory under Contract No. DE-AC52-07NA27344 with the DOE.
2. Neither the United States Government nor Lawrence Livermore National
Security, LLC nor any of their employees, makes any warranty, express
or implied, or assumes any liability or responsibility for the accuracy,
completeness, or usefulness of any information, apparatus, product, or
process disclosed, or represents that its use would not infringe
privately-owned rights.
3. Also, reference herein to any specific commercial products, process, or
services by trade name, trademark, manufacturer or otherwise does not
necessarily constitute or imply its endorsement, recommendation, or
favoring by the United States Government or Lawrence Livermore National
Security, LLC. The views and opinions of authors expressed herein do not
necessarily state or reflect those of the United States Government or
Lawrence Livermore National Security, LLC, and shall not be used for
advertising or product endorsement purposes.
*/
#include <ctype.h> /* isdigit */
#include <limits.h> /* INT_MIN */
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h> /* memcpy, memset */
#include <map>
#include <vector>
#include <mpi.h>
#include "mpi-ext.h"
#include <setjmp.h>
#include <signal.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <mammut/mammut.hpp>
#include "mammut_functions.hpp"
#include "timers.h"
#define max(a,b) ((a>b) ? (a) : (b))
#define min(a,b) ((a<b) ? (a) : (b))
#define DEFAULT_PATH "/tmp"
struct tmp_timings {
double deltatime;
double time;
double dtcourant;
double dthydro;
};
std::map<std::pair<std::pair<int,int>,int>, double *> log_buffer;
std::map<int,struct tmp_timings> log_reduce;
std::map<int,double> log_newdt;
static double total_joules = 0.;
static bool post_failure_sync = false;
static bool post_failure = false;
static std::vector<double> log_joules;
static std::vector<double> log_times;
//#define ENABLE_EVENT_LOG_ 1
//
//we should always use this buffer for transactions and correct local rollback
#define USE_TEMP_BUF_ 1
#define REDUCE_LOGGER (size-1)
int pinit = 1;
struct Domain *pdom;
#define LULESH_SHOW_PROGRESS 0
#define VIZ_MESH 1 // must remove def to turn off
#define MAX_CYCLES INT_MAX
int FAILED_PROC;
int KILL_OUTER_ITER;
int KILL_PHASE;
#ifdef ENABLE_EVENT_LOG_
int EVENT_LOGGER;
#endif
static MPI_Comm reduce_comm;
static int MPIX_Comm_replace(MPI_Comm comm, MPI_Comm *newcomm);
static int me = 0;
int verbose = 1;
//static int rank = MPI_PROC_NULL; /* makes this global (for printfs) */
static char estr[MPI_MAX_ERROR_STRING]=""; static int strl; /* error messages */
static int allowed_to_kill = 1;
std::vector<int> peer_iters;
static int min_iter, max_iter;
char** gargv;
static int ckpt_iteration = 0, last_dead = MPI_PROC_NULL;
static jmp_buf stack_jmp_buf;
/* world will swap between worldc[0] and worldc[1] after each respawn */
static MPI_Comm worldc[2] = { MPI_COMM_NULL, MPI_COMM_NULL };
static int worldi = 0;
#define world (worldc[worldi])
int send_wrapper(const void *buf, int count, MPI_Datatype datatype, int dest, int tag,
MPI_Comm comm, MPI_Request * request, int * cnt, int replay_it, int phase, int stage);
int recv_wrapper(void *buf, int count, MPI_Datatype datatype, int source, int tag,
MPI_Comm comm, MPI_Request * request, int * cnt, int replay_it, int phase, int stage);
int allreduce_wrapper(const void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, int iter, int replay_iter);
/* repair comm world, reload checkpoints, etc...
* Return: true: the app needs to redo some iterations
* false: no failure was fixed, we do not need to redo any work.
*/
static int app_needs_repair(MPI_Comm comm)
{
printf("(Rank %d): enter app_needs_repair\n", me);
/* This is the first time we see an error on this comm, do the swap of the
* worlds. Next time we will have nothing to do. */
if( comm == world ) {
/* swap the worlds */
worldi = (worldi+1)%2;
/* We keep comm around so that the error handler remains attached until the
* user has completed all pending ops; it is expected that the user will
* complete all ops on comm before posting new ops in the new world.
* Beware that if the user does not complete all ops on comm and the handler
* is invoked on the new world inbetween, comm may be freed while
* operations are still pending on it, and a fatal error may be
* triggered when these ops are finally completed (possibly in Finalize)*/
if( MPI_COMM_NULL != world ) MPI_Comm_free(&world);
MPIX_Comm_replace(comm, &world);
// ToDo: shouldn't have to do that here?
if( MPI_COMM_NULL == comm ) return false; /* ok, we repaired nothing, no need to redo any work */
//do_recover = 1;
//goto restart;
longjmp( stack_jmp_buf, 1 );
}
return true; /* we have repaired the world, we need to reexecute */
}
/* Do all the magic in the error handler */
static void errhandler_respawn(MPI_Comm* pcomm, int* errcode, ...)
{
int eclass;
MPI_Error_class(*errcode, &eclass);
if( verbose ) {
MPI_Error_string(*errcode, estr, &strl);
fprintf(stderr, "%04d: errhandler invoked in iter %d with error %s\n", me, peer_iters[me], estr);
}
if( MPIX_ERR_PROC_FAILED != eclass &&
MPIX_ERR_REVOKED != eclass ) {
MPI_Abort(MPI_COMM_WORLD, *errcode);
}
MPIX_Comm_revoke(world);
app_needs_repair(world);
}
static int MPIX_Comm_replace(MPI_Comm comm, MPI_Comm *newcomm)
{
MPI_Comm icomm, /* the intercomm between the spawnees and the old (shrinked) world */
scomm, /* the local comm for each sides of icomm */
mcomm; /* the intracomm, merged from icomm */
MPI_Group cgrp, sgrp, dgrp;
int rc, flag, rflag, i, crank, srank, drank, nc, ns, nd;
printf("process %d enters replace ...\n", me);
redo:
if( comm == MPI_COMM_NULL ) { /* am I a new process? */
/* I am a new spawnee, waiting for my new rank assignment
* it will be sent by rank 0 in the old world */
MPI_Comm_get_parent(&icomm);
scomm = MPI_COMM_WORLD;
MPI_Recv(&crank, 1, MPI_INT, 0, 1, icomm, MPI_STATUS_IGNORE);
printf("New process should be assigned rank %d\n", crank);
if( verbose ) {
MPI_Comm_rank(scomm, &srank);
printf("Spawnee %d: crank=%d\n", srank, crank);
}
} else {
/* I am a survivor: Spawn the appropriate number
* of replacement processes (we check that this operation worked
* before we procees further) */
/* First: remove dead processes */
MPIX_Comm_shrink(comm, &scomm);
MPI_Comm_size(scomm, &ns);
MPI_Comm_size(comm, &nc);
nd = nc-ns; /* number of deads */
if( 0 == nd ) {
/* Nobody was dead to start with. We are done here */
MPI_Comm_free(&scomm);
*newcomm = comm;
return MPI_SUCCESS;
}
/* We handle failures during this function ourselves... */
MPI_Comm_set_errhandler( scomm, MPI_ERRORS_RETURN );
MPI_Info info;
MPI_Info_create(&info);
MPI_Info_set(info, "host", "m1");
// MPI_Info_set(info, "ompi_param", "btl_tcp_if_include eno1");
rc = MPI_Comm_spawn(gargv[0], &gargv[1], nd, info,
0, scomm, &icomm, MPI_ERRCODES_IGNORE);
flag = (MPI_SUCCESS == rc);
MPIX_Comm_agree(scomm, &flag);
if( !flag ) {
if( MPI_SUCCESS == rc ) {
MPIX_Comm_revoke(icomm);
MPI_Comm_free(&icomm);
}
MPI_Comm_free(&scomm);
if( verbose ) fprintf(stderr, "%04d: comm_spawn failed, redo\n", me);
goto redo;
}
/* remembering the former rank: we will reassign the same
* ranks in the new world. */
MPI_Comm_rank(comm, &crank);
MPI_Comm_rank(scomm, &srank);
/* the rank 0 in the scomm comm is going to determine the
* ranks at which the spares need to be inserted. */
if(0 == srank) {
/* getting the group of dead processes:
* those in comm, but not in scomm are the deads */
MPI_Comm_group(comm, &cgrp);
MPI_Comm_group(scomm, &sgrp);
MPI_Group_difference(cgrp, sgrp, &dgrp);
/* Computing the rank assignment for the newly inserted spares */
for(i=0; i<nd; i++) {
MPI_Group_translate_ranks(dgrp, 1, &i, cgrp, &drank);
/* sending their new assignment to all new procs */
MPI_Send(&drank, 1, MPI_INT, i, 1, icomm);
// left border (1 failed)
last_dead = drank;
}
MPI_Group_free(&cgrp); MPI_Group_free(&sgrp); MPI_Group_free(&dgrp);
}
}
/* Merge the intercomm, to reconstruct an intracomm (we check
* that this operation worked before we proceed further) */
rc = MPI_Intercomm_merge(icomm, 1, &mcomm);
rflag = flag = (MPI_SUCCESS==rc);
MPIX_Comm_agree(scomm, &flag);
if( MPI_COMM_WORLD != scomm ) MPI_Comm_free(&scomm);
MPIX_Comm_agree(icomm, &rflag);
MPI_Comm_free(&icomm);
if( !(flag && rflag) ) {
if( MPI_SUCCESS == rc ) {
MPI_Comm_free(&mcomm);
}
if( verbose ) fprintf(stderr, "%04d: Intercomm_merge failed, redo\n", me);
goto redo;
}
/* Now, reorder mcomm according to original rank ordering in comm
* Split does the magic: removing spare processes and reordering ranks
* so that all surviving processes remain at their former place */
//printf("Trying to assign rank %d in split\n", crank);
rc = MPI_Comm_split(mcomm, 1, crank, newcomm);
/* Split or some of the communications above may have failed if
* new failures have disrupted the process: we need to
* make sure we succeeded at all ranks, or retry until it works. */
flag = (MPI_SUCCESS==rc);
MPIX_Comm_agree(mcomm, &flag);
MPI_Comm_free(&mcomm);
if( !flag ) {
if( MPI_SUCCESS == rc ) {
MPI_Comm_free( newcomm );
}
if( verbose ) fprintf(stderr, "%04d: comm_split failed, redo\n", me);
goto redo;
}
/* restore the error handler */
if( MPI_COMM_NULL != comm ) {
MPI_Errhandler errh;
MPI_Comm_get_errhandler( comm, &errh );
MPI_Comm_set_errhandler( *newcomm, errh );
}
printf("Done with the recovery (rank %d)\n", crank);
return MPI_SUCCESS;
}
void setup_failures() {
FILE * f = fopen("failures.lulesh.conf", "r");
if (f == NULL) {
fprintf(stderr, "No failure config file\n");
MPI_Abort(world, -1);
}
int dummy;
fscanf (f, "%d : %d : %d : %d", &FAILED_PROC, &KILL_OUTER_ITER, &KILL_PHASE, &dummy);
fclose(f);
if (me == 0) printf("Will kill %d in iter %d\n", FAILED_PROC, KILL_OUTER_ITER);
}
enum { VolumeError = -1, QStopError = -2 } ;
/****************************************************/
/* Allow flexibility for arithmetic representations */
/****************************************************/
/* Could also support fixed point and interval arithmetic types */
typedef float real4 ;
typedef double real8 ;
typedef real8 Real_t ; /* floating point representation */
typedef long double real10 ; /* 10 bytes on x86 */
typedef int Index_t ; /* array subscript and loop index */
typedef int Int_t ; /* integer representation */
inline real4 SQRT(real4 arg) { return sqrtf(arg) ; }
inline real8 SQRT(real8 arg) { return sqrt(arg) ; }
inline real10 SQRT(real10 arg) { return sqrtl(arg) ; }
inline real4 CBRT(real4 arg) { return cbrtf(arg) ; }
inline real8 CBRT(real8 arg) { return cbrt(arg) ; }
inline real10 CBRT(real10 arg) { return cbrtl(arg) ; }
inline real4 FABS(real4 arg) { return fabsf(arg) ; }
inline real8 FABS(real8 arg) { return fabs(arg) ; }
inline real10 FABS(real10 arg) { return fabsl(arg) ; }
Domain *NewDomain(Index_t colLoc, Index_t rowLoc, Index_t planeLoc, Index_t nx, int tp);
void CopyDomain(Domain * src, Domain * dst);
void DelDomain(Domain *domain);
void DumpDomain(Domain *domain, int myRank, int numProcs, char readOrWrite);
#define MAX(a, b) ( ((a) > (b)) ? (a) : (b))
/*********************************/
/* Data structure implementation */
/*********************************/
/* might want to add access methods so that memory can be */
/* better managed, as in luleshFT */
struct Domain {
/* Elem-centered */
Index_t *matElemlist ; /* material indexset */
#if 1
Index_t *nodelist ; /* elemToNode connectivity */
#else
Index_t *nodelist0 ; /* elemToNode connectivity */
Index_t *nodelist1 ;
Index_t *nodelist2 ;
Index_t *nodelist3 ;
Index_t *nodelist4 ;
Index_t *nodelist5 ;
Index_t *nodelist6 ;
Index_t *nodelist7 ;
#endif
Index_t *lxim ; /* elem connectivity through face */
Index_t *lxip ;
Index_t *letam ;
Index_t *letap ;
Index_t *lzetam ;
Index_t *lzetap ;
Int_t *elemBC ; /* elem face symm/free-surface flag */
Real_t *e ; /* energy */
Real_t *p ; /* pressure */
Real_t *q ; /* q */
Real_t *ql ; /* linear term for q */
Real_t *qq ; /* quadratic term for q */
Real_t *v ; /* relative volume */
Real_t *volo ; /* reference volume */
Real_t *delv ; /* m_vnew - m_v */
Real_t *vdov ; /* volume derivative over volume */
Real_t *arealg ; /* elem characteristic length */
Real_t *ss ; /* "sound speed" */
Real_t *elemMass ; /* mass */
/* Elem temporaries */
Real_t *vnew ; /* new relative volume -- temporary */
Real_t *delv_xi ; /* velocity gradient -- temporary */
Real_t *delv_eta ;
Real_t *delv_zeta ;
Real_t *delx_xi ; /* position gradient -- temporary */
Real_t *delx_eta ;
Real_t *delx_zeta ;
Real_t *dxx ; /* principal strains -- temporary */
Real_t *dyy ;
Real_t *dzz ;
/* Node-centered */
Real_t *x ; /* coordinates */
Real_t *y ;
Real_t *z ;
Real_t *xd ; /* velocities */
Real_t *yd ;
Real_t *zd ;
Real_t *xdd ; /* accelerations */
Real_t *ydd ;
Real_t *zdd ;
Real_t *fx ; /* forces */
Real_t *fy ;
Real_t *fz ;
Real_t *nodalMass ; /* mass */
/* Communication Work space */
Real_t *commDataSend ;
Real_t *commDataRecv ;
/* Maximum number of block neighbors */
MPI_Request recvRequest[26] ; /* 6 faces + 12 edges + 8 corners */
MPI_Request sendRequest[26] ; /* 6 faces + 12 edges + 8 corners */
/* Boundary nodesets */
Index_t *symmX ; /* Nodes on X symmetry plane */
Index_t *symmY ; /* Nodes on Y symmetry plane */
Index_t *symmZ ; /* Nodes on Z symmetry plane */
/* Parameters */
Real_t dtfixed ; /* fixed time increment */
Real_t time ; /* current time */
Real_t deltatime ; /* variable time increment */
Real_t deltatimemultlb ;
Real_t deltatimemultub ;
Real_t stoptime ; /* end time for simulation */
Real_t u_cut ; /* velocity tolerance */
Real_t hgcoef ; /* hourglass control */
Real_t qstop ; /* excessive q indicator */
Real_t monoq_max_slope ;
Real_t monoq_limiter_mult ;
Real_t e_cut ; /* energy tolerance */
Real_t p_cut ; /* pressure tolerance */
Real_t ss4o3 ;
Real_t q_cut ; /* q tolerance */
Real_t v_cut ; /* relative volume tolerance */
Real_t qlc_monoq ; /* linear term coef for q */
Real_t qqc_monoq ; /* quadratic term coef for q */
Real_t qqc ;
Real_t eosvmax ;
Real_t eosvmin ;
Real_t pmin ; /* pressure floor */
Real_t emin ; /* energy floor */
Real_t dvovmax ; /* maximum allowable volume change */
Real_t refdens ; /* reference density */
Real_t dtcourant ; /* courant constraint */
Real_t dthydro ; /* volume change constraint */
Real_t dtmax ; /* maximum allowable time increment */
Int_t cycle ; /* iteration count for simulation */
Int_t numProcs ;
Index_t colLoc ;
Index_t rowLoc ;
Index_t planeLoc ;
Index_t tp ;
Index_t sizeX ;
Index_t sizeY ;
Index_t sizeZ ;
Index_t maxPlaneSize ;
Index_t maxEdgeSize ;
Index_t numElem ;
Index_t numNode ;
} ;
#ifdef USE_TEMP_BUF_
static Domain * tmp_domain;
#endif
template <typename T>
T *Allocate(size_t size)
{
return static_cast<T *>(malloc(sizeof(T)*size)) ;
}
template <typename T>
void Release(T **ptr)
{
if (*ptr != NULL) {
free(*ptr) ;
*ptr = NULL ;
}
}
void append_log(int cycle, int stage, int phase, const void * destAddr, int count) {
if ((LOG_BFR_DEPTH > 0) && (cycle % CKPT_STEP < LOG_BFR_DEPTH)) {
int size;
std::pair<std::pair<int,int>, int> triplet = std::make_pair(std::make_pair(cycle,stage), phase);
if (log_buffer.find(triplet) != log_buffer.end()) {
//printf("(Rank %d:) just deleted existing buffer of size %d in (%d - %d - %d)\n", me, count, cycle, stage, phase);
free(log_buffer[triplet]);
}
MPI_Type_size(MPI_DOUBLE, &size);
double * new_buff = (double *) malloc(size * count);
memcpy(new_buff, destAddr, size*count);
log_buffer[triplet] = new_buff;
}
}
/* Stuff needed for boundary conditions */
/* 2 BCs on each of 6 hexahedral faces (12 bits) */
#define XI_M 0x00007
#define XI_M_SYMM 0x00001
#define XI_M_FREE 0x00002
#define XI_M_COMM 0x00004
#define XI_P 0x00038
#define XI_P_SYMM 0x00008
#define XI_P_FREE 0x00010
#define XI_P_COMM 0x00020
#define ETA_M 0x001c0
#define ETA_M_SYMM 0x00040
#define ETA_M_FREE 0x00080
#define ETA_M_COMM 0x00100
#define ETA_P 0x00e00
#define ETA_P_SYMM 0x00200
#define ETA_P_FREE 0x00400
#define ETA_P_COMM 0x00800
#define ZETA_M 0x07000
#define ZETA_M_SYMM 0x01000
#define ZETA_M_FREE 0x02000
#define ZETA_M_COMM 0x04000
#define ZETA_P 0x38000
#define ZETA_P_SYMM 0x08000
#define ZETA_P_FREE 0x10000
#define ZETA_P_COMM 0x20000
/* Assume 128 byte coherence */
/* Assume Real_t is an "integral power of 2" bytes wide */
#define CACHE_COHERENCE_PAD_REAL (128 / sizeof(Real_t))
#define CACHE_ALIGN_REAL(n) \
(((n) + (CACHE_COHERENCE_PAD_REAL - 1)) & ~(CACHE_COHERENCE_PAD_REAL-1))
/******************************************/
/* Comm Routines */
#define MAX_FIELDS_PER_MPI_COMM 6
#define ALLOW_UNPACKED_PLANE false
#define ALLOW_UNPACKED_ROW false
#define ALLOW_UNPACKED_COL false
#define MSG_COMM_SBN 1024
#define MSG_SYNC_POS_VEL 2048
#define MSG_MONOQ 3072
/*
define one of these three symbols:
SEDOV_SYNC_POS_VEL_NONE
SEDOV_SYNC_POS_VEL_EARLY
SEDOV_SYNC_POS_VEL_LATE
*/
#define SEDOV_SYNC_POS_VEL_EARLY 1
/*
There are coherence issues for packing and unpacking message
buffers. Ideally, you would like a lot of threads to
cooperate in the assembly/dissassembly of each message.
To do that, each thread should really be operating in a
different coherence zone.
Let's assume we have three fields, f1 through f3, defined on
a 61x61x61 cube. If we want to send the block boundary
information for each field to each neighbor processor across
each cube face, then we have three cases for the
memory layout/coherence of data on each of the six cube
boundaries:
(a) Two of the faces will be in contiguous memory blocks
(b) Two of the faces will be comprised of pencils of
contiguous memory.
(c) Two of the faces will have large strides between
every value living on the face.
How do you pack and unpack this data in buffers to
simultaneous achieve the best memory efficiency and
the most thread independence?
Do do you pack field f1 through f3 tighly to reduce message
size? Do you align each field on a cache coherence boundary
within the message so that threads can pack and unpack each
field independently? For case (b), do you align each
boundary pencil of each field separately? This increases
the message size, but could improve cache coherence so
each pencil could be processed independently by a separate
thread with no conflicts.
Also, memory access for case (c) would best be done without
going through the cache (the stride is so large it just causes
a lot of useless cache evictions). Is it worth creating
a special case version of the packing algorithm that uses
non-coherent load/store opcodes?
*/
/*
Currently, all message traffic occurs at once.
We could spread message traffic out like this:
CommRecv(domain) ;
forall(domain->views()-attr("chunk & boundary")) {
... do work in parallel ...
}
CommSend(domain) ;
forall(domain->views()-attr("chunk & ~boundary")) {
... do work in parallel ...
}
CommSBN() ;
or the CommSend() could function as a semaphore
for even finer granularity. When the last chunk
on a boundary marks the boundary as complete, the
send could happen immediately:
CommRecv(domain) ;
forall(domain->views()-attr("chunk & boundary")) {
... do work in parallel ...
CommSend(domain) ;
}
forall(domain->views()-attr("chunk & ~boundary")) {
... do work in parallel ...
}
CommSBN() ;
*/
/* doRecv flag only works with regular block structure */
void CommRecv(Domain *domain, int msgType, Index_t xferFields,
Index_t dx, Index_t dy, Index_t dz, bool doRecv, bool planeOnly, int replay_it, int stage) {
if (domain->numProcs == 1) return ;
/* post recieve buffers for all incoming messages */
int myRank ;
Index_t maxPlaneComm = xferFields * domain->maxPlaneSize ;
Index_t maxEdgeComm = xferFields * domain->maxEdgeSize ;
Index_t pmsg = 0 ; /* plane comm msg */
Index_t emsg = 0 ; /* edge comm msg */
Index_t cmsg = 0 ; /* corner comm msg */
MPI_Datatype baseType = ((sizeof(Real_t) == 4) ? MPI_FLOAT : MPI_DOUBLE) ;
bool rowMin, rowMax, colMin, colMax, planeMin, planeMax ;
/* assume communication to 6 neighbors by default */
rowMin = rowMax = colMin = colMax = planeMin = planeMax = true ;
if (domain->rowLoc == 0) {
rowMin = false ;
}
if (domain->rowLoc == (domain->tp-1)) {
rowMax = false ;
}
if (domain->colLoc == 0) {
colMin = false ;
}
if (domain->colLoc == (domain->tp-1)) {
colMax = false ;
}
if (domain->planeLoc == 0) {
planeMin = false ;
}
if (domain->planeLoc == (domain->tp-1)) {
planeMax = false ;
}
for (Index_t i=0; i<26; ++i) {
domain->recvRequest[i] = MPI_REQUEST_NULL ;
}
MPI_Comm_rank(world, &myRank) ;
/* post receives */
/* receive data from neighboring domain faces */
if (planeMin && doRecv) {
/* contiguous memory */
int fromProc = myRank - domain->tp*domain->tp ;
int recvCount = dx * dy * xferFields ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm],
recvCount, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg], &pmsg, replay_it, 1, stage) ;
}
if (planeMax) {
/* contiguous memory */
int fromProc = myRank + domain->tp*domain->tp ;
int recvCount = dx * dy * xferFields ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm],
recvCount, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg], &pmsg, replay_it, 2, stage) ;
}
if (rowMin && doRecv) {
/* semi-contiguous memory */
int fromProc = myRank - domain->tp ;
int recvCount = dx * dz * xferFields ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm],
recvCount, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg], &pmsg, replay_it, 3, stage) ;
}
if (rowMax) {
/* semi-contiguous memory */
int fromProc = myRank + domain->tp ;
int recvCount = dx * dz * xferFields ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm],
recvCount, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg], &pmsg, replay_it, 4, stage) ;
}
if (colMin && doRecv) {
/* scattered memory */
int fromProc = myRank - 1 ;
int recvCount = dy * dz * xferFields ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm],
recvCount, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg], &pmsg, replay_it, 5, stage) ;
}
if (colMax) {
/* scattered memory */
int fromProc = myRank + 1 ;
int recvCount = dy * dz * xferFields ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm],
recvCount, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg], &pmsg, replay_it, 6, stage) ;
}
if (!planeOnly) {
/* receive data from domains connected only by an edge */
if (rowMin && colMin && doRecv) {
int fromProc = myRank - domain->tp - 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dz * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 7, stage) ;
}
if (rowMin && planeMin && doRecv) {
int fromProc = myRank - domain->tp*domain->tp - domain->tp ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dx * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 8, stage) ;
}
if (colMin && planeMin && doRecv) {
int fromProc = myRank - domain->tp*domain->tp - 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dy * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 9, stage) ;
}
if (rowMax && colMax) {
int fromProc = myRank + domain->tp + 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dz * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 10, stage) ;
}
if (rowMax && planeMax) {
int fromProc = myRank + domain->tp*domain->tp + domain->tp ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dx * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 11, stage) ;
}
if (colMax && planeMax) {
int fromProc = myRank + domain->tp*domain->tp + 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dy * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 12, stage) ;
}
if (rowMax && colMin) {
int fromProc = myRank + domain->tp - 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dz * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 13, stage) ;
}
if (rowMin && planeMax) {
int fromProc = myRank + domain->tp*domain->tp - domain->tp ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dx * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 14, stage) ;
}
if (colMin && planeMax) {
int fromProc = myRank + domain->tp*domain->tp - 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dy * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 15, stage) ;
}
if (rowMin && colMax && doRecv) {
int fromProc = myRank - domain->tp + 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dz * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 16, stage) ;
}
if (rowMax && planeMin && doRecv) {
int fromProc = myRank - domain->tp*domain->tp + domain->tp ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dx * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 17, stage) ;
}
if (colMax && planeMin && doRecv) {
int fromProc = myRank - domain->tp*domain->tp + 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm],
dy * xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg], &emsg, replay_it, 18, stage) ;
}
/* receive data from domains connected only by a corner */
if (rowMin && colMin && planeMin && doRecv) {
/* corner at domain logical coord (0, 0, 0) */
int fromProc = myRank - domain->tp*domain->tp - domain->tp - 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm +
cmsg * CACHE_COHERENCE_PAD_REAL],
xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg+cmsg], &cmsg, replay_it, 19, stage) ;
}
if (rowMin && colMin && planeMax) {
/* corner at domain logical coord (0, 0, 1) */
int fromProc = myRank + domain->tp*domain->tp - domain->tp - 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm +
cmsg * CACHE_COHERENCE_PAD_REAL],
xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg+cmsg], &cmsg, replay_it, 20, stage) ;
}
if (rowMin && colMax && planeMin && doRecv) {
/* corner at domain logical coord (1, 0, 0) */
int fromProc = myRank - domain->tp*domain->tp - domain->tp + 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm +
cmsg * CACHE_COHERENCE_PAD_REAL],
xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg+cmsg], &cmsg, replay_it, 21, stage) ;
}
if (rowMin && colMax && planeMax) {
/* corner at domain logical coord (1, 0, 1) */
int fromProc = myRank + domain->tp*domain->tp - domain->tp + 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm +
cmsg * CACHE_COHERENCE_PAD_REAL],
xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg+cmsg], &cmsg, replay_it, 22, stage) ;
}
if (rowMax && colMin && planeMin && doRecv) {
/* corner at domain logical coord (0, 1, 0) */
int fromProc = myRank - domain->tp*domain->tp + domain->tp - 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm +
cmsg * CACHE_COHERENCE_PAD_REAL],
xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg+cmsg], &cmsg, replay_it, 23, stage) ;
}
if (rowMax && colMin && planeMax) {
/* corner at domain logical coord (0, 1, 1) */
int fromProc = myRank + domain->tp*domain->tp + domain->tp - 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm +
cmsg * CACHE_COHERENCE_PAD_REAL],
xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg+cmsg], &cmsg, replay_it, 24, stage) ;
}
if (rowMax && colMax && planeMin && doRecv) {
/* corner at domain logical coord (1, 1, 0) */
int fromProc = myRank - domain->tp*domain->tp + domain->tp + 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm +
cmsg * CACHE_COHERENCE_PAD_REAL],
xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg+cmsg], &cmsg, replay_it, 25, stage) ;
}
if (rowMax && colMax && planeMax) {
/* corner at domain logical coord (1, 1, 1) */
int fromProc = myRank + domain->tp*domain->tp + domain->tp + 1 ;
recv_wrapper(&domain->commDataRecv[pmsg * maxPlaneComm +
emsg * maxEdgeComm +
cmsg * CACHE_COHERENCE_PAD_REAL],
xferFields, baseType, fromProc, (msgType + fromProc),
world, &domain->recvRequest[pmsg+emsg+cmsg], &cmsg, replay_it, 26, stage) ;
}
}
}
void CommSend(Domain *domain, int msgType,
Index_t xferFields, Real_t **fieldData,
Index_t dx, Index_t dy, Index_t dz, bool doSend, bool planeOnly, int replay_it, int stage)
{
if (domain->numProcs == 1) return ;
/* post recieve buffers for all incoming messages */
int myRank ;
Index_t maxPlaneComm = xferFields * domain->maxPlaneSize ;
Index_t maxEdgeComm = xferFields * domain->maxEdgeSize ;
Index_t pmsg = 0 ; /* plane comm msg */
Index_t emsg = 0 ; /* edge comm msg */
Index_t cmsg = 0 ; /* corner comm msg */
MPI_Datatype baseType = ((sizeof(Real_t) == 4) ? MPI_FLOAT : MPI_DOUBLE) ;
MPI_Status status[26] ;
Real_t *destAddr ;