forked from lanl/MultiMatTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiMatTest.cpp
2233 lines (1961 loc) · 93.7 KB
/
MultiMatTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017, Los Alamos National Security, LLC.
* All rights Reserved.
*
* This is the code released under LANL Copyright Disclosure C17041/LA-CC-17-041
* Copyright 2017. Los Alamos National Security, LLC. This material was produced
* under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National
* Laboratory (LANL), which is operated by Los Alamos National Security, LLC
* for the U.S. Department of Energy. See LICENSE file for details.
*
* Released under the New BSD License
*
* Bob Robey [email protected] and Rao Garimella [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "input.h"
#include "genmalloc.h"
#include "timer.h"
#define CLOCK_RATE 2.7e9 // GHz laptop
#define STREAM_RATE 13375; // MB/sec
#define MININT(i,j) (((i) < (j)) ? (i) : (j))
void get_neighbors(int ncells, int *&num_nbrs, int **&nbrs);
void get_centroids(double (*&cen)[2]);
void get_vol_frac_matrix_rand(double **&Volfrac, float &filled_percentage);
void get_vol_frac_matrix_file(double **&Volfrac, float& filled_percentage);
void get_vol_frac_matrix(int method, double **&Volfrac, float& filled_percentage);
void setup_cell_dominant_data_structure(int method, double *&Vol, double *&Density, double *&Temperature,
double *&Pressure, double **&Volfrac, double **&Densityfrac, double **&Temperaturefrac,
double **&Pressurefrac, float &filled_percentage);
void setup_material_dominant_data_structure(int method, double *&Vol, double *&Density, double *&Temperature,
double *&Pressure, double **&Volfrac, double **&Densityfrac, double **&Temperaturefrac,
double **&Pressurefrac, float &filled_percentage);
void setup_cell_dominant_compact_data_structure(int method, int *&imaterial, int *&nmaterials, double *&Vol,
double *&Density, double *&Temperature, double *&Pressure, int *&imaterialfrac, int *&nextfrac,
int *&frac2cell, double *&Volfrac, double *&Densityfrac, double *&Temperaturefrac,
double *&Pressurefrac, float &filled_percentage);
void setup_mat_dominant_compact_data_structure(int method,
int **&subset2mesh,
int **&mesh2subset,
int *&nmatscell,
int *&matids,
int *&ncellsmat,
double *&Vol,
double *&Density,
double **&Volfrac,
double **&Densityfrac,
double **&Temperaturefrac,
double **&Pressurefrac,
float &filled_percentage);
void convert_compact_material_2_compact_cell(int ncells,
int **subset2mesh,
int **mesh2subset,
int *nmatscell,
int *matids,
int *ncellsmat,
double **Volfrac,
double **Densityfrac,
double **Temperaturefrac,
double **Pressurefrac,
int *&Cimaterial,
int *&Cnmaterials,
int *&Cimaterialfrac,
int *&Cnextfrac,
int *&Cfrac2cell,
double *&CVolfrac,
double *&CDensityfrac,
double *&CTemperaturefrac,
double *&CPressurefrac);
void convert_compact_cell_2_compact_material(int ncells, int nmats,
int *Cimaterial,
int *Cnmaterials,
int *Cimaterialfrac,
double *CVol,
double *CDensity,
double *CTemperature,
double *CVolfrac,
double *CDensityfrac,
double *CTemperaturefrac,
int **&subset2mesh,
int **&mesh2subset,
int *&nmatscell,
int *&matids,
int *&ncellsmat,
double **&Volfrac,
double **&Densityfrac,
double **&Temperaturefrac,
double **&Pressurefrac);
void print_performance_estimates(float est_perf, int64_t memops8byte,
int64_t memops4byte, int64_t flops,
float penalty_msecs);
bool verbose = false;
bool memory_verbose = true;
int itermax = 100;
int ncells = 1000000;
int nmats = 50;
float est_perf, act_perf, model_error;
int main(int argc, char **argv) {
struct timeval tstart_cpu;
double density_ave;
double time_sum;
double VolTotal;
double *Density_average;
double nmatconst = 5.0;
double nmatconsts[nmats];
for (int i = 0; i < nmats; i++){
nmatconsts[i] = 5.0;
}
int64_t memops, memops4byte, memops8byte;
int64_t flops;
float penalty_msecs;
float filled_percentage, filled_fraction;
int method = 0; // VF initialization: 0 - random, 1 - read volfrac.dat
if (argc > 1)
sscanf(argv[1],"%d",&method);
printf("Run stream benchmark for your system\n");
printf("L3 Cache on Macbook Pro is 6MB so problem size is just bigger at 16MB min\n");
printf("First test should give Stream Benchmark or problem size is too small\n");
printf("Second problem should give about twice the first\n");
// Some globals
float L_f = method ? 0.5 : 1.0; // ave frac of nbrs containing material
int nnbrs_ave = 8; // nearly so; 4000 boundary cells in 1 million cells
// // in 3D, nnbrs_ave would be 26
// Build up list of neighbors for each cell
// Assuming a 2D structured mesh, each cell will have a maximum of 8 nbrs
int *nnbrs = (int *) genvector("Num_Neighbors", ncells, sizeof(int));
int **nbrs = (int **) genmatrix("Neighbors", ncells, nmats, sizeof(int));
get_neighbors(ncells, nnbrs, nbrs);
// Compute centroids of cells
double (*cen)[2] = (double (*)[2]) genvector("Centroids", ncells,
sizeof(double[2]));
get_centroids(cen);
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Starting Single Material Data Structure
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("=======================================\n");
printf("Starting Single Material Data Structure\n");
printf("=======================================\n\n");
double *Density = (double *)genvector("Density", ncells, sizeof(double));
double *Temperature = (double *)genvector("Temperature", ncells, sizeof(double));
double *Pressure = (double *)genvector("Pressure", ncells, sizeof(double));
double *Vol = (double *)genvector("Volume", ncells, sizeof(double));
VolTotal = 0.0;
for (int ic = 0; ic < ncells; ic++){
Density[ic] = 2.0;
Temperature[ic] = 0.5;
Vol[ic] = 1.0;
VolTotal += Vol[ic];
}
if (memory_verbose){
genmalloc_MB_memory_report();
}
genmalloc_MB_memory_total();
printf("\n");
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with cell densities (pure cells)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
density_ave = 0.0;
for (int ic = 0; ic < ncells; ic++){
density_ave += Density[ic]*Vol[ic];
}
density_ave /= VolTotal;
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of pure cells %lf, compute time is %lf msecs\n",density_ave,act_perf);
memops = 2*ncells;
flops = 2*ncells;
penalty_msecs = 0.0;
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculate pressure using ideal gas law
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
Pressure[ic] = (nmatconst*Density[ic]*Temperature[ic])/Vol[ic];
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Pressure Calculation for cell, compute time is %lf msecs\n",act_perf);
memops = 4*ncells;
flops = 3*ncells;
penalty_msecs = 0.0;
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
genvectorfree(Vol);
genvectorfree(Density);
genvectorfree(Temperature);
genvectorfree(Pressure);
}
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Starting Cell-Dominant Full Matrix Data Structure
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("\n");
printf("=================================================\n");
printf("Starting Cell-Dominant Full Matrix Data Structure\n");
printf("=================================================\n\n");
double *Vol, *Density, *Temperature, *Pressure;
double **Densityfrac, **Temperaturefrac, **Pressurefrac, **Volfrac;
setup_cell_dominant_data_structure(method, Vol, Density, Temperature, Pressure,
Volfrac, Densityfrac, Temperaturefrac, Pressurefrac, filled_percentage);
filled_fraction = filled_percentage/100.0;
if (memory_verbose){
genmalloc_MB_memory_report();
}
genmalloc_MB_memory_total();
printf("\n");
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with fractional densities
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Density_average = (double *)genvector("Density_average", ncells, sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
density_ave = 0.0;
for (int m = 0; m < nmats; m++){
density_ave += Densityfrac[ic][m]*Volfrac[ic][m];
}
Density_average[ic] = density_ave/Vol[ic];
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of mixed material cells compute time is %lf msecs\n",act_perf);
genvectorfree(Density_average);
memops = 2*ncells*nmats; // line 4 loads
memops += 2*ncells; // line 6 stores
flops = 2*ncells*nmats; // line 4 flops
penalty_msecs = 0.0;
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with fractional densities with if test
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Density_average = (double *)genvector("Density_average", ncells, sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
density_ave = 0.0;
for (int m = 0; m < nmats; m++){
if (Volfrac[ic][m] > 0.0) {
density_ave += Densityfrac[ic][m]*Volfrac[ic][m];
}
}
Density_average[ic] = density_ave/Vol[ic];
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of frac with if compute time is %lf msecs\n",act_perf);
genvectorfree(Density_average);
float cache_miss_freq = method ? 0.7 : 1.0;
memops = ncells*nmats; // line 4 loads
memops += (int64_t)(filled_fraction*(float)(ncells*nmats)); // line 5 loads
memops += 2*ncells; // line 8 stores and loads
flops = (int64_t)(filled_fraction*(float)(2*ncells*nmats)); // line 5 flops
flops += ncells; // line 8 flops
float branch_wait = 1.0/CLOCK_RATE * 16; // Estimate a 16 cycle wait for branch misprediction
// for a 2.7 GHz processor
float cache_wait = 1.0/CLOCK_RATE * 7*16; // Estimate a 7*16 or 112 cycle wait for missing prefetch
// for a 2.7 GHz processor
penalty_msecs = 1000.0*cache_miss_freq*(branch_wait+cache_wait)*
(filled_fraction*(float)(ncells*nmats)); // line 4 if
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculate pressure using ideal gas law
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
for (int m = 0; m < nmats; m++){
if (Volfrac[ic][m] > 0.){
Pressurefrac[ic][m] = (nmatconsts[m]*Densityfrac[ic][m]*Temperaturefrac[ic][m])/(Volfrac[ic][m]);
} else {
Pressurefrac[ic][m] = 0.0;
}
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Pressure Calculation of mixed material cells with if compute time is %lf msecs\n",act_perf);
float sparsity_fraction = 1.0-filled_fraction;
memops = ncells*nmats; // line 3 loads
memops += (int64_t)(filled_fraction*(float)(ncells*nmats)); // line 5 stores
memops += (int64_t)(filled_fraction*(float)(3*ncells*nmats)); // line 6 loads
memops += (int64_t)(sparsity_fraction*(float)(ncells*nmats)); // line 8 stores
flops = (int64_t)(filled_fraction*(float)(3*ncells*nmats)); // line 6 flops
branch_wait = 1.0/CLOCK_RATE * 16; // Estimate a 16 cycle wait for branch misprediction
// for a 2.7 GHz processor
cache_wait = 1.0/CLOCK_RATE * 7*16; // Estimate a 7*16 or 112 cycle wait for missing prefetch
// for a 2.7 GHz processor
penalty_msecs = 1000.0*cache_miss_freq*(branch_wait+cache_wait)*
(filled_fraction*(float)(ncells*nmats)); // line 3 if
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average material density over neighborhood of each cell
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
double **MatDensity_average =
(double **)genmatrix("MatDensity_average", ncells, nmats,
sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
double xc[2];
xc[0] = cen[ic][0]; xc[1] = cen[ic][1];
int nn = nnbrs[ic];
int cnbrs[8];
double dsqr[8];
for (int n = 0; n < nn; n++) cnbrs[n] = nbrs[ic][n];
for (int n = 0; n < nn; n++) {
dsqr[n] = 0.0;
for (int d = 0; d < 1; d++) {
double ddist = (xc[d] - cen[cnbrs[n]][d]);
dsqr[n] += ddist*ddist;
}
}
for (int m = 0; m < nmats; m++){
if (Volfrac[ic][m] > 0.0) {
int nnm = 0; // number of nbrs with this material
for (int n = 0; n < nn; n++){
int jc = cnbrs[n];
if (Volfrac[jc][m] > 0.0) {
MatDensity_average[ic][m] += Densityfrac[ic][m]/dsqr[n];
nnm++;
}
}
MatDensity_average[ic][m] /= nnm;
}
else {
MatDensity_average[ic][m] = 0.0;
}
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Material Density compute time is %lf msecs\n",act_perf);
genmatrixfree((void **)MatDensity_average);
memops = (2 + 2*nmats + (0.5 + 16)*nnbrs_ave)*ncells;
// // Formula differs from paper because it is 2D here
memops += (int64_t)(filled_fraction*8*(1+L_f)*ncells*nmats*nnbrs_ave);
flops = 6*ncells*nnbrs_ave;
flops += (int64_t)(filled_fraction*3*ncells*nmats*nnbrs_ave*L_f);
branch_wait = 1.0/CLOCK_RATE * 16; // Estimate a 16 cycle wait for branch misprediction
// for a 2.7 GHz processor
cache_wait = 1.0/CLOCK_RATE * 7*16; // Estimate a 7*16 or 112 cycle wait for missing prefetch
// for a 2.7 GHz processor
penalty_msecs = 1000.0*cache_miss_freq*(branch_wait+cache_wait)*
(filled_fraction*(float)(ncells*nmats));
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
genvectorfree((void *)Vol);
genvectorfree((void *)Density);
genvectorfree((void *)Temperature);
genvectorfree((void *)Pressure);
genmatrixfree((void **)Volfrac);
genmatrixfree((void **)Densityfrac);
genmatrixfree((void **)Temperaturefrac);
genmatrixfree((void **)Pressurefrac);
}
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Starting Material-Dominant Full Matrix Data Structure
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("\n");
printf("===================================================\n");
printf("Starting Material-Dominant Full Matrix Data Structure\n");
printf("===================================================\n");
double *Vol, *Density, *Temperature, *Pressure;
double **Volfrac, **Densityfrac, **Temperaturefrac, **Pressurefrac;
setup_material_dominant_data_structure(method, Vol, Density, Temperature, Pressure,
Volfrac, Densityfrac, Temperaturefrac, Pressurefrac, filled_percentage);
filled_fraction = filled_percentage/100.0;
if (memory_verbose){
genmalloc_MB_memory_report();
}
genmalloc_MB_memory_total();
printf("\n");
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with fractional densities
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Density_average = (double *)genvector("Density_average", ncells, sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
Density_average[ic] = 0.0;
}
for (int m = 0; m < nmats; m++){
for (int ic = 0; ic < ncells; ic++){
Density_average[ic] += Densityfrac[m][ic]*Volfrac[m][ic];
}
}
for (int ic = 0; ic < ncells; ic++){
Density_average[ic] /= Vol[ic];
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of mixed material cells compute time is %lf msecs\n",act_perf);
genvectorfree(Density_average);
memops = ncells; // line 3 loads
memops += ncells*nmats; // line 6 stores
memops += 2*ncells*nmats; // line 7 loads
flops = 2*ncells*nmats; // line 7 flops
memops += 2*ncells; // line 11 loads/stores
flops += ncells; // line 11 flops
penalty_msecs = 0.0;
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with fractional densities with if test
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Density_average = (double *)genvector("Density_average", ncells, sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
Density[ic] = 0.0;
}
for (int m = 0; m < nmats; m++){
for (int ic = 0; ic < ncells; ic++){
if (Volfrac[m][ic] > 0.0) {
Density[ic] += Densityfrac[m][ic]*Volfrac[m][ic];
}
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of mixed material cells with if compute time is %lf msecs\n",act_perf);
genvectorfree(Density_average);
float cache_miss_freq = method ? 0.2 : 1.0;
memops = ncells; // line 2 loads
memops += ncells*nmats; // line 6 loads
memops += (int64_t)(filled_fraction*(float)(ncells*nmats)); // line 7 stores
memops += (int64_t)(filled_fraction*(float)(ncells*nmats)); // line 8 loads
flops = (int64_t)(filled_fraction*(float)(2*ncells*nmats)); // line 8 flops
memops += 2*ncells; // line 11 stores and loads
flops += ncells; // line 11 flops
float branch_wait = 1.0/CLOCK_RATE * 16; // Estimate a 16 cycle wait for branch misprediction
// for a 2.7 GHz processor
float cache_wait = 1.0/CLOCK_RATE * 7*16; // Estimate a 7*16 or 112 cycle wait for missing prefetch
// for a 2.7 GHz processor
penalty_msecs = 1000.0*cache_miss_freq*(branch_wait+cache_wait)*
(filled_fraction*(float)(ncells*nmats)); // line 6 if
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculate pressure using ideal gas law
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int m = 0; m < nmats; m++){
for (int ic = 0; ic < ncells; ic++){
if (Volfrac[m][ic] > 0.0) {
Pressurefrac[m][ic] = (nmatconsts[m]*Densityfrac[m][ic]*Temperaturefrac[m][ic])/Volfrac[m][ic];
} else {
Pressurefrac[m][ic] = 0.0;
}
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Pressure Calculation of frac with if compute time is %lf msecs\n",act_perf);
float sparsity_fraction = 1.0-filled_fraction;
memops = nmats; // line 2 loads
memops += ncells*nmats; // line 4 loads
memops += (int64_t)(filled_fraction*(float)(ncells*nmats)); // line 5 stores
memops += (int64_t)(filled_fraction*(float)(2*ncells*nmats)); // line 6 loads
flops = (int64_t)(filled_fraction*(float)(3*ncells*nmats)); // line 6 flops
memops += (int64_t)(sparsity_fraction*(float)(ncells*nmats)); // line 8 stores
branch_wait = 1.0/CLOCK_RATE * 16; // Estimate a 16 cycle wait for branch misprediction
// for a 2.7 GHz processor
cache_wait = 1.0/CLOCK_RATE * 7*16; // Estimate a 7*16 or 112 cycle wait for missing prefetch
// for a 2.7 GHz processor
penalty_msecs = 1000.0*cache_miss_freq*(branch_wait+cache_wait)*
(filled_fraction*(float)(ncells*nmats)); // line 6 if
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average material density over neighborhood of each cell
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
double **MatDensity_average =
(double **)genmatrix("MatDensity_average", nmats, ncells,
sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int m = 0; m < nmats; m++) {
for (int ic = 0; ic < ncells; ic++){
if (Volfrac[m][ic] > 0.0) {
double xc[2];
xc[0] = cen[ic][0]; xc[1] = cen[ic][1];
int nn = nnbrs[ic];
int cnbrs[8];
double dsqr[8];
for (int n = 0; n < nn; n++) cnbrs[n] = nbrs[ic][n];
for (int n = 0; n < nn; n++) {
dsqr[n] = 0.0;
for (int d = 0; d < 1; d++) {
double ddist = (xc[d] - cen[cnbrs[n]][d]);
dsqr[n] += ddist*ddist;
}
}
int nnm = 0; // number of nbrs with this material
for (int n = 0; n < nn; n++){
int jc = cnbrs[n];
if (Volfrac[m][jc] > 0.0) {
MatDensity_average[m][ic] += Densityfrac[m][ic]/dsqr[n];
nnm++;
}
}
MatDensity_average[m][ic] /= nnm;
}
else {
MatDensity_average[m][ic] = 0.0;
}
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Material Density compute time is %lf msecs\n",act_perf);
genmatrixfree((void **)MatDensity_average);
memops = 2*ncells*nmats;
// // Formula differs from paper because it is 2D here
memops += (int64_t)(2*filled_fraction*ncells*nmats);
memops += (int64_t)(8.5*filled_fraction*ncells*nmats*nnbrs_ave);
memops += (int64_t)(24*filled_fraction*L_f*ncells*nmats*nnbrs_ave);
flops = (int64_t)(filled_fraction*ncells*nmats);
flops += (int64_t)(9*filled_fraction*ncells*nmats*nnbrs_ave*L_f);
branch_wait = 1.0/CLOCK_RATE * 16; // Estimate a 16 cycle wait for branch misprediction
// for a 2.7 GHz processor
cache_wait = 1.0/CLOCK_RATE * 7*16; // Estimate a 7*16 or 112 cycle wait for missing prefetch
// for a 2.7 GHz processor
penalty_msecs = 1000.0*cache_miss_freq*(branch_wait+cache_wait)*
(filled_fraction*ncells*nmats);
print_performance_estimates(act_perf, memops, 0, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
genvectorfree((void *)Vol);
genvectorfree((void *)Density);
genvectorfree((void *)Temperature);
genvectorfree((void *)Pressure);
genmatrixfree((void **)Volfrac);
genmatrixfree((void **)Densityfrac);
genmatrixfree((void **)Temperaturefrac);
genmatrixfree((void **)Pressurefrac);
}
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Starting Cell-Dominant Compact Data Structure
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("\n");
printf("===================================================\n");
printf("Starting Cell-Dominant Compact Data Structure\n");
printf("===================================================\n");
int *imaterial, *nmaterials, *imaterialfrac, *nextfrac, *frac2cell;
double *Vol, *Density, *Temperature, *Pressure, *Volfrac, *Densityfrac, *Temperaturefrac, *Pressurefrac;
setup_cell_dominant_compact_data_structure(method, imaterial, nmaterials, Vol, Density, Temperature, Pressure,
imaterialfrac, nextfrac, frac2cell, Volfrac, Densityfrac, Temperaturefrac, Pressurefrac,
filled_percentage);
filled_fraction = filled_percentage/100.0;
int nmixlength = 0;
int pure_cell_count = 0;
int mixed_cell_count = 0;
for (int ic = 0; ic < ncells; ic++) {
int ix = imaterial[ic];
if (ix <= 0) {
for (ix = -ix; ix >= 0; ix = nextfrac[ix])
nmixlength++;
mixed_cell_count++;
} else {
pure_cell_count++;
}
}
float mixed_cell_fraction = mixed_cell_count/ncells;
float pure_cell_fraction = pure_cell_count/ncells;
int nmats_ave = (pure_cell_count + nmixlength)/ncells;
if (memory_verbose){
genmalloc_MB_memory_report();
}
genmalloc_MB_memory_total();
printf("\n");
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with fractional densities
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
density_ave = 0.0;
int ix = imaterial[ic];
if (ix <= 0 ) { // material numbers for clean cells start at 1
for (ix = -ix; ix >=0; ix = nextfrac[ix]){
density_ave += Densityfrac[ix]*Volfrac[ix];
}
Density[ic] = density_ave/Vol[ic];
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of mixed material cells compute time is %lf msecs\n",act_perf);
float cache_miss_freq = method ? 0.1 : 1.0;
memops4byte = ncells; // line 3 loads
memops4byte += nmixlength; // line 5 loads
memops8byte = 2*nmixlength; // line 6 loads
flops = 2*nmixlength; // line 6 flops
memops8byte += mixed_cell_fraction*ncells; // line 8 stores
memops8byte += mixed_cell_fraction*ncells; // line 8 loads
flops += mixed_cell_fraction*ncells; // line 8 flops
float loop_overhead = 1.0/CLOCK_RATE * 20; // Estimate a 20 cycle loop exit overhead
penalty_msecs = 1000.0*cache_miss_freq*loop_overhead*mixed_cell_fraction*(float)ncells; // line 5 for
print_performance_estimates(act_perf, memops8byte, memops4byte, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with fractional densities using nmats
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
density_ave = 0.0;
int mstart = imaterial[ic];
if (mstart <= 0 ) { // material numbers for clean cells start at 1
mstart = -mstart;
for (int ix = 0;ix < nmaterials[ic]; ix++){
density_ave += Densityfrac[mstart+ix]*Volfrac[mstart+ix];
}
Density[ic] = density_ave/Vol[ic];
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of mixed material cells with nmats compute time is %lf msecs\n",act_perf);
memops4byte = ncells; // line 3 loads
memops4byte += mixed_cell_fraction*ncells; // line 5 loads
memops8byte = 2*nmixlength; // line 6 loads
flops = 2*nmixlength; // line 6 flops
memops8byte += mixed_cell_fraction*ncells; // line 8 stores
memops8byte += mixed_cell_fraction*ncells; // line 8 loads
flops += mixed_cell_fraction*ncells; // line 8 flops
loop_overhead = 1.0/CLOCK_RATE * 20; // Estimate a 20 cycle loop exit overhead
penalty_msecs = 1000.0*cache_miss_freq*loop_overhead*mixed_cell_fraction*(float)ncells; // line 5 for
print_performance_estimates(act_perf, memops8byte, memops4byte, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with fractional densities
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Density_average = (double *)genvector("Density_average", ncells, sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
density_ave = 0.0;
int ix = imaterial[ic];
if (ix <= 0 ) { // material numbers for clean cells start at 1
for (ix = -ix; ix >=0; ix = nextfrac[ix]){
density_ave += Densityfrac[ix]*Volfrac[ix];
}
Density_average[ic] = density_ave/Vol[ic];
} else {
Density_average[ic] = Density[ic];
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of mixed material cells compute time is %lf msecs\n",act_perf);
genvectorfree(Density_average);
memops4byte = ncells; // line 3 loads
memops4byte += nmixlength; // line 5 loads
memops8byte = 2*nmixlength; // line 6 loads
flops = 2*nmixlength; // line 6 flops
memops8byte += mixed_cell_fraction*ncells; // line 8 stores
memops8byte += mixed_cell_fraction*ncells; // line 8 loads
flops += mixed_cell_fraction*ncells; // line 8 flops
memops8byte += pure_cell_fraction*ncells; // line 10 stores
memops8byte += pure_cell_fraction*ncells; // line 10 loads
loop_overhead = 1.0/CLOCK_RATE * 20; // Estimate a 20 cycle loop exit overhead
penalty_msecs = 1000.0*cache_miss_freq*loop_overhead*mixed_cell_fraction*(float)ncells; // line 5 for
print_performance_estimates(act_perf, memops8byte, memops4byte, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average density with fractional densities with pure calculation filler
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Density_average = (double *)genvector("Density_average", ncells, sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
int ix = imaterial[ic];
if (ix <= 0 ) { // material numbers for clean cells start at 1
density_ave = 0.0;
for (ix = -ix; ix >=0; ix = nextfrac[ix]){
density_ave += Densityfrac[ix]*Volfrac[ix];
}
} else { // Pure cell
density_ave = Density[ic]*Vol[ic];
}
Density_average[ic] = density_ave/Vol[ic];
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Density of mixed materials cells pure filler compute time is %lf msecs\n",act_perf);
genvectorfree(Density_average);
memops4byte = ncells; // line 3 loads
memops4byte += nmixlength; // line 5 loads
memops8byte = 2*nmixlength; // line 6 loads
flops = 2*nmixlength; // line 6 flops
memops8byte += (int64_t) mixed_cell_fraction*ncells; // line 8 stores
memops8byte += (int64_t) mixed_cell_fraction*ncells; // line 8 loads
flops += (int64_t) mixed_cell_fraction*ncells; // line 8 flops
memops8byte += (int64_t) pure_cell_fraction*ncells; // line 10 stores
memops8byte += (int64_t) 2*pure_cell_fraction*ncells; // line 10 loads
flops += (int64_t) pure_cell_fraction*ncells; // line 8 flops
loop_overhead = 1.0/CLOCK_RATE * 20; // Estimate a 20 cycle loop exit overhead
penalty_msecs = 1000.0*cache_miss_freq*loop_overhead*mixed_cell_fraction*(float)ncells; // line 5 for
print_performance_estimates(act_perf, memops8byte, memops4byte, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculate pressure using ideal gas law
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++){
int ix = imaterial[ic];
if (ix <= 0) { // material numbers for clean cells start at 1
for (ix = -ix; ix >=0; ix = nextfrac[ix]){
int m = imaterialfrac[ix];
Pressurefrac[ix] = (nmatconsts[m]*Densityfrac[ix]*Temperaturefrac[ix])/Volfrac[ix];
}
} else {
Pressure[ic] = nmatconsts[ix]*Density[ic]*Temperature[ic]/Vol[ic];
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Pressure Calculation of mixed material cells compute time is %lf msecs\n",act_perf);
memops4byte = ncells; // line 2 loads
memops4byte += nmixlength; // line 4 loads
memops4byte += nmixlength; // line 5 loads
memops8byte = 3*nmixlength; // line 6 loads
memops8byte += nmixlength; // line 6 stores
flops = 3*nmixlength; // line 6 flops
memops8byte += (int64_t) pure_cell_fraction*ncells; // line 9 stores
memops8byte += (int64_t) 4*pure_cell_fraction*ncells; // line 9 loads
flops += (int64_t) 3*pure_cell_fraction*ncells; // line 9 flops
loop_overhead = 1.0/CLOCK_RATE * 20; // Estimate a 20 cycle loop exit overhead
penalty_msecs = 1000.0*cache_miss_freq*loop_overhead*mixed_cell_fraction*(float)ncells; // line 5 for
print_performance_estimates(act_perf, memops8byte, memops4byte, flops, penalty_msecs);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Average material density over neighborhood of each cell
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
double **MatDensity_average =
(double **)genmatrix("MatDensity_average", ncells, nmats,
sizeof(double));
time_sum = 0;
for (int iter=0; iter< itermax; iter++){
cpu_timer_start(&tstart_cpu);
for (int ic = 0; ic < ncells; ic++) {
for (int m = 0; m < nmats; m++)
MatDensity_average[ic][m] = 0.0;
double xc[2];
xc[0] = cen[ic][0]; xc[1] = cen[ic][1];
int nn = nnbrs[ic];
int cnbrs[8];
double dsqr[8];
for (int n = 0; n < nn; n++) cnbrs[n] = nbrs[ic][n];
for (int n = 0; n < nn; n++) {
dsqr[n] = 0.0;
for (int d = 0; d < 1; d++) {
double ddist = (xc[d] - cen[cnbrs[n]][d]);
dsqr[n] += ddist*ddist;
}
}
int ix = imaterial[ic];
if (ix <= 0) {
for (ix = -ix; ix >=0; ix = nextfrac[ix]) {
int m = imaterialfrac[ix];
int nnm = 0; // number of nbrs with this material
for (int n = 0; n < nn; n++){
int jc = cnbrs[n];
int jx = imaterial[jc];
if (jx <= 0) {
for (jx = -jx; jx >= 0; jx = nextfrac[jx]) {
if (imaterialfrac[jx] == m) {
MatDensity_average[ic][m] += Densityfrac[jx]/dsqr[n];
nnm++;
break;
}
}
} else {
if (imaterialfrac[jx] == m) {
MatDensity_average[ic][m] += Densityfrac[jx]/dsqr[n];
nnm++;
}
}
}
MatDensity_average[ic][m] /= nnm;
}
} else {
int m = imaterialfrac[ix];
int nnm = 0; // number of nbrs with this material
for (int n = 0; n < nn; n++){
int jc = cnbrs[n];
int jx = imaterial[jc];
if (jx <= 0) {
for (jx = -jx; jx >= 0; jx = nextfrac[jx]) {
if (imaterialfrac[jx] == m) {
MatDensity_average[ic][m] += Densityfrac[jx]/dsqr[n];
nnm++;
break;
}
}
} else {
if (imaterialfrac[jx] == m) {
MatDensity_average[ic][m] += Densityfrac[jx]/dsqr[n];
nnm++;
}
}
}
MatDensity_average[ic][m] /= nnm;
}
}
time_sum += cpu_timer_stop(tstart_cpu);
}
act_perf = time_sum*1000.0/itermax;
printf("Average Material Density compute time is %lf msecs\n",act_perf);
genmatrixfree((void **)MatDensity_average);
filled_fraction = filled_percentage/100.0;
// Formula differs a bit from paper because it is 2D here
memops = (int64_t)(2.5*ncells*(1 + nnbrs_ave) + 0.5*nmixlength);
memops += (int64_t)(ncells*nmats*(1 + 1.5*filled_fraction));
memops += (int64_t)(4*filled_fraction*ncells*nmats*nnbrs_ave);
memops += (int64_t)(8*filled_fraction*ncells*nmats*nnbrs_ave*nmats_ave);
memops += (int64_t)(8*filled_fraction*ncells*nmats*nnbrs_ave*L_f);
flops = 6*ncells*nnbrs_ave;
flops += (int64_t)(3*filled_fraction*ncells*nmats*nnbrs_ave*L_f);
flops += (int64_t)(filled_fraction*ncells*nmats);
float branch_wait = 1.0/CLOCK_RATE * 16; // Estimate a 16 cycle wait for branch misprediction
// for a 2.7 GHz processor
float cache_wait = 1.0/CLOCK_RATE * 7*16; // Estimate a 7*16 or 112 cycle wait for missing prefetch
// for a 2.7 GHz processor