flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
check_decomposition.py
Go to the documentation of this file.
1#!/usr/bin/env python
2"""Inspect how a grid + rank count decomposes, and how deep the pressure multigrid gets on it.
3
4Answers, without spending any GPU time, the questions that decide distributed multigrid performance:
5
6 * how balanced is the ORB partition (max block / min block)?
7 * is a direction split that must not be (e.g. the wall-normal one in a channel)?
8 * how many multigrid levels does the hierarchy ACTUALLY reach, and where does each axis stop?
9 * does the coarse-first decomposition help here, and at what depth?
10
11Run it before committing a grid or a rank count to a large job. It needs only an MPI build of
12`flow` (the OpenMP one is fine, no GPU), and `mpirun --oversubscribe` happily runs more ranks than
13there are cores because nothing here is timed.
14
15 # one grid, several rank counts, auto vs the legacy aligned ORB vs coarse-first:
16 python check_decomposition.py --grid 480,80,160 --levels 6 --np 1,2,4,8,12,16,24 --mode 0,1,coarse
17
18 # a whole weak-scaling ladder, ORB only (no solver allocation -> any size, instantly):
19 python check_decomposition.py --grid 960,160,320 --np 1 --orb-only
20 python check_decomposition.py --grid 3072,512,1024 --np 32 --orb-only
21
22 # what the hierarchy looks like level by level:
23 python check_decomposition.py --grid 1508,240,503 --levels 5 --np 4 --verbose
24
25 # the ladder after diagnostics.rebalance_by_weights(w), and the alignment it picks:
26 python check_decomposition.py --grid 96,96,96 --levels 8 --np 4,8 --predict --weights w.npy
27
28`--orb-only` skips constructing a Solver, so it reports the partition (balance, splits) for grids far
29too large to allocate on a host — but it cannot report the achieved level count, which needs the real
30MG init. Without it, the grid must fit in host memory (a few hundred M cells at most).
31
32Requires PYTHONPATH to point at an MPI-enabled flow build, e.g.
33 PYTHONPATH=$PWD/build_mpi_omp python scripts/check_decomposition.py ...
34"""
35import argparse
36import os
37import re
38import subprocess
39import sys
40
41HERE = os.path.abspath(__file__)
42
43
44def halvings(d):
45 """How many times an axis can be halved: the multigrid's per-axis depth budget."""
46 n = 0
47 while d % 2 == 0 and d // 2 >= 2:
48 d //= 2
49 n += 1
50 return n
51
52
53# ---- child: runs under mpirun, prints one RESULT line ------------------------------------------
54def child(args):
55 from mpi4py import MPI
56
57 w = MPI.COMM_WORLD
58 import numpy as np
59
60 from peclet import flow
61
62 gx, gy, gz = args.grid
63 origin, size = flow.mpi_block(gx, gy, gz, levels=args.decomp_levels)
64 lnx, lny, lnz = size
65 cells = lnx * lny * lnz
66 hi = w.allreduce(cells, op=MPI.MAX)
67 lo = w.allreduce(cells, op=MPI.MIN)
68 split = [w.allreduce(1 if size[k] < (gx, gy, gz)[k] else 0, op=MPI.SUM) for k in range(3)]
69
70 if not args.orb_only:
71 # Constructing the Solver is what builds the MG hierarchy; with PECLET_FLOW_MG_DEBUG=1 it
72 # prints the level table, which the parent parses for the achieved depth.
73 s = flow.Solver(lnx, lny, lnz)
74 s.set_decomposition(args.decomp_levels) # must match the mpi_block() call above
75 s.init_mpi(gx, gy, gz)
76 s.set_rho(1.0)
77 s.set_mu(0.1)
78 s.set_dt(0.02)
79 s.set_advection(False)
80 s.set_pressure_multigrid(True, args.levels)
81 s.set_pressure_pcg(True, 200, 1e-6)
82 if args.walls:
83 s.set_domain_bc("-y", "wall")
84 s.set_domain_bc("+y", "wall")
85 s.set_pressure_geometry(np.asfortranarray(np.full((lnx, lny, lnz), 1e30)))
86
87 if w.rank == 0:
88 print(
89 f"RESULT np={w.size} block={lnx}x{lny}x{lnz} imbalance={hi / lo:.3f} "
90 f"split=({split[0]},{split[1]},{split[2]})",
91 flush=True,
92 )
93 MPI.Finalize()
94 os._exit(0)
95
96
97# ---- parent: spawn one mpirun per (mode, np) and tabulate ---------------------------------------
98MODES = {"0": ("auto", "0"), "auto": ("auto", "0"),
99 "1": ("aligned", "1"), "aligned": ("aligned", "1"), "legacy": ("aligned", "1")}
100
101
102def mode_spec(tok, levels):
103 """'0'/'auto' -> the default auto policy; '1'/'aligned'/'legacy' -> the legacy aligned ORB;
104 'coarse'/'coarse-first'/<int >= 2> -> coarse-first at that depth."""
105 if tok in MODES:
106 return MODES[tok]
107 if tok in ("coarse", "coarse-first", "cf"):
108 return ("coarse-first", str(levels))
109 return (f"coarse-first({tok})", tok)
110
111
112def predict(args):
113 """Pure-function pre-flight: the hierarchy init_mpi would build, per rank count, with and
114 without telescoping. No mpirun -- this is what makes np=1536 answerable on a laptop."""
115 from peclet import flow
116
117 gx, gy, gz = args.grid
118 for np_ in args.np:
119 for tele in (False, True):
120 if args.weights is not None:
121 # The hierarchy after diagnostics.rebalance_by_weights(w): its aligned weighted
122 # ORB replaces the set_decomposition partition, and the call returns the alignment.
123 rows, align = flow.predict_hierarchy(gx, gy, gz, np_, args.levels, tele,
124 weights=args.weights)
125 if not tele: # the partition, hence the alignment, is the same either way
126 print(f"\n{gx}x{gy}x{gz} np={np_}: rebalance_by_weights -> aligned weighted "
127 f"ORB, align {align} (a = {align.bit_length() - 1})")
128 else:
129 # --decomp-levels must reach predict_hierarchy: it is a pure function with no
130 # process state to read since flow ad917b1, so omitting it predicts the ALIGNED
131 # hierarchy.
132 rows = flow.predict_hierarchy(gx, gy, gz, np_, args.levels, tele,
133 decomposition_levels=args.decomp_levels)
134 cg = rows[-1][0]
135 print(f"\n{gx}x{gy}x{gz} np={np_} levels={args.levels} telescope={'ON' if tele else 'off'}"
136 f" -> {len(rows)} levels, coarsest {cg[0]}x{cg[1]}x{cg[2]}, "
137 f"max extent {max(cg)}, bottom on {rows[-1][1]} rank(s)")
138 for L, (g, ranks, blk, rat, t) in enumerate(rows):
139 print(f" L{L} global {g[0]:5d}x{g[1]:5d}x{g[2]:5d} ranks {ranks:5d} block0 "
140 f"{blk[0]:4d}x{blk[1]:4d}x{blk[2]:4d} ratio({rat[0]},{rat[1]},{rat[2]})"
141 f"{' -> TELESCOPE' if t else ''}")
142
143
144def main():
145 ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
146 ap.add_argument("--grid", required=True, help="GNX,GNY,GNZ")
147 ap.add_argument("--np", default="1", help="comma-separated rank counts")
148 ap.add_argument("--levels", type=int, default=5, help="multigrid levels requested (default 5)")
149 ap.add_argument("--mode", default="0",
150 help="comma-separated: 0/auto (default), 1/aligned/legacy, coarse, or a depth >= 2")
151 ap.add_argument("--walls", action="store_true", help="no-slip -y/+y (the channel's domain-BC MG path)")
152 ap.add_argument("--orb-only", action="store_true", help="partition only; no Solver, so any size")
153 ap.add_argument("--verbose", action="store_true", help="also print the per-level dims/ratios")
154 ap.add_argument("--mpirun", default="mpirun")
155 ap.add_argument("--predict", action="store_true",
156 help="use flow.predict_hierarchy (pure function, no mpirun, any np): print the "
157 "level ladder with and without coarse-level telescoping")
158 ap.add_argument("--weights", default=None,
159 help="with --predict: a .npy of per-cell weights (what "
160 "diagnostics.rebalance_by_weights takes: flat x-fastest, or an (nx,ny,nz) "
161 "array) -- predict the hierarchy AFTER that rebalance and log the "
162 "alignment its aligned weighted ORB picks")
163 ap.add_argument("--decomp-levels", type=int, default=0,
164 help="Solver.set_decomposition(levels) / flow.mpi_block(levels=): 0 = auto "
165 "(default), 1 = the legacy aligned ORB, >= 2 = coarse-first with that "
166 "depth")
167 ap.add_argument("--child", action="store_true", help=argparse.SUPPRESS)
168 args = ap.parse_args()
169 if args.weights is not None and not args.predict:
170 ap.error("--weights needs --predict")
171 if getattr(args, "predict", False):
172 if isinstance(args.grid, str):
173 args.grid = [int(v) for v in args.grid.split(",")]
174 if isinstance(args.np, str):
175 args.np = [int(v) for v in args.np.split(",")]
176 if args.weights is not None:
177 import numpy as np
178
179 w = np.load(args.weights)
180 if w.ndim == 3:
181 if tuple(w.shape) != tuple(args.grid):
182 sys.exit(f"--weights: shape {w.shape} is not the grid {tuple(args.grid)}")
183 w = w.ravel(order="F") # x-fastest, as rebalance_by_weights takes it
184 args.weights = np.asarray(w, dtype=np.float64).ravel().tolist()
185 # The weighted ORB replaces the set_decomposition partition, so --mode does not apply.
186 print("=== weighted: diagnostics.rebalance_by_weights(w) (--mode / --decomp-levels "
187 "ignored) ===")
188 predict(args)
189 return
190 # In --predict, --mode selects the depth (it can list several). --decomp-levels is the
191 # run-mode flag; honour it when the user gave it and left --mode at its default, rather
192 # than silently overwriting it below.
193 if args.decomp_levels and str(args.mode) == "0":
194 args.mode = str(args.decomp_levels)
195 for spec in (args.mode if isinstance(args.mode, list) else str(args.mode).split(",")):
196 label, lv = mode_spec(spec, args.levels)
197 args.decomp_levels = int(lv)
198 print(f"=== decomposition mode {label} (set_decomposition({int(lv)})) ===")
199 predict(args)
200 return
201 args.grid = [int(v) for v in args.grid.split(",")]
202 if args.child:
203 return child(args)
204
205 gx, gy, gz = args.grid
206 print(f"grid {gx}x{gy}x{gz} = {gx * gy * gz / 1e6:.1f} Mcells "
207 f"halvings x/y/z = {halvings(gx)}/{halvings(gy)}/{halvings(gz)}"
208 f"{' <-- an axis with 0 halvings NEVER coarsens' if 0 in (halvings(gx), halvings(gy), halvings(gz)) else ''}")
209 print(f"multigrid levels requested: {args.levels}\n")
210 print(f"{'mode':<20} {'np':>4} {'block':>18} {'imbalance':>10} {'split x,y,z':>12} {'levels':>7}")
211
212 for tok in args.mode.split(","):
213 label, decomp = mode_spec(tok.strip(), args.levels)
214 for np_ in [int(v) for v in args.np.split(",")]:
215 env = dict(os.environ, PECLET_FLOW_MG_DEBUG="1")
216 cmd = [args.mpirun, "--oversubscribe", "-np", str(np_), sys.executable, HERE, "--child",
217 "--grid", ",".join(map(str, args.grid)), "--levels", str(args.levels),
218 "--decomp-levels", str(decomp)]
219 if args.walls:
220 cmd.append("--walls")
221 if args.orb_only:
222 cmd.append("--orb-only")
223 try:
224 out = subprocess.run(cmd, env=env, capture_output=True, text=True, timeout=900).stdout
225 except subprocess.TimeoutExpired:
226 print(f"{label:<20} {np_:>4} TIMED OUT")
227 continue
228 res = re.search(r"RESULT np=(\d+) block=(\S+) imbalance=(\S+) split=\‍((\S+)\‍)", out)
229 if not res:
230 print(f"{label:<20} {np_:>4} FAILED (rerun by hand for the error)")
231 continue
232 lev = re.search(r"-> (\d+) levels", out)
233 cf = re.search(r"coarse-first depth (\d+) \‍(refine (\S+),", out)
234 note = f" [coarse-first depth {cf.group(1)}, refine {cf.group(2)}]" if cf else ""
235 print(f"{label:<20} {res.group(1):>4} {res.group(2):>18} {res.group(3):>10} "
236 f"{res.group(4):>12} {lev.group(1) if lev else '-':>7}{note}")
237 if args.verbose:
238 for line in out.splitlines():
239 if line.startswith("[mg] L"):
240 print(" " + line)
241 print("\nreminders: an ODD axis never coarsens (measured 3.2x slower on one GPU);")
242 print(" in MPI a level coarsens an axis only if EVERY rank's block is even on it,")
243 print(" so the achievable depth is set by the per-rank block, not the global grid.")
244
245
246if __name__ == "__main__":
247 main()