skip to content
==> include/mesh/mps.h <==
1/* @ajax/mesh-mps: meshd's CUDA backend (mesh-host mps.rs).
2 *
3 * Blink mode (the default): every program is a resident kernel of meshd's
4 * one context (@ajax/mesh-cuda); a load, upgrade or unload blinks them all.
5 *
6 * `--isolation mps`: loads that halt no other program.  meshd starts and
7 * owns a private MPS control daemon (pipe and log dirs under --mps-dir, mode
8 * 0700; quit at exit).  The mailbox is the shared-memory file every worker
9 * maps and registers with DEVICEMAP.  Each loaded image runs in its own
10 * worker process (`meshd worker`, @ajax/mesh-mps-worker): an MPS client that
11 * loads only its module and launches mesh_main on its own stream.  A module
12 * load in one MPS client does not wait for the others' kernels, so the
13 * worker is started and its cubin loaded on the JIT thread while everything
14 * runs.  Creating an MPS client stalls the others once, so a pool of idle
15 * workers is started before any program runs.  There is no fallback between
16 * the modes: no daemon, a worker that is not a client of our MPS server, or
17 * a failed register refuses. */
18#ifndef MESH_MPS_H
19#define MESH_MPS_H
20
21#include "mesh/backend.h"
22#include "mesh/cuda.h"
23#include "mesh/nvjitlink.h"
24#include "mesh/shm.h"
25
26#include <stdbool.h>
27#include <stdint.h>
28#include <sys/types.h>
29
30#define MESH_MPS_PIPE_ENV "CUDA_MPS_PIPE_DIRECTORY"
31#define MESH_MPS_LOG_ENV "CUDA_MPS_LOG_DIRECTORY"
32#define MESH_MPS_CONTROL "nvidia-cuda-mps-control"
33/* A worker's first load also starts the MPS server (context creation). */
34#define MESH_MPS_WORKER_LOAD_MS 30000u
35#define MESH_MPS_WORKER_LAUNCH_MS 2000u
36
37/* The CUDA backend.  iso: blink or mps; sms > 0: a green context of that
38 * many SMs (per worker in mps mode); mps_dir NULL: /tmp/meshd-mps.<pid>
39 * (removed at exit); pool: mps workers started before any program runs;
40 * shm: the mailbox (registered in the context, or by each worker); jit: the
41 * compiler (taken).  Registers the MPS cleanup with mesh-backend. */
42bool mesh_cuda_backend_open(mesh_isolation_mode iso, uint32_t sms, const char *mps_dir, uint32_t pool, const mesh_shm *shm,
43                            mesh_nvjitlink *jit, mesh_backend **out, char **err);
44/* Blink mode: the CUDA context (kcheck's module-load probe); NULL in mps mode. */
45CUcontext mesh_cuda_backend_context(const mesh_backend *b);
46bool mesh_cuda_backend_mps(const mesh_backend *b);
47/* Host-native atomics (HOST_NATIVE_ATOMIC_SUPPORTED) of the device. */
48bool mesh_cuda_backend_host_atomics(const mesh_backend *b);
49
50/* Under --isolation mps: the pipe dir a native GPU worker must be started
51 * with to be a client of meshd's server (malloc'd); NULL in blink mode. */
52char *mesh_mps_native_pipe_dir(void);
53/* A native GPU worker that said `ready` must be a client of our server (the
54 * driver silently runs a process that cannot reach it as a plain context).
55 * Fits mesh_worker_cfg.check_gpu_client. */
56bool mesh_mps_check_native(pid_t pid, char **err);
57/* Every worker killed, then the daemon quit; waits for both.  A no-op in blink mode. */
58void mesh_mps_cleanup(void);
59
60#endif