93 auto hx = Kokkos::create_mirror_view(ox), hy = Kokkos::create_mirror_view(oy),
94 hz = Kokkos::create_mirror_view(oz), hb = Kokkos::create_mirror_view(b);
95 const IVec<3> gs = sc.gs;
96 double loc[2] = {0.0, 0.0}, glb[2];
97 for (
int pass = 0; pass < 2; ++pass) {
98 for (
int z = 0; z < ext.
z; ++z)
99 for (
int y = 0; y < ext.
y; ++y)
100 for (
int x = 0; x < ext.
x; ++x) {
101 const long i = (long)x + (
long)y * ext.
x + (long)z * (
long)ext.
x * ext.
y;
103 const int gx =
wrap(x -
G + og.x, (
int)gs[0]), gy = y -
G + og.y,
104 gz =
wrap(z -
G + og.z, (
int)gs[2]);
105 const double cx = gx + 0.5, cy = gy + 0.5, cz = gz + 0.5;
107 x >=
G && x < ext.
x -
G && y >=
G && y < ext.
y -
G && z >=
G && z < ext.
z -
G;
108 const bool fluid =
fluidCell(sc, cx, cy, cz);
109 const double f = std::sin(2.0 * M_PI * gx / gs[0]) * std::cos(M_PI * (gy + 0.5) / gs[1]) +
110 0.5 * std::sin(2.0 * M_PI * (gz + 0.3 * sc.step) / gs[2]) +
111 0.1 * (gy + 0.5) / gs[1];
113 hx(i) = sc.face(0, cx, cy, cz);
114 hy(i) = sc.face(1, cx, cy, cz);
115 hz(i) = sc.face(2, cx, cy, cz);
116 if (inner && fluid) {
121 hb(i) = (inner && fluid) ? f - glb[0] / glb[1] : 0.0;
125 MPI_Allreduce(loc, glb, 2, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
127 Kokkos::deep_copy(ox, hx);
128 Kokkos::deep_copy(oy, hy);
129 Kokkos::deep_copy(oz, hz);
130 Kokkos::deep_copy(b, hb);
149static Run
solveSteps(IVec<3> gs,
int nlev,
int force,
bool repart,
150 peclet::core::decomp::BlockDecomposer<3>* dec0,
int engine) {
154 mg.initMpi(gs[0], gs[1], gs[2], nlev, MPI_COMM_WORLD, dec0);
156 const C3 ext = l0.
ext, og = l0.
og;
157 const std::size_t n = (std::size_t)ext.
x * ext.
y * ext.
z;
158 CCField ox(
"ox", n), oy(
"oy", n), oz(
"oz", n), b(
"b", n), x(
"x", n), r(
"r", n), p(
"p", n),
159 z(
"z", n), Ap(
"Ap", n);
162 for (
int s = 0; s <
STEPS; ++s) {
166 Kokkos::deep_copy(x, 0.0);
167 res.iters.push_back(mg.
solvePCG(b, x, r, p, z, Ap, 300, 1e-10, 2, 2, 8));
171 res.x = Kokkos::create_mirror_view(x);
172 Kokkos::deep_copy(res.x, x);
180int main(
int argc,
char** argv) {
181 MPI_Init(&argc, &argv);
182 Kokkos::initialize(argc, argv);
183 int rank = 0, size = 1;
184 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
185 MPI_Comm_size(MPI_COMM_WORLD, &size);
187 constexpr bool host = std::is_same_v<peclet::flow::CCExec::memory_space, Kokkos::HostSpace>;
190 std::printf(
"SKIP: the distributed 'direct' bottom is host-only (device keeps GraphAMG)\n");
200 bool repart, weighted;
202 const Case cases[] = {
203 {
"A in place 64x48x32", {64, 48, 32}, 4, -1,
false,
false},
204 {
"B forced telescope@1 64x48x32", {64, 48, 32}, 4, 1,
false,
false},
205 {
"C repartition 48x32x32 (weighted, odd root)", {48, 32, 32}, 4, -1,
true,
true},
207 for (
const Case& c : cases) {
208 peclet::core::decomp::BlockDecomposer<3>* dec0 =
nullptr;
209 std::vector<double> w;
211 w.resize((std::size_t)c.gs[0] * c.gs[1] * c.gs[2]);
212 for (Index zz = 0; zz < c.gs[2]; ++zz)
213 for (Index yy = 0; yy < c.gs[1]; ++yy)
214 for (Index xx = 0; xx < c.gs[0]; ++xx)
215 w[(std::size_t)(xx + yy * c.gs[0] + zz * c.gs[0] * c.gs[1])] = xx < 3 ? 20.0 : 1.0;
216 dec0 =
new peclet::core::decomp::BlockDecomposer<3>((std::size_t)size, c.gs, w);
223 }
catch (
const std::exception& e) {
227 int bad = err.empty() ? 0 : 1, anyBad = 0;
228 MPI_Allreduce(&bad, &anyBad, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
231 std::printf(
" %-44s np=%d FAIL: %s\n", c.name, size,
232 err.empty() ?
"raised on another rank" : err.c_str());
237 int maxLv = 0, nBottom = 0;
238 MPI_Allreduce(&rd.localLevels, &maxLv, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
239 int mine = rd.localLevels == maxLv ? 1 : 0;
240 MPI_Allreduce(&mine, &nBottom, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
241 int stages[2] = {rd.teleCount, rd.repCount};
242 MPI_Bcast(stages, 2, MPI_INT, 0, MPI_COMM_WORLD);
243 int cg[3] = {rd.coarsest.x, rd.coarsest.y, rd.coarsest.z};
244 MPI_Bcast(
cg, 3, MPI_INT, 0, MPI_COMM_WORLD);
248 const C3 e = rd.ext, o = rd.og;
250 double loc[2] = {0.0, 0.0}, glb[2];
251 for (
int zz =
G; zz < e.z -
G; ++zz)
252 for (
int yy =
G; yy < e.y -
G; ++yy)
253 for (
int xx =
G; xx < e.x -
G; ++xx) {
254 if (!
fluidCell(sc, xx -
G + o.x + 0.5, yy -
G + o.y + 0.5, zz -
G + o.z + 0.5))
256 const long i = (long)xx + (
long)yy * e.x + (long)zz * (
long)e.x * e.y;
257 loc[0] = std::max(loc[0], std::fabs(rd.x(i) - ra.x(i)));
258 loc[1] = std::max(loc[1], std::fabs(ra.x(i)));
260 MPI_Allreduce(loc, glb, 2, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
261 const double rel = glb[0] / (glb[1] + 1e-300);
262 const bool sameIt = rd.iters == ra.iters;
265 placed = stages[0] >= 1 && nBottom < size;
267 placed = stages[1] >= 1;
268 const bool ok = sameIt && rel <= 1e-12 && placed;
273 " %-44s np=%d bottom %dx%dx%d on %d/%d rank(s), %d telescope / %d repartition "
274 "stage(s); iters direct/algebraic:",
275 c.name, size,
cg[0],
cg[1],
cg[2], nBottom, size, stages[0], stages[1]);
276 for (std::size_t s = 0; s < rd.iters.size(); ++s)
277 std::printf(
" %d/%d", rd.iters[s], ra.iters[s]);
278 std::printf(
" rel %.3e %s%s\n", rel, ok ?
"ok" :
"FAIL",
279 placed ?
"" :
" (the bottom is not below a telescope / repartition point)");
285 const IVec<3> gs{64, 64, 64};
290 lvls = r.localLevels;
291 }
catch (
const std::runtime_error& e) {
294 int raised = (err.find(
"not eligible here") != std::string::npos &&
295 err.find(
"192 cells") != std::string::npos)
299 MPI_Allreduce(&raised, &nRaised, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
300 const bool ok = nRaised == size;
304 std::printf(
" %-44s np=%d raised on %d/%d ranks%s %s\n",
305 "H ineligible 'direct' under a telescope", size, nRaised, size,
306 err.empty() ?
"" : (
" (\"" + err +
"\")").c_str(), ok ?
"ok" :
"FAIL");
311 MPI_Allreduce(&fail, &totalFail, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
314 std::printf(
"OK (np=%d): distributed 'direct' bottom == 'algebraic' (iterations, 1e-12)\n",
317 std::fprintf(stderr,
"FAILED (np=%d)\n", size);
321 return totalFail == 0 ? 0 : 1;
int solvePCG(CCField b, CCField x, CCField r, CCField p, CCField z, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star=nullptr, int nStar=0, C3 nnStar=C3{0, 0, 0})
static void cg(CCField x, CCConst b, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, Exch &&exchange, const Reduce &red, long gCells, int iters)