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.

Return
A new Tensor object.
Parameters
  • shape: Shape of the tensor.
  • k: Constant to initialize elements.

Tensor new_tensor_by_array(const Shape &shape, const float values[])

Provides a new Tensor object with specific values.

Return
A new Tensor object.
Parameters
  • shape: Shape of the tensor.
  • values: Pointer to array of internal values.

Tensor new_tensor_by_vector(const Shape &shape, const std::vector<float> &values)

Provides a new Tensor object with specific values.

Return
A new Tensor object.
Parameters
  • shape: Shape of the tensor.
  • values: List of internal 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 x is always duplicated, and the internal memory of the resulting tensor becomes always different from x even if x.device() is same as this.
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 of y and x: 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.

void inplace_subtract(const Tensor &x, Tensor &y)

Directly subtracts the first tensor from the second tensor.

Remark
The batch broadcasting behavior of this method is same as that of inplace_add.
Parameters
  • x: A tensor to subtract.
  • y: A tensor to be updated.

4.1.1.2. Inherited Classes

class Naive : public primitiv::Device

Device class for the naive function implementations on CPU.

Public Functions

Naive()

Creates a Naive object.

Naive(std::uint32_t seed)

Creates a Naive object.

Parameters
  • seed: The seed value of internal 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.

class Eigen : public primitiv::Device

Device class for the Eigen3 backend.

Public Functions

Eigen()

Creates a Eigen object.

Eigen(std::uint32_t seed)

Creates a Eigen object.

Parameters
  • seed: The seed value of internal 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.

class CUDA : public primitiv::Device

Device class for CUDA.

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.

static void assert_support(std::uint32_t device_id)

Checks whether the device corresponding to the specified ID is supported.

Parameters
  • device_id: Device ID to check.
Exceptions
  • primitiv::Error: This class does not support the specified device.

static bool check_support(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
  • device_id: Device ID to check.

class OpenCL : public primitiv::Device

Device class for OpenCL.

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.