1 // --------------------------------------------------------------------------
4 // Markus Wittmann, 2016-2017
5 // RRZE, University of Erlangen-Nuremberg, Germany
6 // markus.wittmann -at- fau.de or hpc -at- rrze.fau.de
9 // LSS, University of Erlangen-Nuremberg, Germany
11 // This file is part of the Lattice Boltzmann Benchmark Kernels (LbmBenchKernels).
13 // LbmBenchKernels is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
18 // LbmBenchKernels is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
23 // You should have received a copy of the GNU General Public License
24 // along with LbmBenchKernels. If not, see <http://www.gnu.org/licenses/>.
26 // --------------------------------------------------------------------------
27 #include "BenchKernelD3Q19ListAaRiaCommon.h"
38 // Forward definition.
39 void FNAME(D3Q19ListAaRiaKernel)(LatticeDesc * ld, struct KernelData_ * kd, CaseData * cd);
44 // -----------------------------------------------------------------------
45 // Functions which are used as callback by the kernel to read or write
48 static void FNAME(BCGetPdf)(KernelData * kd, int x, int y, int z, int dir, PdfT * pdf)
51 Assert(kd->PdfsActive != NULL);
52 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
55 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
56 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
57 Assert(dir >= 0); Assert(dir < N_D3Q19);
59 KernelDataList * kdl = (KernelDataList *)kd;
61 if (kdl->Iteration % 2 == 0) {
62 // Pdfs are stored inverse, local PDFs are located in remote nodes
64 uint32_t nodeIndex = KDL(kd)->Grid[L_INDEX_4(kd->Dims, x, y, z)];
67 uint32_t adjListIndex = nodeIndex * N_D3Q19_IDX;
69 *pdf = kd->PdfsActive[KDL(kd)->AdjList[adjListIndex + D3Q19_INV[dir]]];
72 *pdf = kd->PdfsActive[P_INDEX_3(KDL(kd)->nCells, nodeIndex, dir)];
77 *pdf = kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)];
84 static void FNAME(BCSetPdf)(KernelData * kd, int x, int y, int z, int dir, PdfT pdf)
87 Assert(kd->PdfsActive != NULL);
88 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
89 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
90 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
91 Assert(dir >= 0); Assert(dir < N_D3Q19);
95 printf("ERROR: setting nan %d %d %d %d %s\n", x, y, z, dir, D3Q19_NAMES[dir]);
101 KernelDataList * kdl = (KernelDataList *)kd;
103 if (kdl->Iteration % 2 == 0) {
104 // Pdfs are stored inverse, local PDFs are located in remote nodes
106 uint32_t nodeIndex = KDL(kd)->Grid[L_INDEX_4(kd->Dims, x, y, z)];
108 if (dir != D3Q19_C) {
109 uint32_t adjListIndex = nodeIndex * N_D3Q19_IDX;
111 kd->PdfsActive[KDL(kd)->AdjList[adjListIndex + D3Q19_INV[dir]]] = pdf;
114 kd->PdfsActive[P_INDEX_3(KDL(kd)->nCells, nodeIndex, dir)] = pdf;
119 kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)] = pdf;
126 static void GetNode(KernelData * kd, int x, int y, int z, PdfT * pdfs)
129 Assert(kd->PdfsActive != NULL);
130 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
131 Assert(pdfs != NULL);
132 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
133 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
135 KernelDataList * kdl = (KernelDataList *)kd;
137 if(kdl->Iteration % 2 == 0){
139 uint32_t nodeIndex = kdl->Grid[L_INDEX_4(kdl->kd.Dims, x, y, z)];
140 uint32_t adjListIndex = nodeIndex * N_D3Q19_IDX;
142 // Load PDFs of local cell: pdf_N = src[adjList[adjListIndex + D3Q19_S]]; ...
143 pdfs[D3Q19_C] = kd->PdfsActive[P_INDEX_3(kdl->nCells, nodeIndex, D3Q19_C)];
145 #define X(name, idx, idxinv, _x, _y, _z) pdfs[idx] = kd->PdfsActive[kdl->AdjList[adjListIndex + idxinv]];
151 #define I(x, y, z, dir) P_INDEX_5(KDL(kd), (x), (y), (z), (dir))
152 #define X(name, idx, idxinv, _x, _y, _z) pdfs[idx] = kd->PdfsActive[I(x, y, z, idx)];
160 for (int d = 0; d < 19; ++d) {
161 if(isnan(pdfs[d]) || isinf(pdfs[d])) {
162 printf("%d %d %d %d nan! get node\n", x, y, z, d);
163 for (int d2 = 0; d2 < 19; ++d2) {
164 printf("%d: %e\n", d2, pdfs[d2]);
175 static void SetNode(KernelData * kd, int x, int y, int z, PdfT * pdfs)
178 Assert(kd->PdfsActive != NULL);
179 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
180 Assert(pdfs != NULL);
182 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
183 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
186 for (int d = 0; d < 19; ++d) {
188 printf("%d %d %d %d nan! get node\n", x, y, z, d);
189 for (int d2 = 0; d2 < 19; ++d2) {
190 printf("%d: %e\n", d2, pdfs[d2]);
197 KernelDataList * kdl = (KernelDataList *)kd;
199 if(kdl->Iteration % 2 == 0){
201 uint32_t nodeIndex = kdl->Grid[L_INDEX_4(kdl->kd.Dims, x, y, z)];
202 uint32_t adjListIndex = nodeIndex * N_D3Q19_IDX;
204 // Load PDFs of local cell: pdf_N = src[adjList[adjListIndex + D3Q19_S]]; ...
205 kd->PdfsActive[P_INDEX_3(kdl->nCells, nodeIndex, D3Q19_C)] = pdfs[D3Q19_C];
207 #define X(name, idx, idxinv, _x, _y, _z) kd->PdfsActive[kdl->AdjList[adjListIndex + idxinv]] = pdfs[idx];
213 #define I(x, y, z, dir) P_INDEX_5(KDL(kd), (x), (y), (z), (dir))
214 #define X(name, idx, idxinv, _x, _y, _z) kd->PdfsActive[I(x, y, z, idx)] = pdfs[idx];
224 static void ParameterUsage()
226 printf("Kernel parameters:\n");
227 printf(" [-blk <n>] [-blk-[xyz] <n>]\n");
232 static void ParseParameters(Parameters * params, int * blk)
236 blk[0] = 0; blk[1] = 0; blk[2] = 0;
238 #define ARG_IS(param) (!strcmp(params->KernelArgs[i], param))
239 #define NEXT_ARG_PRESENT() \
241 if (i + 1 >= params->nKernelArgs) { \
242 printf("ERROR: argument %s requires a parameter.\n", params->KernelArgs[i]); \
248 for (int i = 0; i < params->nKernelArgs; ++i) {
249 if (ARG_IS("-blk") || ARG_IS("--blk")) {
252 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
255 printf("ERROR: blocking parameter must be > 0.\n");
259 blk[0] = blk[1] = blk[2] = tmp;
261 else if (ARG_IS("-blk-x") || ARG_IS("--blk-x")) {
264 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
267 printf("ERROR: blocking parameter must be > 0.\n");
273 else if (ARG_IS("-blk-y") || ARG_IS("--blk-y")) {
276 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
279 printf("ERROR: blocking parameter must be > 0.\n");
285 else if (ARG_IS("-blk-z") || ARG_IS("--blk-z")) {
288 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
291 printf("ERROR: blocking parameter must be > 0.\n");
297 else if (ARG_IS("-h") || ARG_IS("-help") || ARG_IS("--help")) {
302 printf("ERROR: unknown kernel parameter.\n");
309 #undef NEXT_ARG_PRESENT
314 static void SetupConsecNodes(LatticeDesc * ld, KernelDataListRia * kdlr, int nThreads)
317 Assert(kdlr != NULL);
318 Assert(nThreads > 0);
320 uint32_t * adjList = kdlr->kdl.AdjList;
322 uint32_t nConsecNodes = 0;
323 uint32_t consecIndex = 0;
325 int nFluid = kdlr->kdl.nFluid;
327 uint32_t * consecThreadIndices = (uint32_t *)malloc(sizeof(uint32_t) * (nThreads + 1));
328 int * fluidNodeThreadIndices = (int *)malloc(sizeof(int) * (nThreads + 1));
330 int nNodesPerThread = nFluid / nThreads;
332 for (int i = 0; i < nThreads; ++i) {
333 consecThreadIndices[i] = i * nNodesPerThread + MinI(i, nFluid % nThreads);
334 fluidNodeThreadIndices[i] = consecThreadIndices[i];
336 consecThreadIndices[nThreads] = -1;
337 fluidNodeThreadIndices[nThreads] = nFluid;
341 // We execute following code two times.
342 // - The first time to get the count of how many entries we need for the
343 // consecNodes array.
344 // - The second time to fill the array.
346 // Loop over adjacency list of all nodes.
347 // Compare if adjacent nodes share the same access pattern.
348 for (int index = 1; index < nFluid; ++index) {
352 // Loop over all directions except the center one.
353 for(int d = 0; d < N_D3Q19 - 1; ++d) {
354 Assert(d != D3Q19_C);
356 if (adjList[index * N_D3Q19_IDX + d] != adjList[(index - 1) * N_D3Q19_IDX + d] + 1) {
357 // Different access pattern.
363 if (consecThreadIndices[indexThread] == index) {
364 // We are at a thread boundary. Starting from this index the fluids
365 // belong to another thread. Force a break, if nodes are consecutive.
376 nConsecNodes = consecIndex + 1;
379 uint32_t * consecNodes;
380 MemAlloc((void **)&consecNodes, sizeof(uint32_t) * nConsecNodes);
385 consecNodes[consecIndex] = 1;
389 consecThreadIndices[0] = 0;
391 // Loop over adjacency list of all nodes.
392 // Compare if adjacent nodes share the same access pattern.
393 for (int index = 1; index < nFluid; ++index) {
397 // Loop over all directions except the center one.
398 for(int d = 0; d < N_D3Q19 - 1; ++d) {
399 Assert(d != D3Q19_C);
401 if (adjList[index * N_D3Q19_IDX + d] != adjList[(index - 1) * N_D3Q19_IDX + d] + 1) {
402 // Different access pattern.
408 if (consecThreadIndices[indexThread] == index) {
409 // We are at a thread boundary. Starting from this index the fluids
410 // belong to another thread. Force a break, if nodes are consecutive.
411 consecThreadIndices[indexThread] = consecIndex + 1;
418 Assert(consecIndex < nConsecNodes);
419 consecNodes[consecIndex] = 1;
422 Assert(consecIndex < nConsecNodes);
423 consecNodes[consecIndex] += 1;
428 kdlr->ConsecNodes = consecNodes;
429 kdlr->nConsecNodes = nConsecNodes;
431 kdlr->ConsecThreadIndices = consecThreadIndices;
432 kdlr->nConsecThreadIndices = nThreads;
434 kdlr->FluidNodeThreadIndices = fluidNodeThreadIndices;
435 kdlr->nFluidNodeThreadIndices = nThreads;
437 printf("# total fluid nodes: %d consecutive blocks: %d\n", nFluid, nConsecNodes);
442 void FNAME(D3Q19ListAaRiaInit)(LatticeDesc * ld, KernelData ** kernelData, Parameters * params)
445 KernelDataList * kdl;
446 KernelDataListRia * kdlr;
447 MemAlloc((void **)&kdlr, sizeof(KernelDataListRia));
449 kd = (KernelData *)kdlr;
457 kd->PdfsActive = NULL;
463 kd->GlobalDims[0] = -1;
464 kd->GlobalDims[1] = -1;
465 kd->GlobalDims[2] = -1;
470 kd->ObstIndices = NULL;
471 kd->nObstIndices = -1;
472 kd->BounceBackPdfsSrc = NULL;
473 kd->BounceBackPdfsDst = NULL;
474 kd->nBounceBackPdfs = -1;
482 kdlr->ConsecNodes = NULL;
483 kdlr->nConsecNodes = 0;
484 kdlr->ConsecThreadIndices = NULL;
485 kdlr->nConsecThreadIndices = 0;
488 // Ajust the dimensions according to padding, if used.
489 kd->Dims[0] = kd->GlobalDims[0] = ld->Dims[0];
490 kd->Dims[1] = kd->GlobalDims[1] = ld->Dims[1];
491 kd->Dims[2] = kd->GlobalDims[2] = ld->Dims[2];
493 int * lDims = ld->Dims;
499 int nTotalCells = lX * lY * lZ;
500 int nCells = ld->nFluid; // TODO: + padding
501 int nFluid = ld->nFluid;
503 kdl->nCells = nCells;
504 kdl->nFluid = nFluid;
510 ParseParameters(params, blk);
512 if (blk[0] == 0) blk[0] = lX;
513 if (blk[1] == 0) blk[1] = lY;
514 if (blk[2] == 0) blk[2] = lZ;
516 printf("# blocking x: %3d y: %3d z: %3d\n", blk[0], blk[1], blk[2]);
518 double latMiB = nCells * sizeof(PdfT) * N_D3Q19 / 1024.0 / 1024.0;
519 double latFluidMib = nFluid * sizeof(PdfT) * N_D3Q19 / 1024.0 / 1024.0;
520 double latPadMib = (nCells - nFluid) * sizeof(PdfT) * N_D3Q19 / 1024.0 / 1024.0;
522 printf("# lattice size: %e MiB\n", latMiB);
523 printf("# fluid lattice size: %e MiB\n", latFluidMib);
524 printf("# lattice padding: %e MiB\n", latPadMib);
528 printf("# aligning lattices to: %d b\n", PAGE_4K);
530 MemAllocAligned((void **)&pdfs[0], sizeof(PdfT) * nCells * N_D3Q19, PAGE_4K);
532 kd->Pdfs[0] = pdfs[0];
534 // Initialize PDFs with some (arbitrary) data for correct NUMA placement.
535 // Here we touch only the fluid nodes as this loop is OpenMP parallel and
536 // we want the same scheduling as in the kernel.
538 #pragma omp parallel for
540 for (int i = 0; i < nFluid; ++i) { for(int d = 0; d < N_D3Q19; ++d) {
541 pdfs[0][P_INDEX_3(nCells, i, d)] = 1.0;
544 // Initialize all PDFs to some standard value.
545 for (int i = 0; i < nFluid; ++i) { for(int d = 0; d < N_D3Q19; ++d) {
546 pdfs[0][P_INDEX_3(nCells, i, d)] = 0.0;
549 // ----------------------------------------------------------------------
550 // create grid which will hold the index numbers of the fluid nodes
554 if (MemAlloc((void **)&grid, nTotalCells * sizeof(uint32_t))) {
555 printf("ERROR: allocating grid for numbering failed: %lu bytes.\n", nTotalCells * sizeof(uint32_t));
563 for(int z = 0; z < lZ; ++z) {
564 for(int y = 0; y < lY; ++y) {
565 for(int x = 0; x < lX; ++x) {
567 latticeIndex = L_INDEX_4(ld->Dims, x, y, z);
569 grid[latticeIndex] = ~0;
575 // ----------------------------------------------------------------------
576 // generate numbering over grid
580 if (MemAlloc((void **)&coords, nFluid * sizeof(uint32_t) * 3)) {
581 printf("ERROR: allocating coords array failed: %lu bytes.\n", nFluid * sizeof(uint32_t) * 3);
585 kdl->Coords = coords;
587 // Index for the PDF nodes can start at 0 as we distinguish solid and fluid nodes
588 // through the ld->Lattice array.
591 // Blocking is implemented via setup of the adjacency list. The kernel later will
592 // walk through the lattice blocked automatically.
593 for (int bZ = 0; bZ < lZ; bZ += blk[2]) {
594 for (int bY = 0; bY < lY; bY += blk[1]) {
595 for (int bX = 0; bX < lX; bX += blk[0]) {
597 int eX = MIN(bX + blk[0], lX);
598 int eY = MIN(bY + blk[1], lY);
599 int eZ = MIN(bZ + blk[2], lZ);
602 for (int z = bZ; z < eZ; ++z) {
603 for (int y = bY; y < eY; ++y) {
604 for (int x = bX; x < eX; ++x) {
606 latticeIndex = L_INDEX_4(lDims, x, y, z);
608 if (ld->Lattice[latticeIndex] != LAT_CELL_OBSTACLE) {
609 grid[latticeIndex] = counter;
611 coords[C_INDEX_X(counter)] = x;
612 coords[C_INDEX_Y(counter)] = y;
613 coords[C_INDEX_Z(counter)] = z;
620 Verify(counter == nFluid);
624 double indexMib = nFluid * sizeof(uint32_t) * N_D3Q19_IDX / 1024.0 / 1024.0;
626 printf("# index size: %e MiB\n", indexMib);
628 // AdjList only requires 18 instead of 19 entries per node, as
629 // the center PDF needs no addressing.
630 if (MemAlloc((void **)&adjList, nFluid * sizeof(uint32_t) * N_D3Q19_IDX)) {
631 printf("ERROR: allocating adjList array failed: %lu bytes.\n", nFluid * sizeof(uint32_t) * N_D3Q19_IDX);
635 kdl->AdjList = adjList;
639 uint32_t neighborIndex;
642 int nx, ny, nz, px, py, pz;
644 // Loop over all fluid nodes and compute the indices to the neighboring
645 // PDFs for configured data layout (AoS/SoA).
646 // TODO: Parallelized loop to ensure correct NUMA placement.
647 // #ifdef _OPENMP --> add line continuation
648 // #pragma omp parallel for default(none)
649 // shared(nFluid, nCells, coords, D3Q19_INV, D3Q19_X, D3Q19_Y, D3Q19_Z,
651 // lDims, grid, ld, lX, lY, lZ, adjList)
652 // private(x, y, z, nx, ny, nz, neighborIndex, dstIndex)
654 for (int index = 0; index < nFluid; ++index) {
655 x = coords[C_INDEX_X(index)];
656 y = coords[C_INDEX_Y(index)];
657 z = coords[C_INDEX_Z(index)];
659 Assert(x >= 0 && x < lX);
660 Assert(y >= 0 && y < lY);
661 Assert(z >= 0 && z < lZ);
663 Assert(ld->Lattice[L_INDEX_4(lDims, x, y, z)] != LAT_CELL_OBSTACLE);
665 // Loop over all directions except the center one.
666 for(int d = 0; d < N_D3Q19 - 1; ++d) {
667 Assert(d != D3Q19_C);
669 #ifdef PROP_MODEL_PUSH
674 #elif PROP_MODEL_PULL
679 #error No implementation for this PROP_MODEL_NAME.
681 // If the neighbor is outside the latcie in X direction and we have a
682 // periodic boundary then we need to wrap around.
683 if ( ((nx < 0 || nx >= lX) && ld->PeriodicX) ||
684 ((ny < 0 || ny >= lY) && ld->PeriodicY) ||
685 ((nz < 0 || nz >= lZ) && ld->PeriodicZ)
717 if (ld->Lattice[L_INDEX_4(lDims, px, py, pz)] == LAT_CELL_OBSTACLE) {
718 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
721 neighborIndex = grid[L_INDEX_4(lDims, px, py, pz)];
723 AssertMsg(neighborIndex != ~0, "Neighbor has no Index. (%d %d %d) direction %s (%d)\n", px, py, pz, D3Q19_NAMES[d], d);
725 dstIndex = P_INDEX_3(nCells, neighborIndex, d);
728 else if (nx < 0 || ny < 0 || nz < 0 || nx >= lX || ny >= lY || nz >= lZ) {
729 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
731 else if (ld->Lattice[L_INDEX_4(lDims, nx, ny, nz)] == LAT_CELL_OBSTACLE) {
732 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
735 neighborIndex = grid[L_INDEX_4(lDims, nx, ny, nz)];
737 Assert(neighborIndex != ~0);
739 dstIndex = P_INDEX_3(nCells, neighborIndex, d);
742 Assert(dstIndex >= 0);
743 Assert(dstIndex < nCells * N_D3Q19);
745 adjList[index * N_D3Q19_IDX + d] = dstIndex;
752 nThreads = omp_get_max_threads();
755 SetupConsecNodes(ld, KDLR(kd), nThreads);
757 double loopBalanceEven = 2.0 * 19 * sizeof(PdfT);
758 double loopBalanceOdd = 2.0 * 19 * sizeof(PdfT) + (double)kdlr->nConsecNodes / nFluid * (18 * 4.0 + 4.0);
759 double loopBalance = (loopBalanceEven + loopBalanceOdd) / 2.0;
761 printf("# loop balance: %.2f B/FLUP even: %.2f B/FLUP odd %.2f B/FLUP\n",
762 loopBalance, loopBalanceEven, loopBalanceOdd);
764 // Fill remaining KernelData structures
765 kd->GetNode = GetNode;
766 kd->SetNode = SetNode;
768 kd->BoundaryConditionsGetPdf = FNAME(BCGetPdf);
769 kd->BoundaryConditionsSetPdf = FNAME(BCSetPdf);
771 kd->Kernel = FNAME(D3Q19ListAaRiaKernel);
774 kd->PdfsActive = kd->Pdfs[0];
779 void FNAME(D3Q19ListAaRiaDeinit)(LatticeDesc * ld, KernelData ** kernelData)
781 KernelDataListRia ** kdlr = (KernelDataListRia **)kernelData;
783 MemFree((void **)&((*kdlr)->ConsecNodes));
785 if ((*kdlr)->ConsecThreadIndices != NULL) {
786 MemFree((void **)&((*kdlr)->ConsecThreadIndices));
789 if ((*kdlr)->FluidNodeThreadIndices != NULL) {
790 MemFree((void **)&((*kdlr)->FluidNodeThreadIndices));
793 KernelDataList ** kdl = (KernelDataList **)kernelData;
795 MemFree((void **)&((*kdl)->AdjList));
796 MemFree((void **)&((*kdl)->Coords));
797 MemFree((void **)&((*kdl)->Grid));
799 MemFree((void **)&((*kernelData)->Pdfs[0]));
801 MemFree((void **)kernelData);