4.1.1. Devices¶
4.1.1.1. Base Class¶
-
class
Device: public primitiv::mixins::DefaultSettable<Device>, primitiv::mixins::Nonmovable<Device>¶ Interface of the Tensor provider.
Subclassed by primitiv::devices::CUDA, primitiv::devices::CUDA16, primitiv::devices::Eigen, primitiv::devices::Naive, primitiv::devices::OpenCL
Public Functions
-
virtual void
dump_description() const = 0¶ Prints device description to stderr.
-
virtual DeviceType
type() const = 0¶ Retrieves the type of the device.
- Return
- A DeviceType value.
-
Tensor
new_tensor_by_constant(const Shape &shape, float k)¶ Provides a new Tensor object with same-value elements.
-
Tensor
new_tensor_by_array(const Shape &shape, const float values[])¶ Provides a new Tensor object with specific values.
-
Tensor
new_tensor_by_vector(const Shape &shape, const std::vector<float> &values)¶ Provides a new Tensor object with specific values.
-
Tensor
copy_tensor(const Tensor &x)¶ Copies the tensor to this device with allocating a new memory.
- Return
- Copied tensor.
- Remark
- The value of
xis always duplicated, and the internal memory of the resulting tensor becomes always different fromxeven ifx.device()is same asthis. - Parameters
x: A tensor to be copied.
-
void
inplace_multiply_const(float k, Tensor &x)¶ Directly multiplies all elements by a constant.
- Parameters
k: A constant to multiply.x: A tensor to be updated.
-
void
inplace_add(const Tensor &x, Tensor &y)¶ Directly adds the first tensor to the second tensor.
- Remark
- This method keeps the shape of
y, and the behavior is conditioned according to the batch size ofyandx: y.shape == x.shape: y += x y.shape == 1: y += batch_sum(x) x.shape == 1: y += batch_broadcast(x) otherwise: error. - Parameters
x: A tensor to add.y: A tensor to be udpated.
-
virtual void
4.1.1.2. Inherited Classes¶
-
class
CUDA: public primitiv::Device¶ -
Public Functions
-
CUDA(std::uint32_t device_id)¶ Creates a new CUDA device.
- Remark
- The random number generator is initialized using
std::random_device. - Parameters
device_id: ID of the physical GPU.
-
CUDA(std::uint32_t device_id, std::uint32_t rng_seed)¶ Creates a new CUDA device.
- Parameters
device_id: ID of the physical GPU.rng_seed: The seed value of the random number generator.
-
void
dump_description() const¶ Prints device description to stderr.
-
DeviceType
type() const¶ Retrieves the type of the device.
- Return
- A DeviceType value.
Public Static Functions
-
static std::uint32_t
num_devices()¶ Retrieves the number of active hardwares.
- Return
- Number of active hardwares.
-
-
class
OpenCL: public primitiv::Device¶ -
Public Functions
-
OpenCL(std::uint32_t platform_id, std::uint32_t device_id)¶ Creates a new OpenCL device.
- Parameters
platform_id: Platform ID.device_id: Device ID on the selected platform.
-
OpenCL(std::uint32_t platform_id, std::uint32_t device_id, std::uint32_t rng_seed)¶ Creates a new OpenCL device.
- Parameters
platform_id: Platform ID.device_id: Device ID on the selected platform.rng_seed: Seed value of the random number generator.
-
void
dump_description() const¶ Prints device description to stderr.
-
DeviceType
type() const¶ Retrieves the type of the device.
- Return
- A DeviceType value.
Public Static Functions
-
static std::uint32_t
num_platforms()¶ Retrieves the number of active platforms.
- Return
- Number of active platforms.
-
static std::uint32_t
num_devices(std::uint32_t platform_id)¶ Retrieves the number of active devices on the specified platform.
- Return
- Number of active devices.
- Parameters
platform_id: Platform ID. This value should be between 0 to num_platforms() - 1.
-
static void
assert_support(std::uint32_t platform_id, std::uint32_t device_id)¶ Checks whether the device corresponding to the specified IDs is supported.
- Parameters
platform_id: Platform ID to check.device_id: Device ID to check.
- Exceptions
primitiv::Error: This class does not support the specified device.
-
static bool
check_support(std::uint32_t platform_id, std::uint32_t device_id)¶ Checks whether the device corresponding to the specified ID is supported.
- Return
- true if this class supports the specified device, false otherwise.
- Parameters
platform_id: Platform ID to check.device_id: Device ID to check.
-