Device Tensor#
-
template<typename T, size_t Rank>
struct DeviceTensor : public virtual einsums::tensor_props::DeviceTensorBase, public virtual einsums::tensor_props::TRBasicTensorBase<T, Rank>, public virtual einsums::tensor_props::AlgebraOptimizedTensor, public virtual einsums::tensor_props::DevTypedTensorBase<T>, public virtual einsums::tensor_props::LockableTensorBase# Makes tensor functionality available to the GPU.
- Template Parameters:
T – The type of the data managed by the tensor.
Rank – The rank of the tensor.
Public Types
Public Functions
-
DeviceTensor() = default#
Construct a new tensor on the GPU.
-
DeviceTensor(const DeviceTensor<T, Rank> &other, detail::HostToDeviceMode mode = detail::UNKNOWN)#
Copy construct a new GPU tensor.
-
~DeviceTensor()#
Destructor.
-
template<typename ...Dims>
explicit DeviceTensor(std::string name, detail::HostToDeviceMode mode, Dims... dims)# Construct a new DeviceTensor object with the given name and dimensions.
Constructs a new DeviceTensor object using the information provided in
name
anddims
.auto A = DeviceTensor("A", 3, 3);
The newly constructed DeviceTensor is NOT zeroed out for you. If you start having NaN issues in your code try calling DeviceTensor.zero() or zero(DeviceTensor) to see if that resolves it.
- Template Parameters:
Dims – Variadic template arguments for the dimensions. Must be castable to size_t.
- Parameters:
name – Name of the new tensor.
dims – The dimensions of each rank of the tensor.
-
template<typename ...Dims>
explicit DeviceTensor(std::string name, Dims... dims)# Construct a new DeviceTensor object with the given name and dimensions.
Constructs a new DeviceTensor object using the information provided in
name
anddims
.auto A = DeviceTensor("A", 3, 3);
The newly constructed DeviceTensor is NOT zeroed out for you. If you start having NaN issues in your code try calling DeviceTensor.zero() or zero(DeviceTensor) to see if that resolves it.
- Template Parameters:
Dims – Variadic template arguments for the dimensions. Must be castable to size_t.
- Parameters:
name – Name of the new tensor.
dims – The dimensions of each rank of the tensor.
-
template<size_t OtherRank, typename ...Dims>
explicit DeviceTensor(DeviceTensor<T, OtherRank> &&existingTensor, std::string name, Dims... dims)# Construct a new DeviceTensor object.
Moving
existingTensor
data to the new tensor.This constructor is useful for reshaping a tensor. It does not modify the underlying tensor data. It only creates new mapping arrays for how the data is viewed.
auto A = DeviceTensor("A", 27); // Creates a rank-1 tensor of 27 elements auto B = DeviceTensor(std::move(A), "B", 3, 3, 3); // Creates a rank-3 tensor of 27 elements // At this point A is no longer valid.
Supports using -1 for one of the ranks to automatically compute the dimensional of it.
auto A = DeviceTensor("A", 27); auto B = DeviceTensor(std::move(A), "B", 3, -1, 3); // Automatically determines that -1 should be 3.
- Template Parameters:
OtherRank – The rank of
existingTensor
can be different than the rank of the new tensorDims – Variadic template arguments for the dimensions. Must be castable to size_t.
- Parameters:
existingTensor – The existing tensor that holds the tensor data.
name – The name of the new tensor
dims – The dimensionality of each rank of the new tensor.
-
explicit DeviceTensor(Dim<Rank> dims, detail::HostToDeviceMode mode = detail::DEV_ONLY)#
Construct a new DeviceTensor object using the dimensions given by Dim object.
- Parameters:
dims – The dimensions of the new tensor in Dim form.
-
DeviceTensor(const DeviceTensorView<T, Rank> &other)#
Construct a new Tensor object from a TensorView.
Data is explicitly copied from the view to the new tensor.
- Parameters:
other – The tensor view to copy.
-
template<typename ...Dims>
inline std::enable_if_t<(std::is_integral_v<Dims> && ... && (sizeof...(Dims) == Rank)), void> resize(Dims... dims)# Resize a tensor.
- Parameters:
dims – The new dimensions of a tensor.
-
void zero()#
Zeroes out the tensor data.
-
void set_all(T value)#
Set the all entries to the given value.
- Parameters:
value – Value to set the elements to.
-
inline dev_datatype *gpu_data()#
Returns a pointer to the data.
Try very hard to not use this function. Current data may or may not exist on the host device at the time of the call if using GPU backend.
- Returns:
T* A pointer to the data.
-
inline const dev_datatype *gpu_data() const#
Returns a constant pointer to the data.
Try very hard to not use this function. Current data may or may not exist on the host device at the time of the call if using GPU backend.
- Returns:
const T* An immutable pointer to the data.
-
template<typename ...MultiIndex>
dev_datatype *gpu_data(MultiIndex... index)# Returns a pointer into the tensor at the given location.
auto A = DeviceTensor("A", 3, 3, 3); // Creates a rank-3 tensor of 27 elements double* A_pointer = A.data(1, 2, 3); // Returns the pointer to element (1, 2, 3) in A.
- Template Parameters:
MultiIndex – The datatypes of the passed parameters. Must be castable to
- Parameters:
index – The explicit desired index into the tensor. Must be castable to std::int64_t.
- Returns:
A pointer into the tensor at the requested location.
-
template<typename ...MultiIndex>
const dev_datatype *gpu_data(MultiIndex... index) const#
-
inline virtual host_datatype *data() override#
Returns a pointer to the host-readable data.
Returns a pointer to the data as available to the host. If the tensor is in DEV_ONLY mode, then this will return a null pointer. Otherwise, the pointer should be useable, but the data may be outdated.
- Returns:
T* A pointer to the data.
-
inline virtual const host_datatype *data() const override#
Returns a pointer to the host-readable data.
Returns a pointer to the data as available to the host. If the tensor is in DEV_ONLY mode, then this will return a null pointer. Otherwise, the pointer should be useable, but the data may be outdated.
- Returns:
const T* An immutable pointer to the data.
-
template<typename ...MultiIndex>
host_datatype *data(MultiIndex... index)# Returns a pointer into the tensor at the given location.
auto A = DeviceTensor("A", 3, 3, 3); // Creates a rank-3 tensor of 27 elements double* A_pointer = A.data(1, 2, 3); // Returns the pointer to element (1, 2, 3) in A.
- Template Parameters:
MultiIndex – The datatypes of the passed parameters. Must be castable to
- Parameters:
index – The explicit desired index into the tensor. Must be castable to std::int64_t.
- Returns:
A pointer into the tensor at the requested location.
-
template<typename ...MultiIndex>
const host_datatype *data(MultiIndex... index) const#
-
void read(const std::vector<T> &data)#
Sends data from the host to the device.
- Parameters:
data – The vector data.
-
void write(std::vector<T> &data)#
Sends data from the device to the host.
- Parameters:
data – The vector that will be filled.
-
void read(const T *data)#
Sends data from the host to the device.
- Parameters:
data – The vector data.
-
void write(T *data)#
Sends data from the device to the host.
- Parameters:
data – The vector that will be filled.
-
DeviceTensor<T, Rank> &assign(const DeviceTensor<T, Rank> &other)#
Assignments.
-
DeviceTensor<T, Rank> &init(const DeviceTensor<T, Rank> &other, einsums::detail::HostToDeviceMode mode = einsums::detail::UNKNOWN)#
-
template<typename TOther>
DeviceTensor<T, Rank> &assign(const DeviceTensor<TOther, Rank> &other)#
-
template<typename TOther>
DeviceTensor<T, Rank> &assign(const DeviceTensorView<TOther, Rank> &other)#
-
template<typename ...MultiIndex>
T operator()(MultiIndex... index) const# Subscripts into the tensor.
This version works when all elements are explicit values into the tensor. It does not work with the All or Range tags.
- Template Parameters:
MultiIndex – Datatype of the indices. Must be castable to std::int64_t.
- Parameters:
index – The explicit desired index into the tensor. Elements must be castable to std::int64_t.
- Returns:
const T&
-
template<typename ...MultiIndex>
HostDevReference<T> operator()(MultiIndex... index)# Subscripts into the tensor.
This version works when all elements are explicit values into the tensor. It does not work with the All or Range tags.
- Template Parameters:
MultiIndex – Datatype of the indices. Must be castable to std::int64_t.
- Parameters:
index – The explicit desired index into the tensor. Elements must be castable to std::int64_t.
- Returns:
const T&
-
template<typename ...MultiIndex>
DeviceTensorView<T, count_of_type<AllT, MultiIndex...>() + count_of_type<Range, MultiIndex...>()> operator()(MultiIndex... index)# Subscripts into the tensor and creates a view.
-
template<typename ...MultiIndex>
DeviceTensorView<T, Rank> operator()(MultiIndex... index) const# Subscripts into the tensor and creates a view.
-
DeviceTensor<T, Rank> &operator=(const DeviceTensor<T, Rank> &other)#
Copy data from one tensor to another.
-
template<typename TOther>
DeviceTensor<T, Rank> &operator=(const DeviceTensor<TOther, Rank> &other)# Copy data from one tensor to another, and convert types.
-
template<typename TOther>
DeviceTensor<T, Rank> &operator=(const DeviceTensorView<TOther, Rank> &other)# Copy data from a tensor view into a tensor.
-
DeviceTensor<T, Rank> &operator=(const Tensor<T, Rank> &other)#
Copy data from one tensor to another.
-
DeviceTensor<T, Rank> &operator=(const T &fill_value)#
Fill a tensor with a value.
-
DeviceTensor<T, Rank> &add_assign(const T &other)#
Operate and assign every element with a scalar.
-
DeviceTensor<T, Rank> &mult_assign(const T &other)#
-
DeviceTensor<T, Rank> &sub_assign(const T &other)#
-
DeviceTensor<T, Rank> &div_assign(const T &other)#
-
inline DeviceTensor<T, Rank> &operator*=(const T &other)#
-
inline DeviceTensor<T, Rank> &operator+=(const T &other)#
-
inline DeviceTensor<T, Rank> &operator-=(const T &other)#
-
inline DeviceTensor<T, Rank> &operator/=(const T &other)#
-
DeviceTensor<T, Rank> &add_assign(const DeviceTensor<T, Rank> &other)#
Operate and assign two tensors element-wise.
-
DeviceTensor<T, Rank> &mult_assign(const DeviceTensor<T, Rank> &other)#
-
DeviceTensor<T, Rank> &sub_assign(const DeviceTensor<T, Rank> &other)#
-
DeviceTensor<T, Rank> &div_assign(const DeviceTensor<T, Rank> &other)#
-
inline DeviceTensor<T, Rank> &operator*=(const DeviceTensor<T, Rank> &other)#
-
inline DeviceTensor<T, Rank> &operator+=(const DeviceTensor<T, Rank> &other)#
-
inline DeviceTensor<T, Rank> &operator-=(const DeviceTensor<T, Rank> &other)#
-
inline DeviceTensor<T, Rank> &operator/=(const DeviceTensor<T, Rank> &other)#
-
inline virtual size_t dim(int d) const override#
Get the dimension for the given rank.
-
inline size_t *gpu_dims()#
Get the dimensions available to the GPU.
-
inline const size_t *gpu_dims() const#
Get the dimensions available to the GPU.
-
inline virtual const std::string &name() const override#
Get the name of the tensor.
-
inline virtual void set_name(const std::string &name) override#
Set the name of the tensor.
-
inline virtual size_t stride(int d) const noexcept override#
Get the stride of the given rank.
-
inline size_t *gpu_strides()#
Get the strides available to the GPU.
-
inline const size_t *gpu_strides() const#
Get the strides available to the GPU.
-
inline DeviceTensorView<T, 1> to_rank_1_view() const#
Convert to a rank 1 tensor view.
-
inline size_t size() const#
Returns the linear size of the tensor.
-
inline virtual bool full_view_of_underlying() const noexcept override#
Whether this object is the full view.
-
inline detail::HostToDeviceMode mode() const#
Return the mode of the tensor.