Parsl
The previous page wrote the pipeline of this section in Nextflow. With Parsl, it is a Python program. Each step is a Python function, decorated as an app, and calling an app submits it to a worker and returns a future, as an executor of concurrent.futures does. The dependencies are the futures passed from one app to the next.
The program
#!/usr/bin/env python3
"""The volume of the unit ball in d dimensions, with Parsl: for each d, a pilot
batch, a plan that reads it, as many batches as the plan says, and a reduce;
then the table, and its plot.
python ball_parsl.py [DMAX]
"""
import os
import sys
import parsl
from parsl import join_app, python_app
from parsl.config import Config
from parsl.dataflow.memoization import BasicMemoizer
from parsl.executors import HighThroughputExecutor
from parsl.monitoring import MonitoringHub
from parsl.providers import LocalProvider
import ball
DMAX = int(sys.argv[1]) if len(sys.argv) > 1 else 12
WORKERS = int(os.environ.get("SLURM_CPUS_PER_TASK", 1))
HERE = os.path.dirname(os.path.abspath(__file__))
# Batch k of dimension d; batch 0 is the pilot. It runs on a worker.
@python_app(cache=True)
def count(d, k):
import ball
return ball.count_hits(d, k)
@python_app(cache=True)
def reduce(d, *hits):
import ball
return ball.row(d, len(hits), len(hits) * ball.BATCH, sum(hits))
# How many batches d needs is known only once its pilot has run: Parsl calls
# this with the pilot's hits, and waits for the reduce it returns.
@join_app
def batches(d, pilot):
n = ball.batches_needed(pilot)
return reduce(d, *[count(d, k) for k in range(1, n + 1)])
@python_app
def plot(table, svg):
import ball
ball.plot(table, svg)
def main():
config = Config(
executors=[
HighThroughputExecutor(
max_workers_per_node=WORKERS,
# The workers import ball.py from this directory.
provider=LocalProvider(worker_init=f"export PYTHONPATH={HERE}"),
)
],
# Keep each result of an app with cache=True in runinfo/, for next time.
memoizer=BasicMemoizer(checkpoint_mode="task_exit"),
# Record every task in runinfo/monitoring.db.
monitoring=MonitoringHub(),
)
with parsl.load(config):
rows = [batches(d, count(d, 0)) for d in range(2, DMAX + 1)]
# The table, here in the main program, once every row is ready.
with open("volumes.tsv", "w") as f:
print(ball.table([row.result() for row in rows]), file=f)
table = os.path.abspath("volumes.tsv")
plot(table, os.path.abspath("volumes.svg")).result()
if __name__ == "__main__":
main()count, reduce and plot are Python apps: calling one returns at once, with a future for its result, and the function runs later, on a worker, a process of its own. That is why each imports ball inside the function, as Parsl’s documentation asks of an app defined in the script that starts Parsl. A future passed as an argument makes the app wait for it, and receive its result, so reduce(d, *[count(d, k) for ...]) runs once every batch of d is counted, with their hits as integers.
batches is a join app, and is the plan. It runs in the main program rather than on a worker, once its arguments are ready, here the pilot’s hits, and returns a future rather than a value, the future of the reduce it submits. Its own future then stands for that reduce’s result. So each d plans and submits its batches as soon as its own pilot is done, whatever the other dimensions are doing. Calling .result() on each pilot in the main program would work too, but the main program would wait on each in turn, in the order of the loop.
main loads the configuration, submits a pilot and a plan for every d, and writes the table itself, in plain Python, once every row is ready, then submits the plot, and waits for it. The if __name__ == "__main__" is needed: the monitoring starts processes that import the script again, and without it each of them would start the whole workflow again.
The configuration
The configuration, Config in main, has one HighThroughputExecutor, which runs a pool of workers, started by its provider: LocalProvider starts them on the node the program runs on, one for each CPU of the job, from max_workers_per_node. worker_init is a line of shell run before the workers start, here to put the example’s directory on the PYTHONPATH, so that they find ball.py. The memoizer and the monitoring are below.
Running it
The environment has Parsl, with what its monitoring needs, and everything the apps import, since they all run in it:
name: ball-parsl
channels:
- conda-forge
dependencies:
- python=3.13
- parsl-with-monitoring
- numpy
- matplotlib-base#!/bin/bash
#SBATCH --job-name=ball-parsl
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=8
#SBATCH --time=00:10:00
set -eo pipefail
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate "${CONDA_ENV:-ball-parsl}"
# Every task is a process with one thread, so as many tasks as CPUs at a time.
source ../pi/num-threads.sh 1
# The whole workflow inside this job, with a worker on each of its CPUs.
python ball_parsl.py
cat volumes.tsvThe job prints the table of the intro, after about a minute on 8 workers, and leaves volumes.svg next to it, and the logs of the run in runinfo/.
The environment of each app
Every app runs in the environment of the workers it is sent to, and every worker of an executor starts in the same environment, set by the worker_init of its provider, e.g. source /path/to/miniforge3/etc/profile.d/conda.sh && conda activate /path/to/env. Here there is one executor, so every app runs in the same environment. To give plot one of its own, add a second HighThroughputExecutor, with a label such as "plot" and a worker_init that activates that environment, and send the app there with @python_app(executors=["plot"]). An app that names no executor can be sent to any of them, so the others need an executors= of their own too.
Each such environment has to have Parsl itself, at the same version as the main program, and Python of the same minor version: the executor refuses a worker that differs in either. The HighThroughputExecutor does not create these environments, so make each one from a lockfile, as in Freezing an environment.
Caching and checkpoints
cache=True on count and reduce makes Parsl keep their results, and return the kept result, without running the app, when the same app is called again with the same arguments, which on its own lasts for one run. The memoizer, BasicMemoizer(checkpoint_mode="task_exit"), also writes each result into runinfo/ as its app finishes, and loads every result kept there when the program starts. So running the job again runs no batch, and takes seconds, with the 90 counts and 11 reduces of the first run from the checkpoint. python ball_parsl.py 13 runs only the pilot, the 134 batches and the reduce of d = 13, and the plot, and takes the other 101 results from the checkpoint.
A result is keyed on the name of its app and the values of its arguments, here d and k, and not on the app’s code, so a change to ball.py, or to the body of count itself, reruns nothing: when the code changes, delete runinfo/. The key does not depend on where the result was made either, so a copy of the directory, runinfo/ with it, reuses every result. The plan, a join app, is not cached, and runs again every time, from the pilot’s kept result.
The monitoring database
MonitoringHub() records the run in runinfo/monitoring.db, an SQLite database. Its tables hold each run of the program, with its user, host, start and end; each task, with its app, the tasks it depended on, and when it was submitted and returned; each try at running it, with the host, the executor, and when it was launched, started and ended; and the resources each used. parsl-visualize, with the packages of parsl-with-visualization installed, serves them as web pages, and any SQLite client, such as Python’s sqlite3 module, can read them.
On Slurm
To spread the workers over Slurm jobs, replace LocalProvider with SlurmProvider, which asks Slurm for blocks, each a job of nodes_per_block nodes for walltime, with the same worker_init, and starts the workers inside them. Where to run the program itself, and how not to overwhelm the scheduler, are on the page on workflow managers. On nodes of the same kind, the table is the same either way, since each batch is seeded by its (d, k). Across kinds of node, it is the same within a tolerance, as the page on the machine explains.
Limitations
There is no dry run: the graph exists only as the program runs it. Only the apps marked cache=True are kept, and a result is reused by its app’s name, so a change to the code is invisible, and clearing the checkpoints when it matters is up to you. And the environments are yours to make, and to keep at the same Parsl and Python as the program.
That is the end of this section. The last page of this part is about archiving a result, with every input it depends on, the steps of its pipeline among them.