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"
36 // Forward definition.
37 void FNAME(D3Q19ListKernel)(LatticeDesc * ld, struct KernelData_ * kd, CaseData * cd);
42 // -----------------------------------------------------------------------
43 // Functions which are used as callback by the kernel to read or write
46 static void FNAME(BCGetPdf)(KernelData * kd, int x, int y, int z, int dir, PdfT * pdf)
49 Assert(kd->PdfsActive != NULL);
50 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
53 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
54 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
55 Assert(dir >= 0); Assert(dir < N_D3Q19);
58 *pdf = kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)];
60 #ifdef PROP_MODEL_PUSH
61 *pdf = kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)];
65 // The relevant PDFs here are the ones, which will get streamed in later
66 // during propagation. So we must return the *remote* PDFs.
67 uint32_t nodeIndex = KDL(kd)->Grid[L_INDEX_4(kd->Dims, x, y, z)];
71 uint32_t adjListIndex = nodeIndex * N_D3Q19_IDX;
73 *pdf = kd->PdfsActive[KDL(kd)->AdjList[adjListIndex + dir]];
76 *pdf = kd->PdfsActive[P_INDEX_3(KDL(kd)->nCells, nodeIndex, dir)];
85 static void FNAME(BCSetPdf)(KernelData * kd, int x, int y, int z, int dir, PdfT pdf)
88 Assert(kd->PdfsActive != NULL);
89 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
90 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
91 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
92 Assert(dir >= 0); Assert(dir < N_D3Q19);
96 printf("ERROR: setting nan %d %d %d %d %s\n", x, y, z, dir, D3Q19_NAMES[dir]);
103 kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)] = pdf;
105 #ifdef PROP_MODEL_PUSH
106 kd->PdfsActive[P_INDEX_5(KDL(kd), x, y, z, dir)] = pdf;
107 #elif PROP_MODEL_PULL
109 // The relevant PDFs here are the ones, which will get streamed in later
110 // during propagation. So we must set this *remote* PDFs.
111 uint32_t nodeIndex = KDL(kd)->Grid[L_INDEX_4(kd->Dims, x, y, z)];
113 if (dir != D3Q19_C) {
115 uint32_t adjListIndex = nodeIndex * N_D3Q19_IDX;
117 kd->PdfsActive[KDL(kd)->AdjList[adjListIndex + dir]] = pdf;
120 kd->PdfsActive[P_INDEX_3(KDL(kd)->nCells, nodeIndex, dir)] = pdf;
130 static void GetNode(KernelData * kd, int x, int y, int z, PdfT * pdfs)
133 Assert(kd->PdfsActive != NULL);
134 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
135 Assert(pdfs != NULL);
136 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
137 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
141 // TODO: pull scheme?
142 #define I(x, y, z, dir) P_INDEX_5(KDL(kd), (x), (y), (z), (dir))
143 #define X(name, idx, idxinv, _x, _y, _z) pdfs[idx] = kd->PdfsActive[I(x, y, z, idx)]; sum += pdfs[idx];
149 // printf("%d %d %d negative density \n", x, y, z);
154 for (int d = 0; d < 19; ++d) {
155 if(isnan(pdfs[d]) || isinf(pdfs[d])) {
156 printf("%d %d %d %d nan! get node\n", x, y, z, d);
157 for (int d2 = 0; d2 < 19; ++d2) {
158 printf("%d: %e\n", d2, pdfs[d2]);
168 static void SetNode(KernelData * kd, int x, int y, int z, PdfT * pdfs)
171 Assert(kd->PdfsActive != NULL);
172 Assert(kd->PdfsActive == kd->Pdfs[0] || kd->PdfsActive == kd->Pdfs[1]);
173 Assert(pdfs != NULL);
175 Assert(x >= 0); Assert(y >= 0); Assert(z >= 0);
176 Assert(x < kd->Dims[0]); Assert(y < kd->Dims[1]); Assert(z < kd->Dims[2]);
179 for (int d = 0; d < 19; ++d) {
181 printf("%d %d %d %d nan! get node\n", x, y, z, d);
182 for (int d2 = 0; d2 < 19; ++d2) {
183 printf("%d: %e\n", d2, pdfs[d2]);
190 // TODO: pull scheme?
191 #define I(x, y, z, dir) P_INDEX_5(KDL(kd), (x), (y), (z), (dir))
192 #define X(name, idx, idxinv, _x, _y, _z) kd->PdfsActive[I(x, y, z, idx)] = pdfs[idx];
200 static void ParameterUsage()
202 printf("Kernel parameters:\n");
203 printf(" [-blk <n>] [-blk-[xyz] <n>]\n");
204 #ifdef DATA_LAYOUT_SOA
205 printf(" [-pad auto|modulus_1+offset_1(,modulus_n+offset_n)*]\n");
211 static void ParseParameters(Parameters * params, int * blk, PadInfo ** padInfo)
215 blk[0] = 0; blk[1] = 0; blk[2] = 0;
218 #define ARG_IS(param) (!strcmp(params->KernelArgs[i], param))
219 #define NEXT_ARG_PRESENT() \
221 if (i + 1 >= params->nKernelArgs) { \
222 printf("ERROR: argument %s requires a parameter.\n", params->KernelArgs[i]); \
228 for (int i = 0; i < params->nKernelArgs; ++i) {
229 if (ARG_IS("-blk") || ARG_IS("--blk")) {
232 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
235 printf("ERROR: blocking parameter must be >= 0.\n");
239 blk[0] = blk[1] = blk[2] = tmp;
241 else if (ARG_IS("-blk-x") || ARG_IS("--blk-x")) {
244 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
247 printf("ERROR: blocking parameter must be >= 0.\n");
253 else if (ARG_IS("-blk-y") || ARG_IS("--blk-y")) {
256 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
259 printf("ERROR: blocking parameter must be >= 0.\n");
265 else if (ARG_IS("-blk-z") || ARG_IS("--blk-z")) {
268 int tmp = strtol(params->KernelArgs[++i], NULL, 0);
271 printf("ERROR: blocking parameter must be >= 0.\n");
277 else if (ARG_IS("-h") || ARG_IS("-help") || ARG_IS("--help")) {
281 #ifdef DATA_LAYOUT_SOA
282 else if (ARG_IS("-pad") || ARG_IS("--pad")) {
285 *padInfo = PadInfoFromStr(params->KernelArgs[++i]);
289 printf("ERROR: unknown kernel parameter.\n");
296 #undef NEXT_ARG_PRESENT
301 void FNAME(D3Q19ListInit)(LatticeDesc * ld, KernelData ** kernelData, Parameters * params)
304 KernelDataList * kdl;
305 MemAlloc((void **)&kdl, sizeof(KernelDataList));
307 kd = (KernelData *)kdl;
313 kd->PdfsActive = NULL;
319 kd->GlobalDims[0] = -1;
320 kd->GlobalDims[1] = -1;
321 kd->GlobalDims[2] = -1;
326 kd->ObstIndices = NULL;
327 kd->nObstIndices = -1;
328 kd->BounceBackPdfsSrc = NULL;
329 kd->BounceBackPdfsDst = NULL;
330 kd->nBounceBackPdfs = -1;
340 PadInfo * padInfo = NULL;
342 ParseParameters(params, blk, &padInfo);
345 // Ajust the dimensions according to padding, if used.
346 kd->Dims[0] = kd->GlobalDims[0] = ld->Dims[0];
347 kd->Dims[1] = kd->GlobalDims[1] = ld->Dims[1];
348 kd->Dims[2] = kd->GlobalDims[2] = ld->Dims[2];
350 int * lDims = ld->Dims;
356 int nTotalCells = lX * lY * lZ;
357 int nCells = ld->nFluid;
358 int nFluid = ld->nFluid;
360 #ifdef DATA_LAYOUT_SOA
362 nCells = PadCellsAndReport(nCells, sizeof(PdfT), &padInfo);
363 PadInfoFree(padInfo); padInfo = NULL;
367 kdl->nCells = nCells;
368 kdl->nFluid = nFluid;
372 if (blk[0] == 0) blk[0] = lX;
373 if (blk[1] == 0) blk[1] = lY;
374 if (blk[2] == 0) blk[2] = lZ;
376 printf("# blocking x: %3d y: %3d z: %3d\n", blk[0], blk[1], blk[2]);
378 printf("# allocating data for %d fluid LB nodes with padding (%lu bytes = %f MiB for both lattices)\n",
379 nCells, 2 * sizeof(PdfT) * nCells * N_D3Q19,
380 2 * sizeof(PdfT) * nCells * N_D3Q19 / 1024.0 / 1024.0);
382 MemAlloc((void **)&pdfs[0], sizeof(PdfT) * nCells * N_D3Q19);
383 MemAlloc((void **)&pdfs[1], sizeof(PdfT) * nCells * N_D3Q19);
385 kd->Pdfs[0] = pdfs[0];
386 kd->Pdfs[1] = pdfs[1];
388 // Initialize PDFs with some (arbitrary) data for correct NUMA placement.
389 // Here we touch only the fluid nodes as this loop is OpenMP parallel and
390 // we want the same scheduling as in the kernel.
392 #pragma omp parallel for
394 for (int i = 0; i < nFluid; ++i) { for(int d = 0; d < N_D3Q19; ++d) {
395 pdfs[0][P_INDEX_3(nCells, i, d)] = 1.0;
396 pdfs[1][P_INDEX_3(nCells, i, d)] = 1.0;
399 // Initialize all PDFs to some standard value.
400 for (int i = 0; i < nFluid; ++i) { for(int d = 0; d < N_D3Q19; ++d) {
401 pdfs[0][P_INDEX_3(nCells, i, d)] = 0.0;
402 pdfs[1][P_INDEX_3(nCells, i, d)] = 0.0;
405 // ----------------------------------------------------------------------
406 // create grid which will hold the index numbers of the fluid nodes
410 if (MemAlloc((void **)&grid, nTotalCells * sizeof(uint32_t))) {
411 printf("ERROR: allocating grid for numbering failed: %lu bytes.\n", nTotalCells * sizeof(uint32_t));
419 for(int z = 0; z < lZ; ++z) {
420 for(int y = 0; y < lY; ++y) {
421 for(int x = 0; x < lX; ++x) {
423 latticeIndex = L_INDEX_4(ld->Dims, x, y, z);
425 grid[latticeIndex] = ~0;
431 // ----------------------------------------------------------------------
432 // generate numbering over grid
436 if (MemAlloc((void **)&coords, nFluid * sizeof(uint32_t) * 3)) {
437 printf("ERROR: allocating coords array failed: %lu bytes.\n", nFluid * sizeof(uint32_t) * 3);
441 kdl->Coords = coords;
443 // Index for the PDF nodes can start at 0 as we distinguish solid and fluid nodes
444 // through the ld->Lattice array.
447 // Blocking is implemented via setup of the adjacency list. The kernel later will
448 // walk through the lattice blocked automatically.
449 for (int bX = 0; bX < lX; bX += blk[0]) {
450 for (int bY = 0; bY < lY; bY += blk[1]) {
451 for (int bZ = 0; bZ < lZ; bZ += blk[2]) {
453 int eX = MIN(bX + blk[0], lX);
454 int eY = MIN(bY + blk[1], lY);
455 int eZ = MIN(bZ + blk[2], lZ);
457 for (int x = bX; x < eX; ++x) {
458 for (int y = bY; y < eY; ++y) {
459 for (int z = bZ; z < eZ; ++z) {
461 latticeIndex = L_INDEX_4(lDims, x, y, z);
463 if (ld->Lattice[latticeIndex] != LAT_CELL_OBSTACLE) {
464 grid[latticeIndex] = counter;
466 coords[C_INDEX_X(counter)] = x;
467 coords[C_INDEX_Y(counter)] = y;
468 coords[C_INDEX_Z(counter)] = z;
475 Verify(counter == nFluid);
479 // AdjList only requires 18 instead of 19 entries per node, as
480 // the center PDF needs no addressing.
481 if (MemAlloc((void **)&adjList, nFluid * sizeof(uint32_t) * N_D3Q19_IDX)) {
482 printf("ERROR: allocating adjList array failed: %lu bytes.\n", nFluid * sizeof(uint32_t) * N_D3Q19_IDX);
486 kdl->AdjList = adjList;
490 uint32_t neighborIndex;
493 int nx, ny, nz, px, py, pz;
495 // Loop over all fluid nodes and compute the indices to the neighboring
496 // PDFs for configured data layout (AoS/SoA).
498 #pragma omp parallel for
500 for (int index = 0; index < nFluid; ++index) {
501 for (int d = 0; d < N_D3Q19_IDX; ++d) {
502 adjList[index * N_D3Q19_IDX + d] = -1;
506 // #ifdef _OPENMP --> add line continuation
507 // #pragma omp parallel for default(none)
508 // shared(nFluid, nCells, coords, D3Q19_INV, D3Q19_X, D3Q19_Y, D3Q19_Z,
510 // lDims, grid, ld, lX, lY, lZ, adjList)
511 // private(x, y, z, nx, ny, nz, neighborIndex, dstIndex)
513 for (int index = 0; index < nFluid; ++index) {
514 x = coords[C_INDEX_X(index)];
515 y = coords[C_INDEX_Y(index)];
516 z = coords[C_INDEX_Z(index)];
518 Assert(x >= 0 && x < lX);
519 Assert(y >= 0 && y < lY);
520 Assert(z >= 0 && z < lZ);
522 Assert(ld->Lattice[L_INDEX_4(lDims, x, y, z)] != LAT_CELL_OBSTACLE);
524 // Loop over all directions except the center one.
525 for(int d = 0; d < N_D3Q19 - 1; ++d) {
526 Assert(d != D3Q19_C);
527 #ifdef PROP_MODEL_PUSH
531 #elif PROP_MODEL_PULL
536 #error No implementation for this PROP_MODEL_NAME.
538 // If the neighbor is outside the latcie in X direction and we have a
539 // periodic boundary then we need to wrap around.
540 if ( ((nx < 0 || nx >= lX) && ld->PeriodicX) ||
541 ((ny < 0 || ny >= lY) && ld->PeriodicY) ||
542 ((nz < 0 || nz >= lZ) && ld->PeriodicZ)
574 if (ld->Lattice[L_INDEX_4(lDims, px, py, pz)] == LAT_CELL_OBSTACLE) {
575 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
578 neighborIndex = grid[L_INDEX_4(lDims, px, py, pz)];
580 AssertMsg(neighborIndex != ~0, "Neighbor has no Index. (%d %d %d) direction %s (%d)\n", px, py, pz, D3Q19_NAMES[d], d);
582 dstIndex = P_INDEX_3(nCells, neighborIndex, d);
585 else if (nx < 0 || ny < 0 || nz < 0 || nx >= lX || ny >= lY || nz >= lZ) {
586 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
588 else if (ld->Lattice[L_INDEX_4(lDims, nx, ny, nz)] == LAT_CELL_OBSTACLE) {
589 dstIndex = P_INDEX_3(nCells, index, D3Q19_INV[d]);
592 neighborIndex = grid[L_INDEX_4(lDims, nx, ny, nz)];
594 Assert(neighborIndex != ~0);
596 dstIndex = P_INDEX_3(nCells, neighborIndex, d);
599 Assert(dstIndex >= 0);
600 Assert(dstIndex < nCells * N_D3Q19);
602 adjList[index * N_D3Q19_IDX + d] = dstIndex;
607 // Fill remaining KernelData structures
608 kd->GetNode = GetNode;
609 kd->SetNode = SetNode;
611 kd->BoundaryConditionsGetPdf = FNAME(BCGetPdf);
612 kd->BoundaryConditionsSetPdf = FNAME(BCSetPdf);
614 kd->Kernel = FNAME(D3Q19ListKernel);
617 kd->PdfsActive = kd->Pdfs[0];
622 void FNAME(D3Q19ListDeinit)(LatticeDesc * ld, KernelData ** kernelData)
624 KernelDataList ** kdl = (KernelDataList **)kernelData;
626 MemFree((void **)&((*kernelData)->Pdfs[0]));
627 MemFree((void **)&((*kernelData)->Pdfs[1]));
629 MemFree((void **)&((*kdl)->AdjList));
630 MemFree((void **)&((*kdl)->Coords));
631 MemFree((void **)&((*kdl)->Grid));
633 MemFree((void **)kernelData);