Skip to content

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 0

The 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

NameDescription
SkippedPlotWhat a plot returns on MPI ranks other than 0, where nothing is drawn.

Functions

NameDescription
is_plotting_rankTell whether this process draws plots and writes their files.
mpi_rankReturn this process' rank in MPI_COMM_WORLD, without initializing MPI.
rank_zeroDecorate 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

NameTypeDescription
namestrThe skipped plot function, shown by repr.
rankintThe rank that skipped it.

Examples

>>> skipped = SkippedPlot("plot_slice", rank=1)
>>> skipped.save("slice.png").fig is skipped # nothing is written
True
>>> list(skipped), bool(skipped)
([], False)

is_plotting_rankfunction#

def is_plotting_rank() -> bool

Tell whether this process draws plots and writes their files.

Returns

bool
True on rank 0 of an MPI job and in any process outside one.

mpi_rankfunction#

def mpi_rank() -> int

Return 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 run
0

rank_zerofunction#

def rank_zero(func)

Decorate a plotting function so that it runs on the plotting rank only.

Parameters

NameTypeDescription
funccallableA function that draws a figure or writes files.

Returns

callable
func on rank 0 and outside MPI; on other ranks a function that returns a SkippedPlot without calling func.