Speclib  0.1.2
The library for writing better CUDA libraries
sp::MemTensorLike< Subclass, T, TensorRank, Options > Class Template Reference

CRTP base class for TensorLikes directly backed by memory. More...

#include <MemTensorLike.hpp>

Inheritance diagram for sp::MemTensorLike< Subclass, T, TensorRank, Options >:
[legend]

Public Types

using ThisType = MemTensorLike< Subclass, T, TensorRank, Options >
 
using ValueType = sp::remove_cvref_t< sp::remove_addrspace_t< T > >
 
using Type = T
 
using Opts = Options
 
using Base = TensorLike< Subclass, TensorRank >
 
template<typename NewT >
using InValueType = MemTensorLike< Subclass, NewT, TensorRank, Options >
 
using PtrType = T *
 The type of the data pointer. More...
 

Public Member Functions

auto getSizes () const
 
void setSizes (sp::Vec< int, Rank > sz)
 
auto & getSize (int n)
 
const auto & getSize (int n) const
 
auto getStrides () const
 
void setStrides (sp::Vec< int, getNumStrides()> sz)
 
auto & getStride (int n)
 
const auto & getStride (int n) const
 
auto & getTotalSize ()
 
const auto & getTotalSize () const
 
auto & sizeQuantisation ()
 
const auto & sizeQuantisation () const
 
void setDefaultSizeQuantisation ()
 
sp::Vec< int, Rank > getDimsWithPadding ()
 
void validateFields ()
 
void computeStrides ()
 Compute strides in the default way. More...
 
int totalSizeImpl () const
 Get the total memory occupied by the Tensor, in terms of elements. More...
 
sp::Vec< int, Rank > stridesImpl () const
 Default implementation of the stride-getter. More...
 
sp::Vec< int, Rank > strides () const
 
template<typename Defer = void>
int stride (int i) const
 
 MemTensorLike (PtrType ptr, const Vec< int, Rank > &sizes)
 Create a Tensor. More...
 
 MemTensorLike (PtrType ptr, const Vec< int, Rank > &sizes, const Vec< int, Rank > &strides)
 Create a Tensor with specified strides. More...
 
int computeOffsetImpl (const Vec< int, Rank > &pos) const
 Calculate the offset from the start of the buffer that a particular element can be found at. More...
 
int computeOffset (const Vec< int, Rank > &pos) const
 
const PtrType ptr (const Vec< int, Rank > &pos) const
 Get a pointer to a particular element. More...
 
PtrType ptr (const Vec< int, Rank > &pos)
 
const PtrType ptr () const
 Get a pointer to the start of the underlying buffer. More...
 
PtrType ptr ()
 
template<int Dummy = 0>
auto begin () const
 
template<int Dummy = 0>
auto end () const
 
auto getViewImpl (const sp::Vec< int, Rank > &start, const sp::Vec< int, Rank > &size)
 Create a view into this Tensor using identical striding. More...
 
auto & operator[] (int idx)
 Handy 1D subscript operator. More...
 
const auto & operator[] (int idx) const
 
void setSizeQuantisation (int x)
 Set the size quantisation for the bounds checks. More...
 
- Public Member Functions inherited from sp::TensorLike< Subclass, TensorRank >
auto dims () const
 
bool boundsCheck (const Vec< int, Rank > &pos) const
 
template<int L, CacheMode Mode = CacheMode::DEFAULT>
auto vectorRead (const Vec< int, Rank > &pos) const
 
template<int L, CacheMode Mode = CacheMode::DEFAULT>
auto vectorOffsetRead (const Vec< int, Rank > &base, const Vec< int, Rank > &offset) const
 
template<int L, CacheMode Mode = CacheMode::DEFAULT>
auto maskedVectorRead (const Vec< int, Rank > &pos) const
 
template<int L, CacheMode Mode = CacheMode::DEFAULT, typename T >
void vectorWrite (const Vec< int, Rank > &pos, const Vec< T, L > &values)
 
template<int L, CacheMode Mode = CacheMode::DEFAULT, typename T >
void maskedVectorWrite (const Vec< int, Rank > &pos, const Vec< T, L > &values)
 
int sizeQuantisation () const
 
int totalSize () const
 Get the total memory occupied by the Tensor, in terms of elements. More...
 
auto getView (const sp::Vec< int, Rank > &start, const sp::Vec< int, Rank > &size)
 Get an object that represents (and aliases) a portion of this object. More...
 
int dim (int d) const
 Behaviour common to all TensorLikes ///. More...
 
template<CacheMode Mode = CacheMode::DEFAULT>
auto read (const Vec< int, Rank > &pos) const
 Get the element at a given position. More...
 
template<CacheMode Mode = CacheMode::DEFAULT, typename T >
void write (const Vec< int, Rank > &pos, const T &value)
 Set a single element. More...
 
template<int Dummy = 0>
int size () const
 
template<int L>
void boundsCheckAccess (Vec< int, Rank > pos) const
 Bounds-check an L-element vector read at pos. More...
 

Static Public Member Functions

static constexpr int calculateNumStrides ()
 Compute the default value for NumStrides. More...
 

Public Attributes

friend Base
 
PtrType dataPtr
 
sp::Vec< int, Rank+getNumStrides()+HasTotalSize+AssertionsEnabled > variousIntegers
 

Static Public Attributes

static constexpr int Rank = TensorRank
 
static constexpr bool AllowVectorMemoryOps = true
 An overrideable flag to indicate if a concrete MemTensorLike subclass supports vector memory operations. More...
 
static constexpr int NumStrides = calculateNumStrides()
 The number of strides to explicitly store. More...
 
static constexpr int ImplicitStrides = Rank - getNumStrides()
 The number of strides that are not stored. More...
 
constexpr static bool HasTotalSize = Rank > 1 && Options::Packedness != PackMode::FULL
 
- Static Public Attributes inherited from sp::TensorLike< Subclass, TensorRank >
constexpr static int Rank = TensorRank
 

Protected Member Functions

sp::Vec< int, Rank > dimsImpl () const
 
int sizeQuantisationImpl () const
 
template<int L, CacheMode Mode>
Vec< ValueType, L > vectorReadImpl (const Vec< int, Rank > &pos) const
 This will yield a vector load instruction if the alignment permits. More...
 
template<int L, CacheMode Mode>
void vectorWriteImpl (const Vec< int, Rank > &pos, const Vec< ValueType, L > &values)
 
- Protected Member Functions inherited from sp::TensorLike< Subclass, TensorRank >
template<typename Dummy = void*>
auto dimsImpl () const
 Get an sp::Vec<int, X> representing the dimensions of the object. More...
 
bool boundsCheckImpl (const sp::Vec< int, Rank > &) const
 Return true iff the given coordinates are inside the object (Default implementation below) More...
 
int sizeQuantisationImpl () const
 The last dimension is rounded up to the next multiple of this value for the purposes of bounds checks. More...
 
template<int L, CacheMode Mode = CacheMode::DEFAULT>
auto vectorReadImpl (const Vec< int, Rank > &) const
 Read L elements, adjacent in the last dimension, starting at the given position. More...
 
template<int L, CacheMode Mode = CacheMode::DEFAULT>
auto vectorOffsetReadImpl (const Vec< int, Rank > &base, const Vec< int, Rank > &offset) const
 Read L elements, adjacent in the last dimension, from position base + offset. More...
 
template<int L, CacheMode Mode = CacheMode::DEFAULT>
auto maskedVectorReadImpl (const Vec< int, Rank > &pos) const
 Do a vectorRead that copes with the possibility of part of the vector being out of bounds. More...
 
template<int L, CacheMode Mode = CacheMode::DEFAULT, typename T >
void maskedVectorWriteImpl (const Vec< int, Rank > &pos, const Vec< T, L > &values)
 Do a vectorWrite that copes with the possibility of part of the vector being out of bounds. More...
 
template<int L, CacheMode Mode, typename T >
void vectorWriteImpl (const Vec< int, Rank > &pos, const Vec< T, L > &values)
 Write L elements, adjacent in the last dimension, starting at the given position. More...
 
auto getViewImpl (const sp::Vec< int, Rank > &start, const sp::Vec< int, Rank > &size)
 The default implementation just makes a TensorView. Not hugely fast, but always works. More...
 

Detailed Description

template<typename Subclass, typename T, int TensorRank, typename Options = sp::DefaultTensorOptions>
class sp::MemTensorLike< Subclass, T, TensorRank, Options >

CRTP base class for TensorLikes directly backed by memory.

To represent mathematical objects that are directly backed by memory, subclass this.

See also
sp::TriangularMatrix
sp::PackedTriangularMatrix

To represent a TensorLike that is not directly backed by memory, consider sp::TensorExpr

For TensorLikes that manage their own memory:

See also
sp::NomadicTensor
sp::TensorOwner.

TODO: Simplify state storage with C++20 [[no_unique_address]]

Member Typedef Documentation

◆ PtrType

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
using sp::MemTensorLike< Subclass, T, TensorRank, Options >::PtrType = T*

The type of the data pointer.

Constructor & Destructor Documentation

◆ MemTensorLike() [1/2]

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
sp::MemTensorLike< Subclass, T, TensorRank, Options >::MemTensorLike ( PtrType  ptr,
const Vec< int, Rank > &  sizes 
)

Create a Tensor.

Strides will be inferred from the sizes according to the alignment assertions specified by the template parameters.

◆ MemTensorLike() [2/2]

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
sp::MemTensorLike< Subclass, T, TensorRank, Options >::MemTensorLike ( PtrType  ptr,
const Vec< int, Rank > &  sizes,
const Vec< int, Rank > &  strides 
)

Create a Tensor with specified strides.

Member Function Documentation

◆ calculateNumStrides()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
static constexpr int sp::MemTensorLike< Subclass, T, TensorRank, Options >::calculateNumStrides ( )
staticconstexpr

Compute the default value for NumStrides.

◆ computeOffsetImpl()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
int sp::MemTensorLike< Subclass, T, TensorRank, Options >::computeOffsetImpl ( const Vec< int, Rank > &  pos) const

Calculate the offset from the start of the buffer that a particular element can be found at.

◆ computeStrides()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
void sp::MemTensorLike< Subclass, T, TensorRank, Options >::computeStrides ( )

Compute strides in the default way.

The strides are like the strides of a Rank-dimensional row-major array. If the Pitched flag is set, padding is inserted into every row to make it start at an address that's a multiple of four (to permit the use of vector load instructions).

◆ getViewImpl()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
auto sp::MemTensorLike< Subclass, T, TensorRank, Options >::getViewImpl ( const sp::Vec< int, Rank > &  start,
const sp::Vec< int, Rank > &  size 
)

Create a view into this Tensor using identical striding.

This produces a new MemTensorLike of the same type as this one representing the Dims-dimensional sub-region of this one starting at point start and extending size elements in each dimension.

The new object refers to the same underlying buffer as this one.

◆ operator[]()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
auto & sp::MemTensorLike< Subclass, T, TensorRank, Options >::operator[] ( int  idx)

Handy 1D subscript operator.

◆ ptr() [1/2]

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
const PtrType sp::MemTensorLike< Subclass, T, TensorRank, Options >::ptr ( ) const

Get a pointer to the start of the underlying buffer.

◆ ptr() [2/2]

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
const PtrType sp::MemTensorLike< Subclass, T, TensorRank, Options >::ptr ( const Vec< int, Rank > &  pos) const

Get a pointer to a particular element.

◆ setSizeQuantisation()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
void sp::MemTensorLike< Subclass, T, TensorRank, Options >::setSizeQuantisation ( int  x)

Set the size quantisation for the bounds checks.

Bounds checks will round up the last dimension to the next multiple of this value. This is usually used when you want to allow reading some junk during vector loads to avoid having to generate scalarised loads.

Since bounds checks are only performed in debug builds, this call does nothing in release builds. This feature exists only to poke a "hole" in the bounds checks to allow this particular optimisation.

◆ stridesImpl()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
sp::Vec< int, Rank > sp::MemTensorLike< Subclass, T, TensorRank, Options >::stridesImpl ( ) const

Default implementation of the stride-getter.

◆ totalSizeImpl()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
int sp::MemTensorLike< Subclass, T, TensorRank, Options >::totalSizeImpl ( ) const

Get the total memory occupied by the Tensor, in terms of elements.

◆ vectorReadImpl()

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
template<int L, CacheMode Mode>
Vec< ValueType, L > sp::MemTensorLike< Subclass, T, TensorRank, Options >::vectorReadImpl ( const Vec< int, Rank > &  pos) const
protected

This will yield a vector load instruction if the alignment permits.

If this Tensor is allocated in a pitched fashion, then reads that overlap the padding regions are permitted. The caller is expected to understand that they've asked for this, and expect some of the returned vector to be junk.

Member Data Documentation

◆ AllowVectorMemoryOps

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
constexpr bool sp::MemTensorLike< Subclass, T, TensorRank, Options >::AllowVectorMemoryOps = true
staticconstexpr

An overrideable flag to indicate if a concrete MemTensorLike subclass supports vector memory operations.

◆ ImplicitStrides

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
constexpr int sp::MemTensorLike< Subclass, T, TensorRank, Options >::ImplicitStrides = Rank - getNumStrides()
staticconstexpr

The number of strides that are not stored.

◆ NumStrides

template<typename Subclass , typename T , int TensorRank, typename Options = sp::DefaultTensorOptions>
constexpr int sp::MemTensorLike< Subclass, T, TensorRank, Options >::NumStrides = calculateNumStrides()
staticconstexpr

The number of strides to explicitly store.