Mila 0.13.48
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Compute::DeviceRegistry Class Referenceexport

Registry of discovered compute devices with lazy instantiation. More...

Public Types

using DeviceFactory = std::function<std::shared_ptr<Device>( DeviceConstructionKey )>

Public Member Functions

 DeviceRegistry (const DeviceRegistry &)=delete
std::shared_ptr< DevicegetDevice (DeviceId id) const
 Retrieve or create a device by ID.
std::size_t getDeviceCount (DeviceType device_type) const noexcept
 Return number of registered devices for the given DeviceType.
bool hasDevice (DeviceId id) const noexcept
 Non-throwing check whether a specific DeviceId has been registered.
bool hasDeviceType (DeviceType type) const
 Check whether a specific DeviceType has been registered.
std::vector< DeviceIdlistDeviceIds () const
 List registered DeviceId values.
std::vector< DeviceTypelistDeviceTypes () const
 List registered device types (unique).
DeviceRegistryoperator= (const DeviceRegistry &)=delete
void registerDevice (DeviceId id, DeviceFactory factory)
 Register a discovered DeviceId with factory callback.

Static Public Member Functions

static DeviceRegistryinstance ()

Private Member Functions

 DeviceRegistry ()=default

Private Attributes

std::unordered_map< DeviceId, std::shared_ptr< Device > > device_cache_
std::unordered_map< DeviceId, DeviceFactoryfactories_
std::mutex mutex_
std::vector< DeviceIdregistered_ids_

Detailed Description

Registry of discovered compute devices with lazy instantiation.

Thread-safe singleton that stores registered DeviceId values and creates concrete Device instances on-demand. Device registrars register lightweight DeviceId values during initialization; Device objects are created lazily when first requested and then cached for subsequent calls.

Member Typedef Documentation

◆ DeviceFactory

Constructor & Destructor Documentation

◆ DeviceRegistry() [1/2]

Mila::Dnn::Compute::DeviceRegistry::DeviceRegistry ( const DeviceRegistry & )
delete
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeviceRegistry() [2/2]

Mila::Dnn::Compute::DeviceRegistry::DeviceRegistry ( )
privatedefault

Member Function Documentation

◆ getDevice()

std::shared_ptr< Device > Mila::Dnn::Compute::DeviceRegistry::getDevice ( DeviceId id) const
inline

Retrieve or create a device by ID.

Validates that the DeviceId is registered, then returns a cached Device instance. If this is the first request, invokes the factory callback to create the device and caches it.

Parameters
idDevice identifier (e.g., Device::Cuda(0), Device::Cpu())
Returns
std::shared_ptr<Device> Cached device instance with properties
Exceptions
std::runtime_errorIf DeviceId is not registered or creation fails
Here is the call graph for this function:

◆ getDeviceCount()

std::size_t Mila::Dnn::Compute::DeviceRegistry::getDeviceCount ( DeviceType device_type) const
inlinenoexcept

Return number of registered devices for the given DeviceType.

Parameters
device_typeDevice type to count
Returns
std::size_t Number of registered devices of this type (0 on error)

◆ hasDevice()

bool Mila::Dnn::Compute::DeviceRegistry::hasDevice ( DeviceId id) const
inlinenoexcept

Non-throwing check whether a specific DeviceId has been registered.

Parameters
idDevice identifier to check
Returns
bool True if device is registered, false otherwise (or on error)

◆ hasDeviceType()

bool Mila::Dnn::Compute::DeviceRegistry::hasDeviceType ( DeviceType type) const
inline

Check whether a specific DeviceType has been registered.

Parameters
typeDevice type to check (Cpu, Cuda, Metal, Rocm)
Returns
bool True if at least one device of this type is registered

◆ instance()

DeviceRegistry & Mila::Dnn::Compute::DeviceRegistry::instance ( )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ listDeviceIds()

std::vector< DeviceId > Mila::Dnn::Compute::DeviceRegistry::listDeviceIds ( ) const
inline

List registered DeviceId values.

Returns
std::vector<DeviceId> Vector of registered device identifiers

◆ listDeviceTypes()

std::vector< DeviceType > Mila::Dnn::Compute::DeviceRegistry::listDeviceTypes ( ) const
inline

List registered device types (unique).

Returns
std::vector<DeviceType> Vector of unique device types

◆ operator=()

DeviceRegistry & Mila::Dnn::Compute::DeviceRegistry::operator= ( const DeviceRegistry & )
delete
Here is the call graph for this function:

◆ registerDevice()

void Mila::Dnn::Compute::DeviceRegistry::registerDevice ( DeviceId id,
DeviceFactory factory )
inline

Register a discovered DeviceId with factory callback.

Parameters
idDevice identifier to register
factoryFactory function to create the device instance
Exceptions
std::runtime_errorIf DeviceId is already registered
Here is the call graph for this function:

Member Data Documentation

◆ device_cache_

std::unordered_map<DeviceId, std::shared_ptr<Device> > Mila::Dnn::Compute::DeviceRegistry::device_cache_
mutableprivate

◆ factories_

std::unordered_map<DeviceId, DeviceFactory> Mila::Dnn::Compute::DeviceRegistry::factories_
private

◆ mutex_

std::mutex Mila::Dnn::Compute::DeviceRegistry::mutex_
mutableprivate

◆ registered_ids_

std::vector<DeviceId> Mila::Dnn::Compute::DeviceRegistry::registered_ids_
private

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