Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions benchmarks/scaling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# patchwork on a cluster: strong and weak scaling

`campaign.py` runs patchwork's benchmarks over many ranks and nodes and turns the results
into efficiency tables. The cases:

| Case | Driver | What is timed |
|---|---|---|
| B2 | `../amrex/bench_solve.py --mode nodal --ndim 3` | `NodalPoisson` on three levels: setup (numbering, assembly, AMG setup) and solve; peak memory |
| B4-2d, B4-3d | `../amrex/bench_mesh.py --mode halo` | `exchange_halos` (first call with the schedule, then repeated) and `restrict` on many fine patches |
| B6 | `../amrex/bench_particles.py` | markers: migrate, deposit, gather, sort (mpiparticles) |
| MHD | `bench_mhd.py` | a step of carafe's `MHD2D` (Orszag-Tang, three levels regridded on the current) and a regrid |

Strong scaling keeps the size of a case (the sizes in `CASES`); weak scaling grows the cells
per axis by `P^(1/d)` and the markers by `P`. A run is its own `srun` (or `mpiexec`) with a
time limit and prints one JSON line, which `campaign.py label` tags with the case, the
scaling, the ranks and the nodes and appends to the results file.

## On the cluster

Generate the scripts on the cluster (they `cd` to this directory), in an environment with
patchwork (`mpi`, `pyccel`, `petsc` extras), mpiparticles, carafe and slurm-script-generator:

```sh
python campaign.py jobs --nodes 1 2 4 8 --tasks-per-node 72 \
--cluster pitagora --account <account> \
--setup "source /path/to/max-models/.venv/bin/activate" --out jobs/
jobs/submit.sh # one job per node count
python campaign.py report results/scaling.jsonl > RESULTS.md
```

The one-node job runs 1, 2, 4, ... ranks up to the full node (the scaling inside a node);
the others run every case on all their cores. `--cluster` takes slurm-script-generator's
presets (partition and QOS); `--partition`, `--qos`, `--module` and `--time` set them by
hand. `--cases` and `--scaling` choose a subset.

## Here

```sh
python campaign.py local --ranks 1 2 4 --small --backend amg --out results/local.jsonl
python campaign.py report results/local.jsonl
```

runs the same commands with `mpiexec`, at small sizes (`--small`), to check the drivers and the
scripts before a cluster job (a few seconds per run). `--backend amg` uses pyamg for B2
where PETSc is not installed.

## What the results are for

They set the targets of the open performance steps of `plans/patchwork_plan.md` (todo
repo): 8n (the solver setup distributed over the ranks), what is left of 8k (halos and
restriction without a Python loop per patch), 8g (setup and memory of the solvers) and later
8i (GPU). A profile of one run shows where a case spends its time:

```sh
scope-profiler run --enable-tag patchwork --enable-tag carafe -o prof.h5 \
bench_mhd.py --n 128 --levels 2
scope-profiler inspect prof.h5 --sort total --top 15
```

One core, the quick case of carafe's `examples/plasmoid_reconnection.py` (25 patches,
18k leaf cells): HLLD in NumPy 44% of a step, `FluxRegister.update` (once per conserved
field) 26%, halos and restriction 13%; the refined run then costs 1.6-2x the uniform mesh
of the finest level per step, with 55% of its cells.
109 changes: 109 additions & 0 deletions benchmarks/scaling/bench_mhd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""One step of carafe's MHD on patches, for the scaling campaign (needs carafe).

The Orszag-Tang vortex on a periodic ``n x n`` base, regridded on the current density up to
``--levels`` refined levels (the tags of the initial state, so the mesh is the same on any
number of ranks), then timed: ``--steps`` steps of `carafe.MHD2D` (Heun, HLLD, constrained
transport of the potential on the nodes, refluxing), and one regrid. The phases of a step
come from scope-profiler's regions when the ``carafe`` and ``patchwork`` tags are on.

Run under ``mpiexec -n <ranks>``. Prints one JSON line (rank 0).
"""

from __future__ import annotations

import argparse
import json
import resource
import time

import numpy as np
from mpi4py import MPI

from patchwork import AMRHierarchy


def peak_mb() -> float:
"""Peak resident memory of the process (Linux: kB)."""
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024


def main() -> None:
"""Build the case, time the steps and a regrid, print the JSON line."""
from carafe.mhd import MHD2D

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--n", type=int, default=64)
parser.add_argument("--levels", type=int, default=2)
parser.add_argument("--steps", type=int, default=5)
parser.add_argument("--threshold", type=float, default=0.05)
parser.add_argument("--max-patch", type=int, default=32)
parser.add_argument("--placement", default="balanced")
args = parser.parse_args()
comm = MPI.COMM_WORLD
mesh = AMRHierarchy(
(args.n, args.n),
bounds=((0.0, 1.0), (0.0, 1.0)),
periodic=(True, True),
comm=comm,
placement=args.placement,
)
model = MHD2D(mesh, resistivity=1e-4)
k = 2.0 * np.pi
state = {
"density": 25.0 / (36.0 * np.pi),
"velocity": (lambda x, y: -np.sin(k * y), lambda x, y: np.sin(k * x), 0.0),
"pressure": 5.0 / (12.0 * np.pi),
"vector_potential": lambda x, y: (
(np.cos(2 * k * x) / (2 * k) + np.cos(k * y) / k) / np.sqrt(4 * np.pi)
),
}
model.set_state(**state)
for _ in range(args.levels):
model.regrid(
model.current_tags(args.threshold, levels=args.levels),
max_patch_size=args.max_patch,
)
model.set_state(**state)
dt = model.stable_step()
model.step(dt) # builds the halo schedules
times = []
for _ in range(args.steps):
comm.Barrier()
start = time.perf_counter()
model.step(dt)
comm.Barrier()
times.append(time.perf_counter() - start)
comm.Barrier()
start = time.perf_counter()
model.regrid(
model.current_tags(args.threshold, levels=args.levels),
max_patch_size=args.max_patch,
)
comm.Barrier()
regrid = time.perf_counter() - start
from patchwork import CompositeCells

leaf = CompositeCells(model.hierarchy).num_cells
peak = comm.allreduce(peak_mb())
if comm.rank == 0:
print(
json.dumps(
{
"code": "patchwork",
"mode": "mhd",
"ranks": comm.size,
"n_cell": args.n,
"nlevels": args.levels + 1,
"placement": args.placement,
"patches": len(model.hierarchy.patches),
"leaf_cells": int(leaf),
"step_s": float(np.median(times)),
"regrid_s": regrid,
"peak_mb": peak,
}
)
)


if __name__ == "__main__":
main()
Loading