rexlib 0.1.0
Device-agnostic array computing library for CryoEM and CryoET image processing, with a pluggable backend architecture
Loading...
Searching...
No Matches
Public Member Functions | List of all members
rexlib::device_context Class Reference

Scoped, configurable view over a device_session. More...

#include <rexlib/core/hardware/device_context.hpp>

Public Member Functions

 device_context () noexcept=default
 Construct an empty context holding no session.
 
 device_context (std::shared_ptr< const device_session > session)
 Construct a context for session.
 
 device_context (const device_context &other)=default
 
 device_context (device_context &&other) noexcept=default
 
device_context & operator= (const device_context &other)=default
 
device_context & operator= (device_context &&other) noexcept=default
 
const std::shared_ptr< const device_session > & get_device_session () const noexcept
 Retrieve the wrapped device session.
 
const std::shared_ptr< command_queue > & get_active_queue () const noexcept
 Retrieve the active command queue.
 
const std::shared_ptr< memory_allocator > & get_allocator (memory_resource_affinity affinity) const noexcept
 Retrieve the allocator for the given affinity.
 
device_context on_queue (std::shared_ptr< command_queue > queue) const
 Derive a context that submits onto a different queue.
 
device_context with_allocator (memory_resource_affinity affinity, std::shared_ptr< memory_allocator > allocator) const
 Derive a context with a different allocator for an affinity.
 

Detailed Description

Scoped, configurable view over a device_session.

A device_context layers the per-use, configurable state on top of the immutable device_session. That state is the active command_queue that work is submitted to, and the memory_allocator selected for each memory_resource_affinity.

The context is a lightweight, copyable value with functional-update semantics: on_queue and with_allocator do not mutate the receiver, they return a modified copy. This makes a context safe to share (including across threads) while still allowing callers to derive scoped variants, e.g. to bind a special-purpose allocator for a single operation without disturbing any other user of the original context. Copies are cheap: the session, the queue and the allocators are all shared pointers.

Allocator slots
For a context wrapping an session, every affinity slot always holds the allocator that get_allocator would return: at construction the slots are seeded from the device_session allocators, and with_allocator either installs an override or, when given a null allocator, reverts the slot back to the session allocator.
Empty state
A default-constructed or moved-from context is empty: it holds no session, no queue and no allocators. Querying it is well defined; the accessors simply return null shared pointers. A populated context (one constructed from a non-null session) never returns null from any accessor.

Constructor & Destructor Documentation

◆ device_context() [1/2]

rexlib::device_context::device_context ( )
defaultnoexcept

Construct an empty context holding no session.

Every accessor of the resulting context returns null until it is replaced by assignment.

◆ device_context() [2/2]

rexlib::device_context::device_context ( std::shared_ptr< const device_session >  session)
explicit

Construct a context for session.

The active queue is set to the default queue of the wrapped device and every allocator slot is seeded with the corresponding session allocator.

Parameters
sessionDevice session to wrap. Must not be null.
Exceptions
std::invalid_argumentif session is null.

Member Function Documentation

◆ get_active_queue()

const std::shared_ptr< command_queue > & rexlib::device_context::get_active_queue ( ) const
noexcept

Retrieve the active command queue.

Work produced through this context is submitted onto this queue.

Returns
A reference to the active queue, or to a null pointer if this context is empty.

◆ get_allocator()

const std::shared_ptr< memory_allocator > & rexlib::device_context::get_allocator ( memory_resource_affinity  affinity) const
noexcept

Retrieve the allocator for the given affinity.

Returns the override installed through with_allocator, or the device_session allocator when no override is in effect.

Parameters
affinityThe desired memory_resource_affinity (host or device).
Returns
A reference to the allocator for affinity, or to a null pointer if this context is empty.

◆ get_device_session()

const std::shared_ptr< const device_session > & rexlib::device_context::get_device_session ( ) const
noexcept

Retrieve the wrapped device session.

Returns
A reference to the wrapped session, or to a null pointer if this context is empty.

◆ on_queue()

device_context rexlib::device_context::on_queue ( std::shared_ptr< command_queue >  queue) const

Derive a context that submits onto a different queue.

Parameters
queueThe active queue of the returned context. The provided queue must have been provided by the device held by this context. Passing null uses device_session's default queue on the returned device_context.
Returns
A copy of this context with queue as its active queue.

◆ with_allocator()

device_context rexlib::device_context::with_allocator ( memory_resource_affinity  affinity,
std::shared_ptr< memory_allocator >  allocator 
) const

Derive a context with a different allocator for an affinity.

Installs allocator as the allocator for affinity. Passing a null allocator reverts the slot to the device_session allocator for affinity (or to null when this context is empty).

The override must produce buffers reachable by the role implied by the slot: an allocator bound to memory_resource_affinity::host must be host-accessible, and one bound to memory_resource_affinity::device must be device-accessible.

Parameters
affinityThe affinity slot to update.
allocatorThe override to install, or null to revert to the session allocator. Must satisfy the allocator requirements for the slot where it is being installed.
Returns
A copy of this context with the updated allocator slot.

The documentation for this class was generated from the following file: