Dragon Native Backend¶
DragonNativeExecutionBackend (backend name dragon_native) is a second Dragon backend. It sits
beside DragonExecutionBackend, which goes through dragon.workflows.batch, and does not replace
it. It drives Dragon's Process, ProcessGroup, Policy and Queue directly, on dragonhpc
0.15.0.
Both take the same task descriptions, so a workflow switches by changing only the backend. Some
results and behaviour differ, though: a function with a process_template returns its value, not
its exit code, and MPI on Cray MPICH needs pmi="cray" here, among others. See
How it differs from DragonExecutionBackend.
from rhapsody.backends import DragonNativeExecutionBackend
backend = await DragonNativeExecutionBackend() # one worker per physical core
backend = await DragonNativeExecutionBackend(workers_per_node=32) # or a set number per node
async with Session(backends=[backend]) as session: # through RHAPSODY's API
...
flow = await WorkflowEngine.create(backend=backend) # or through AsyncFlow
Install it with the dragon extra, and start your script with the Dragon launcher, as for
DragonExecutionBackend: dragon my_workflow.py.
How each task runs¶
| Task | Runs as | return_value |
|---|---|---|
| Function, no templates | a pool worker | the function's value |
Function with placement ({"node_id", "host", "cores", "gpu_ids"}: the host is host, else node_id; cores is not used), or a process_template holding only a policy with a host and/or GPUs |
a free pool worker on that host; for gpu_ids, a worker started for that GPU set |
the function's value |
Function with any other process_template ({}, cwd, env, a CPU affinity, ...) |
one native Process |
the function's value |
| Executable | one native Process |
the exit code (also exit_code); a non-zero one also sets exception to the TaskExecutionError a Session raises |
process_templates (function or executable) |
one ProcessGroup, with the task's or the backend's pmi (none by default) |
the list of exit codes; exit_code is the first non-zero one |
- The pool. At start the backend queries
System().nodesand starts, on every node,workers_per_nodeworkers, by default one per physical core ofNode.cpus: the CPUs the job has there, neveros.cpu_count(). See Choosing the pool size. Each worker is one nativeProcesspinned to its host and its share of the node's CPUs, with its own inboxQueueon its node. A share is whole physical cores while there are at least as many cores as workers, so no two workers share a core. All workers put results on oneQueueon the client's node. - Pinning. A worker runs only on its share. With the default pool that share is one core, so a
function that starts its own threads, as NumPy and OpenMP do, gets that core's hardware threads.
Set a lower
workers_per_nodefor such functions, or give them aprocess_templateof their own. - Dispatch. The client sends a task only to an idle worker; a task with no idle worker for its
host waits in the backend, first in, first out. Unplaced function tasks fill the client's node
first, where a round trip is cheapest. Unplaced process tasks get no policy, so Dragon spreads
them round robin over all nodes; place them to keep them local. A task that asks for GPUs goes to
a worker started, on demand, with
gpu_affinityset to exactly those GPUs (gpu_workers_per_setof them, default 1), so Dragon sets the vendor's visible-devices variable once, at worker start. Workers stay up for the backend's life. On a host, tasks placed there go before unplaced ones. - Pickling and the loop. A pool task's function is cloudpickled for its first two tasks, and
once both pickles come out the same, that pickle is given again while nothing it holds has
changed (its code, closure, defaults, attributes and the globals its code reads); a function
holding an object of a class is pickled for every task, and so is one that changes before every
task or whose pickle has been over 1 MB. Each task pickles
only its arguments, continuing the function's pickle, so the worker loads both as from one
pickle.
submit_taskslets the asyncio loop run every 20 ms while it builds a large batch, and the callback gets the backend's state changes for at most 20 ms per pass of the loop: neither holds the loop for a whole batch. A task a submit has not reached yet can be cancelled, and is cancelled at shutdown. - The relay. A relay process on the client's node puts each task on its worker's inbox and takes each result off the result queue, and the client trades batches with it: the tasks it gave to workers in one pass, and the results that came while it handled the last batch. On its own node the relay puts a task on its inbox at once, unless the put takes more than 10 ms; other puts go to four threads per node, each worker's tasks in order but the workers apart. An inbox that takes nothing holds up neither the results nor other nodes' tasks, nor, up to three such inboxes on a node, its node's other workers. It forwards a batch once the client has acknowledged the last one, or after 10 s without an acknowledgement, and after the client sent tasks it waits briefly for them (a tenth of the client's last pass, at most 1 ms), so short tasks come back in the next batch rather than one batch each. The client's threads share the GIL of the client's process, often an agent or workflow that keeps it busy. Each queue call gives the GIL up about twice, and getting it back from a thread that does not let go can take a switch interval (5 ms) each time: with one put and one get per task, the pool started about 50 tasks a second next to such a thread. In batches, the client makes three such calls per pass, however many tasks the pass holds, and the relay's process has its GIL to itself. The busier the client, the larger the batches.
- Results. A worker pickles its message with cloudpickle and puts the bytes on the result
queue. One client thread takes the relay's batches, applies each under one lock, sends the relay
the next tasks for the workers that are free, and hands the state changes to the asyncio loop
with one
loop.call_soon_threadsafe. The backend calls the callback only on the loop. Results travel inline on the queue, so they use Dragon's memory pool until the relay takes them: on a full pool a worker waits up to 60 s for room, and a result that does not fit then, or could never fit, fails its task with Dragon's error. Sending a task waits the same way, and never on the asyncio loop: when the relay's queue on the client's node has no room, the client holds the batch and tries again from a background thread with a backoff, and when a worker's inbox has no room in the pool, the relay holds the task and tries again. Either way only a task that waited 60 s fails, with Dragon's error, and its worker takes the next; the relay is not lost, and tasks run again once the pool has room. A task too large for the pool ever to hold fails at once, with Dragon's error, and its worker carries on, as for a result too large. A send that fails for any other reason, or takes more than 10 s, replaces the worker, and the task goes to the next idle worker, or waits for one; it fails only if that send fails too. A function with aprocess_templateflushes its output and its result before it exits, so the client knows when one is missing. Its task is reported when the result comes, without waiting for the exit: a process can hang at interpreter exit, as one did in a Dragon queue's__del__. The process then haskill_graceseconds to exit, or it is killed, with a warning, and its ranks stay taken until it exits or is killed. An exit code other than 0 after the result is logged and leaves the task as its result reported it; a process that exits before its result comes fails on a non-zero exit code, as before. The pool is 4 GiB per node by default (DRAGON_DEFAULT_SEG_SZ), so workers that finish together should return much less than that between them; write larger results to files. - Process tasks. A few threads start process tasks, and one thread finds those that exited,
all at once, with a non-blocking
multi_joinevery 10 to 100 ms. Each wait inProcess.join()is a request to Global Services, and with a thread waiting in one per task, runs of 512 tasks at once have stalled Dragon's request path for good. A group task has a thread of its own, which waits on the group's manager. At mostmax_process_ranksprocesses run at once (default: the runtime's CPUs); the rest wait, strictly first in, first out, so a large job at the head of the queue holds back smaller tasks behind it. A task that needs more than the limit runs alone. The limit counts only these tasks, over all nodes, not the pool: a node can hold its pool and as many ranks again. - Output. With
capture_stdio, output goes to<work_dir>/<uid>.stdoutand.stderr, and those paths come back, as withDragonExecutionBackend. The files are written on the task's node, and the directory is made there if it is missing, so on a work directory the nodes don't share, the paths name files on that node. Withoutcapture_stdio, the text comes back instdoutandstderr:- a pool worker points descriptors 1 and 2 at two unnamed files on its node for the task, and sends their text with the result;
- a process task writes to files in
<work_dir>/.dragon_native.<id>/, which the client reads and deletes after the task. This needs the work directory on a file system the nodes share; inline_stdio=Falsesends that output to the console instead;- a
stdoutorstderrin a task's own template takes precedence, andcapture_stdiois then skipped.
Open files on nodes with many CPUs
Local Services holds about three file descriptors for each Python process on its node and one
for each executable, and Dragon does not raise the soft limit. At a soft limit of 1,024, a node
has room for about 320 workers, or fewer with process tasks running beside them. Past the limit,
the pool fails to start, or a process task fails, with Too many open files at the end of a long
error. Raise the limit with ulimit -n before starting Dragon; the backend logs a warning when
a node may not fit.
Functions that wait on a Dragon process
With dragonhpc 0.15.0, a process killed while it waits in Process.join() aborts the whole
Dragon runtime when the process it waits for ends. So cancelling a task, or giving it a
timeout that it reaches, aborts the runtime if its function has started a Dragon Process and
is waiting for it, in the pool or with a process_template. Submit that child as a task of its
own instead, and let RHAPSODY wait for it.
Choosing the pool size¶
The default is one worker per physical core of the job on each node, so that no two workers share a
core: 32 workers on a node with 32 cores and 64 hardware threads, 128 on a Perlmutter CPU node or a
Bridges-2 RM node. It is a default, not a limit: workers_per_node takes any number, above or below
it.
The best size depends on how long tasks run and what they do. Here are tasks per second on one node with 32 cores and 64 hardware threads, for functions that do nothing or sleep. Each range covers the waves of tasks in one run per setting. These runs carried timing code; plain runs of the first row gave about 15,500 and about 4,100 tasks/s.
| Task | 32 workers | 64 workers |
|---|---|---|
| Nothing | 8,700–10,200 | 2,100–2,400 |
| Sleep 1 ms | 7,690–8,768 | 2,928–6,275 |
| Sleep 5 ms | 4,170–4,284 | 2,658–3,226 |
| Sleep 20 ms | 1,417–1,440 | 2,041–2,286 |
- Many tasks under about 10 ms: a smaller pool can do better, even one below the core count. Bridges-2 RM nodes have no hardware threads, so the default fills them. On two of those nodes, tasks that did nothing ran at about 13,100 tasks/s with 32 workers per node, and about 3,800 with 128.
- Tasks that sleep or wait, about 20 ms or longer: more workers than cores did better, as the table shows.
- CPU-bound tasks: not measured.
Short tasks suffer because each one allocates and frees its messages in the node's Dragon memory pool, and every process on the node shares that pool's lock. On the node above, each of those calls took microseconds with 32 workers and up to about 1 ms with 64. Contention on that lock is our reading of these timings, not something we traced directly.
MPI jobs: PMI and timeout¶
A task with process_templates runs as one ProcessGroup. Unlike a Batch job, which gets Batch's
default PMIBackend.CRAY, a group has no PMI unless one is chosen. Without one, MPI ranks never
wire up: with Cray MPICH they hang in MPI_Init, and with Open MPI each may run as a job of one
rank. Choose the PMI for every group on the backend, or per task (the task's choice wins):
backend = await DragonNativeExecutionBackend(pmi="pmix") # Open MPI and other PMIx-based MPIs
task = ComputeTask(
executable="./mpi_app",
task_backend_specific_kwargs={
"process_templates": [(16, {})],
"pmi": "cray", # or "pmix", or "none" for jobs of non-MPI processes
"timeout": 600, # seconds
},
)
The values are "pmix", "cray", "none" and a Dragon PMIBackend. An unknown name raises
ValueError on the backend, before anything starts, and fails only its own task on a task. Tasks
with process_template, or with no template, run without a PMI, and ignore pmi.
When neither sets one, the first multi-rank job logs a warning. Under Cray MPICH
(CRAY_MPICH_DIR or CRAY_MPICH_VERSION set) a one-rank job logs it too. Give MPI jobs a timeout
as well: when a rank dies, Dragon does not end the group, and the other ranks wait for it. The
first multi-rank job without one logs a warning. RHAPSODY can't tell whether a job's processes use
MPI, so set "pmi": "none" for jobs that don't: it logs neither warning.
PMIx needs libpmix.so
Dragon loads PMIx with dlopen("libpmix.so"), so a directory holding that name must be on
LD_LIBRARY_PATH; some distributions ship it only in their PMIx development package. When it
can't be loaded, the whole Dragon runtime aborts.
PMIx groups across nodes
With dragonhpc 0.15.0, PMIx groups that span nodes can fail or hang at startup, with or without
RHAPSODY. Dragon's dictionary client keys each request on an index that nothing sets, so two
requests can collide, and a PMIx fence then fails. Ranks log
Failed to receive UCX worker address, or wait with no output. On Bridges-2 (Open MPI 5.0.8,
HSTA), about 3% of two-node groups failed this way, and about 13% beside heavy queue traffic, more
often when groups started within a fraction of a second of each other. Give these tasks a
timeout, and resubmit the ones that fail.
In one run of about 180 MPI tasks on Bridges-2, the whole runtime was lost at about the time a hung task's timeout ended. The cause of that isn't known, and it hasn't been tied to this bug.
Cray PMI and Python functions
With dragonhpc 0.15.0, a rank that runs a Python function under Cray PMI can abort at exit,
after the function has done its work and whether or not it uses MPI. Run MPI from Python as an
executable (python script.py with process_templates), and give function jobs that are not
MPI "pmi": "none".
How it differs from DragonExecutionBackend¶
- No Batch. No Batch manager loop, no results DDict, no polling. A completion reaches the asyncio loop as soon as the relay delivers it.
- Placement is the caller's. A task's
placementis honoured: placed functions run in the pool, on a worker of that host.DragonExecutionBackendignores it. - Sized from the job. The pool and the process limit come from
Node.cpus, so a partial-node job is not oversubscribed. The default pool has one pinned worker per physical core.DragonExecutionBackendstarts half the CPUs, unpinned, or one worker per allowed core when the job has fewer, and places process and MPI jobs as if the job had whole nodes. - Functions with a
process_templatereturn their value, through the result queue, and a failing one setsexceptionto its own exception.DragonExecutionBackendreturns the exit code. - Groups have no PMI by default. A
process_templatesgroup starts with the task's or the backend'spmi, and with none when neither sets one, where a Batch job has a default of its own. MPI on Cray MPICH needspmi="cray": see MPI jobs. - Executables start one process, not two. A Batch process task builds a
ProcessGroup, whose manager is a Python process that imports Dragon. Here an executable is one nativeProcess, started throughsh -c 'exec "$@" >>out 2>>err'. - Output stays out of Local Services' pipes. With inline or captured output, process tasks get
stdout=DEVNULLand redirect themselves to files, so a background child holding a task's output cannot stall Local Services for the whole node.inline_stdio=Falseand a template's ownstdoutdo go through them. - Cancel and timeout stop the work. A pool task's worker gets SIGKILL and is replaced, so a
function stuck in a C call stops at once. Process tasks get SIGTERM, then SIGKILL after
kill_grace, sent per puid: Dragon 0.15.0'sProcessGroup.stop()never sends its SIGKILL. Dragon signals only a task's own process, so every process a task starts carriesRHAPSODY_DRAGON_NATIVE_TASK=<token>in its environment, and after the kill a shortshreaper on each of the task's hosts kills whatever still carries it. - A timeout is a
TimeoutError, for functions and processes alike, and the task is FAILED. Its clock starts when the task starts running, not when it is submitted. Atimeoutof zero or less, and aprocess_templatesentry with no processes, fail the task before it reaches Dragon. - Cancel and shutdown.
cancel_taskreports CANCELED at once, the kill follows in the background, and it returns False for an unknown or finished task, as the Concurrent and Dask backends do.shutdown()kills what still runs at once and reports it CANCELED, whereDragonExecutionBackendgives tasks up to 10 s to end. - Shutdown ends the processes 64 at a time. Idle workers are told to stop, busy workers get
SIGKILL, and process and group tasks SIGTERM, then SIGKILL after
kill_grace; a worker that has not exited 5 s after its stop gets SIGKILL too. One non-blockingmulti_joinfinds the processes that exited, and the next ones get their stop as they go: 768 workers told to stop at once wedged Global Services, and one at a time took a request or more each. One reaper per node then kills what the tasks left, and the queues are destroyed eight at a time. A process that outlives its SIGKILL by 20 s, and whatever is left after 120 s, is logged and left for Dragon to end with the runtime. The last log line ofshutdown()gives the seconds of each step. - A process already reported on its result gets
kill_graceto exit at shutdown. Such a process may still be tearing down, waiting on processes of its own. Killed then, it leaves Global Services joins it can no longer receive, and when those processes exit, Dragon 0.15.0 fails to answer them and ends the runtime. So shutdown waits up tokill_gracefor it, without a signal; one that exits is neither killed nor reaped, and one that does not is stopped as above, with a warning. During the run, such a process is checked before its kill too, and one that has exited is left alone. - Bad placements fail only their task. Hosts and GPU ids in a
placementor apolicyare checked against Dragon's nodes before anything is submitted: Dragon aborts the whole runtime on an unknown host and silently drops unknown GPU ids. - A repeated uid joins its task. A task submitted while another task with its uid has not
finished does not run: it gets that task's states and, when it ends, its result or error. A
Sessionkeeps one future per uid, so a repeat that failed would fail the first task's future. The uid decides: a repeat with another function or executable joins too, and the backend logs a warning. Once the task has ended, its uid runs again. - Dead workers are noticed. A worker that dies on its own fails its task with the exit code,
and is replaced (once: a replacement that dies before it is ready is not replaced again). A
replacement or GPU worker that is not ready
worker_start_timeoutafter its launch counts as dead, and is killed. When no worker is left for a host, or a GPU set, the tasks waiting for one fail, and losing a host's last pool worker is logged as an error. - A dead relay is noticed too. If the relay process exits, every pool task fails, and so do pool
tasks submitted after it, and functions with a
process_template, whose results it carried. Executables and groups still run. Only the relay's exit counts: a queue or pool with no room is waited out. The relay is not restarted: shut the backend down and start another. - The garbage collector. Dragon's
gs_requestturns Python's garbage collector off at the start of every Global Services request and back on at the end of any, for the whole process, so a thread blocked inProcess.join()would keep it off until another request ends. The worker watcher and the process watcher therefore check with a non-blockingmulti_join. Joins that wait are left to killing and retiring, which wait up to a few seconds for a process they just killed. - Not here.
wait(),create_ddict()and.batchbelong toDragonExecutionBackend. Before the backend starts,topology()returns an empty list.
Telemetry¶
Task events, metrics and spans come from the Session, as for every backend. Node resources come
from the same DragonTelemetryAdapter as for DragonExecutionBackend: it starts its own process
group on the runtime's nodes and does not use Batch. See Telemetry.
Several clients on one runtime: hosts¶
A runtime of several nodes can serve several clients, each with a backend of its own. By default
each backend uses every node: each one starts workers_per_node workers on every node, so N clients
start N pools on each node. hosts limits a backend to some nodes. hosts="local" is the client's
own node:
To name other nodes, give a list of hostnames as Dragon names them: the ids of
backend.topology() on a backend without hosts. Dragon may not spell a node as
socket.gethostname() does: a runtime of one node calls it localhost.
The backend then acts as if the runtime had only those nodes:
- its pool starts on them, and its unplaced function tasks run there;
- a process task whose policy names no node (no policy, or one with only CPU or GPU affinity, or
placement LOCAL, ANYWHERE or DEFAULT) goes to those nodes in turn, the client's node first,
and to one that has its GPUs if it asks for some. It keeps its CPU and GPU affinity. Without
hosts, Dragon places it anywhere in the runtime;
- a template of a process_templates group whose policy names no node has its ranks spread over
those nodes in blocks, one per node: 8 ranks over 2 nodes are 4 on each. A template of one rank
goes to the next node;
- a task placed on another node fails, alone, before Dragon sees it;
- max_process_ranks defaults to those nodes' CPUs, and topology() lists only them.
The relay and the result queue stay on the client's node, whether or not it is one of hosts. A
host that is not a node of the runtime raises ValueError when the backend starts. The error lists
the runtime's nodes, and gives Dragon's name for a host where it can tell: the client's own
hostname, long or short, is the client's node, and a name with the short name of one node is that
node.
hosts does not reach telemetry: the DragonTelemetryAdapter samples every node of the runtime,
so N clients with telemetry on start N samplers on each node.
What it does not do yet¶
- A result store. Results travel inline on the queue; there is no DDict for large values.
- Per-task isolation in the pool. As with Batch's workers, tasks in one worker share its interpreter: imported modules, global state, environment variables, threads and signal handlers persist between tasks. The backend restores the working directory and its token variable after each task, nothing else.
- Data linking.
link_implicit_data_depsandlink_explicit_data_depsare not implemented, as inDragonExecutionBackend. - Resource usage per task (peak memory, CPU time).
- Scheduling beyond "a free worker of this host". No priorities, no per-node limit for process tasks (the limit is global), no worker recycling, no memory limits.
- A CPU layout per node. Physical cores are read from the client's node. A node numbered differently is grouped less well: it gets valid CPUs, but its workers may share cores, and its default pool may not have one worker per core.
Configuration¶
| Argument | Default | Meaning |
|---|---|---|
workers_per_node |
one per physical core | pool workers per node, each pinned to its share of the node's CPUs |
gpu_workers_per_set |
1 | workers started on demand for each host and GPU set |
max_process_ranks |
the runtime's CPUs | processes of process and group tasks that may run at once |
inline_stdio |
True |
return the output of tasks without capture_stdio as text |
kill_grace |
2.0 s | SIGTERM to SIGKILL for process tasks |
worker_start_timeout |
300 s | time allowed for the pool to start, and for each replacement or GPU worker to be ready |
pmi |
None |
PMI of every process_templates group whose task sets none |
hosts |
every node | the nodes the backend uses: "local" for the client's node, or Dragon's hostnames; see Several clients on one runtime |
num_workers is the number of live CPU workers over all nodes: fewer than at start once a
replacement has failed to start. topology() lists each node's CPUs and GPUs.
Tests¶
tests/unit/test_backend_execution_dragon_native.py runs the backend against a fake Dragon
(threads for Python processes, real /bin/sh for executables), so it needs no Dragon runtime. Its
last tests need one:
tests/performance/dragon_native_busy_client.py measures how fast the pool starts tasks while a
thread keeps the client's GIL busy (--busy thread), or while nothing does (--busy none):
tests/performance/dragon_native_many_processes.py runs many executables at once, and reports the
rate and the client's threads, or, if the run stalls, where they wait:
tests/performance/dragon_native_shutdown.py times the shutdown of a large pool, with busy
workers and running executables if asked, and samples where the client's threads spend it: