plasma_plots.mpi
Plot on MPI rank 0 only.
A post-processing script started with mpirun -n 4 python script.py runs every plot on every
rank; without care, four processes draw the same figure and write the same files at once. Every
plotting function here draws on rank 0 and returns a SkippedPlot on the other ranks, so
the same script runs unchanged in serial and under MPI::
out.plot.scalars().save("scalars.png") # written once, by rank 0The rank is read without importing mpi4py, which would initialize MPI in a serial run: from
mpi4py.MPI.COMM_WORLD when the application has already initialized MPI, otherwise from the
per-rank variables that MPI launchers export. As in Struphy, STRUPHY_MPI=0 disables the
detection, and every process plots.
Nothing waits for rank 0: a rank that reads a file rank 0 writes must synchronize first, e.g.
with MPI.COMM_WORLD.Barrier().
Classes
| Name | Description |
|---|---|
SkippedPlot | What a plot returns on MPI ranks other than 0, where nothing is drawn. |
Functions
| Name | Description |
|---|---|
is_plotting_rank | Tell whether this process draws plots and writes their files. |
mpi_rank | Return this process' rank in MPI_COMM_WORLD, without initializing MPI. |
rank_zero | Decorate a plotting function so that it runs on the plotting rank only. |
SkippedPlotclass#
class SkippedPlot(name: str, rank: int)What a plot returns on MPI ranks other than 0, where nothing is drawn.
Any public attribute or call returns the same object, so plot(...).save(path),
plotter.show() or animation.save(path) run on every rank but act only on rank 0. It is
false and iterates as empty, like the (empty) list of files it wrote.
Parameters
Examples
>>> skipped = SkippedPlot("plot_slice", rank=1)>>> skipped.save("slice.png").fig is skipped # nothing is writtenTrue>>> list(skipped), bool(skipped)([], False)is_plotting_rankfunction#
def is_plotting_rank() -> boolTell whether this process draws plots and writes their files.
Returns
boolTrueon rank 0 of an MPI job and in any process outside one.
mpi_rankfunction#
def mpi_rank() -> intReturn this process’ rank in MPI_COMM_WORLD, without initializing MPI.
The rank comes from mpi4py if the application has already initialized MPI, otherwise from
the per-rank variables MPI launchers export. STRUPHY_MPI=0 makes every process rank 0.
Returns
int- The rank, or 0 outside an MPI job.
Examples
>>> mpi_rank() # in a serial run0rank_zerofunction#
def rank_zero(func)Decorate a plotting function so that it runs on the plotting rank only.
Parameters
| Name | Type | Description |
|---|---|---|
func | callable | A function that draws a figure or writes files. |
Returns
callablefuncon rank 0 and outside MPI; on other ranks a function that returns aSkippedPlotwithout callingfunc.