Template Class TensorBatchWrap
Defined in File TensorBatchWrap.hpp
Class Documentation
-
template<typename T, int... Strides>
class TensorBatchWrap TensorBatchWrap class is a non-owning wrap of a batch of N-D tensors used for easy access of its elements in CUDA device.
TensorBatchWrap is a wrapper of a batch of multi-dimensional tensors that can have one or more of its N dimension strides, or pitches, defined either at compile-time or at run-time. Each pitch in
Strides
represents the offset in bytes as a compile-time template parameter that will be applied from the first (slowest changing) dimension to the last (fastest changing) dimension of the tensor, in that order. Each dimension with run-time pitch is specified as -1 in theStrides
template parameter.Template arguments:
T type of the values inside the tensors
Strides sequence of compile- or run-time pitches (-1 indicates run-time)
Y compile-time pitches
X run-time pitches
N dimensions, where N = X + Y
For example, in the code below a wrap is defined for a batch of HWC 3D tensors where each row in H has a run-time row pitch (second -1), a pixel in W has a compile-time constant pitch as the size of the pixel type and a channel in C has also a compile-time constant pitch as the size of the channel type.
using DataType = ...; using ChannelType = BaseType<DataType>; using TensorBatchWrap = TensorBatchWrap<ChannelType, -1, sizeof(DataType), sizeof(ChannelType)>; TensorBatch tensorBatch = ...; TensorBatchWrap tensorBatchWrap(tensorBatch.data()); // Elements may be accessed via operator[] using an int4 argument. They can also be accessed via pointer using // the ptr method with up to 4 integer arguments or by accessing each TensorWrap separately with tensor(...) method.
TensorBatch wrapper class specialized for non-constant value type.
See also
- Template Parameters:
T – Type (it can be const) of each element inside the tensor wrapper.
Strides – Each compile-time (use -1 for run-time) pitch in bytes from first to last dimension.
T – Type (non-const) of each element inside the tensor batch wrapper.
Strides – Each compile-time (use -1 for run-time) pitch in bytes from first to last dimension.
Public Functions
-
inline __host__ TensorBatchWrap(const TensorBatchDataStridedCuda &data)
Constructs a TensorBatchWrap by wrapping a
data
argument.- Parameters:
data – [in] Tensor batch data to wrap.
- inline __host__ __device__ TensorBatchWrap (NVCVTensorBatchData &data)
Constructs a TensorBatchWrap by wrapping a
data
argument.- Parameters:
data – [in] Tensor batch data to wrap.
- template<typename... Coords> inline __host__ __device__ T * ptr (int t, Coords... c) const
Get a read-and-write proxy (as pointer) of the given tensor at the given coordinates.
- Parameters:
t – [in] Tensor index in the list.
c – [in] Coordinates in the given tensor;
- Returns:
The const pointer to the beginning of the given coordinates.
- inline __host__ __device__ auto tensor (int t) const
Constructs a read-and-write wrapper for the tensor on index
t
The list of static strides can be provided as a template parameter. It should be a list of N outer strides (from inner to outer).- Template Parameters:
Strides – static strides
- Parameters:
t – index of the tensor
- template<typename DimType, class = Require<std::is_same_v<int, BaseType<DimType>>>> inline __host__ __device__ T & operator[] (DimType c) const
Subscript operator for read-and-write access.
- Parameters:
t – [in] Tensor index in the list.
c – [in] (N+1)-D coordinates - tensor index and coords (from inner to outer) to be accessed. E.g. for a 2-dimensional tensors, the coordinates would be: {tensor_id, column, row}
- Returns:
Accessed reference.
- template<typename... Args> inline __host__ __device__ T * doGetPtr (int t, Args... c) const
- inline const __host__ __device__ int64_t * strides (int t) const
Returns a pointer to a stride buffer of the tensor at index
t
.- Parameters:
t – tensor index
Public Members
-
NVCVTensorBatchElementStridedRec *m_tensors
Public Static Attributes
-
static constexpr int kNumDimensions