Speclib  0.1.2
The library for writing better CUDA libraries
sp::StringView< CharT, IntT, Options > Class Template Reference

A GPU-compatible string view. More...

#include <StringView.hpp>

Inheritance diagram for sp::StringView< CharT, IntT, Options >:
[legend]

Public Types

using value_type = sp::remove_addrspace_t< std::remove_const_t< CharT > >
 
using traits_type = std::char_traits< value_type >
 
using pointer = CharT *
 
using const_pointer = const CharT *
 
using reference = CharT &
 
using const_reference = const CharT &
 
using size_type = IntT
 
using difference_type = IntT
 
using ThisType = StringView< CharT, IntT >
 
using BaseClass = Vector< CharT, Options >
 
using CharType = sp::remove_addrspace_t< CharT >
 
- Public Types inherited from sp::Tensor< T, R, Options >
using Opts = Options
 
using Base = MemTensorLike< Tensor< T, Rank, Options >, T, Rank, Options >
 
using ValueType = sp::remove_cvref_t< sp::remove_addrspace_t< T > >
 
using Type = T
 
using PtrType = typename Base::PtrType
 
using ConstTensor = Tensor< const T, R, Options >
 
using ThisT = Tensor< T, R, Options >
 
template<typename NewT >
using InValueType = Tensor< NewT, R, Options >
 
- Public Types inherited from sp::MemTensorLike< Tensor< T, R, sp::DefaultTensorOptions >, T, R, sp::DefaultTensorOptions >
using ThisType = MemTensorLike< Tensor< T, R, sp::DefaultTensorOptions >, T, TensorRank, sp::DefaultTensorOptions >
 
using ValueType = sp::remove_cvref_t< sp::remove_addrspace_t< T > >
 
using Type = T
 
using Opts = sp::DefaultTensorOptions
 
using Base = TensorLike< Tensor< T, R, sp::DefaultTensorOptions >, TensorRank >
 
using InValueType = MemTensorLike< Tensor< T, R, sp::DefaultTensorOptions >, NewT, TensorRank, sp::DefaultTensorOptions >
 
using PtrType = T *
 The type of the data pointer. More...
 

Public Member Functions

constexpr StringView (CharT *ptr, IntT size)
 
constexpr StringView (CharT *ptr, IntT size, int quant)
 
constexpr StringView (CharT *ptr)
 
constexpr StringView (const std::basic_string_view< value_type > &other)
 
constexpr StringView (const std::basic_string< value_type > &other)
 
constexpr auto length () const
 Get the size of the view. More...
 
__host__ int indexOf (CharType c) const
 
template<typename Str >
__host__ int indexOfAny () const
 
constexpr IntT max_size () const noexcept
 Get the largest possible size of a string view using this IntT. More...
 
constexpr bool empty () const
 Checks whether the view is empty. More...
 
constexpr const CharType at (IntT idx) const
 Get a copy of the character at position idx. More...
 
constexpr auto & front () const
 Access the first character. More...
 
constexpr auto & back () const
 Access the last character. More...
 
constexpr void remove_prefix (IntT amount)
 Move the start of the view forward by amount. More...
 
constexpr void remove_suffix (IntT amount)
 Move the end of the view backward by amount. More...
 
constexpr ThisType substr (IntT pos=0, IntT count=npos)
 Get a view representing a substring of this one. More...
 
constexpr ThisType substr (sp::Vec< int, 2 > range)
 
constexpr const CharT * data () const
 Get the underlying data pointer. More...
 
constexpr CharT * data ()
 Get the underlying data pointer. More...
 
constexpr bool operator== (const ThisType &other) const
 Compare the contents of two StringViews. More...
 
constexpr bool operator!= (const ThisType &other) const
 Compare the contents of two StringViews. More...
 
constexpr operator StringView< const CharT, IntT, Options > ()
 Allow implicitly adding constness to the view. More...
 
template<typename Q >
constexpr bool starts_with (StringView< const CharT, Q > chars)
 Implicitly convert to std::string_view when on the host. More...
 
template<int L>
constexpr bool starts_with (const sp::Vec< std::remove_const_t< CharT >, L > chars)
 Determine if the view starts with the given constant sequence of characters. More...
 
template<typename Q >
constexpr bool ends_with (StringView< const CharT, Q > chars)
 Determine if this string view ends with the other one. More...
 
template<int L>
constexpr bool ends_with (const sp::Vec< std::remove_const_t< CharT >, L > chars)
 Determine if the view ends with the given constant sequence of characters. More...
 
- Public Member Functions inherited from sp::Tensor< T, R, Options >
 operator ConstTensor () const
 
 Tensor (PtrType data, const Vec< int, Rank > &sizes)
 Create an R-dimensional packed Tensor of the given sizes. More...
 
 Tensor (PtrType data, const Vec< int, Rank > &sizes, const Vec< int, Rank > &strides)
 Create an R-dimensional Tensor with given strides and sizes. More...
 
template<typename U = T>
 Tensor (PtrType data, int size, typename std::enable_if< Rank==1 &&std::is_same< U, T >()>::type *=nullptr)
 Construct a 1D packed Tensor of given size. More...
 
template<typename U = T>
 Tensor (PtrType data, int size, int stride, typename std::enable_if< Rank==1 &&std::is_same< U, T >()>::type *=nullptr)
 Construct a 1D packed Tensor with given size and stride. More...
 
template<int Dummy = 0>
auto row (int n)
 For a 2D tensor, get the nth row as a 1D tensor. More...
 
template<int Dummy = 0>
auto rows (int start, int n)
 
template<int Dummy = 0>
auto col (int n)
 
template<int Dummy = 0>
auto cols (int start, int n)
 
- Public Member Functions inherited from sp::MemTensorLike< Tensor< T, R, sp::DefaultTensorOptions >, T, R, sp::DefaultTensorOptions >
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
 
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 ()
 
auto begin () const
 
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 Attributes

static constexpr size_type npos = size_type(-1)
 
- Static Public Attributes inherited from sp::Tensor< T, R, Options >
static constexpr int Rank = R
 
static constexpr int MaxVectorSize = (Opts::Pitched && Opts::Packedness != PackMode::NONE) ? sp::getMaxGMemVectorSize<T>() : 1
 
- Static Public Attributes inherited from sp::MemTensorLike< Tensor< T, R, sp::DefaultTensorOptions >, T, R, sp::DefaultTensorOptions >
static constexpr int Rank
 
static constexpr bool AllowVectorMemoryOps
 An overrideable flag to indicate if a concrete MemTensorLike subclass supports vector memory operations. More...
 
static constexpr int NumStrides
 The number of strides to explicitly store. More...
 
static constexpr int ImplicitStrides
 The number of strides that are not stored. More...
 
constexpr static bool HasTotalSize
 
- Static Public Attributes inherited from sp::TensorLike< Subclass, TensorRank >
constexpr static int Rank = TensorRank
 

Additional Inherited Members

- Static Public Member Functions inherited from sp::MemTensorLike< Tensor< T, R, sp::DefaultTensorOptions >, T, R, sp::DefaultTensorOptions >
static constexpr int calculateNumStrides ()
 Compute the default value for NumStrides. More...
 
- Public Attributes inherited from sp::MemTensorLike< Tensor< T, R, sp::DefaultTensorOptions >, T, R, sp::DefaultTensorOptions >
friend Base
 
PtrType dataPtr
 
sp::Vec< int, Rank+getNumStrides()+HasTotalSize+AssertionsEnabled > variousIntegers
 
- Protected Member Functions inherited from sp::MemTensorLike< Tensor< T, R, sp::DefaultTensorOptions >, T, R, sp::DefaultTensorOptions >
sp::Vec< int, Rank > dimsImpl () const
 
int sizeQuantisationImpl () const
 
Vec< ValueType, L > vectorReadImpl (const Vec< int, Rank > &pos) const
 This will yield a vector load instruction if the alignment permits. More...
 
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 CharT, typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
class sp::StringView< CharT, IntT, Options >

A GPU-compatible string view.

This is very similar to std::string_view, except:

  • By default, 32-bit integers are used to store the size. This yields a significant performance benefit on GPUs, which typically only have 16-bit integer hardware. You can opt to use even shorter ints if your strings are short enough.
  • If given a non-constant character type, the view can be used to mutate the underlying buffer. Avoiding memory corruption is left as an exercise for the reader.
  • Address-space specifiers are allowed.

Typically, GPU kernels that want to process strings will be given a sp::StringView obtained via sp::StringBuffer::deviceView(), which will also take care of any necessary host<->device copies.

Template Parameters
CharTThe type of characters the string is comprised of.
IntTThe integer type to use to represent the length.

Member Function Documentation

◆ at()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr const CharType sp::StringView< CharT, IntT, Options >::at ( IntT  idx) const
constexpr

Get a copy of the character at position idx.

◆ back()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr auto & sp::StringView< CharT, IntT, Options >::back ( ) const
constexpr

Access the last character.

◆ data() [1/2]

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr CharT * sp::StringView< CharT, IntT, Options >::data ( )
constexpr

Get the underlying data pointer.

◆ data() [2/2]

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr const CharT * sp::StringView< CharT, IntT, Options >::data ( ) const
constexpr

Get the underlying data pointer.

◆ empty()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr bool sp::StringView< CharT, IntT, Options >::empty ( ) const
constexpr

Checks whether the view is empty.

◆ ends_with() [1/2]

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
template<int L>
constexpr bool sp::StringView< CharT, IntT, Options >::ends_with ( const sp::Vec< std::remove_const_t< CharT >, L >  chars)
constexpr

Determine if the view ends with the given constant sequence of characters.

This overload is particularly useful when comparing vector-loaded string fragments.

◆ ends_with() [2/2]

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
template<typename Q >
constexpr bool sp::StringView< CharT, IntT, Options >::ends_with ( StringView< const CharT, Q >  chars)
constexpr

Determine if this string view ends with the other one.

◆ front()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr auto & sp::StringView< CharT, IntT, Options >::front ( ) const
constexpr

Access the first character.

◆ length()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr auto sp::StringView< CharT, IntT, Options >::length ( ) const
constexpr

Get the size of the view.

◆ max_size()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr IntT sp::StringView< CharT, IntT, Options >::max_size ( ) const
constexprnoexcept

Get the largest possible size of a string view using this IntT.

◆ operator StringView< const CharT, IntT, Options >()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr sp::StringView< CharT, IntT, Options >::operator StringView< const CharT, IntT, Options > ( )
constexpr

Allow implicitly adding constness to the view.

◆ operator!=()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr bool sp::StringView< CharT, IntT, Options >::operator!= ( const ThisType other) const
constexpr

Compare the contents of two StringViews.

◆ operator==()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr bool sp::StringView< CharT, IntT, Options >::operator== ( const ThisType other) const
constexpr

Compare the contents of two StringViews.

◆ remove_prefix()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr void sp::StringView< CharT, IntT, Options >::remove_prefix ( IntT  amount)
constexpr

Move the start of the view forward by amount.

◆ remove_suffix()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr void sp::StringView< CharT, IntT, Options >::remove_suffix ( IntT  amount)
constexpr

Move the end of the view backward by amount.

◆ starts_with() [1/2]

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
template<int L>
constexpr bool sp::StringView< CharT, IntT, Options >::starts_with ( const sp::Vec< std::remove_const_t< CharT >, L >  chars)
constexpr

Determine if the view starts with the given constant sequence of characters.

This overload is particularly useful when comparing vector-loaded string fragments.

◆ starts_with() [2/2]

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
template<typename Q >
constexpr bool sp::StringView< CharT, IntT, Options >::starts_with ( StringView< const CharT, Q >  chars)
constexpr

Implicitly convert to std::string_view when on the host.

Determine if this string view starts with the other one.

◆ substr()

template<typename CharT , typename IntT = uint32_t, typename Options = PackedTensorOpts<1>>
constexpr ThisType sp::StringView< CharT, IntT, Options >::substr ( IntT  pos = 0,
IntT  count = npos 
)
constexpr

Get a view representing a substring of this one.