4.1.8 pde.backends.registry module

Defines the registry for managing backends.

BackendRegistry

Class handling all backends and their configurations.

load_default_config

Load a default configuration from a module.

get_backend

Return backend specified by string of instance.

registered_backends

Returns all registered backends.

class BackendRegistry[source]

Bases: object

Class handling all backends and their configurations.

Backends can exist in three different states in registry: * Registered meta-information on how to load a backend package * Loaded backend module, so the class is available * Fully instantiated BackendBase classes

get_backend(name, *, config=None, **kwargs)[source]

Return backend object, potentially loading the respective package.

The returned backend is cached if config is not specified. Consequently, the same object will be returned for repeated calls to get_backend, which allows sharing configuration parameters. Moreover, if name only specifies a backend, i.e., does not contain a colon :, the configuration of this backend is linked with the global configuration pde.config, such that changes to the config are reflected globally.

Parameters:
  • name (str) – Name of the backend to be loaded.

  • config (dict) – Additional configuration options for this specific backend. The full configuration will be taken from the global configuration and merged with the given options here.

  • **kwargs – Additional options of the backend

Returns:

An instance of the backend with the particular configuration

Return type:

BackendBase

register_backend(backend)[source]

Register a loaded backend object.

Parameters:

backend (BackendBase) – Implementation of the backend

Return type:

None

register_class(name, cls)[source]

Register a backend class.

Parameters:
  • name (str) – Name of the backend

  • cls (subclass of BackendBase) – The class for creating a backend

register_package(name, package_path, *, config=None)[source]

Register a backend python package (without loading it yet)

Parameters:
  • name (str) – Name of the backend

  • package_path (str) – Import path for the package

  • config (list) – Configuration options for the package

Return type:

None

values()[source]

Iterate over all backends that can be imported.

Return type:

Iterator[BackendBase]

get_backend(backend, config=None)[source]

Return backend specified by string of instance.

The returned backend is cached if config is not specified. Consequently, the same object will be returned for repeated calls to get_backend, which allows sharing configuration parameters. Moreover, if name only specifies a backend, i.e., does not contain a colon :, the configuration of this backend is linked with the global configuration pde.config, such that changes to the config are reflected globally.

Parameters:
  • backend (str or BackendBase) – Backend specified by name given as a string. If the string contains a colon, the first part determines the backend, whereas the second part can be used to convey additional information. For example, torch:cuda may load a torch backend and use a cuda device. As a special case, we also allow full backend objects, which are simply returned. This is a simple way to allow providing full backend objects in places where we otherwise would expect a backend name.

  • config (dict) – Additional configuration options for this specific backend. The full configuration will be taken from the global configuration and merged with the given options here. This option is only permitted if backend is a string since there otherwise might be unintended side effects of modifying an existing backend.

Returns:

An initialized backend

Return type:

BackendBase

load_default_config(module_path)[source]

Load a default configuration from a module.

Parameters:

module_path (str) – String to the module to be loaded

Return type:

list[Parameter] | None

registered_backends()[source]

Returns all registered backends.

Return type:

list[str]