Skip to content
class

Dispatcher

Dispatcher

Routes compute operations to the backend registered for a given Device.

Holds one IBackend slot per Device variant (currently CPU and GPU). register_backend transfers ownership via std::unique_ptr; for_device returns a non-owning reference valid for the lifetime of the process. Every higher-level op in the Lucid engine fetches its backend through this class — the dispatcher is the single seam between device-agnostic op code and device-specific compute libraries.

See Also

IBackend — abstract per-device interface. CpuBackend::register_self — installs the CPU backend. GpuBackend::register_self — installs the GPU backend.

Notes

Thread safety: register_backend is called exactly once per device at process startup, before any worker threads exist. After that, for_device is read-only and safe to call concurrently from any thread.

The internal storage is a fixed-size std::array indexed by the Device enum value; kNumDevices must match the number of Device variants (currently CPU=0, GPU=1).

Static methods

4
static

for_device

→IBackend&
IBackend & for_device(Device d)

Returns the backend registered for device d.

The returned reference is non-owning and remains valid for the entire process lifetime; callers must not delete it.

See Also

register_backend — installer counterpart.

Parameters

dDevice
Device tag selecting which backend to return (CPU or GPU).

Returns

IBackend&

Reference to the singleton backend instance for d.

Notes

Behaviour is undefined if no backend has been registered for d — register_backend must have been called for that device before any tensor compute occurs. noexcept; safe to call concurrently after initialisation.

While PhantomMode is on for the calling thread, the answer is the phantom backend for d instead, which computes nothing — every op on that thread then dispatches with the shape it already computed and gets a phantom storage back.

static

instance

→Dispatcher&
Dispatcher & instance()

Returns the Meyers-singleton dispatcher instance.

Constructed on first call and destroyed at program exit per the standard C++11 magic-statics guarantee.

Returns

Dispatcher&

The process-wide dispatcher.

static

phantom_backend

IBackend & phantom_backend(Device d)

The PhantomBackend for d — one per device, a static inside the function. Defined in BackendInit.cpp, the one translation unit that builds the backends, so the generated class is compiled once rather than in every op that dispatches.

static

register_backend

void register_backend(Device d, int be)

Registers (or replaces) the backend for device d.

Transfers ownership of be into the dispatcher. Any backend previously stored at that slot is destroyed.

Parameters

dDevice
Device tag identifying which slot to populate.
bestd::unique_ptr<IBackend>
Owning pointer to the concrete backend instance to install.

Notes

Called exactly once per Device during process initialisation (typically from CpuBackend::register_self and GpuBackend::register_self, invoked by BackendInit.cpp). Not thread-safe — must complete before any worker threads start issuing for_device calls.