# Executors

> An executor owns host threads or device streams, the activation scratch, and the pinned buffers staging passes through, grown to a high-water mark.

`#include <uform/types.hpp>``#include <uform/train.hpp>` 4.0.0-rc.1 · c58749fe

An executor owns host threads or device streams, the activation scratch, and the pinned buffers staging passes through, grown to a high-water mark. One executor serves any number of models of its residency.

```cpp
auto [threads, threads_status] = uf::executor_t::for_ram({.threads = 8});         // eight threads, where zero takes 7/8 of the cores less one
std::int32_t const domains[] = {1};
auto [numa, numa_status] = uf::executor_t::for_ram({.ram = {.memory_domains = domains, .memory_domains_count = 1}});
auto [serial, serial_status] = uf::executor_t::for_ram({.ram = {.cpu_class_ceiling = uform_cpu_class_serial_k}});
std::int32_t const devices[] = {0, 1};
auto [streams, streams_status] = uf::executor_t::for_vram({.vram = {.devices = devices, .devices_count = 2}});
uf::executor_t local;                                                   // owns nothing, runs on the calling thread

uf::status_t const drained = threads.drain(uform_mood_spinning_k);     // waits until no call holds it, then keeps spinning
```

Every factory takes the C argument struct by designator, so a field left out is zero, which the library derives. Naming one memory domain spawns the threads near it, while none or several interleave them across the machine. `for_vram` opens one stream per device, or adopts the caller’s `streams`, one per named device, and the executor destroys them; naming no devices drives every listed device of the first vendor with a known class. `uf::gpu_devices(devices)` fills a span of `uform_gpu_device_t` and answers how many GPUs the process can drive, and a device’s position there is the ordinal `for_vram` and `load_vram` name it by, while `uf::version_name()` spells the linked library’s version. An executor runs one call at a time, so two threads that genuinely overlap take one apiece. Destroying an executor waits until no call holds it, and asserts in Debug builds that no arena made with it lives.

## API

### Types

#### `mesh_t`

Owns one process's place among the processes of a run; each trainer made over it holds it, so either may be freed first.

[train.hpp:33 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/train.hpp#L33)

```cpp
class mesh_t
```

#### Class `executor_t`

Owns one executor: the host threads or device streams every call runs on, apart from any model; an empty one runs every call on the caller's own thread.3

[types.hpp:538 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L538)

```cpp
class executor_t
```

It outlives every arena it made.

##### `drain`

Waits until no call holds the executor, then sets its threads' idle `mood`.

[types.hpp:560 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L560)

```cpp
status_t drain(uform_mood_t mood = uform_mood_parked_k) noexcept;
```

##### `for_ram`

Creates an executor of host threads for RAM-resident models, near the one memory domain `arguments` names, or interleaved across the machine for none or several.

[types.hpp:547 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L547)

```cpp
expected<executor_t> for_ram(
    uform_executor_for_ram_t arguments = {}) noexcept;
```

##### `for_vram`

Opens one submission stream on every device `arguments` names, or adopts the caller's `streams` : a CUDA or HIP stream becomes the executor's, which synchronizes and destroys it; a Metal queue is retained, the executor releasing its own reference while the caller keeps and releases theirs.

[types.hpp:555 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L555)

```cpp
expected<executor_t> for_vram(
    uform_executor_for_vram_t arguments = {}) noexcept;
```

### Functions

#### `free_executor`

Frees `executor` as its owner dies, which every arena made with it refuses while it lives; declaring the executor before its arenas destroys them first.

[types.hpp:531 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L531)

```cpp
void free_executor(uform_executor_t executor) noexcept;
```

#### `gpu_devices`

Fills `devices` with the GPUs this process can drive, in ordinal order, answering how many there are, which may exceed what `devices` holds.

[types.hpp:606 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/cpp/uform/types.hpp#L606)

```cpp
expected<std::size_t> gpu_devices(
    std::span<uform_gpu_device_t> devices) noexcept;
```
