Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Public Member Functions | List of all members
gko::scoped_device_id_guard Class Reference

This move-only class uses RAII to set the device id within a scoped block, if necessary. More...

#include <ginkgo/core/base/scoped_device_id_guard.hpp>

Public Member Functions

 scoped_device_id_guard (const ReferenceExecutor *exec, int device_id)
 Create a scoped device id from an Reference. More...
 
 scoped_device_id_guard (const OmpExecutor *exec, int device_id)
 Create a scoped device id from an OmpExecutor. More...
 
 scoped_device_id_guard (const CudaExecutor *exec, int device_id)
 Create a scoped device id from an CudaExecutor. More...
 
 scoped_device_id_guard (const HipExecutor *exec, int device_id)
 Create a scoped device id from an HipExecutor. More...
 
 scoped_device_id_guard (const DpcppExecutor *exec, int device_id)
 Create a scoped device id from an DpcppExecutor. More...
 
 scoped_device_id_guard (const scoped_device_id_guard &)=delete
 
 scoped_device_id_guard (scoped_device_id_guard &&)=default
 
scoped_device_id_guardoperator= (const scoped_device_id_guard &)=delete
 
scoped_device_id_guardoperator= (scoped_device_id_guard &&)=default
 

Detailed Description

This move-only class uses RAII to set the device id within a scoped block, if necessary.

The class behaves similar to std::scoped_lock. The scoped guard will make sure that the device code is run on the correct device within one scoped block, when run with multiple devices. Depending on the executor it will record the current device id and set the device id to the one being passed in. After the scope has been exited, the destructor sets the device_id back to the one before entering the scope. The OmpExecutor and DpcppExecutor don't require setting an device id, so in those cases, the class is a no-op.

The device id scope has to be constructed from a executor with concrete type (not plain Executor) and a device id. Only the type of the executor object is relevant, so the pointer will not be accessed, and may even be a nullptr. From the executor type the correct derived class of detail::generic_scoped_device_id_guard is picked. The following illustrates the usage of this class:

{
scoped_device_id_guard g{static_cast<CudaExecutor>(nullptr), 1};
// now the device id is set to 1
}
// now the device id is reverted again

Constructor & Destructor Documentation

◆ scoped_device_id_guard() [1/5]

gko::scoped_device_id_guard::scoped_device_id_guard ( const ReferenceExecutor exec,
int  device_id 
)

Create a scoped device id from an Reference.

The resulting object will be a noop.

Parameters
execNot used.
device_idNot used.

◆ scoped_device_id_guard() [2/5]

gko::scoped_device_id_guard::scoped_device_id_guard ( const OmpExecutor exec,
int  device_id 
)

Create a scoped device id from an OmpExecutor.

The resulting object will be a noop.

Parameters
execNot used.
device_idNot used.

◆ scoped_device_id_guard() [3/5]

gko::scoped_device_id_guard::scoped_device_id_guard ( const CudaExecutor exec,
int  device_id 
)

Create a scoped device id from an CudaExecutor.

The resulting object will set the cuda device id accordingly.

Parameters
execNot used.
device_idThe device id to use within the scope.

◆ scoped_device_id_guard() [4/5]

gko::scoped_device_id_guard::scoped_device_id_guard ( const HipExecutor exec,
int  device_id 
)

Create a scoped device id from an HipExecutor.

The resulting object will set the hip device id accordingly.

Parameters
execNot used.
device_idThe device id to use within the scope.

◆ scoped_device_id_guard() [5/5]

gko::scoped_device_id_guard::scoped_device_id_guard ( const DpcppExecutor exec,
int  device_id 
)

Create a scoped device id from an DpcppExecutor.

The resulting object will be a noop.

Parameters
execNot used.
device_idNot used.

The documentation for this class was generated from the following file:
gko::scoped_device_id_guard::scoped_device_id_guard
scoped_device_id_guard(const ReferenceExecutor *exec, int device_id)
Create a scoped device id from an Reference.