forked from UoB-HPC/advanced-hpc-lbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d2q9-bgk.c
1449 lines (1258 loc) · 65.7 KB
/
d2q9-bgk.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
/*
================================================================
**************** HPC cw by Luke Zhang (az16408) ****************
================================================================
** Code to implement a d2q9-bgk lattice boltzmann scheme.
** 'd2' inidates a 2-dimensional grid, and
** 'q9' indicates 9 velocities per grid cell.
** 'bgk' refers to the Bhatnagar-Gross-Krook collision step.
**
** The 'speeds' in each cell are numbered as follows:
**
** 6 2 5
** \|/
** 3-0-1
** /|\
** 7 4 8
**
** A 2D grid:
**
** cols
** --- --- ---
** | D | E | F |
** rows --- --- ---
** | A | B | C |
** --- --- ---
**
** 'unwrapped' in row major order to give a 1D array:
**
** --- --- --- --- --- ---
** | A | B | C | D | E | F |
** --- --- --- --- --- ---
**
** Grid indicies are:
**
** ny
** ^ cols(ii)
** | ----- ----- -----
** | | ... | ... | etc |
** | ----- ----- -----
** rows(jj) | | 1,0 | 1,1 | 1,2 |
** | ----- ----- -----
** | | 0,0 | 0,1 | 0,2 |
** | ----- ----- -----
** ----------------------> nx
**
** Note the names of the input parameter and obstacle files
** are passed on the command line, e.g.:
**
** ./d2q9-bgk input.params obstacles.dat
**
** Be sure to adjust the grid dimensions in the parameter file
** if you choose a different obstacle file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <string.h>
// #include <omp.h>
#include <mpi.h>
#define NSPEEDS 9
#define FINALSTATEFILE "final_state.dat"
#define AVVELSFILE "av_vels.dat"
// #define DEBUG
#define MASTER 0 /* master rank */
/* struct to hold the parameter values */
typedef struct
{
int nx; /* no. of cells in x-direction */
int ny; /* no. of cells in y-direction */
int maxIters; /* no. of iterations */
int reynolds_dim; /* dimension for Reynolds number */
float density; /* density per link */
float accel; /* density redistribution */
float omega; /* relaxation parameter */
int tot_cells; /* total number of cells that have obstacles==0*/
/* mpi parameters */
int kk; /* index for looping over ranks */
int size; /* number of processes in the communicator */
int rank; /* the rank of this process */
int left; /* the rank of the process to the left */
int right; /* the rank of the process to the right */
// int tag=0; /* scope for adding extra information to a message */
// MPI_Status status=MPI_STATUS_IGNORE; /* struct used by MPI_Recv */
int local_nrows; /* number of rows apportioned to this rank */
int local_ncols; /* number of columns apportioned to this rank(excluding the extra two halo columns) */
int start_col; /* determine the index of the starting column in obstacles&collated cells for this rank */
int end_col; /* determine the index of the end column in obstacles&collated cells for this rank */
} t_param;
/* struct to hold the 'speed' values */
// Array of Structure
// typedef struct
// {
// float speeds[NSPEEDS];
// } t_speed;
// Structure of Array/Pointer
typedef struct
{
float* restrict speeds_0;
float* restrict speeds_1;
float* restrict speeds_2;
float* restrict speeds_3;
float* restrict speeds_4;
float* restrict speeds_5;
float* restrict speeds_6;
float* restrict speeds_7;
float* restrict speeds_8;
} t_speed;
/*
** function prototypes
*/
/* load params, allocate memory, load obstacles & initialise fluid particle densities */
int initialise(const char* paramfile, const char* obstaclefile, t_param* params,
t_speed* restrict cells_ptr, t_speed* restrict tmp_cells_ptr, t_speed* restrict collated_cells,
int** obstacles_ptr, float** av_vels_ptr, float** sendbuf, float** recvbuf,
float** send_blockbuf, float** recv_blockbuf);
int cal_tot_cells(t_param* params, int* obstacles);
/*
** The main calculation methods.
** timestep calls, in order, the functions:
** accelerate_flow(), propagate(), rebound() & collision()
*/
// float timestep(const t_param params, t_speed* restrict cells, t_speed* restrict tmp_cells, int* obstacles);
int accelerate_flow(const t_param params, t_speed* cells, int* obstacles);
int halo_exchange(t_param params, t_speed* restrict cells, float* sendbuf, float* recvbuf);
float timestep(const t_param params, t_speed* restrict cells, t_speed* restrict tmp_cells, int* obstacles);
int collate_cells(const t_param params, t_speed* restrict cells, t_speed* restrict collated_cells, float* send_blockbuf, float* recv_blockbuf);
int write_values(const t_param params, t_speed* collated_cells, int* obstacles, float* av_vels);
/* finalise, including freeing up allocated memory */
int finalise(const t_param* params, t_speed* restrict cells_ptr, t_speed* restrict tmp_cells_ptr,
t_speed* restrict collated_cells, int** obstacles_ptr, float** av_vels_ptr, float** sendbuf,
float** recvbuf, float** send_blockbuf, float** recv_blockbuf);
/* Sum all the densities in the grid.
** The total should remain constant from one timestep to the next. */
float total_density(const t_param params, t_speed* cells);
/* compute average velocity */
float av_velocity(const t_param params, t_speed* collated_cells, int* obstacles);
/* calculate Reynolds number */
float calc_reynolds(const t_param params, t_speed* collated_cells, int* obstacles);
/* calculate number of columns for each rank */
int calc_ncols_from_rank(int rank, int size, int tot_colmns);
/* calculate the starting&end column in obstacles&collated cells for each rank */
int calc_start_column_from_rank(int rank, int local_ncols, int size, int tot_colmns);
int calc_end_column_from_rank(int start_col, int local_ncols);
/* utility functions */
void die(const char* message, const int line, const char* file);
void usage(const char* exe);
/* print functions for debugging */
void printrankspeed(t_param* params, float* speed, int rank, char* PrintType, int tt, float tot_u, int tot_cells, float av_vels);
void printcollatedspeed(t_param* params, float* speed, int rank, char* PrintType, float av_vels);
void printrankobstacles(t_param* params, int* obstacles, int rank, char* PrintType);
void write_compute_time(const char* paramfile, const t_param params, double comp_time, char* comp_out_csv);
/*
** main program:
** initialise, timestep loop, finalise
*/
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
char* paramfile = NULL; /* name of the input parameter file */
char* obstaclefile = NULL; /* name of a the input obstacle file */
t_param params; /* struct to hold parameter values */
t_speed cells; /* grid containing fluid densities */
t_speed tmp_cells; /* scratch space */
t_speed collated_cells; /* grid containing fluid densities from all ranks */
int* obstacles = NULL; /* grid indicating which cells are blocked */
float* av_vels = NULL; /* a record of the av. velocity computed for each timestep */
struct timeval timstr; /* structure to hold elapsed time */
double tot_tic, tot_toc, init_tic, init_toc, comp_tic, comp_toc, col_tic, col_toc; /* floating point numbers to calculate elapsed wallclock time */
float* sendbuf = NULL; /* buffer to hold halo values to send */
float* recvbuf = NULL; /* buffer to hold received halo values */
float* send_blockbuf = NULL; /* buffer to send block values for collation */
float* recv_blockbuf = NULL; /* buffer to receive block values for collation */
/*
** MPI_Init returns once it has started up processes
** Get size of cohort and rank for this process
*/
MPI_Comm_size(MPI_COMM_WORLD, ¶ms.size);
MPI_Comm_rank(MPI_COMM_WORLD, ¶ms.rank);
/* parse the command line */
if (argc != 3)
{
usage(argv[0]);
}
else
{
paramfile = argv[1];
obstaclefile = argv[2];
}
/* Total/init time starts here: initialise our data structures and load values from file */
gettimeofday(&timstr, NULL);
tot_tic = timstr.tv_sec + (timstr.tv_usec / 1000000.0);
init_tic=tot_tic;
initialise(paramfile, obstaclefile, ¶ms, &cells, &tmp_cells, &collated_cells, &obstacles, &av_vels, &sendbuf, &recvbuf, &send_blockbuf, &recv_blockbuf);
/* Init time stops here, compute time starts*/
gettimeofday(&timstr, NULL);
init_toc = timstr.tv_sec + (timstr.tv_usec / 1000000.0);
comp_tic=init_toc;
for (int tt = 0; tt < params.maxIters; tt++)
{
accelerate_flow(params, &cells, obstacles);
// (tt == params.maxIters-1) ? printrankspeed(¶ms, cells.speeds_5, MASTER, "cells_speed5.csv",tt, 0, params.tot_cells, av_vels[tt]) : 0; /* print speeds_5 of a certain iteration on MASTER rank before halo*/
halo_exchange(params, &cells, sendbuf, recvbuf);
// (tt == params.maxIters-1) ? printrankspeed(¶ms, cells.speeds_5, MASTER, "cells_speed5.csv",tt, 0, params.tot_cells, av_vels[tt]) : 0; /* print speeds_5 of a certain iteration on MASTER rank after halo*/
float tot_u = timestep(params, &cells, &tmp_cells, obstacles); /* main compute step */
if(params.rank == MASTER){
// (tt==0) ? printf("rank: %d \t tot_u[%d]: %f \n",params.rank,tt,tot_u) : 0; /* print the value of tot_u on MASTER rank before reduction */
if (params.size > 1){
MPI_Reduce(MPI_IN_PLACE, &tot_u, 1, MPI_FLOAT, MPI_SUM, MASTER, MPI_COMM_WORLD);
}
av_vels[tt] = tot_u / (float)params.tot_cells;
// (tt==0) ? printf("rank: %d \t sum tot_u[%d]: %f \n",params.rank,tt,tot_u) : 0; /* print the value of the sum of tot_u on MASTER rank after reduction */
// (tt==params.maxIters-1) ? printf("av_vels[%d]: %f \n",tt,av_vels[tt]) : 0; /* print av_vel[tt] */
// (tt==params.maxIters-1) ? printf("rank: %d \t params.tot_cells: %d \n",params.rank,params.tot_cells) : 0; /* print stored tot_cells value */
}else{
// (tt==0) ? printf("rank: %d \t tot_u[%d]: %f \n",params.rank,tt,tot_u) : 0; /* print the value of tot_u on other ranks other than MASTER rank */
MPI_Reduce(&tot_u, NULL, 1, MPI_FLOAT, MPI_SUM, MASTER, MPI_COMM_WORLD);
}
/*pointer swap here*/
t_speed tmp_tmp_cells = cells;
cells = tmp_cells;
tmp_cells = tmp_tmp_cells;
/* print speed 5 from cells on MASTER rank after pointer swap, then store them in a csv file */
// (tt == params.maxIters-1) ? printrankspeed(¶ms, cells.speeds_5, MASTER, "cells_speed5.csv",tt, tot_u, params.tot_cells, av_vels[tt]) : 0;
/* print speed 5 from tmp_cells on MASTER rank after pointer swap, then store them in a csv file */
// (tt == params.maxIters-1) ? printrankspeed(¶ms, tmp_cells.speeds_5, MASTER, "tmp_cells_speed5.csv",tt, tot_u, params.tot_cells, av_vels[tt]) : 0;
// if(params.rank == MASTER){ (tt==params.maxIters-1) ? printf("timestep(tt):%d\tav_vel:%f\n",tt,av_vels[tt]) : 0; } /* print the av_vels for the last iteration on MASTER rank */
#ifdef DEBUG
float total = total_density(params, &cells);
// if (tt == params.maxIters-1){
if (params.rank == MASTER){
MPI_Reduce(MPI_IN_PLACE, &total, 1, MPI_FLOAT, MPI_SUM, MASTER, MPI_COMM_WORLD);
printf("==timestep: %d==\n", tt);
printf("av velocity: %.12E\n", av_vels[tt]);
printf("tot density: %.12E\n", total);
}else {
MPI_Reduce(&total, NULL, 1, MPI_FLOAT, MPI_SUM, MASTER, MPI_COMM_WORLD);
}
// }
#endif
}
// printrankobstacles(¶ms, obstacles, MASTER, "obstacles_rank.csv"); /* print the obstacle grid for this rank */
/* Compute time stops here, collate time starts*/
gettimeofday(&timstr, NULL);
comp_toc = timstr.tv_sec + (timstr.tv_usec / 1000000.0);
col_tic=comp_toc;
// Collate data from all ranks to MASTER rank here
collate_cells(params, &cells, &collated_cells, send_blockbuf, recv_blockbuf);
/* verify that cells have been collated on MASTER rank from all other ranks */
// printcollatedspeed(¶ms, collated_cells.speeds_5, MASTER,"collated_cells_speed5.csv", av_vels[params.maxIters-1]);
/* Total/collate time stops here.*/
gettimeofday(&timstr, NULL);
col_toc = timstr.tv_sec + (timstr.tv_usec / 1000000.0);
tot_toc = col_toc;
if (params.rank == MASTER){
/* Calculate Reynolds Number, write final state & av_vels values on MASTER rank */
printf("==done==\n");
printf("Reynolds number:\t\t%.12E\n", calc_reynolds(params, &collated_cells, obstacles));
printf("Elapsed Init time:\t\t\t%.6lf (s)\n", init_toc - init_tic);
printf("Elapsed Compute time:\t\t\t%.6lf (s)\n", comp_toc - comp_tic);
printf("Elapsed Collate time:\t\t\t%.6lf (s)\n", col_toc - col_tic);
printf("Elapsed Total time:\t\t\t%.6lf (s)\n", tot_toc - tot_tic);
write_values(params, &collated_cells, obstacles, av_vels);
}
// write_compute_time(paramfile, params, comp_toc - comp_tic, "compute_time.csv");
/* Free memory on all ranks */
finalise(¶ms, &cells, &tmp_cells, &collated_cells, &obstacles, &av_vels, &sendbuf, &recvbuf, &send_blockbuf, &recv_blockbuf);
MPI_Finalize();
return EXIT_SUCCESS;
}
int cal_tot_cells(t_param* params, int* obstacles){
params->tot_cells=0;
__assume(params->nx%8==0);
__assume(params->ny%8==0);
for (int jj = 0; jj < params->ny; jj++)
{
for (int ii = 0; ii < params->nx; ii++)
{
/* calculate params->tot_cells after initialisation */
params->tot_cells += (!obstacles[jj*params->nx + ii] ? 1 : 0);
}
}
// printf("params->rank: %d, params->tot_cells: %d\n",params->rank, params->tot_cells); /* print the value of tot_cells, this should be 15876 for a 128x128 grid */
return params->tot_cells;
}
int accelerate_flow(const t_param params, t_speed* cells, int* obstacles)
{
/* compute weighting factors */
const float w1 = params.density * params.accel / 9.f;
const float w2 = params.density * params.accel / 36.f;
/* modify the 2nd row of the grid */
const int jj = params.ny - 2;
__assume_aligned(cells->speeds_0, 64);
__assume_aligned(cells->speeds_1, 64);
__assume_aligned(cells->speeds_2, 64);
__assume_aligned(cells->speeds_3, 64);
__assume_aligned(cells->speeds_4, 64);
__assume_aligned(cells->speeds_5, 64);
__assume_aligned(cells->speeds_6, 64);
__assume_aligned(cells->speeds_7, 64);
__assume_aligned(cells->speeds_8, 64);
__assume_aligned(obstacles, 64);
__assume(params.nx%8==0);
#pragma omp simd
for (int ii = 1; ii < params.local_ncols+1; ii++)
{
/* if the cell is not occupied and
** we don't send a negative density */
if (!obstacles[(jj*params.nx+params.start_col) + ii-1] //obstacles index still start from ii=0
&& (cells->speeds_3[ii + jj*(params.local_ncols+2)] - w1) > 0.f
&& (cells->speeds_6[ii + jj*(params.local_ncols+2)] - w2) > 0.f
&& (cells->speeds_7[ii + jj*(params.local_ncols+2)] - w2) > 0.f)
{
/* increase 'east-side' densities */
cells->speeds_1[ii + jj*(params.local_ncols+2)] += w1;
cells->speeds_5[ii + jj*(params.local_ncols+2)] += w2;
cells->speeds_8[ii + jj*(params.local_ncols+2)] += w2;
/* decrease 'west-side' densities */
cells->speeds_3[ii + jj*(params.local_ncols+2)] -= w1;
cells->speeds_6[ii + jj*(params.local_ncols+2)] -= w2;
cells->speeds_7[ii + jj*(params.local_ncols+2)] -= w2;
}
}
return EXIT_SUCCESS;
}
float timestep(const t_param params, t_speed* restrict cells, t_speed* restrict tmp_cells, int* obstacles)
{
float* restrict cells_speeds_0 = cells->speeds_0;
float* restrict cells_speeds_1 = cells->speeds_1;
float* restrict cells_speeds_2 = cells->speeds_2;
float* restrict cells_speeds_3 = cells->speeds_3;
float* restrict cells_speeds_4 = cells->speeds_4;
float* restrict cells_speeds_5 = cells->speeds_5;
float* restrict cells_speeds_6 = cells->speeds_6;
float* restrict cells_speeds_7 = cells->speeds_7;
float* restrict cells_speeds_8 = cells->speeds_8;
float* restrict tmp_cells_speeds_0 = tmp_cells->speeds_0;
float* restrict tmp_cells_speeds_1 = tmp_cells->speeds_1;
float* restrict tmp_cells_speeds_2 = tmp_cells->speeds_2;
float* restrict tmp_cells_speeds_3 = tmp_cells->speeds_3;
float* restrict tmp_cells_speeds_4 = tmp_cells->speeds_4;
float* restrict tmp_cells_speeds_5 = tmp_cells->speeds_5;
float* restrict tmp_cells_speeds_6 = tmp_cells->speeds_6;
float* restrict tmp_cells_speeds_7 = tmp_cells->speeds_7;
float* restrict tmp_cells_speeds_8 = tmp_cells->speeds_8;
float tot_u = 0.f; /* accumulated magnitudes of velocity for each cell */
const float c_sq = 1.f / 3.f; /* square of speed of sound */
const float w0 = 4.f / 9.f; /* weighting factor */
const float w1 = 1.f / 9.f; /* weighting factor */
const float w2 = 1.f / 36.f; /* weighting factor */
__assume_aligned(cells_speeds_0, 64);
__assume_aligned(cells_speeds_1, 64);
__assume_aligned(cells_speeds_2, 64);
__assume_aligned(cells_speeds_3, 64);
__assume_aligned(cells_speeds_4, 64);
__assume_aligned(cells_speeds_5, 64);
__assume_aligned(cells_speeds_6, 64);
__assume_aligned(cells_speeds_7, 64);
__assume_aligned(cells_speeds_8, 64);
__assume_aligned(tmp_cells_speeds_0, 64);
__assume_aligned(tmp_cells_speeds_1, 64);
__assume_aligned(tmp_cells_speeds_2, 64);
__assume_aligned(tmp_cells_speeds_3, 64);
__assume_aligned(tmp_cells_speeds_4, 64);
__assume_aligned(tmp_cells_speeds_5, 64);
__assume_aligned(tmp_cells_speeds_6, 64);
__assume_aligned(tmp_cells_speeds_7, 64);
__assume_aligned(tmp_cells_speeds_8, 64);
__assume_aligned(obstacles, 64);
__assume(params.nx%2==0);
__assume(params.nx%4==0);
__assume(params.nx%8==0);
__assume(params.nx%16==0);
__assume(params.nx%32==0);
__assume(params.ny%2==0);
__assume(params.ny%4==0);
__assume(params.ny%8==0);
__assume(params.ny%16==0);
__assume(params.ny%32==0);
/* loop over _all_ cells */
// #pragma omp parallel for reduction(+:tot_u) //schedule(static)
for (int jj = 0; jj < params.local_nrows; jj++)
{
// printf("Thread ID: %d \t Number of threads:%d\n",omp_get_thread_num(),omp_get_num_threads());
#pragma omp simd
for (int ii = 1; ii < params.local_ncols+1; ii++)
{
/******************* propagate & rebound *********************/
/* determine indices of axis-direction neighbours
** respecting periodic boundary conditions (wrap around) */
const int y_n = (jj + 1) % params.local_nrows; // still wrap around to the north
const int x_e = (ii + 1); // doesn't wrap around to the east anymore thanks to the halo regions
const int y_s = (jj == 0) ? (jj + params.local_nrows - 1) : (jj - 1); // still wrap around to the south
const int x_w = (ii - 1); // dosen't wrap around to the west anymore thanks to the halo regions
/* propagate densities from neighbouring cells, following
** appropriate directions of travel and writing into
** scratch space grid */
/*Fuse propagate,collision&rebound. Instead of reading from cells and writing to tmp_cells(propagate), then reading from tmp_cells and
writing to cells(rebound&collision), we can do this with one read from cells and one write to tmp_cells, followed by a pointer swap
between cells and tmp_cells afterwards*/
const float cells0 = cells_speeds_0[ii + jj*(params.local_ncols+2)]; /* central cell, no movement */
const float cells1 = cells_speeds_1[x_w + jj*(params.local_ncols+2)]; /* east */
const float cells2 = cells_speeds_2[ii + y_s*(params.local_ncols+2)]; /* north */
const float cells3 = cells_speeds_3[x_e + jj*(params.local_ncols+2)]; /* west */
const float cells4 = cells_speeds_4[ii + y_n*(params.local_ncols+2)]; /* south */
const float cells5 = cells_speeds_5[x_w + y_s*(params.local_ncols+2)]; /* north-east */
const float cells6 = cells_speeds_6[x_e + y_s*(params.local_ncols+2)]; /* north-west */
const float cells7 = cells_speeds_7[x_e + y_n*(params.local_ncols+2)]; /* south-west */
const float cells8 = cells_speeds_8[x_w + y_n*(params.local_ncols+2)]; /* south-east */
/* compute local density total */
const float local_density = cells0
+ cells1
+ cells2
+ cells3
+ cells4
+ cells5
+ cells6
+ cells7
+ cells8;
/* compute x velocity component */
const float u_x = (cells1
+ cells5
+ cells8
- (cells3
+ cells6
+ cells7))
/ local_density;
/* compute y velocity component */
const float u_y = (cells2
+ cells5
+ cells6
- (cells4
+ cells7
+ cells8))
/ local_density;
/* velocity squared */
const float u_sq = u_x * u_x + u_y * u_y;
/* directional velocity components */
// float u[NSPEEDS];
const float u_1 = u_x; /* east */
const float u_2 = u_y; /* north */
const float u_3 = - u_x; /* west */
const float u_4 = - u_y; /* south */
const float u_5 = u_x + u_y; /* north-east */
const float u_6 = - u_x + u_y; /* north-west */
const float u_7 = - u_x - u_y; /* south-west */
const float u_8 = u_x - u_y; /* south-east */
/* equilibrium densities */
// float d_equ[NSPEEDS];
/* zero velocity density: weight w0 */
const float d_equ_0 = w0 * local_density * (1.f - u_sq * 1.5f);
/* axis speeds: weight w1 */
const float d_equ_1 = w1 * local_density * (1.f + u_1 * 3.f + (u_1 * u_1) * 4.5f - u_sq * 1.5f);
const float d_equ_2 = w1 * local_density * (1.f + u_2 * 3.f + (u_2 * u_2) * 4.5f - u_sq * 1.5f);
const float d_equ_3 = w1 * local_density * (1.f + u_3 * 3.f + (u_3 * u_3) * 4.5f - u_sq * 1.5f);
const float d_equ_4 = w1 * local_density * (1.f + u_4 * 3.f + (u_4 * u_4) * 4.5f - u_sq * 1.5f);
/* diagonal speeds: weight w2 */
const float d_equ_5 = w2 * local_density * (1.f + u_5 * 3.f + (u_5 * u_5) * 4.5f - u_sq * 1.5f);
const float d_equ_6 = w2 * local_density * (1.f + u_6 * 3.f + (u_6 * u_6) * 4.5f - u_sq * 1.5f);
const float d_equ_7 = w2 * local_density * (1.f + u_7 * 3.f + (u_7 * u_7) * 4.5f - u_sq * 1.5f);
const float d_equ_8 = w2 * local_density * (1.f + u_8 * 3.f + (u_8 * u_8) * 4.5f - u_sq * 1.5f);
/******************* propagate & collision/rebound *********************/
tmp_cells_speeds_0[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells0 : cells0 * (1.f-params.omega) + params.omega*d_equ_0;
tmp_cells_speeds_1[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells3 : cells1 * (1.f-params.omega) + params.omega*d_equ_1;
tmp_cells_speeds_2[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells4 : cells2 * (1.f-params.omega) + params.omega*d_equ_2;
tmp_cells_speeds_3[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells1 : cells3 * (1.f-params.omega) + params.omega*d_equ_3;
tmp_cells_speeds_4[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells2 : cells4 * (1.f-params.omega) + params.omega*d_equ_4;
tmp_cells_speeds_5[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells7 : cells5 * (1.f-params.omega) + params.omega*d_equ_5;
tmp_cells_speeds_6[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells8 : cells6 * (1.f-params.omega) + params.omega*d_equ_6;
tmp_cells_speeds_7[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells5 : cells7 * (1.f-params.omega) + params.omega*d_equ_7;
tmp_cells_speeds_8[ii + jj*(params.local_ncols+2)] = (obstacles[(jj*params.nx+params.start_col) + ii-1]) ? cells6 : cells8 * (1.f-params.omega) + params.omega*d_equ_8;
/* accumulate the norm of x- and y- velocity components */
tot_u += (!obstacles[(jj*params.nx+params.start_col) + ii-1]) ? sqrtf((u_x * u_x) + (u_y * u_y)) : 0;
}
}
return tot_u;
}
float av_velocity(const t_param params, t_speed* collated_cells, int* obstacles)
{
float tot_u = 0.f; /* accumulated magnitudes of velocity for each cell */
__assume(params.nx%8==0);
__assume(params.ny%8==0);
/* loop over all non-blocked cells */
// #pragma omp parallel for reduction(+:tot_u) //num_threads(28)
for (int jj = 0; jj < params.ny; jj++)
{
__assume_aligned(collated_cells->speeds_0, 64);
__assume_aligned(collated_cells->speeds_1, 64);
__assume_aligned(collated_cells->speeds_2, 64);
__assume_aligned(collated_cells->speeds_3, 64);
__assume_aligned(collated_cells->speeds_4, 64);
__assume_aligned(collated_cells->speeds_5, 64);
__assume_aligned(collated_cells->speeds_6, 64);
__assume_aligned(collated_cells->speeds_7, 64);
__assume_aligned(collated_cells->speeds_8, 64);
__assume_aligned(obstacles, 64);
#pragma omp simd
for (int ii = 0; ii < params.nx; ii++)
{
/* local density total */
const float local_density = collated_cells->speeds_0[ii + jj*params.nx]
+ collated_cells->speeds_1[ii + jj*params.nx]
+ collated_cells->speeds_2[ii + jj*params.nx]
+ collated_cells->speeds_3[ii + jj*params.nx]
+ collated_cells->speeds_4[ii + jj*params.nx]
+ collated_cells->speeds_5[ii + jj*params.nx]
+ collated_cells->speeds_6[ii + jj*params.nx]
+ collated_cells->speeds_7[ii + jj*params.nx]
+ collated_cells->speeds_8[ii + jj*params.nx];
/* x-component of velocity */
const float u_x = (collated_cells->speeds_1[ii + jj*params.nx]
+ collated_cells->speeds_5[ii + jj*params.nx]
+ collated_cells->speeds_8[ii + jj*params.nx]
- (collated_cells->speeds_3[ii + jj*params.nx]
+ collated_cells->speeds_6[ii + jj*params.nx]
+ collated_cells->speeds_7[ii + jj*params.nx]))
/ local_density;
/* compute y velocity component */
const float u_y = (collated_cells->speeds_2[ii + jj*params.nx]
+ collated_cells->speeds_5[ii + jj*params.nx]
+ collated_cells->speeds_6[ii + jj*params.nx]
- (collated_cells->speeds_4[ii + jj*params.nx]
+ collated_cells->speeds_7[ii + jj*params.nx]
+ collated_cells->speeds_8[ii + jj*params.nx]))
/ local_density;
/* accumulate the norm of x- and y- velocity components */
tot_u += (!obstacles[jj*params.nx + ii]) ? sqrtf((u_x * u_x) + (u_y * u_y)) : 0;
}
}
return tot_u / (float)params.tot_cells;
}
int initialise(const char* paramfile, const char* obstaclefile, t_param* params,
t_speed* restrict cells_ptr, t_speed* restrict tmp_cells_ptr, t_speed* restrict collated_cells,
int** obstacles_ptr, float** av_vels_ptr, float** sendbuf, float** recvbuf,
float** send_blockbuf, float** recv_blockbuf)
{
char message[1024]; /* message buffer */
FILE* fp; /* file pointer */
int xx, yy; /* generic array indices */
int blocked; /* indicates whether a cell is blocked by an obstacle */
int retval; /* to hold return value for checking */
/* open the parameter file */
fp = fopen(paramfile, "r");
if (fp == NULL)
{
sprintf(message, "could not open input parameter file: %s", paramfile);
die(message, __LINE__, __FILE__);
}
/* read in the parameter values */
retval = fscanf(fp, "%d\n", &(params->nx));
if (retval != 1) die("could not read param file: nx", __LINE__, __FILE__);
retval = fscanf(fp, "%d\n", &(params->ny));
if (retval != 1) die("could not read param file: ny", __LINE__, __FILE__);
retval = fscanf(fp, "%d\n", &(params->maxIters));
if (retval != 1) die("could not read param file: maxIters", __LINE__, __FILE__);
retval = fscanf(fp, "%d\n", &(params->reynolds_dim));
if (retval != 1) die("could not read param file: reynolds_dim", __LINE__, __FILE__);
retval = fscanf(fp, "%f\n", &(params->density));
if (retval != 1) die("could not read param file: density", __LINE__, __FILE__);
retval = fscanf(fp, "%f\n", &(params->accel));
if (retval != 1) die("could not read param file: accel", __LINE__, __FILE__);
retval = fscanf(fp, "%f\n", &(params->omega));
if (retval != 1) die("could not read param file: omega", __LINE__, __FILE__);
/* and close up the file */
fclose(fp);
/*
** determine process ranks to the left and right of rank
** respecting periodic boundary conditions
*/
params->left = (params->rank == MASTER) ? (params->rank + params->size - 1) : (params->rank - 1);
params->right = (params->rank + 1) % params->size;
/*
** determine local grid size
** each rank gets all the rows, but a subset of the number of columns
*/
params->local_nrows = params->ny;
params->local_ncols = calc_ncols_from_rank(params->rank, params->size, params->nx);
if (params->local_ncols < 1) {
fprintf(stderr,"Error: too many processes:- local_ncols < 1\n");
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
/* determine the index of the starting column in obstacles/collated cells for this rank */
params->start_col = calc_start_column_from_rank(params->rank, params->local_ncols, params->size, params->nx);
params->end_col = calc_end_column_from_rank(params->start_col, params->local_ncols);
/*
** Allocate memory.
**
** Remember C is pass-by-value, so we need to
** pass pointers into the initialise function.
**
** NB we are allocating a 1D array, so that the
** memory will be contiguous. We still want to
** index this memory as if it were a (row major
** ordered) 2D array, however. We will perform
** some arithmetic using the row and column
** coordinates, inside the square brackets, when
** we want to access elements of this array.
**
** Note also that we are using a structure to
** hold an array of 'speeds'. We will allocate
** a 1D array of these structs.
*/
/* main grid */
/* After changing to SOA we to change how we initialise cells too (need to allocate memory for each of the speed_0, speed_1...) */
cells_ptr->speeds_0 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
cells_ptr->speeds_1 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
cells_ptr->speeds_2 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
cells_ptr->speeds_3 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
cells_ptr->speeds_4 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
cells_ptr->speeds_5 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
cells_ptr->speeds_6 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
cells_ptr->speeds_7 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
cells_ptr->speeds_8 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
// if (*cells_ptr == NULL) die("cannot allocate memory for cells", __LINE__, __FILE__);
/* 'helper' grid, used as scratch space */
tmp_cells_ptr->speeds_0 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
tmp_cells_ptr->speeds_1 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
tmp_cells_ptr->speeds_2 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
tmp_cells_ptr->speeds_3 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
tmp_cells_ptr->speeds_4 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
tmp_cells_ptr->speeds_5 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
tmp_cells_ptr->speeds_6 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
tmp_cells_ptr->speeds_7 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
tmp_cells_ptr->speeds_8 = (float*)_mm_malloc(sizeof(float) * (params->ny * (params->local_ncols+2)),64);
// if (*tmp_cells_ptr == NULL) die("cannot allocate memory for tmp_cells", __LINE__, __FILE__);
/* the map of obstacles */
*obstacles_ptr = _mm_malloc(sizeof(int) * (params->ny * params->nx),64);
if (*obstacles_ptr == NULL) die("cannot allocate column memory for obstacles", __LINE__, __FILE__);
/* initialise densities */
const float w0 = params->density * 4.f / 9.f;
const float w1 = params->density / 9.f;
const float w2 = params->density / 36.f;
// __assume_aligned(cells_ptr, 64);
__assume_aligned(cells_ptr->speeds_0, 64);
__assume_aligned(cells_ptr->speeds_1, 64);
__assume_aligned(cells_ptr->speeds_2, 64);
__assume_aligned(cells_ptr->speeds_3, 64);
__assume_aligned(cells_ptr->speeds_4, 64);
__assume_aligned(cells_ptr->speeds_5, 64);
__assume_aligned(cells_ptr->speeds_6, 64);
__assume_aligned(cells_ptr->speeds_7, 64);
__assume_aligned(cells_ptr->speeds_8, 64);
__assume_aligned(*obstacles_ptr, 64);
// __assume((*params).nx%128==0);
// __assume((*params).ny%128==0);
/*alternatively*/
__assume(params->nx%8==0);
__assume(params->ny%8==0);
// omp_set_num_threads(28);
// #pragma omp parallel for
for (int jj = 0; jj < params->ny; jj++)
{
#pragma omp simd
for (int ii = 1; ii < params->local_ncols+1; ii++)
{
/* initialise main grid for this rank (excluding halo) */
/* centre */
cells_ptr->speeds_0[ii + jj*(params->local_ncols+2)] = w0;
/* axis directions */
cells_ptr->speeds_1[ii + jj*(params->local_ncols+2)] = w1;
cells_ptr->speeds_2[ii + jj*(params->local_ncols+2)] = w1;
cells_ptr->speeds_3[ii + jj*(params->local_ncols+2)] = w1;
cells_ptr->speeds_4[ii + jj*(params->local_ncols+2)] = w1;
/* diagonals */
cells_ptr->speeds_5[ii + jj*(params->local_ncols+2)] = w2;
cells_ptr->speeds_6[ii + jj*(params->local_ncols+2)] = w2;
cells_ptr->speeds_7[ii + jj*(params->local_ncols+2)] = w2;
cells_ptr->speeds_8[ii + jj*(params->local_ncols+2)] = w2;
}
}
for (int jj = 0; jj < params->ny; jj++)
{
#pragma omp simd
for (int ii = 0; ii < params->nx; ii++)
{
/* first set all cells in obstacle array to zero */
(*obstacles_ptr)[ii + jj*params->nx] = 0;
}
}
/* open the obstacle data file */
fp = fopen(obstaclefile, "r");
if (fp == NULL)
{
sprintf(message, "could not open input obstacles file: %s", obstaclefile);
die(message, __LINE__, __FILE__);
}
/* read-in the blocked cells list */
while ((retval = fscanf(fp, "%d %d %d\n", &xx, &yy, &blocked)) != EOF)
{
/* some checks */
if (retval != 3) die("expected 3 values per line in obstacle file", __LINE__, __FILE__);
if (xx < 0 || xx > params->nx - 1) die("obstacle x-coord out of range", __LINE__, __FILE__);
if (yy < 0 || yy > params->ny - 1) die("obstacle y-coord out of range", __LINE__, __FILE__);
if (blocked != 1) die("obstacle blocked value should be 1", __LINE__, __FILE__);
/* assign to array */
(*obstacles_ptr)[xx + yy*params->nx] = blocked;
}
/* and close the file */
fclose(fp);
/*
** Calculate totoal number of cells that have obstacles==0, note only the MASTER rank
** needs this value to compute av_vel[tt] at each timestep
*/
params->tot_cells = cal_tot_cells(params, (*obstacles_ptr));
/*
** Allocate memory to store a record of the send and receive columns for this rank's halo regions
** at each timestep
*/
*sendbuf = (float*)_mm_malloc(sizeof(float) * (params->ny * NSPEEDS),64);
*recvbuf = (float*)_mm_malloc(sizeof(float) * (params->ny * NSPEEDS),64);
/*
** Allocate memory to store a record of the send blocks of cells' values (rows&columns)
** from all ranks other than the MASTER rank , then collate cells on MASTER with receive block
*/
if (params->rank != MASTER){
*send_blockbuf = (float*)_mm_malloc(sizeof(float) * (params->ny * params->local_ncols * NSPEEDS),64);
}else{
*recv_blockbuf = (float*)_mm_malloc(sizeof(float) * (params->ny * params->local_ncols * NSPEEDS),64);
}
/* Allocate memory for collating cells on MASTER rank */
if (params->rank == MASTER) {
collated_cells->speeds_0 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
collated_cells->speeds_1 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
collated_cells->speeds_2 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
collated_cells->speeds_3 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
collated_cells->speeds_4 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
collated_cells->speeds_5 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
collated_cells->speeds_6 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
collated_cells->speeds_7 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
collated_cells->speeds_8 = (float*)_mm_malloc(sizeof(float) * (params->ny * params->nx),64);
}
/*
** Allocate space to hold a record of the avarage velocities computed
** at each timestep
*/
*av_vels_ptr = (float*)_mm_malloc(sizeof(float) * params->maxIters,64);
// if (params->rank==MASTER){
// for (int n = 0; n < params->size; n++){
// (n==0) ? printf("<- rank %d",n) : printf(" <-> rank %d",n);
// }
// printf(" ->\n");
// }
// // printf("params->ny: %d, params->nx: %d\n",params->ny,params->nx);
// printf("rank %d : sendbuf size: %d\n",params->rank,(params->local_nrows * NSPEEDS));
// printf("rank: %d/%d, params->local_ncols: %d, params->start_col: %d, params->end_col: %d\n",params->rank, params->size, params->local_ncols, params->start_col, params->end_col);
return EXIT_SUCCESS;
}
int finalise(const t_param* params, t_speed* restrict cells_ptr, t_speed* restrict tmp_cells_ptr,
t_speed* restrict collated_cells, int** obstacles_ptr, float** av_vels_ptr, float** sendbuf,
float** recvbuf, float** send_blockbuf, float** recv_blockbuf)
{
/*
** free up allocated memory
*/
_mm_free(cells_ptr->speeds_0);
_mm_free(cells_ptr->speeds_1);
_mm_free(cells_ptr->speeds_2);
_mm_free(cells_ptr->speeds_3);
_mm_free(cells_ptr->speeds_4);
_mm_free(cells_ptr->speeds_5);
_mm_free(cells_ptr->speeds_6);
_mm_free(cells_ptr->speeds_7);
_mm_free(cells_ptr->speeds_8);
_mm_free(tmp_cells_ptr->speeds_0);
_mm_free(tmp_cells_ptr->speeds_1);
_mm_free(tmp_cells_ptr->speeds_2);
_mm_free(tmp_cells_ptr->speeds_3);
_mm_free(tmp_cells_ptr->speeds_4);
_mm_free(tmp_cells_ptr->speeds_5);
_mm_free(tmp_cells_ptr->speeds_6);
_mm_free(tmp_cells_ptr->speeds_7);
_mm_free(tmp_cells_ptr->speeds_8);
if (params->rank == MASTER){
_mm_free(collated_cells->speeds_0);
_mm_free(collated_cells->speeds_1);
_mm_free(collated_cells->speeds_2);
_mm_free(collated_cells->speeds_3);
_mm_free(collated_cells->speeds_4);
_mm_free(collated_cells->speeds_5);
_mm_free(collated_cells->speeds_6);
_mm_free(collated_cells->speeds_7);
_mm_free(collated_cells->speeds_8);
}
_mm_free(*obstacles_ptr);
*obstacles_ptr = NULL;
_mm_free(*av_vels_ptr);
*av_vels_ptr = NULL;
_mm_free(*sendbuf);
*sendbuf = NULL;
_mm_free(*recvbuf);
*recvbuf = NULL;
_mm_free(*send_blockbuf);
*send_blockbuf = NULL;
_mm_free(*recv_blockbuf);
*recv_blockbuf = NULL;
return EXIT_SUCCESS;
}
float calc_reynolds(const t_param params, t_speed* collated_cells, int* obstacles)
{
const float viscosity = 1.f / 6.f * (2.f / params.omega - 1.f);
return av_velocity(params, collated_cells, obstacles) * params.reynolds_dim / viscosity;
}
float total_density(const t_param params, t_speed* cells)
{
float total = 0.f; /* accumulator */
__assume(params.nx%16==0);
__assume(params.ny%16==0);
for (int jj = 0; jj < params.ny; jj++)
{
__assume_aligned(cells->speeds_0, 64);
__assume_aligned(cells->speeds_1, 64);
__assume_aligned(cells->speeds_2, 64);
__assume_aligned(cells->speeds_3, 64);
__assume_aligned(cells->speeds_4, 64);
__assume_aligned(cells->speeds_5, 64);
__assume_aligned(cells->speeds_6, 64);
__assume_aligned(cells->speeds_7, 64);
__assume_aligned(cells->speeds_8, 64);
#pragma omp simd
for (int ii = 1; ii < (params.local_ncols+1); ii++)
{
total += cells->speeds_0[ii + jj*(params.local_ncols+2)]
+ cells->speeds_1[ii + jj*(params.local_ncols+2)]
+ cells->speeds_2[ii + jj*(params.local_ncols+2)]
+ cells->speeds_3[ii + jj*(params.local_ncols+2)]
+ cells->speeds_4[ii + jj*(params.local_ncols+2)]
+ cells->speeds_5[ii + jj*(params.local_ncols+2)]
+ cells->speeds_6[ii + jj*(params.local_ncols+2)]
+ cells->speeds_7[ii + jj*(params.local_ncols+2)]
+ cells->speeds_8[ii + jj*(params.local_ncols+2)];
}
}
return total;
}
int write_values(const t_param params, t_speed* collated_cells, int* obstacles, float* av_vels)
{
FILE* fp; /* file pointer */
const float c_sq = 1.f / 3.f; /* sq. of speed of sound */
// float local_density; /* per grid cell sum of densities */
float pressure; /* fluid pressure in grid cell */
float u_x; /* x-component of velocity in grid cell */
float u_y; /* y-component of velocity in grid cell */
float u; /* norm--root of summed squares--of u_x and u_y */
fp = fopen(FINALSTATEFILE, "w");
if (fp == NULL)
{
die("could not open file output file", __LINE__, __FILE__);