8#ifndef PECLET_FLOW_FLOW_IBM_MPI_HPP
9#define PECLET_FLOW_FLOW_IBM_MPI_HPP
11#if defined(PECLET_FLOW_MPI) && defined(__linux__) && defined(KOKKOS_ENABLE_OPENMP)
26inline int hostL3PlacementStatus() {
27#if defined(__linux__) && defined(KOKKOS_ENABLE_OPENMP)
28 if constexpr (!std::is_same_v<Kokkos::DefaultExecutionSpace, Kokkos::OpenMP>) {
31 const int T = Kokkos::OpenMP().concurrency();
32 std::vector<int> cpu(T, -1);
33#pragma omp parallel num_threads(T)
37 if (sched_getaffinity(0,
sizeof m, &m) == 0 && CPU_COUNT(&m) == 1)
38 for (
int c = 0;
c < CPU_SETSIZE; ++
c)
39 if (CPU_ISSET(c, &m)) {
40 cpu[omp_get_thread_num()] =
c;
49 std::ifstream
f(
"/sys/devices/system/cpu/cpu" + std::to_string(c) +
"/cache/index3/id");
67inline void warnL3Placement(MPI_Comm comm) {
68 static bool printed =
false;
70 MPI_Comm_rank(comm, &rank);
71 MPI_Comm_size(comm, &size);
74 const int mine = hostL3PlacementStatus();
75 std::vector<int>
all(rank == 0 ? size : 0);
76 MPI_Gather(&mine, 1, MPI_INT,
all.data(), 1, MPI_INT, 0, comm);
77 if (rank != 0 || printed)
81 for (
int r = 0;
r <
size; ++
r)
84 list += (
n ?
"," :
"") + std::to_string(r);
93 "peclet.flow: rank%s %s run%s OpenMP threads on more than one L3 cache domain "
94 "(measured up to +18 %% step time); use threads per rank dividing the cores per L3 "
95 "domain (AMD EPYC Zen 4: 8), ideally one rank per domain.\n",
96 n > 1 ?
"s" :
"", list.c_str(), n > 1 ?
"" :
"s");
104 MPI_Comm_size(comm, &size);
105 detail::warnL3Placement(comm);
106 weightedDec_ =
false;
111 initMpi(CutcellMG::decomposition(
static_cast<std::size_t
>(size), gnx, gny, gnz, decompLevels_,
112 decompMaxImbalance_),
128 if (geometryBuilt_ && !distributed_)
129 throw std::runtime_error(
130 "init_mpi: call BEFORE the geometry (set_solid / set_pressure_geometry / "
131 "set_solid_from_scene) -- the geometry builds the pressure and velocity multigrids for the "
132 "partition in force when it runs, so a geometry built first gives every rank an "
133 "independent, block-periodic pressure solve (a wrong answer at np > 1, with no error)");
140 int csize = 1, crank = 0;
141 MPI_Comm_size(comm, &csize);
142 MPI_Comm_rank(comm, &crank);
143 bool bad = dec.numBlocks() !=
static_cast<std::size_t
>(csize);
145 const auto b = dec.block(
static_cast<std::size_t
>(crank));
146 bad = (
int)
b.size[0] != nx_ || (
int)
b.size[1] != ny_ || (
int)
b.size[2] != nz_;
148 const std::uint64_t fp = CutcellMG::fingerprint(dec);
149 std::uint64_t loc[3] = {bad ? 1ull : 0ull, fp, ~fp},
all[3] = {0, 0, 0};
150 MPI_Allreduce(loc, all, 3, MPI_UINT64_T, MPI_MAX, comm);
151 if (all[0] != 0 || all[1] != ~all[2])
152 throw std::runtime_error(
154 ?
"init_mpi: the solver's local block does not match the decomposition on at least "
155 "one rank -- size Solver(*size) from flow.mpi_block(gnx, gny, gnz, levels=, "
156 "max_imbalance=) with the SAME values given to set_decomposition (C++: "
157 "CutcellMG::decomposition(np, gnx, gny, gnz, levels, max_imbalance).block(rank))"
158 :
"init_mpi: the ranks hold different decompositions -- every rank must pass the "
159 "same set_decomposition(levels, max_imbalance) and flow.mpi_block(...) values");
165 const auto&
gs = dec.globalSize();
170 MPI_Comm_rank(comm, &rank);
171 std::array<bool, 3> per{
true,
true,
true};
172 velHalo_ = std::make_shared<GridHaloTopology<3>>();
173 velHalo_->buildTopology(dec, rank,
G, per, comm);
174 velDev_ = std::make_shared<GridHalo<double>>();
175 velDev_->setLabel(
"velocity g2");
176 velDev_->init(*velHalo_);
181 velDevF_ = std::make_shared<GridHalo<MReal>>();
182 velDevF_->init(*velHalo_);
185 long minExt = std::numeric_limits<long>::max();
186 for (
const auto& s : dec.sizes())
187 for (
int k = 0;
k < 3; ++
k)
188 minExt = std::min(minExt, (
long)s[k]);
191 bool oddPeriodic =
false;
192 for (
int a = 0;
a < 3; ++
a)
193 oddPeriodic = oddPeriodic || (per[a] && gs[a] % 2 != 0);
194 caMomentum_ = (caMode_ &
kCaMomentum) && minExt >= 4 && !oddPeriodic;
195 for (
bool& d : momStencilDirty_)
197 dec_ = std::make_shared<peclet::core::decomp::BlockDecomposer<3>>(dec);
198 const auto oig = velHalo_->indexer().originInclGhost();
199 og_ = {(
int)oig[0] +
G, (
int)oig[1] +
G, (
int)oig[2] +
G};
211 if (!distributed_ || !dec_)
214 MPI_Comm_rank(comm_, &rank);
215 const auto ob = dec_->block(rank),
nb = newDec.block(rank);
216 const int oex = (
int)
ob.size[0] + 2 *
G, oey = (
int)
ob.size[1] + 2 *
G,
217 oez = (
int)
ob.size[2] + 2 *
G;
218 const int nex = (
int)
nb.size[0] + 2 *
G, ney = (
int)
nb.size[1] + 2 *
G,
219 nez = (
int)
nb.size[2] + 2 *
G;
222 const auto names = fields_.names();
223 std::vector<std::vector<double>> oldHost(
names.size()), newHost(
names.size());
224 for (std::size_t k = 0;
k <
names.size(); ++
k) {
225 CCField f = fields_.at(names[k]).data;
226 auto h = Kokkos::create_mirror_view(f);
227 Kokkos::deep_copy(h, f);
228 oldHost[
k].assign(
h.data(),
h.data() + (std::size_t)oex * oey * oez);
229 newHost[
k].assign((std::size_t)nex * ney * nez, 0.0);
232 std::vector<const double*>
op(
names.size());
233 std::vector<double*> np(
names.size());
234 for (std::size_t k = 0;
k <
names.size(); ++
k) {
235 op[
k] = oldHost[
k].data();
236 np[
k] = newHost[
k].data();
243 int warmLocal = mg_.eigenWarmReady() ? 1 : 0, warmAll = 0;
244 MPI_Allreduce(&warmLocal, &warmAll, 1, MPI_INT, MPI_MIN, comm_);
245 std::vector<double> eigOld[2], eigNew[2];
247 const CCField src[2] = {mg_.eigenWarmMax(), mg_.eigenWarmMin()};
248 const int o1x = (
int)
ob.size[0] + 2, o1y = (
int)
ob.size[1] + 2;
249 for (
int v = 0;
v < 2; ++
v) {
250 auto h = Kokkos::create_mirror_view(src[v]);
251 Kokkos::deep_copy(h, src[v]);
252 eigOld[
v].assign((std::size_t)oex * oey * oez, 0.0);
253 eigNew[
v].assign((std::size_t)nex * ney * nez, 0.0);
254 for (
int z = 0;
z < (
int)
ob.size[2]; ++
z)
255 for (
int y = 0;
y < (
int)
ob.size[1]; ++
y)
256 for (
int x = 0;
x < (
int)
ob.size[0]; ++
x)
257 eigOld[v][(std::size_t)(
x +
G) + (std::size_t)(
y +
G) * oex +
258 (std::size_t)(
z +
G) * oex * oey] =
259 h((std::size_t)(x + 1) + (std::size_t)(y + 1) * o1x +
260 (std::size_t)(z + 1) * o1x * o1y);
261 op.push_back(eigOld[v].data());
262 np.push_back(eigNew[v].data());
265 peclet::core::decomp::redistributeGridFields<double>(*dec_, newDec, rank,
G, op, np, comm_);
272 allocateBlock((
int)
nb.size[0], (
int)
nb.size[1], (
int)
nb.size[2]);
274 initMpi(newDec, comm_);
276 auto scatterPadded = [&](
const std::string& name,
const std::vector<double>& src) {
278 auto h = Kokkos::create_mirror_view(f);
279 std::memcpy(
h.data(), src.data(),
sizeof(
double) * (std::size_t)nex * ney * nez);
280 Kokkos::deep_copy(f, h);
283 for (std::size_t k = 0;
k <
names.size(); ++
k)
284 scatterPadded(names[k], newHost[k]);
292 const bool keepBfpBounds = bfpChebSet_;
293 setSolid(gatherInner(sdf_), cutcellPressure_);
294 bfpChebSet_ = keepBfpBounds;
296 for (std::size_t k = 0;
k <
names.size(); ++
k)
297 if (names[k] !=
"sdf")
298 scatterPadded(names[k], newHost[k]);
302 const int n1x = (
int)
nb.size[0] + 2, n1y = (
int)
nb.size[1] + 2, n1z = (
int)
nb.size[2] + 2;
304 for (
int v = 0;
v < 2; ++
v) {
305 dst[
v] =
CCField(v == 0 ?
"ev_vmax" :
"ev_vmin", (std::size_t)n1x * n1y * n1z);
306 auto h = Kokkos::create_mirror_view(dst[v]);
307 Kokkos::deep_copy(h, 0.0);
308 for (
int z = 0;
z < (
int)
nb.size[2]; ++
z)
309 for (
int y = 0;
y < (
int)
nb.size[1]; ++
y)
310 for (
int x = 0;
x < (
int)
nb.size[0]; ++
x)
311 h((std::size_t)(x + 1) + (std::size_t)(y + 1) * n1x +
312 (std::size_t)(z + 1) * n1x * n1y) =
313 eigNew[
v][(std::size_t)(x +
G) + (std::size_t)(y +
G) * nex +
314 (std::size_t)(z +
G) * nex * ney];
315 Kokkos::deep_copy(dst[v], h);
317 mg_.setEigenWarm(dst[0], dst[1]);
326 for (std::size_t k = 0;
k <
names.size(); ++
k)
327 fillGhosts(fields_.at(names[k]).data);
328 for (
auto& sc : scalars_)
339 if (epsPrev_.extent(0) == n_ && epsField_.extent(0) == n_)
340 Kokkos::deep_copy(epsPrev_, epsField_);
350 seedFaceFieldFromCells();
358 MPI_Comm_size(comm_, &size);
359 MPI_Comm_rank(comm_, &rank);
369 const auto c = peclet::core::decomp::chooseAlignedWeighted(
370 (std::size_t)size, peclet::core::IVec<3>{gnx_, gny_, gnz_},
w);
373 "[mg] rebalanceByWeights: aligned weighted ORB a = %d (align %d), weight imbalance "
375 c.a, 1 <<
c.a, (
double)
c.imbalance);
385 return mg_.builtPlan();
390void Solver<Grid>::resizeForBlock() {
391 reallocOwnedFields();
392 rebindFieldAliases();
393 resizeBlockScratch();
394 for (
int f = 0;
f < 6; ++
f)
395 resampleBcProfile(f);
399void Solver<Grid>::reallocOwnedFields() {
400 for (
const auto& nm : fields_.names()) {
401 auto& rec = fields_.at(nm);
402 if (!rec.ownStorage || rec.data.extent(0) == n_)
409void Solver<Grid>::rebindFieldAliases() {
410 auto bind = [
this](
CCField&
f,
const std::string& nm) {
412 f = fields_.at(nm).data;
415 bind(kappaField_,
"kappa");
416 bind(kappaBranch_,
"kappa_branch");
417 bind(rhoField_,
"rho");
418 bind(Pb_,
"p_balanced");
419 bind(muField_,
"mu");
420 bind(epsField_,
"eps");
421 bind(dragBeta_,
"drag_beta");
422 bind(pcMdot_,
"mdot");
423 bind(pcSrc_,
"pc_source");
424 bind(pcUser_,
"div_source");
425 bind(pcSink_,
"div_sink");
426 static const char*
fn[3] = {
"force_x",
"force_y",
"force_z"};
427 for (
int c = 0;
c < 3; ++
c)
428 bind(cellForce_[c], fn[c]);
429 for (
auto& sc : scalars_) {
431 if (
sc.cut &&
sc.cut->solid.extent(0) > 0)
432 bind(
sc.cut->solid,
sc.name +
"_solid");
434 for (
auto& cl : closures_) {
435 if (fields_.has(cl.outName))
436 cl.out = fields_.at(cl.outName).data;
437 if (fields_.has(cl.in0Name))
438 cl.in0 =
CCConst(fields_.at(cl.in0Name).data);
439 if (!cl.in1Name.empty() && fields_.has(cl.in1Name))
440 cl.in1 =
CCConst(fields_.at(cl.in1Name).data);
445void Solver<Grid>::resizeIfAllocated(
CCField& f,
const char* label, std::size_t n) {
446 if (
f.extent(0) != 0 &&
f.extent(0) != n)
451void Solver<Grid>::resizeBlockScratch() {
452 const std::size_t
n = n_, n1 = n1_;
454 resizeIfAllocated(velRes_,
"velRes", n);
455 resizeIfAllocated(zp1_,
"zp1", n1);
456 resizeIfAllocated(vmgTheta_,
"vmgTheta", n);
457 resizeIfAllocated(vmgClean_,
"vmgClean", n);
458 for (
int c = 0;
c < 3; ++
c) {
459 resizeIfAllocated(uStar_[c],
"uStar", n);
460 resizeIfAllocated(advRhs_[c],
"advRhs", n);
461 resizeIfAllocated(uBc_[c],
"uBc", n);
462 resizeIfAllocated(uwCell_[c],
"uwCell", n);
463 resizeIfAllocated(uwAdv_[c],
"uwAdv", n);
464 resizeIfAllocated(faceAcc_[c],
"faceAcc", n);
466 resizeIfAllocated(pb1_,
"pb1",
471 resizeIfAllocated(oxb_,
"oxb", n);
472 resizeIfAllocated(oyb_,
"oyb", n);
473 resizeIfAllocated(ozb_,
"ozb", n);
474 resizeIfAllocated(sdfGp_,
"peclet::flow::sdfGp", n);
475 resizeIfAllocated(gpRh_,
"gpRh", n1);
476 resizeIfAllocated(gpT_,
"gpT", n1);
477 resizeIfAllocated(gpZ2_,
"gpZ2", n1);
478 resizeIfAllocated(gpX2_,
"gpXg2", n);
480 resizeIfAllocated(vofCs_,
"vofCs", n);
481 resizeIfAllocated(vofSolidG2_,
"vofSolidG2", n);
482 for (
int c = 0;
c < 3; ++
c) {
483 resizeIfAllocated(uAdv_[c],
"uAdv", n);
484 resizeIfAllocated(vofDynVel_[c],
"vofDynVel", n);
485 resizeIfAllocated(csfBlkF_[c],
"vof::blockcsf", n);
488 resizeIfAllocated(pcArea_,
"pc_area", n);
489 for (
int c = 0;
c < 3; ++
c) {
490 resizeIfAllocated(pcNrm_[c],
"pc_nrm", n);
491 resizeIfAllocated(pcGn_[c],
"pc_gn", n);
493 resizeIfAllocated(pcDep_,
"pc_dep", n);
494 resizeIfAllocated(pcTgt_,
"pc_tgt", n);
495 resizeIfAllocated(pcCnew_,
"pc_cnew", n);
496 resizeIfAllocated(pcDefic_,
"pc_defic", n);
497 resizeIfAllocated(pcKcell_,
"pc_k", n);
498 resizeIfAllocated(pcRcp_,
"pc_rcp", n);
499 resizeIfAllocated(pcClsPrev_,
"pc_cls_prev", n);
500 resizeIfAllocated(pcCarrySrc_,
"pc_carry_src", n);
501 resizeIfAllocated(pcMdotFit_,
"pc_mdot_fit", n);
502 resizeIfAllocated(pcKappa_,
"pc_kappa", n);
503 resizeIfAllocated(pcAreaCg2_,
"pc_area_cascade", n);
504 resizeIfAllocated(pcTgam_,
"pc_tgam", n);
505 resizeIfAllocated(pcGphi_,
"pc_gphi", n);
507 pcMaskFresh_ =
false;
516 sharpActive_ =
false;
521 for (
int a = 0;
a < 3; ++
a)
528 for (
auto& sc : scalars_) {
529 resizeIfAllocated(
sc.cOld,
"scalar_old", n);
530 resizeIfAllocated(
sc.b,
"scalar_b", n);
531 resizeIfAllocated(
sc.AC,
"scalar_AC", n);
532 resizeIfAllocated(
sc.AW,
"scalar_AW", n);
533 resizeIfAllocated(
sc.AE,
"scalar_AE", n);
534 resizeIfAllocated(
sc.AS,
"scalar_AS", n);
535 resizeIfAllocated(
sc.AN,
"scalar_AN", n);
536 resizeIfAllocated(
sc.AB,
"scalar_AB", n);
537 resizeIfAllocated(
sc.AT,
"scalar_AT", n);
538 resizeIfAllocated(
sc.dmask,
"scalar_dmask", n);
539 resizeIfAllocated(
sc.dval,
"scalar_dval", n);
540 resizeIfAllocated(
sc.gfmB,
"scalar_gfmb", n);
541 sc.stencilBuilt =
false;
547 if (pcEnergy_ && !pcTName_.empty() && hasScalar(pcTName_)) {
553 resizeIfAllocated(rho1_,
"rho1", n1);
554 resizeIfAllocated(cx1_,
"cx1", n1);
555 resizeIfAllocated(cy1_,
"cy1", n1);
556 resizeIfAllocated(cz1_,
"cz1", n1);
557 resizeIfAllocated(eps1_,
"eps1", n1);
558 resizeIfAllocated(beta1_,
"beta1", n1);
559 resizeIfAllocated(depsdt_,
"depsdt", n);
560 resizeIfAllocated(divAdv_,
"divAdv", n);
561 resizeIfAllocated(epsRho_,
"epsRho", n);
562 resizeIfAllocated(epsPrev_,
"epsPrev", n);
563 for (
int c = 0;
c < 3; ++
c)
564 for (
int k = 0;
k < 3; ++
k)
565 if (tEx_[c][k].extent(0) != 0 && tEx_[c][k].extent(0) != (std::size_t)nx_ * ny_ * nz_) {
567 hasExactCross_ =
false;
Kokkos::View< std::uint8_t *, CCMem > SvByte
Per-cell byte fields on the solver block: the band class and the fit-fallback flag.
Kokkos::View< long *, CCMem > SvList
The compacted interfacial cells of a fit (local linear indices), the device fit's worklist.
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
The band geometry and the closure constants, by value into every kernel.