-
Notifications
You must be signed in to change notification settings - Fork 24
/
processdata.c
2070 lines (1837 loc) · 64 KB
/
processdata.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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "processdata.h"
//Reads the results stored in temporary files and outputs them conveniently to a new file.
//Use this for parallel implementations.
//There could be problems if there is lots of data in the temporary files. Improve on this in the future.
//Link** sys: The data for the riversystem.
//int* my_sys: The index in sys of all links assigned to this process.
//int my_N: The number of links assigned to this process.
//int save_size: Number of links for which data is to be written to file. This is the grand total for all processes.
//int my_save_size: Number of links for which data is to be written to file. This is the number for just this process.
//Return value = 0 means everything is good.
//1 means a database related error.
//2 means a file system related error.
//!!!! Is additional_temp needed? !!!!
int Process_Data(Link** sys,UnivVars* GlobalVars,unsigned int N,unsigned int* save_list,unsigned int save_size,unsigned int my_save_size,unsigned int** id_to_loc,int* assignments,char* additional_temp,char* additional_out,ConnData* conninfo,FILE** my_tempfile)
{
int i,proc,k;
unsigned int j,l,m,loc,id,counter,total_spaces,my_max_disk,max_disk,*space_counter,size = 16;
char filename[GlobalVars->string_size],filenamespace[GlobalVars->string_size],outputfilename[GlobalVars->string_size],outputfilename_index[GlobalVars->string_size];
char data_storage[size];
fpos_t *positions;
unsigned int dim = GlobalVars->num_print;
FILE *inputfile = NULL,*outputfile = NULL,*outputfile_index = NULL;
long long int jump_size;
Link* current;
//Close the temp file, if open
if(my_tempfile && *my_tempfile)
{
fclose(*my_tempfile);
*my_tempfile = NULL;
}
for(i=0;i<size;i++)
data_storage[i] = 0;
//Find total size of a line in the temp files
unsigned int line_size = CalcTotalOutputSize(GlobalVars);
if(GlobalVars->hydros_loc_flag == 1) //.dat
{
//Create output .dat file
if(my_rank == 0)
{
if(GlobalVars->print_par_flag == 1)
{
if(!additional_out)
sprintf(outputfilename,"%s",GlobalVars->hydros_loc_filename);
else
sprintf(outputfilename,"%s_%s",GlobalVars->hydros_loc_filename,additional_out);
for(i=0;i<GlobalVars->global_params->dim;i++)
{
sprintf(filenamespace,"_%.4e",GlobalVars->global_params->ve[i]);
strcat(outputfilename,filenamespace);
}
sprintf(filenamespace,".dat");
strcat(outputfilename,filenamespace);
}
else
if(!additional_out)
sprintf(outputfilename,"%s.dat",GlobalVars->hydros_loc_filename);
else
sprintf(outputfilename,"%s_%s.dat",GlobalVars->hydros_loc_filename,additional_out);
outputfile = fopen(outputfilename,"w");
if(!outputfile)
{
printf("[%i]: Error opening outputfile %s.\n",my_rank,outputfilename);
return 2;
}
//Header
fprintf(outputfile,"%i\n%i\n",save_size,dim);
}
//Open temporary files
if(my_save_size)
{
if(!additional_temp)
sprintf(filename,"%s",GlobalVars->temp_filename);
else
sprintf(filename,"%s_%s",GlobalVars->temp_filename,additional_temp); //!!!! When is additional_temp useful? Get rid of it? Global params?? !!!!
inputfile = fopen(filename,"rb");
if(!inputfile)
{
printf("\n[%i]: Error opening inputfile %s.\n",my_rank,filename);
return 2;
}
}
//Move data to final output
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(save_list[i],id_to_loc,N);
proc = assignments[loc];
current = sys[loc];
if(proc == my_rank)
{
//Find the link in the temp file inputfile
counter = 0; //index of link in my_save_list of its process
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
while(id != save_list[i] && !feof(inputfile))
{
jump_size = (long long int) total_spaces * (long long int) line_size; //Watch overflows!
fseek(inputfile,jump_size,SEEK_CUR);
counter++;
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
}
if(feof(inputfile))
{
printf("\n[%i]: Error: could not find id %u in temp file %s.\n",my_rank,save_list[i],filename);
return 2;
}
//Write id and number of steps
if(my_rank == 0)
fprintf(outputfile,"\n%u %u\n",save_list[i],current->disk_iterations);
else
MPI_Send(&(current->disk_iterations),1,MPI_UNSIGNED,0,save_list[i],MPI_COMM_WORLD);
//Read data in the temp file
if(my_rank == 0)
{
for(k=0;k<current->disk_iterations;k++)
{
for(m=0;m<dim;m++)
{
fread(data_storage,GlobalVars->output_sizes[m],1,inputfile);
WriteValue(outputfile,GlobalVars->output_specifiers[m],data_storage,GlobalVars->output_types[m]," ");
}
fprintf(outputfile,"\n");
}
}
else
{
for(k=0;k<current->disk_iterations;k++)
{
for(m=0;m<dim;m++)
{
fread(data_storage,GlobalVars->output_sizes[m],1,inputfile);
MPI_Send(&data_storage,size,MPI_CHAR,0,save_list[i],MPI_COMM_WORLD);
}
}
}
//Skip over the last unused space. This is done so that the file does not need to be rewound.
for(k=0;k<total_spaces-current->disk_iterations;k++)
for(j=0;j<dim;j++)
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
}
else if(my_rank == 0)
{
//Write to file
MPI_Recv(&(current->disk_iterations),1,MPI_UNSIGNED,proc,save_list[i],MPI_COMM_WORLD,MPI_STATUS_IGNORE);
fprintf(outputfile,"\n%u %u\n",save_list[i],current->disk_iterations);
for(k=0;k<current->disk_iterations;k++)
{
for(m=0;m<dim;m++)
{
MPI_Recv(data_storage,size,MPI_CHAR,proc,save_list[i],MPI_COMM_WORLD,MPI_STATUS_IGNORE);
WriteValue(outputfile,GlobalVars->output_specifiers[m],data_storage,GlobalVars->output_types[m]," ");
}
fprintf(outputfile,"\n");
}
}
}
//Cleanup
if(inputfile) fclose(inputfile);
if(outputfile) fclose(outputfile);
}
else if(GlobalVars->hydros_loc_flag == 2) //.csv
{
//Create output file
if(my_rank == 0)
{
if(GlobalVars->print_par_flag == 1)
{
if(!additional_out)
sprintf(outputfilename,"%s",GlobalVars->hydros_loc_filename);
else
sprintf(outputfilename,"%s_%s",GlobalVars->hydros_loc_filename,additional_out);
for(i=0;i<GlobalVars->global_params->dim;i++)
{
sprintf(filenamespace,"_%.4e",GlobalVars->global_params->ve[i]);
strcat(outputfilename,filenamespace);
}
sprintf(filenamespace,".csv");
strcat(outputfilename,filenamespace);
}
else
{
if(!additional_out)
sprintf(outputfilename,"%s.csv",GlobalVars->hydros_loc_filename);
else
sprintf(outputfilename,"%s_%s.csv",GlobalVars->hydros_loc_filename,additional_out);
}
outputfile = fopen(outputfilename,"w");
if(!outputfile)
{
printf("\n[%i]: Error opening outputfile %s.\n",my_rank,outputfilename);
return 2;
}
}
//Open input files
if(my_save_size)
{
if(!additional_temp)
sprintf(filename,"%s",GlobalVars->temp_filename);
else
sprintf(filename,"%s_%s",GlobalVars->temp_filename,additional_temp);
inputfile = fopen(filename,"rb");
if(!inputfile)
{
printf("\n[%i]: Error opening inputfile %s.\n",my_rank,filename);
return 2;
}
}
//Initializations
space_counter = (unsigned int*) calloc(save_size,sizeof(unsigned int));
my_max_disk = 0;
for(j=0;j<save_size;j++)
{
loc = find_link_by_idtoloc(save_list[j],id_to_loc,N);
current = sys[loc];
if(assignments[loc] == my_rank)
{
if(my_max_disk < current->disk_iterations) my_max_disk = current->disk_iterations;
if(my_rank != 0)
MPI_Send(&(current->disk_iterations),1,MPI_INT,0,save_list[j],MPI_COMM_WORLD);
}
else if(my_rank == 0)
MPI_Recv(&(current->disk_iterations),1,MPI_INT,assignments[loc],save_list[j],MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}
MPI_Allreduce(&my_max_disk,&max_disk,1,MPI_UNSIGNED,MPI_MAX,MPI_COMM_WORLD);
positions = (fpos_t*) malloc(save_size*sizeof(fpos_t));
//Find the starting position of each link in each file
//Note: We assume the ids in the temp files are ordered by save_list
for(j=0;j<save_size;j++)
{
loc = find_link_by_idtoloc(save_list[j],id_to_loc,N); //!!!! Ugh... !!!!
if(assignments[loc] == my_rank)
{
fseek(inputfile,sizeof(unsigned int),SEEK_CUR); //Skip ID
fread(&total_spaces,sizeof(unsigned int),1,inputfile); //Grab total spaces for this link
fgetpos(inputfile,&(positions[j])); //Here is the place to start for this link
jump_size = (long long int) total_spaces * (long long int) line_size; //Watch overflows!
fseek(inputfile,jump_size,SEEK_CUR); //Skip to next link
}
}
//Make the .csv header
if(my_rank == 0)
{
for(i=0;i<save_size;i++)
{
fprintf(outputfile,"Link %u",save_list[i]);
for(k=0;k<dim;k++) fprintf(outputfile," ,");
}
fprintf(outputfile,"\n");
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(save_list[i],id_to_loc,N); //!!!! Ugh... !!!!
for(k=0;k<dim;k++) fprintf(outputfile,"Output_%u,",k); //!!!! Use names. What if skipping some? !!!!
}
fprintf(outputfile,"\n");
}
//Make the .csv body
if(my_rank == 0)
{
for(m=0;m<max_disk;m++)
{
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(save_list[i],id_to_loc,N); //!!!! Ugh... !!!!
current = sys[loc];
if(space_counter[i] > current->disk_iterations) //This link is done, leave blanks
for(k=0;k<dim;k++) fprintf(outputfile,",");
else
{
if(assignments[loc] == 0)
{
fsetpos(inputfile,&(positions[i]));
for(l=0;l<dim;l++)
{
fread(data_storage,GlobalVars->output_sizes[l],1,inputfile);
WriteValue(outputfile,GlobalVars->output_specifiers[l],data_storage,GlobalVars->output_types[l],",");
}
fgetpos(inputfile,&(positions[i]));
(space_counter[i])++;
}
else
{
for(l=0;l<dim;l++)
{
MPI_Recv(&data_storage,16,MPI_CHAR,assignments[loc],save_list[i],MPI_COMM_WORLD,MPI_STATUS_IGNORE);
WriteValue(outputfile,GlobalVars->output_specifiers[l],data_storage,GlobalVars->output_types[l],",");
}
(space_counter[i])++;
}
}
}
fprintf(outputfile,"\n");
}
}
else
{
for(m=0;m<max_disk;m++)
{
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(save_list[i],id_to_loc,N); //!!!! Ugh... !!!!
current = sys[loc];
if(assignments[loc] == my_rank && space_counter[i] <= current->disk_iterations)
{
fsetpos(inputfile,&(positions[i]));
for(l=0;l<dim;l++)
{
fread(data_storage,GlobalVars->output_sizes[l],1,inputfile);
MPI_Send(&data_storage,16,MPI_CHAR,0,save_list[i],MPI_COMM_WORLD);
}
fgetpos(inputfile,&(positions[i]));
(space_counter[i])++;
}
}
}
}
//Clean up
if(outputfile) fclose(outputfile);
if(inputfile) fclose(inputfile);
free(positions);
free(space_counter);
}
else if(GlobalVars->hydros_loc_flag == 4) //Radek's patented compact binary files
{
size_t io_val;
//Create output .rad and .irad files
//if(my_rank == 0)
{
if(GlobalVars->print_par_flag == 1)
{
if(!additional_out)
sprintf(outputfilename,"%s",GlobalVars->hydros_loc_filename);
else
sprintf(outputfilename,"%s_%s",GlobalVars->hydros_loc_filename,additional_out);
for(i=0;i<GlobalVars->global_params->dim;i++)
{
sprintf(filenamespace,"_%.4e",GlobalVars->global_params->ve[i]);
strcat(outputfilename,filenamespace);
}
sprintf(outputfilename_index,"%s_%i.irad",outputfilename,my_rank);
sprintf(filenamespace,"_%i.rad",my_rank);
strcat(outputfilename,filenamespace);
}
else
{
if(!additional_out)
{
sprintf(outputfilename_index,"%s_%i.irad",GlobalVars->hydros_loc_filename,my_rank);
sprintf(outputfilename,"%s_%i.rad",GlobalVars->hydros_loc_filename,my_rank);
}
else
{
sprintf(outputfilename_index,"%s_%s_%i.irad",GlobalVars->hydros_loc_filename,additional_out,my_rank);
sprintf(outputfilename,"%s_%s_%i.rad",GlobalVars->hydros_loc_filename,additional_out,my_rank);
}
}
outputfile = fopen(outputfilename,"wb");
if(!outputfile)
{
printf("[%i]: Error creating outputfile %s.\n",my_rank,outputfilename);
return 2;
}
outputfile_index = fopen(outputfilename_index,"wb");
if(!outputfile_index)
{
printf("[%i]: Error creating index outputfile %s.\n",my_rank,outputfilename_index);
fclose(outputfile);
return 2;
}
//Write index file
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(save_list[i],id_to_loc,N);
if(assignments[loc] == my_rank)
{
io_val = fwrite(&(save_list[i]),sizeof(unsigned int),1,outputfile_index);
while(io_val != 1)
{
printf("[%i]: Error writing id %u to .irad file. Trying again...\n",my_rank,save_list[i]);
sleep(2);
io_val = fwrite(&(save_list[i]),sizeof(unsigned int),1,outputfile_index);
}
}
}
//fwrite(save_list,sizeof(unsigned int),save_size,outputfile_index);
fclose(outputfile_index);
}
if(my_save_size)
{
//Open temporary file
if(!additional_temp)
sprintf(filename,"%s",GlobalVars->temp_filename);
else
sprintf(filename,"%s_%s",GlobalVars->temp_filename,additional_temp); //!!!! When is additional_temp useful? Get rid of it? Global params?? !!!!
inputfile = fopen(filename,"rb");
if(!inputfile)
{
printf("\n[%i]: Error opening inputfile %s.\n",my_rank,filename);
return 2;
}
//Move data to final output
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(save_list[i],id_to_loc,N);
proc = assignments[loc];
current = sys[loc];
if(proc == my_rank)
{
//Find the link in the temp file inputfile
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
while(id != save_list[i] && !feof(inputfile))
{
jump_size = (long long int) total_spaces * (long long int) line_size; //Watch overflows!
fseek(inputfile,jump_size,SEEK_CUR);
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
}
if(feof(inputfile))
{
printf("\n[%i]: Error: could not find id %u in temp file %s.\n",my_rank,save_list[i],filename);
return 2;
}
//Read data in the temp file
//if(my_rank == 0)
{
for(k=0;k<current->disk_iterations;k++)
{
for(m=0;m<dim;m++)
{
fread(data_storage,GlobalVars->output_sizes[m],1,inputfile);
io_val = fwrite(data_storage,GlobalVars->output_sizes[m],1,outputfile);
while(io_val != 1)
{
printf("[%i]: Error writing data for id %u to .rad file. Trying again...\n",my_rank,save_list[i]);
sleep(2);
io_val = fwrite(data_storage,GlobalVars->output_sizes[m],1,outputfile);
}
}
}
}
//Skip over the last unused space. This is done so that the file does not need to be rewound.
for(k=0;k<total_spaces-current->disk_iterations;k++)
for(j=0;j<dim;j++)
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
}
}
}
//Cleanup
if(inputfile) fclose(inputfile);
if(outputfile) fclose(outputfile);
}
/*
//Create output .dat file
if(my_rank == 0)
{
if(GlobalVars->print_par_flag == 1)
{
if(!additional_out)
sprintf(outputfilename,"%s",GlobalVars->hydros_loc_filename);
else
sprintf(outputfilename,"%s_%s",GlobalVars->hydros_loc_filename,additional_out);
for(i=0;i<GlobalVars->global_params->dim;i++)
{
sprintf(filenamespace,"_%.4e",GlobalVars->global_params->ve[i]);
strcat(outputfilename,filenamespace);
}
sprintf(outputfilename_index,"%s.irad",outputfilename);
sprintf(filenamespace,".rad");
strcat(outputfilename,filenamespace);
}
else
{
if(!additional_out)
{
sprintf(outputfilename_index,"%s.irad",GlobalVars->hydros_loc_filename);
sprintf(outputfilename,"%s.rad",GlobalVars->hydros_loc_filename);
}
else
{
sprintf(outputfilename_index,"%s_%s.irad",GlobalVars->hydros_loc_filename,additional_out);
sprintf(outputfilename,"%s_%s.rad",GlobalVars->hydros_loc_filename,additional_out);
}
}
outputfile = fopen(outputfilename,"wb");
if(!outputfile)
{
printf("[%i]: Error creating outputfile %s.\n",my_rank,outputfilename);
return 2;
}
outputfile_index = fopen(outputfilename_index,"wb");
if(!outputfile_index)
{
printf("[%i]: Error creating index outputfile %s.\n",my_rank,outputfilename_index);
fclose(outputfile);
return 2;
}
//Write index file
fwrite(save_list,sizeof(unsigned int),save_size,outputfile_index);
fclose(outputfile_index);
}
//Open temporary files
if(my_save_size)
{
if(!additional_temp)
sprintf(filename,"%s",GlobalVars->temp_filename);
else
sprintf(filename,"%s_%s",GlobalVars->temp_filename,additional_temp); //!!!! When is additional_temp useful? Get rid of it? Global params?? !!!!
inputfile = fopen(filename,"rb");
if(!inputfile)
{
printf("\n[%i]: Error opening inputfile %s.\n",my_rank,filename);
return 2;
}
}
//Move data to final output
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(save_list[i],id_to_loc,N);
proc = assignments[loc];
current = sys[loc];
if(proc == my_rank)
{
//Find the link in the temp file inputfile
counter = 0; //index of link in my_save_list of its process
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
while(id != save_list[i] && !feof(inputfile))
{
jump_size = (long long int) total_spaces * (long long int) line_size; //Watch overflows!
fseek(inputfile,jump_size,SEEK_CUR);
counter++;
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
}
if(feof(inputfile))
{
printf("\n[%i]: Error: could not find id %u in temp file %s.\n",my_rank,save_list[i],filename);
return 2;
}
//Write id and number of steps
//if(my_rank == 0)
//fprintf(outputfile,"\n%u %u\n",save_list[i],current->disk_iterations);
//else
//if(!my_rank)
// MPI_Send(&(current->disk_iterations),1,MPI_UNSIGNED,0,save_list[i],MPI_COMM_WORLD);
//Read data in the temp file
if(my_rank == 0)
{
for(k=0;k<current->disk_iterations;k++)
{
for(m=0;m<dim;m++)
{
fread(data_storage,GlobalVars->output_sizes[m],1,inputfile);
fwrite(data_storage,GlobalVars->output_sizes[m],1,outputfile);
//WriteValue(outputfile,GlobalVars->output_specifiers[m],data_storage,GlobalVars->output_types[m]," ");
}
}
}
else
{
//printf("[%i] Sending iters about %i...\n",my_rank,save_list[i]);
MPI_Send(&(current->disk_iterations),1,MPI_UNSIGNED,0,save_list[i],MPI_COMM_WORLD);
//printf("[%i] Sent iters\n",my_rank);
for(k=0;k<current->disk_iterations;k++)
{
//printf("[%i] Sending data about %i (%i/%i)...\n",my_rank,save_list[i],k,current->disk_iterations);
for(m=0;m<dim;m++)
{
fread(data_storage,GlobalVars->output_sizes[m],1,inputfile);
//MPI_Send(&data_storage,size,MPI_CHAR,0,save_list[i],MPI_COMM_WORLD);
MPI_Send(data_storage,size,MPI_CHAR,0,save_list[i],MPI_COMM_WORLD); //!!!! Why do some sends/recvs have & and some don't? !!!!
}
//printf("[%i] Sent data (%i/%i)\n",my_rank,k,current->disk_iterations);
}
}
//Skip over the last unused space. This is done so that the file does not need to be rewound.
for(k=0;k<total_spaces-current->disk_iterations;k++)
for(j=0;j<dim;j++)
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
}
else if(my_rank == 0)
{
//printf("[%i] Recving iters from %i about %i...\n",my_rank,proc,save_list[i]);
//Write to file
MPI_Recv(&(current->disk_iterations),1,MPI_UNSIGNED,proc,save_list[i],MPI_COMM_WORLD,MPI_STATUS_IGNORE);
//fprintf(outputfile,"\n%u %u\n",save_list[i],current->disk_iterations);
//printf("[%i] Recved iters\n",my_rank);
for(k=0;k<current->disk_iterations;k++)
{
//printf("[%i] Recving data about %i (%i/%i)...\n",my_rank,save_list[i],k,current->disk_iterations);
for(m=0;m<dim;m++)
{
MPI_Recv(data_storage,size,MPI_CHAR,proc,save_list[i],MPI_COMM_WORLD,MPI_STATUS_IGNORE);
fwrite(data_storage,GlobalVars->output_sizes[m],1,outputfile);
//WriteValue(outputfile,GlobalVars->output_specifiers[m],data_storage,GlobalVars->output_types[m]," ");
}
//printf("[%i] Recved data about (%i/%i)\n",my_rank,k,current->disk_iterations);
}
}
}
//Cleanup
if(inputfile) fclose(inputfile);
if(outputfile) fclose(outputfile);
}
*/
if(my_rank == 0)
printf("\nResults written to file %s.\n",outputfilename);
MPI_Barrier(MPI_COMM_WORLD);
//Reopen the tempfile
if(my_tempfile && my_save_size > 0)
{
if(additional_temp != NULL)
sprintf(filename,"%s_%s",GlobalVars->temp_filename,additional_temp);
else
sprintf(filename,"%s",GlobalVars->temp_filename);
*my_tempfile = fopen(filename,"r+b");
if(!*my_tempfile)
printf("[%i]: Error reopening temp file %s.\n",my_rank,filename);
}
return 0;
}
void PrepareDatabaseTable(UnivVars* GlobalVars,ConnData* conninfo)
{
unsigned int num_cols,i;
PGresult* res;
char* query;
if(conninfo == NULL) return;
if(my_rank == 0)
{
ConnectPGDB(conninfo);
query = conninfo->query;
//Create table
if(conninfo->num_queries > 1)
{
sprintf(query,conninfo->queries[1],GlobalVars->hydro_table);
res = PQexec(conninfo->conn,query);
CheckResError(res,"deleting hydrograph table");
PQclear(res);
}
sprintf(query,conninfo->queries[0],GlobalVars->hydro_table);
res = PQexec(conninfo->conn,query);
CheckResError(res,"creating hydrograph table");
PQclear(res);
//Make sure table is consistent with outputs
sprintf(query,"SELECT data_type FROM information_schema.columns WHERE table_name='%s' ORDER BY ordinal_position;",GlobalVars->hydro_table);
res = PQexec(conninfo->conn,query);
CheckResError(res,"obtaining data types from hydrograph table");
num_cols = PQntuples(res);
if(num_cols > GlobalVars->num_print)
{
printf("[%i]: Error: need more outputs in .gbl file.\n",my_rank);
MPI_Abort(MPI_COMM_WORLD,1);
}
else if(num_cols < GlobalVars->num_print)
{
printf("[%i]: Error: need more columns in table %s. Got %i, expected %i.\n",my_rank,GlobalVars->hydro_table,num_cols,GlobalVars->num_print);
MPI_Abort(MPI_COMM_WORLD,1);
}
for(i=0;i<num_cols;i++)
{
if(GlobalVars->output_types[i] == ASYNCH_BAD_TYPE)
{
printf("[%i]: Error: output %i must be set to prepare database tables.\n",my_rank,i);
MPI_Abort(MPI_COMM_WORLD,1);
}
else if( strcmp(PQgetvalue(res,i,0),"double precision") == 0 )
{
if(GlobalVars->output_types[i] != ASYNCH_DOUBLE)
{
printf("[%i]: Error: Output %i is of type double precision in output table, but should not be. %hi\n",my_rank,i,GlobalVars->output_types[i]);
MPI_Abort(MPI_COMM_WORLD,1);
}
}
else if( strcmp(PQgetvalue(res,i,0),"integer") == 0 )
{
if(GlobalVars->output_types[i] != ASYNCH_INT)
{
printf("[%i]: Error: Output %i is of type integer in output table, but should not be. %hi\n",my_rank,i,GlobalVars->output_types[i]);
MPI_Abort(MPI_COMM_WORLD,1);
}
}
else if( strcmp(PQgetvalue(res,i,0),"single precision") == 0 )
{
if(GlobalVars->output_types[i] != ASYNCH_FLOAT)
{
printf("[%i]: Error: Output %i is of type single precision in output table, but should not be. %hi\n",my_rank,i,GlobalVars->output_types[i]);
MPI_Abort(MPI_COMM_WORLD,1);
}
}
else if( strcmp(PQgetvalue(res,i,0),"short integer") == 0 )
{
if(GlobalVars->output_types[i] != ASYNCH_SHORT)
{
printf("[%i]: Error: Output %i is of type short integer in output table, but should not be. %hi\n",my_rank,i,GlobalVars->output_types[i]);
MPI_Abort(MPI_COMM_WORLD,1);
}
}
else if( strcmp(PQgetvalue(res,i,0),"character") == 0 )
{
if(GlobalVars->output_types[i] != ASYNCH_CHAR)
{
printf("[%i]: Error: Output %i is of type character in output table, but should not be. %hi\n",my_rank,i,GlobalVars->output_types[i]);
MPI_Abort(MPI_COMM_WORLD,1);
}
}
else
{
printf("[%i]: Error: Bad datatype for output %i while preparing database tables. (%s)\n",my_rank,i,PQgetvalue(res,i,0));
MPI_Abort(MPI_COMM_WORLD,1);
}
}
PQclear(res);
DisconnectPGDB(conninfo);
}
}
//Assumes temp files.
//Return value = 0 means everything is good.
//1 means a database related error.
//2 means a file system related error.
int UploadHydrosDB(Link** sys,UnivVars* GlobalVars,unsigned int N,unsigned int* save_list,unsigned int save_size,unsigned int my_save_size,unsigned int** id_to_loc,int* assignments,char* additional_temp,char* additional_out,ConnData* conninfo,FILE** my_tempfile)
{
int i,k,nbytes,my_result = 0,result = 0,return_val = 0;
unsigned int j,l,m,loc,id,total_spaces,max_disk;
char filename[GlobalVars->string_size],filenamespace[GlobalVars->string_size],temptablename[GlobalVars->query_size],tempfilename[GlobalVars->string_size];
char* submission;
char data_storage[16];
fpos_t **positions;
unsigned int dim = GlobalVars->num_print;
FILE *inputfile;
PGresult *res;
Link* current;
long long int jump_size;
//Close the temp file, if open
if(my_tempfile && *my_tempfile)
{
fclose(*my_tempfile);
*my_tempfile = NULL;
}
//Assumes 24 chars max per double, plus delims, plus link id. So %.8e should work.
submission = (char*) malloc( (dim*24 + dim) * sizeof(char));
//Find total size of a line in the temp files
unsigned int line_size = CalcTotalOutputSize(GlobalVars);
//Open temporary file
if(!additional_temp)
sprintf(filename,"%s",GlobalVars->temp_filename);
else
sprintf(filename,"%s_%s",GlobalVars->temp_filename,additional_temp);
inputfile = fopen(filename,"rb");
if(my_save_size > 0) //!!!! This wasn't here before. But I think it should be... !!!!
{
if(!inputfile)
{
printf("\n[%i]: Error opening inputfile %s.\n",my_rank,filename);
my_result = 1;
}
else
my_result = 0;
}
//Check if an error occurred
MPI_Allreduce(&my_result,&result,1,MPI_INT,MPI_LOR,MPI_COMM_WORLD);
if(result)
{
return_val = 2;
goto finished; //Don't bother uploading anymore
}
if(my_rank == 0)
{
//sprintf(temptablename,"tmp_%s",GlobalVars->hydro_table);
sprintf(temptablename,"%s_tmp",GlobalVars->hydro_table);
return_val = 0;
ConnectPGDB(conninfo);
//Delete temporary table, if it exists
sprintf(conninfo->query,"DROP TABLE IF EXISTS %s;",temptablename);
res = PQexec(conninfo->conn,conninfo->query);
result = CheckResError(res,"dropping temporary output table");
PQclear(res);
//Create temporary table
sprintf(conninfo->query,conninfo->queries[0],temptablename);
res = PQexec(conninfo->conn,conninfo->query);
result = CheckResError(res,"creating temporary output table");
PQclear(res);
//Tell database to prepare for copying
sprintf(conninfo->query,"COPY %s FROM STDIN WITH DELIMITER ',';",temptablename);
res = PQexec(conninfo->conn,conninfo->query);
result = CheckResState(res,PGRES_COPY_IN);
PQclear(res);
}
//Check if an error occurred
MPI_Bcast(&result,1,MPI_INT,0,MPI_COMM_WORLD);
if(result)
{
return_val = 1;
goto finished;
}
//Upload data
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(save_list[i],id_to_loc,N);
current = sys[loc];
if(my_rank == 0)
{
//Find the link in the temp file inputfile
if(assignments[loc] == my_rank)
{
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
while(id != save_list[i] && !feof(inputfile))
{
jump_size = (long long int) total_spaces * (long long int) line_size; //Watch overflows!
fseek(inputfile,jump_size,SEEK_CUR);
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
}
if(feof(inputfile))
{
printf("\n[%i]: Error: could not find id %u in temp file %s.\n",my_rank,save_list[i],filename);
return_val = 2;
goto finished; //Don't bother uploading anymore
}
//Now read in the data in the temp file, and submit them to the database
for(k=0;k<current->disk_iterations;k++)
{
nbytes = 0;
for(j=0;j<dim-1;j++)
{
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
nbytes += CatBinaryToString(&(submission[nbytes]),GlobalVars->output_specifiers[j],data_storage,GlobalVars->output_types[j],",");
}
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
nbytes += CatBinaryToString(&(submission[nbytes]),GlobalVars->output_specifiers[j],data_storage,GlobalVars->output_types[j],"\n");
result = PQputCopyData(conninfo->conn,submission,nbytes);
}
//Skip over the last unused space. This is done so that the file does not need to be rewound.
for(k=0;k<total_spaces-current->disk_iterations;k++)
for(j=0;j<dim;j++)
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
}
else
{
//Get disk_iterations
MPI_Recv(&(current->disk_iterations),1,MPI_UNSIGNED,assignments[loc],loc,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
//Now read in the data in the temp file, and submit them to the database
for(k=0;k<current->disk_iterations;k++)
{
MPI_Recv(&nbytes,1,MPI_UNSIGNED,assignments[loc],loc,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
MPI_Recv(submission,nbytes,MPI_CHAR,assignments[loc],loc,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
result = PQputCopyData(conninfo->conn,submission,nbytes);
}
}
//Check if an error occurred.
MPI_Bcast(&result,1,MPI_INT,0,MPI_COMM_WORLD);
if(result != 1)
{
printf("Error: copy returned result %i.\n",result);
return_val = 1;
goto finished; //Don't bother uploading anymore
}
}
else
{
//Find the link in the temp file inputfile
if(assignments[loc] == my_rank)
{
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
while(id != save_list[i] && !feof(inputfile))
{
jump_size = (long long int) total_spaces * (long long int) line_size; //Watch overflows!
fseek(inputfile,jump_size,SEEK_CUR);
fread(&id,sizeof(unsigned int),1,inputfile);
fread(&total_spaces,sizeof(unsigned int),1,inputfile);
}
if(feof(inputfile))
{
printf("\n[%i]: Error: could not find id %u in temp file %s.\n",my_rank,save_list[i],filename);
return_val = 2;
goto finished; //Don't bother uploading anymore
}
//Send disk_iterations
MPI_Send(&(current->disk_iterations),1,MPI_UNSIGNED,0,loc,MPI_COMM_WORLD);
//Now read in the data in the temp file, and submit them to the database
for(k=0;k<current->disk_iterations;k++)
{
nbytes = 0;
for(j=0;j<dim-1;j++)
{
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
nbytes += CatBinaryToString(&(submission[nbytes]),GlobalVars->output_specifiers[j],data_storage,GlobalVars->output_types[j],",");
}
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
nbytes += CatBinaryToString(&(submission[nbytes]),GlobalVars->output_specifiers[j],data_storage,GlobalVars->output_types[j],"\n");
MPI_Send(&nbytes,1,MPI_UNSIGNED,0,loc,MPI_COMM_WORLD);
MPI_Send(submission,nbytes,MPI_CHAR,0,loc,MPI_COMM_WORLD);
}
//Skip over the last unused space. This is done so that the file does not need to be rewound.
for(k=0;k<total_spaces-current->disk_iterations;k++)
for(j=0;j<dim;j++)
fread(data_storage,GlobalVars->output_sizes[j],1,inputfile);
}
//Check if an error occurred
MPI_Bcast(&result,1,MPI_INT,0,MPI_COMM_WORLD);
if(result != 1)
{
return_val = 1;
goto finished; //Don't bother uploading anymore
}
}
}
finished:
if(my_rank == 0)
{
//Tell database everything is uploaded