-
Notifications
You must be signed in to change notification settings - Fork 7
/
dumpgen.c
918 lines (683 loc) · 26.8 KB
/
dumpgen.c
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
#include "decs.h"
int dump_gen(int readwrite, long dump_cnt, int bintxt, int whichdump, MPI_Datatype datatype, char *fileprefix, char *fileformat, char *filesuffix, int (*headerfun) (int whichdump, int whichdumpversion, int numcolumns, int bintxt, FILE*headerptr),int (*setgetcontent) (int i, int j, int k, MPI_Datatype datatype, void*setbuf))
{
int i = 0, j = 0, k = 0, l = 0, col = 0;
FILE **fpp;
char dfnam[MAXFILENAME];
char dfnamreal[MAXFILENAME];
char localfileformat[MAXFILENAME];
void *jonio;
static void *writebuf; // must be static so ROMIO non-blocking has permenant pointer address for this
char truemyidtxt[MAXFILENAME];
char filerw[MAXFILENAME];
FILE *headerptr;
int numfiles,coliter;
void *setbuf;
int sizeofdatatype;
int romiocloopend;
int headerbintxt;
int dumpbintxt;
fpos_t headerendpos;
long int uptodatabytesize;
int binextension;
int checkstatus;
int gopastlinebreak(FILE *stream);
int whichdumpversion;
// see if want to only do ROMIO finish-up when doing MPIVERSION>=2
if(whichdump==FAKEDUMPTYPE){
// only writebufptr and which realy used, so just ignore other values
// -1 indicates separate finish romio call, so that next call to mpiio_final() will not think some new file is ready to be finished.
mpiio_final(0, 0, NULL, 0, MPICOMBINEROMIO, NULL, -1, 0, NULL, &writebuf);
// done with finishing-up prior ROMIO combine if using ROMIO
return(0);
}
////////////
//
// setup file format for header and dump
//
////////////
if(readwrite==READFILE){
strcpy(filerw,"rb"); //atch
//if(bintxt==BINARYOUTPUT)
//else strcpy(filerw,"rt");
}
else if(readwrite==WRITEFILE){
strcpy(filerw,"wb"); //atch
//if(bintxt==BINARYOUTPUT) strcpy(filerw,"w");
//else strcpy(filerw,"wt");
}
if(bintxt==BINARYOUTPUT) headerbintxt=dumpbintxt=BINARYOUTPUT;
else if(bintxt==TEXTOUTPUT) headerbintxt=dumpbintxt=TEXTOUTPUT;
else if(bintxt==MIXEDOUTPUT){
headerbintxt=TEXTOUTPUT;
dumpbintxt=BINARYOUTPUT;
}
numcolumns=dnumcolumns[whichdump];
whichdumpversion=dnumversion[whichdump];
docolsplit=DOCOLSPLIT[whichdump]; // docolsplit global var for now
////////////////////
//
// See if enough HD space
//
////////////////////
if(mpicombine){
if(dumpbintxt==BINARYOUTPUT){
if(myid==0) isenoughfreespace((unsigned long long)(sizeof(FTYPE))*(unsigned long long)numcolumns*(unsigned long long)(totalsize[1])*(unsigned long long)(totalsize[2])*(unsigned long long)(totalsize[3]) );
else isenoughfreespace(0);
}
else{// text
if(myid==0) isenoughfreespace((unsigned long long)(22)*(unsigned long long)numcolumns*(unsigned long long)(totalsize[1])*(unsigned long long)(totalsize[2])*(unsigned long long)(totalsize[3]) );
else isenoughfreespace(0);
}
}
else{
if(dumpbintxt==BINARYOUTPUT){
isenoughfreespace((unsigned long long)(sizeof(FTYPE))*(unsigned long long)numcolumns*(unsigned long long)(N1)*(unsigned long long)(N2)*(unsigned long long)(N3) );
}
else{// text
isenoughfreespace((unsigned long long)(21)*(unsigned long long)numcolumns*(unsigned long long)(N1)*(unsigned long long)(N2)*(unsigned long long)(N3) );
}
}
/////////////////////
//
// Allocate memory for setting up setbuf
//
//////////////////////
sizeofdatatype=getsizeofdatatype(datatype);
setbuf=malloc(numcolumns*sizeofdatatype);
if(setbuf==NULL){
dualfprintf(fail_file,"cannot allocate memory to setbuf in %s %s with numcolumns=%d and sizeofdatatype=%d\n",fileprefix,filesuffix,numcolumns,sizeofdatatype);
myexit(927656247);
}
// trifprintf("numcolumns=%d sizeofdatatype=%d setbuf=%d total=%d\n",numcolumns,sizeofdatatype,setbuf,numcolumns*sizeofdatatype);
//////////////////////////////////
//
// Set up DOCOLSPLIT for normal and ROMIO loop
//
///////////////////////////////////
if(docolsplit){
numfiles=numcolumns;
if(mpicombine&&USEMPI&&USEROMIO) romiocloopend=numfiles;
else romiocloopend=1;
}
else{
numfiles=1;
romiocloopend=1;
}
//////////////////////////////////
//
// Define file output and open it
//
///////////////////////////////////
// say whether .bin is allowed or not if binary
if(fileprefix[0]=='i') binextension=0; // images don't need binary extension
else binextension=1;
// sometimes all CPUs need to know filename (e.g. ROMIO)
// setup file suffix
if((dumpbintxt==BINARYOUTPUT)&&(binextension)){
if(USEMPI&&(mpicombine==0)&&(numprocs>1)) sprintf(truemyidtxt,".bin.%04d",myid);
else strcpy(truemyidtxt,".bin");
}
else{
if(USEMPI&&(mpicombine==0)&&(numprocs>1)) sprintf(truemyidtxt,".%04d",myid);
else strcpy(truemyidtxt,"");
}
// setup filename
if(dump_cnt>=0){
strcpy(localfileformat,"%s");
strcat(localfileformat,fileformat);
strcat(localfileformat,"%s");
strcat(localfileformat,"%s");
sprintf(dfnam, localfileformat, fileprefix, dump_cnt, filesuffix, truemyidtxt);
}
else{ // then no file number wanted (i.e. for gdump())
sprintf(dfnam, "%s%s%s", fileprefix, filesuffix, truemyidtxt);
}
////////////////
//
// open files, or open files for header if mpicombine==1, which for mpicombine==1 gets reopened later by MPI routines
//
///////////////
checkstatus=0; // no error so far
int problemloadingfile=0;
if((USEMPI&&(myid==0)&&(mpicombine==1))||(mpicombine==0)){// for mpicombine==1 even with ROMIO, real filename and header not needed
// only one CPU does header if mpicombine==1, header+dump done in all CPUs if mpicombine==0
// create files for each column, or each column's header if mpicombine==1
if((fpp=(FILE**)malloc(sizeof(FILE*)*numfiles))==NULL){
dualfprintf(fail_file,"couldn't open fpp in dump()\n");
myexit(836565474);
}// now fpp[i] indexes a list of file pointers
// setup each file corresponding to each column
COLLOOP(coliter){
if(docolsplit&&(numfiles>1)){
sprintf(dfnamreal,"%s-col%04d",dfnam,coliter);
}
else strcpy(dfnamreal,dfnam);
if ((fpp[coliter] = fopen(dfnamreal, filerw)) == NULL) {
dualfprintf(fail_file, "error opening %s %s (fullname=%s) file\n",fileprefix,filesuffix,dfnamreal);
dualfprintf(fail_file, "Check if disk full or have correct permissions\n");
problemloadingfile=1;
}
}// end COLLOOP
}// end if myid or mpicombine==0 (split the loop so that can check for file existence first)
// need to broadcast whether got all files or had problem
#if(USEMPI)
MPI_Bcast(&problemloadingfile,1,MPI_INT,MPIid[0], MPI_COMM_GRMHD);
#endif
if(problemloadingfile){
// indicate failure, but one may wish to set some defaults if no file, so don't hard fail.
return(FILENOTFOUND);
}
checkstatus=0; // no error so far
if((USEMPI&&(myid==0)&&(mpicombine==1))||(mpicombine==0)){// for mpicombine==1 even with ROMIO, real filename and header not needed
// setup each file corresponding to each column
COLLOOP(coliter){
//////////////////////////////////
//
// read or write header: header is read/written in whatever style chosen to the top of each dump file created
//
///////////////////////////////////
if(headerfun!=NULL){
headerfun(whichdump,whichdumpversion,numcolumns,headerbintxt,fpp[coliter]); // outputs header to each column file (or just one file, or all CPU files, etc.)
}
////////////////////////////
//
// check that file one is reading is in right format (no need to check writing format)
// assumed puts stream back to location before entered function
//
////////////////////////////
if(readwrite==READFILE){
checkstatus=check_fileformat(readwrite, bintxt, whichdump, numcolumns, docolsplit, mpicombine, sizeofdatatype, fpp[coliter]);
}
// deal with transition between header and data
if(readwrite==READFILE){
if(bintxt==TEXTOUTPUT || bintxt==MIXEDOUTPUT){
if(headerfun!=NULL){
// now move past \n
if(gopastlinebreak(fpp[coliter])) checkstatus=1;
}
}
}
// get position that would start real data
uptodatabytesize=ftell(fpp[coliter]);
}
// don't close if mpicombine==0, since used in a moment, else mpicombine==1 it's reopened by MPI routines
if(USEMPI&&(myid==0)&&(mpicombine==1)) COLLOOP(coliter) fclose(fpp[coliter]); // will get reopened later by MPI routines
}
// need to broadcast the header size to other CPUs for ROMIO
#if(USEMPI&&USEROMIO)
MPI_Bcast(&uptodatabytesize,1,MPI_LONG,MPIid[0], MPI_COMM_GRMHD);
#endif
///////////////////////////////////////////////////////////
//
// Check file format status
//
//
////////////////////////////////////////////////////////////
if(failed==0) failed=checkstatus;
error_check(ERRORCODEBELOWCLEANFINISH+100); // number should be >ERRORCODEBELOWCLEANFINISH for myexit to avoid dumping
///////////////////////////////////////////////////////////
//
// loop over columns for per-column buffer ROMIO dump
//
//
////////////////////////////////////////////////////////////
ROMIOCOLLOOP(romiocoliter) { // only loop if mpicombine&&USEMPI&&USEROMIO&&docolsplit==1
if(romiocloopend>1) trifprintf("romiocoliter=%d of romiocloopend=%d\n",romiocoliter,romiocloopend);
// setup MPI buffer if mpicombine==1
if( mpicombine == 0 ) { // then one file per CPU if USEMPI or just normal file writing on 1CPU
writebuf=NULL;
}
else mpiio_init(dumpbintxt,sortedoutput, fpp, uptodatabytesize, readwrite, dfnam, numcolumns, datatype, &jonio, &writebuf);
// if USEROMIO==1 then numcolumns interpreted properly for docolsplit
if(readwrite==READFILE){
//////////////////////////////////
//
// read DUMP
//
//////////////////////////////////
if (mpicombine == 1) {
#if(USEMPI)
mpiio_seperate(binaryoutput,sortedoutput, STAGE1, numcolumns, datatype, fpp, jonio, writebuf);
#endif
}
}
//////////////////
//
// DUMP LOOP
//
//////////////////
if(readwrite==READFILE){
BUFFERINIT0;
DUMPGENLOOP { // diagnostic loop
// buffer init starts the parallel index
BUFFERINIT;
// initialize to 0th column
COLINIT;
///////////////////////////////////////
//
// READFILE
//
//////////////////////
if((mpicombine)&&(truempicombinetype==MPICOMBINEMINMEM)) mpiio_minmem(READFILE,whichdump,i,j,k,dumpbintxt,sortedoutput,numcolumns,datatype, fpp,jonio,writebuf);
// read all at once
myfread(dumpbintxt,datatype,setbuf,0,numcolumns,i,j,k,fpp,writebuf);
// check
if(nextbuf!=numcolumns){
dualfprintf(fail_file,"Number of columns (numcolumns=%d) isn't equal to number of columns/buffers attempted (nextbuf=%lld)\n",numcolumns,nextbuf);
myexit(932736466);
}
// get the content of 1 row
setgetcontent(i,j,k,datatype,setbuf);
// check
if(nextcol!=numcolumns){
dualfprintf(fail_file,"Number of columns (numcolumns=%d) isn't equal to number of columns attempted (nextcol=%d)\n",numcolumns,nextcol);
myexit(836745613);
}
}// end DUMPGENLOOP
}// end readwrite==READFILE
else if(readwrite==WRITEFILE){
BUFFERINIT0;
DUMPGENLOOP { // diagnostic loop
// buffer init starts the parallel index
BUFFERINIT;
// initialize to 0th column
COLINIT;
///////////////////////////////////////
//
// WRITEFILE
//
//////////////////////
// set the content of 1 row
setgetcontent(i,j,k,datatype,setbuf);
// check
if(nextcol!=numcolumns){
dualfprintf(fail_file,"Number of columns (numcolumns=%d) isn't equal to number of columns attempted (nextcol=%d)\n",numcolumns,nextcol);
myexit(19785566);
}
// write all at once
myfwrite(dumpbintxt,datatype,setbuf,0,numcolumns,i,j,k,fpp,writebuf);
// check
if(nextbuf!=numcolumns){
dualfprintf(fail_file,"Number of columns (numcolumns=%d) isn't equal to number of columns/buffers attempted (nextbuf=%d)\n",numcolumns,nextbuf);
myexit(94675455);
}
// finish up this row
if((mpicombine==0)&&(dumpbintxt==TEXTOUTPUT)) COLLOOP(coliter) fprintf(fpp[coliter],"\n");
if((mpicombine)&&(truempicombinetype==MPICOMBINEMINMEM)) mpiio_minmem(WRITEFILE,whichdump,i,j,k,dumpbintxt,sortedoutput,numcolumns,datatype, fpp, jonio,writebuf);
}// end DUMPGENLOOP
}//end readwrite==WRITEFILE
//////////////////
//
// Close dump file and write/close file if mpicombine==1
//
//////////////////
if (mpicombine == 0){
COLLOOP(coliter) if (fpp[coliter] != NULL) fclose(fpp[coliter]);
}
else{
#if(USEMPI)
if(readwrite==WRITEFILE) mpiio_combine(dumpbintxt, sortedoutput, numcolumns, datatype, fpp, jonio, writebuf);
else if(readwrite==READFILE) mpiio_seperate(binaryoutput,sortedoutput, STAGE2, numcolumns, datatype, fpp, jonio, writebuf);
#endif
}
}// end column loop for ROMIO&&docolsplit
// free the set/get buffer
if(setbuf!=NULL) free(setbuf);
return (0);
}
// In reading/writing any header one has binary/text format
// Need single function that read/write in binary/text so have consistent input/output format always
// also need to Bcast it sometimes
// examples:
// binary read
// fread(&idum1, sizeof(int), 1, headerptr);
// text read:
// fscanf(headerptr,"%ld",&DTr);
// Bcast example:
// MPI_Bcast(&avgscheme[1],1, MPI_INT, MPIid[0], MPI_COMM_GRMHD);
// binary write:
// fwrite(&totalsize[1], sizeof(int), 1, headerptr);
// text write:
// fprintf(headerptr,"%ld",DTr);
// format assumed to have no space at end and will add that if writing
// assume root=MPIid[0] and MPI_COMM_GRMHD for Bcast
// readwrite:
//#define WRITEHEAD 0
//#define READHEAD 1
// bintxt: BINARYOUTPUT TEXTOUTPUT only choices -- if mixed then choose text
int header1_gen(int accessmemory, int readwrite, int bintxt, int bcasthead, void *ptr, size_t size, char *format, size_t nmemb, MPI_Datatype datatype, FILE *stream)
{
void *ptrlocal;
//
unsigned char *ptr1;
float *ptr4;
double *ptr8;
long double *ptr16;
int *ptr4i;
long *ptr4l;
long long int *ptr8i;
//
unsigned char var1;
float var4;
double var8;
long double var16;
int var4i;
long var4l;
long long int var8i;
//
char formatwithspace[MAXFILENAME]; // really format length, not file name
size_t ii;
char largedumbspace[200]; // GODMARK: Should be larger than long doubles
int jj;
if(accessmemory){
ptrlocal=ptr;
}
else{
// emulates argument passing to this function through assignment of pointers
var1=0;
var4=0.0;
var8=0.0;
var16=0.0;
var4i=0;
var4l=0;
var8i=0;
if (datatype == MPI_UNSIGNED_CHAR) ptrlocal = (unsigned char *) &var1;
else if (datatype == MPI_FLOAT) ptrlocal = (float *) &var4;
else if (datatype == MPI_DOUBLE) ptrlocal = (double *) &var8;
else if (datatype == MPI_LONG_DOUBLE) ptrlocal = (long double *) &var16;
else if (datatype == MPI_INT) ptrlocal = (int *) &var4i;
else if (datatype == MPI_LONG) ptrlocal = (long *) &var4l;
else if (datatype == MPI_LONG_LONG_INT) ptrlocal = (long long int *) &var8i;
else{
dualfprintf(fail_file,"No such datatype=%d\n",datatype);
myexit(76293623);
}
}
///////////////////////
//
// resolve data type as necessary for fprintf
//
///////////////////////
if (datatype == MPI_UNSIGNED_CHAR) ptr1 = (unsigned char *) ptrlocal;
else if (datatype == MPI_FLOAT) ptr4 = (float *) ptrlocal;
else if (datatype == MPI_DOUBLE) ptr8 = (double *) ptrlocal;
else if (datatype == MPI_LONG_DOUBLE) ptr16 = (long double *) ptrlocal;
else if (datatype == MPI_INT) ptr4i = (int *) ptrlocal;
else if (datatype == MPI_LONG) ptr4l = (long *) ptrlocal;
else if (datatype == MPI_LONG_LONG_INT) ptr8i = (long long int *) ptrlocal;
else{
dualfprintf(fail_file,"No such datatype=%d\n",datatype);
myexit(76293623);
}
if(readwrite==READHEAD){
if(bintxt==BINARYOUTPUT){
if(accessmemory) fread(ptrlocal,size,nmemb,stream);
else{
for(ii=0;ii<nmemb;ii++){
fread(ptrlocal,size,1,stream); // just repeatedly fill same dummy space
}
}
}
else if(bintxt==TEXTOUTPUT || bintxt==MIXEDOUTPUT){
for(ii=0;ii<nmemb;ii++){
if(accessmemory) jj=ii;
else jj=0; // repeatedly read into same dummy space
if (datatype == MPI_UNSIGNED_CHAR) fscanf(stream,format,&ptr1[jj]);
else if (datatype == MPI_FLOAT) fscanf(stream,format,&ptr4[jj]);
else if (datatype == MPI_DOUBLE) fscanf(stream,format,&ptr8[jj]);
else if (datatype == MPI_LONG_DOUBLE) fscanf(stream,format,&ptr16[jj]);
else if (datatype == MPI_INT) fscanf(stream,format,&ptr4i[jj]);
else if (datatype == MPI_LONG) fscanf(stream,format,&ptr4l[jj]);
else if (datatype == MPI_LONG_LONG_INT) fscanf(stream,format,&ptr8i[jj]);
else{
dualfprintf(fail_file,"No such datatype=%d\n",datatype);
myexit(40968321);
}
}
}
else{
dualfprintf(fail_file,"No such bintxt=%d in readwrite=%d\n",bintxt,readwrite);
myexit(249684962);
}
}
else if(readwrite==WRITEHEAD){
if(bintxt==BINARYOUTPUT){
if(accessmemory) fwrite(ptrlocal,size,nmemb,stream);
else{
for(ii=0;ii<nmemb;ii++){
fwrite(ptrlocal,size,1,stream); // repeatedly write from same dummy space (value=0)
}
}
}
else if(bintxt==TEXTOUTPUT || bintxt==MIXEDOUTPUT){
sprintf(formatwithspace,"%s ",format);
for(ii=0;ii<nmemb;ii++){
if(accessmemory) jj=ii;
else jj=0; // repeatedly read into same dummy space
if (datatype == MPI_UNSIGNED_CHAR) fprintf(stream,formatwithspace,ptr1[jj]);
else if (datatype == MPI_FLOAT) fprintf(stream,formatwithspace,ptr4[jj]);
else if (datatype == MPI_DOUBLE) fprintf(stream,formatwithspace,ptr8[jj]);
else if (datatype == MPI_LONG_DOUBLE) fprintf(stream,formatwithspace,ptr16[jj]);
else if (datatype == MPI_INT) fprintf(stream,formatwithspace,ptr4i[jj]);
else if (datatype == MPI_LONG) fprintf(stream,formatwithspace,ptr4l[jj]);
else if (datatype == MPI_LONG_LONG_INT) fprintf(stream,formatwithspace,ptr8i[jj]);
else{
dualfprintf(fail_file,"No such datatype=%d\n",datatype);
myexit(98346834);
}
}
}
else{
dualfprintf(fail_file,"No such bintxt=%d in readwrite=%d\n",bintxt,readwrite);
myexit(24934963);
}
}
else if(readwrite==NOTHINGHEAD){
if(!bcasthead){
dualfprintf(fail_file,"Entered header1_gen() with nothing do to\n");
myexit(24672672);
}
}
else{
dualfprintf(fail_file,"Entered header1_gen() with nothing do to version2\n");
myexit(24672673);
}
// assume only broadcast when reading header information
// only do if accessing memory (not dummy memory)
if(bcasthead && accessmemory){
// bintxt doesn't matter
// assume root=MPIid[0] and MPI_COMM_GRMHD
#if(USEMPI)
MPI_Bcast(ptr, (int)nmemb, datatype, MPIid[0], MPI_COMM_GRMHD);
#endif
}
// return(0); // indicates no failure
return(nmemb);
}
// check that file read is in right format to avoid error in setup of restart header or data sizes
int check_fileformat(int readwrite, int bintxt, int whichdump, int numcolumns, int docolsplit, int mpicombine, int sizeofdatatype, FILE *stream)
{
long onlyheaderbytesize;
long uptodatabytesize;
long withintransitionbytesize;
long totalbytesize;
//
int truenumcolumns;
long long int datawordnumber;
long long int totaldatasize;
long long int wordtotal;
long long int badwordtotal;
long long int databytesize;
long long int fullheaderbytesize;
int get_word_count(long long int databytesize, long long int *wordtotal, FILE *stream);
unsigned char mychar;
int gopastlinebreak(FILE *stream);
// get position of stream, which indicates size of header in bytes
onlyheaderbytesize=ftell(stream);
// find transition between header and data
if(readwrite==READFILE){
if(bintxt==TEXTOUTPUT || bintxt==MIXEDOUTPUT){
gopastlinebreak(stream);
}
}
// up to and including '\n' minus just beyond header
// will be same as onlyheaderbytesize if nothing between header and data or if binary
uptodatabytesize=ftell(stream);
// get number of bytes up to first \n
if(readwrite==READFILE){
if(bintxt==TEXTOUTPUT || bintxt==MIXEDOUTPUT){
fseek(stream,0,SEEK_SET);
gopastlinebreak(stream);
}
}
fullheaderbytesize=ftell(stream);
// see if header is really up to first \n and not multiple \n
if(fullheaderbytesize!=uptodatabytesize){
dualfprintf(fail_file,"restart read found \\n mismatch: fullheaderbytesize=%lld uptodatabytesize=%lld\n",fullheaderbytesize,uptodatabytesize);
return(1);
}
// go back to where was before getting up to first \n
fseek(stream,uptodatabytesize,SEEK_SET);
// byte size of transition region
withintransitionbytesize = uptodatabytesize - onlyheaderbytesize;
// DEBUG:
// dualfprintf(fail_file,"onlyheaderbytesize=%ld uptodatabytesize=%ld withintransitionbytesize=%ld\n",onlyheaderbytesize,uptodatabytesize,withintransitionbytesize);
// go to end of file
fseek(stream,0,SEEK_END);
// get byte size
totalbytesize=ftell(stream);
// get bytes in data region
databytesize = totalbytesize-uptodatabytesize;
// determine number of columns within a file
if(docolsplit) truenumcolumns=1;
else truenumcolumns=numcolumns;
// determine number of words in data section
if(mpicombine) datawordnumber=totalsize[1]*totalsize[2]*totalsize[3]*truenumcolumns;
else datawordnumber=N1*N2*N3*truenumcolumns;
// determine total bytes in data section
totaldatasize=datawordnumber*sizeofdatatype;
// only have checks for this case so far
if(readwrite==READFILE && whichdump==RESTARTDUMPTYPE){
///////////////////////
//
// Check header
//
///////////////////////
if(bintxt==TEXTOUTPUT || bintxt==MIXEDOUTPUT){
// first start back where ended header that will be first byte of data section
fseek(stream,onlyheaderbytesize,SEEK_SET);
// get word count from onlyheaderbytesize up to '\n'
get_word_count(withintransitionbytesize, &badwordtotal, stream);
if(badwordtotal>0){
dualfprintf(fail_file,"restart read found extra words (badwordtotal=%lld) in header or could be that reading of header passed into data section\n",badwordtotal);
return(1);
}
}
///////////////////////
//
// Check data
//
///////////////////////
if(bintxt==BINARYOUTPUT || bintxt==MIXEDOUTPUT){
// header is binary for BINARYOUTPUT and text for MIXEDOUTPUT
// data is binary
// just check that data section is right size
// this is easier since data section has a single data type unlike header
// header has to be right size for this to work out
if(databytesize != totaldatasize){
dualfprintf(fail_file,"restart read binary header/data found databytesize=%d and totaldatasize=%d\n",databytesize,totaldatasize);
return(1);
}
}
else if(bintxt==TEXTOUTPUT){
// header is text
// data is text
// in this case we don't grab \n just treating it as space and counting words as normal. This is a more strict test that restart file is in correct format
// for data as text could count words somehow (using wc and system) but many clusters don't allow system()
// so have to do it manually
// first start back where ended header that will be first byte of data section
// don't use uptodatabytesize since want to catch errors in word count (want data to be exactly expected word count -- this checks that header is not too long)
//fseek(stream,onlyheaderbytesize,SEEK_SET);
fseek(stream,uptodatabytesize,SEEK_SET); //ATCH: otherwise, would miss the last word in dump file since databytesize definition uses uptodatabytesize as data start pos
// get word count
get_word_count(databytesize, &wordtotal, stream);
if(wordtotal!=datawordnumber){
dualfprintf(fail_file,"restart read text data found wordtotal=%lld while datawordnumber=%lld\n",wordtotal,datawordnumber);
dualfprintf(fail_file,"onlyheaderbytesize=%lld ,totalbytesize=%lld,databytesize=%lld,truenumcolumns=%lld,datawordnumber=%lld,totaldatasize=%lld\n",onlyheaderbytesize,totalbytesize,databytesize,truenumcolumns,datawordnumber,totaldatasize);
return(1);
}
}
}
//////////////////////////
//
// whatever we did to the stream, put it back to just after header function called
//
//////////////////////////
fseek(stream,onlyheaderbytesize,SEEK_SET);
return(0);
}
int gopastlinebreak(FILE *stream)
{
int mychar;
// then need to grab up to '\n'
while(1){
mychar=fgetc(stream);
if(mychar=='\n' || mychar=='\r') break;
if(feof(stream)){
dualfprintf(fail_file,"Reached end of file while seeking \\n or \\r in header\n");
return(1);
}
}
return(0);
}
int get_word_count(long long int databytesize, long long int *wordtotal, FILE *stream)
{
unsigned char mychar;
int wordchar,spacechar;
long long int i;
int thischarisword;
// now run through text data section
// a word is defined as some characters not including space, \n, \r, etc. and being bounded at least on one side by such characters
wordchar=0;
spacechar=0;
*wordtotal=0;
// go through each byte
for(i=0;i<databytesize;i++){
// read-in a byte (character) at a time
mychar=fgetc(stream);
if(feof(stream)){
dualfprintf(fail_file,"Something is wrong with databytesize or loop since databytesize=%lld but found EOF\n",databytesize);
return(1);
}
thischarisword=-1;
if(mychar=='\n' || mychar=='\r' || mychar==' ' || mychar=='\t' || mychar=='\v'){
// then got word-break (delimiter) type character
spacechar++;
thischarisword=0;
}
else{
wordchar++;
thischarisword=1;
}
// check for word at start
if(i==0 && wordchar==1 && thischarisword){
// then detected new word appearing and this is what we are counting (instead of ends of words)
(*wordtotal)++;
}
// check for word entering as "space word"
if(wordchar==1 && spacechar>0 && thischarisword){
// this defines presence of word we just entered
(*wordtotal)++;
// reset spacechar
spacechar=0;
}
// reset wordchar if within space region
if(spacechar>0){
wordchar=0;
}
// DEBUG:
// dualfprintf(fail_file,"i=%lld mychar=%c spacechar=%d wordchar=%d wordtotal=%lld\n",i,mychar,spacechar,wordchar,*wordtotal);
}// end over all bytes
return(0);
}