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 "BenchKernelD3Q19ListCommon.h"
35 // Forward definition.
36 void FNAME(D3Q19ListKernel)(LatticeDesc * ld, struct KernelData_ * kd, CaseData * cd);
41 // -----------------------------------------------------------------------
42 // Functions which are used as callback by the kernel to read or write
45 static void FNAME(BCGetPdf)(KernelData * kd, int x, int y, int z, int dir, PdfT * pdf)
48 Assert(kd->PdfsActive != NULL);
49 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
52 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
53 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
54 Assert(dir >= 0); Assert(dir < N_D3Q19);
57 *pdf = kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)];
59 #ifdef PROP_MODEL_PUSH
60 *pdf = kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)];
64 // The relevant PDFs here are the ones, which will get streamed in later
65 // during propagation. So we must return the *remote* PDFs.
66 uint32_t nodeIndex = KDL(kd)->Grid[L_INDEX_4(kd->Dims, x, y, z)];
70 uint32_t adjListIndex = nodeIndex * N_D3Q19_IDX;
72 *pdf = kd->PdfsActive[KDL(kd)->AdjList[adjListIndex + dir]];
75 *pdf = kd->PdfsActive[P_INDEX_3(KDL(kd)->nCells, nodeIndex, 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]);
102 kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)] = pdf;
104 #ifdef PROP_MODEL_PUSH
105 kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)] = pdf;
106 #elif PROP_MODEL_PULL
108 // The relevant PDFs here are the ones, which will get streamed in later
109 // during propagation. So we must set this *remote* PDFs.
110 uint32_t nodeIndex = KDL(kd)->Grid[L_INDEX_4(kd->Dims, x, y, z)];
112 if (dir != D3Q19_C) {
114 uint32_t adjListIndex = nodeIndex * N_D3Q19_IDX;
116 kd->PdfsActive[KDL(kd)->AdjList[adjListIndex + dir]] = pdf;
119 kd->PdfsActive[P_INDEX_3(KDL(kd)->nCells, nodeIndex, dir)] = pdf;
129 static void GetNode(KernelData * kd, int x, int y, int z, PdfT * pdfs)
132 Assert(kd->PdfsActive != NULL);
133 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
134 Assert(pdfs != NULL);
135 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
136 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
140 // TODO: pull scheme?
141 #define I(x, y, z, dir) P_INDEX_5(KDL(kd), (x), (y), (z), (dir))
142 #define X(name, idx, idxinv, _x, _y, _z) pdfs[idx] = kd->PdfsActive[I(x, y, z, idx)]; sum += pdfs[idx];
148 // printf("%d %d %d negative density \n", x, y, z);
153 for (int d = 0; d < 19; ++d) {
154 if(isnan(pdfs[d]) || isinf(pdfs[d])) {
155 printf("%d %d %d %d nan! get node\n", x, y, z, d);
156 for (int d2 = 0; d2 < 19; ++d2) {
157 printf("%d: %e\n", d2, pdfs[d2]);
167 static void SetNode(KernelData * kd, int x, int y, int z, PdfT * pdfs)
170 Assert(kd->PdfsActive != NULL);
171 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
172 Assert(pdfs != NULL);
174 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
175 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
178 for (int d = 0; d < 19; ++d) {
180 printf("%d %d %d %d nan! get node\n", x, y, z, d);
181 for (int d2 = 0; d2 < 19; ++d2) {
182 printf("%d: %e\n", d2, pdfs[d2]);
189 // TODO: pull scheme?
190 #define I(x, y, z, dir) P_INDEX_5(KDL(kd), (x), (y), (z), (dir))
191 #define X(name, idx, idxinv, _x, _y, _z) kd->PdfsActive[I(x, y, z, idx)] = pdfs[idx];
199 static void ParameterUsage()
201 printf("Kernel parameters:\n");
202 printf(" [-blk <n>] [-blk-[xyz] <n>]\n");
207 static void ParseParameters(Parameters * params, int * blk)
211 blk[0] = 0; blk[1] = 0; blk[2] = 0;
213 #define ARG_IS(param) (!strcmp(params->KernelArgs[i], param))
214 #define NEXT_ARG_PRESENT() \
216 if (i + 1 >= params->nKernelArgs) { \
217 printf("ERROR: argument %s requires a parameter.\n", params->KernelArgs[i]); \
223 for (int i = 0; i < params->nKernelArgs; ++i) {
224 if (ARG_IS("-blk") || ARG_IS("--blk")) {
227 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
230 printf("ERROR: blocking parameter must be > 0.\n");
234 blk[0] = blk[1] = blk[2] = tmp;
236 else if (ARG_IS("-blk-x") || ARG_IS("--blk-x")) {
239 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
242 printf("ERROR: blocking parameter must be > 0.\n");
248 else if (ARG_IS("-blk-y") || ARG_IS("--blk-y")) {
251 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
254 printf("ERROR: blocking parameter must be > 0.\n");
260 else if (ARG_IS("-blk-z") || ARG_IS("--blk-z")) {
263 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
266 printf("ERROR: blocking parameter must be > 0.\n");
272 else if (ARG_IS("-h") || ARG_IS("-help") || ARG_IS("--help")) {
277 printf("ERROR: unknown kernel parameter.\n");
284 #undef NEXT_ARG_PRESENT
289 void FNAME(D3Q19ListInit)(LatticeDesc * ld, KernelData ** kernelData, Parameters * params)
292 KernelDataList * kdl;
293 MemAlloc((void **)&kdl, sizeof(KernelDataList));
295 kd = (KernelData *)kdl;
301 kd->PdfsActive = NULL;
307 kd->GlobalDims[0] = -1;
308 kd->GlobalDims[1] = -1;
309 kd->GlobalDims[2] = -1;
314 kd->ObstIndices = NULL;
315 kd->nObstIndices = -1;
316 kd->BounceBackPdfsSrc = NULL;
317 kd->BounceBackPdfsDst = NULL;
318 kd->nBounceBackPdfs = -1;
327 // Ajust the dimensions according to padding, if used.
328 kd->Dims[0] = kd->GlobalDims[0] = ld->Dims[0];
329 kd->Dims[1] = kd->GlobalDims[1] = ld->Dims[1];
330 kd->Dims[2] = kd->GlobalDims[2] = ld->Dims[2];
332 int * lDims = ld->Dims;
338 int nTotalCells = lX * lY * lZ;
339 int nCells = ld->nFluid; // TODO: + padding
340 int nFluid = ld->nFluid;
342 kdl->nCells = nCells;
343 kdl->nFluid = nFluid;
349 ParseParameters(params, blk);
351 if (blk[0] == 0) blk[0] = lX;
352 if (blk[1] == 0) blk[1] = lY;
353 if (blk[2] == 0) blk[2] = lZ;
355 printf("# blocking x: %3d y: %3d z: %3d\n", blk[0], blk[1], blk[2]);
357 printf("# allocating data for %d fluid LB nodes with padding (%lu bytes = %f MiB for both lattices)\n",
358 nCells, 2 * sizeof(PdfT) * nCells * N_D3Q19,
359 2 * sizeof(PdfT) * nCells * N_D3Q19 / 1024.0 / 1024.0);
361 MemAlloc((void **)&pdfs[0], sizeof(PdfT) * nCells * N_D3Q19);
362 MemAlloc((void **)&pdfs[1], sizeof(PdfT) * nCells * N_D3Q19);
364 kd->Pdfs[0] = pdfs[0];
365 kd->Pdfs[1] = pdfs[1];
367 // Initialize PDFs with some (arbitrary) data for correct NUMA placement.
368 // Here we touch only the fluid nodes as this loop is OpenMP parallel and
369 // we want the same scheduling as in the kernel.
371 #pragma omp parallel for
373 for (int i = 0; i < nFluid; ++i) { for(int d = 0; d < N_D3Q19; ++d) {
374 pdfs[0][P_INDEX_3(nCells, i, d)] = 1.0;
375 pdfs[1][P_INDEX_3(nCells, i, d)] = 1.0;
378 // Initialize all PDFs to some standard value.
379 for (int i = 0; i < nFluid; ++i) { for(int d = 0; d < N_D3Q19; ++d) {
380 pdfs[0][P_INDEX_3(nCells, i, d)] = 0.0;
381 pdfs[1][P_INDEX_3(nCells, i, d)] = 0.0;
384 // ----------------------------------------------------------------------
385 // create grid which will hold the index numbers of the fluid nodes
389 if (MemAlloc((void **)&grid, nTotalCells * sizeof(uint32_t))) {
390 printf("ERROR: allocating grid for numbering failed: %lu bytes.\n", nTotalCells * sizeof(uint32_t));
398 for(int z = 0; z < lZ; ++z) {
399 for(int y = 0; y < lY; ++y) {
400 for(int x = 0; x < lX; ++x) {
402 latticeIndex = L_INDEX_4(ld->Dims, x, y, z);
404 grid[latticeIndex] = ~0;
410 // ----------------------------------------------------------------------
411 // generate numbering over grid
415 if (MemAlloc((void **)&coords, nFluid * sizeof(uint32_t) * 3)) {
416 printf("ERROR: allocating coords array failed: %lu bytes.\n", nFluid * sizeof(uint32_t) * 3);
420 kdl->Coords = coords;
422 // Index for the PDF nodes can start at 0 as we distinguish solid and fluid nodes
423 // through the ld->Lattice array.
426 // Blocking is implemented via setup of the adjacency list. The kernel later will
427 // walk through the lattice blocked automatically.
428 for (int bZ = 0; bZ < lZ; bZ += blk[2]) {
429 for (int bY = 0; bY < lY; bY += blk[1]) {
430 for (int bX = 0; bX < lX; bX += blk[0]) {
432 int eX = MIN(bX + blk[0], lX);
433 int eY = MIN(bY + blk[1], lY);
434 int eZ = MIN(bZ + blk[2], lZ);
437 for (int z = bZ; z < eZ; ++z) {
438 for (int y = bY; y < eY; ++y) {
439 for (int x = bX; x < eX; ++x) {
441 latticeIndex = L_INDEX_4(lDims, x, y, z);
443 if (ld->Lattice[latticeIndex] != LAT_CELL_OBSTACLE) {
444 grid[latticeIndex] = counter;
446 coords[C_INDEX_X(counter)] = x;
447 coords[C_INDEX_Y(counter)] = y;
448 coords[C_INDEX_Z(counter)] = z;
455 Verify(counter == nFluid);
459 // AdjList only requires 18 instead of 19 entries per node, as
460 // the center PDF needs no addressing.
461 if (MemAlloc((void **)&adjList, nFluid * sizeof(uint32_t) * N_D3Q19_IDX)) {
462 printf("ERROR: allocating adjList array failed: %lu bytes.\n", nFluid * sizeof(uint32_t) * N_D3Q19_IDX);
466 kdl->AdjList = adjList;
470 uint32_t neighborIndex;
473 int nx, ny, nz, px, py, pz;
475 // Loop over all fluid nodes and compute the indices to the neighboring
476 // PDFs for configured data layout (AoS/SoA).
477 // TODO: Parallelized loop to ensure correct NUMA placement.
478 // #ifdef _OPENMP --> add line continuation
479 // #pragma omp parallel for default(none)
480 // shared(nFluid, nCells, coords, D3Q19_INV, D3Q19_X, D3Q19_Y, D3Q19_Z,
482 // lDims, grid, ld, lX, lY, lZ, adjList)
483 // private(x, y, z, nx, ny, nz, neighborIndex, dstIndex)
485 for (int index = 0; index < nFluid; ++index) {
486 x = coords[C_INDEX_X(index)];
487 y = coords[C_INDEX_Y(index)];
488 z = coords[C_INDEX_Z(index)];
490 Assert(x >= 0 && x < lX);
491 Assert(y >= 0 && y < lY);
492 Assert(z >= 0 && z < lZ);
494 Assert(ld->Lattice[L_INDEX_4(lDims, x, y, z)] != LAT_CELL_OBSTACLE);
496 // Loop over all directions except the center one.
497 for(int d = 0; d < N_D3Q19 - 1; ++d) {
498 Assert(d != D3Q19_C);
499 #ifdef PROP_MODEL_PUSH
503 #elif PROP_MODEL_PULL
508 #error No implementation for this PROP_MODEL_NAME.
510 // If the neighbor is outside the latcie in X direction and we have a
511 // periodic boundary then we need to wrap around.
512 if ( ((nx < 0 || nx >= lX) && ld->PeriodicX) ||
513 ((ny < 0 || ny >= lY) && ld->PeriodicY) ||
514 ((nz < 0 || nz >= lZ) && ld->PeriodicZ)
546 if (ld->Lattice[L_INDEX_4(lDims, px, py, pz)] == LAT_CELL_OBSTACLE) {
547 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
550 neighborIndex = grid[L_INDEX_4(lDims, px, py, pz)];
552 AssertMsg(neighborIndex != ~0, "Neighbor has no Index. (%d %d %d) direction %s (%d)\n", px, py, pz, D3Q19_NAMES[d], d);
554 dstIndex = P_INDEX_3(nCells, neighborIndex, d);
557 else if (nx < 0 || ny < 0 || nz < 0 || nx >= lX || ny >= lY || nz >= lZ) {
558 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
560 else if (ld->Lattice[L_INDEX_4(lDims, nx, ny, nz)] == LAT_CELL_OBSTACLE) {
561 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
564 neighborIndex = grid[L_INDEX_4(lDims, nx, ny, nz)];
566 Assert(neighborIndex != ~0);
568 dstIndex = P_INDEX_3(nCells, neighborIndex, d);
571 Assert(dstIndex >= 0);
572 Assert(dstIndex < nCells * N_D3Q19);
574 adjList[index * N_D3Q19_IDX + d] = dstIndex;
579 // Fill remaining KernelData structures
580 kd->GetNode = GetNode;
581 kd->SetNode = SetNode;
583 kd->BoundaryConditionsGetPdf = FNAME(BCGetPdf);
584 kd->BoundaryConditionsSetPdf = FNAME(BCSetPdf);
586 kd->Kernel = FNAME(D3Q19ListKernel);
589 kd->PdfsActive = kd->Pdfs[0];
594 void FNAME(D3Q19ListDeinit)(LatticeDesc * ld, KernelData ** kernelData)
596 KernelDataList ** kdl = (KernelDataList **)kernelData;
598 MemFree((void **)&((*kernelData)->Pdfs[0]));
599 MemFree((void **)&((*kernelData)->Pdfs[1]));
601 MemFree((void **)&((*kdl)->AdjList));
602 MemFree((void **)&((*kdl)->Coords));
603 MemFree((void **)&((*kdl)->Grid));
605 MemFree((void **)kernelData);