MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space > Class Template Reference

Contiguous memory block with compile-time stack/heap selection. More...

#include <contiguous_memory_block.hpp>

Public Types

using value_type = Value
using size_type = std::size_t
using memory_space = Space
using handle_type = handle
 How Space names the storage: Value* for host memory and for every space whose handle is a pointer (#1017).
using const_handle_type = const_handle

Public Member Functions

bool assigns_in_place (std::size_t other_size) const noexcept
 Whether assigning a block of other_size writes into this block's elements in place, rather than replacing its storage.
 contiguous_memory_block ()
 contiguous_memory_block (std::size_t n)
 contiguous_memory_block (handle ptr, std::size_t n, bool is_view)
 contiguous_memory_block (const contiguous_memory_block &other)
 contiguous_memory_block (contiguous_memory_block &&other) noexcept(!fixed_heap)
contiguous_memory_block & operator= (const contiguous_memory_block &other)
contiguous_memory_block & operator= (contiguous_memory_block &&other) noexcept
 ~contiguous_memory_block ()
handle_type data ()
 The handle of the storage: a pointer in every space whose handle is one.
const_handle_type data () const
Value * host_data ()
 The host address of the elements, in a host-accessible space.
const Value * host_data () const
Value & operator[] (std::size_t i)
const Value & operator[] (std::size_t i) const
std::size_t size () const
bool empty () const
Value * begin ()
const Value * begin () const
Value * end ()
const Value * end () const
memory_category category () const
void realloc (std::size_t n)
void swap (contiguous_memory_block &other) noexcept

Static Public Attributes

static constexpr bool fixed_heap = !on_stack && StaticSize > 0

Detailed Description

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
class mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >

Contiguous memory block with compile-time stack/heap selection.

Stack path: StaticSize-element array with alignas. Heap path: new[]/delete[] with three ownership modes (own, external, view).

Assignment never re-points a non-owning (external or view) block (#829):

  • from a block of the same size, the elements are copied (or moved) into the caller's buffer, and the block stays non-owning;
  • copy assignment from a block of another size throws std::domain_error;
  • move assignment from a block of another size is noexcept, so it aborts, in every build (#856; it was an assert, and under NDEBUG it took over the source's storage). An owning block stays owning: move assignment from a non-owning block copies its elements instead of taking its pointer (#856). realloc() to another size is an explicit resize, and it still detaches.

A fixed-size heap block (StaticSize > 0) always holds StaticSize elements: the default constructor allocates them, and a moved-from block gets fresh storage (move assignment swaps instead, so it does not allocate).

Space is the memory space the heap storage lives in (#223), by default memory_space::host: over-aligned new and delete, as before. An owning block allocates and frees through Space, so it needs an AllocatingMemorySpace; a non-owning block (external or view) only records where the caller's memory lives. Copying or moving elements, and element access, are host operations and need a host-accessible space; they fail to compile otherwise. Stack storage is host memory.

The heap storage is named by Space's handle (#1017): a Value* for every space whose handle is a pointer, which keeps the layout and code of host blocks unchanged, and an object with an offset for a space whose storage is not addressed by a pointer (a Metal buffer, an OpenCL cl_mem). data() returns the handle; element access goes through the space's host address for it (detail::handle_traits).

Member Typedef Documentation

◆ const_handle_type

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
using mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::const_handle_type = const_handle

◆ handle_type

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
using mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::handle_type = handle

How Space names the storage: Value* for host memory and for every space whose handle is a pointer (#1017).

◆ memory_space

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
using mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::memory_space = Space

◆ size_type

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
using mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::size_type = std::size_t

◆ value_type

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
using mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::value_type = Value

Constructor & Destructor Documentation

◆ contiguous_memory_block() [1/5]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::contiguous_memory_block ( )
inline

◆ contiguous_memory_block() [2/5]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::contiguous_memory_block ( std::size_t n)
inlineexplicit

◆ contiguous_memory_block() [3/5]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::contiguous_memory_block ( handle ptr,
std::size_t n,
bool is_view )
inline

◆ contiguous_memory_block() [4/5]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::contiguous_memory_block ( const contiguous_memory_block< Value, Storage, StaticSize, Space > & other)
inline

◆ contiguous_memory_block() [5/5]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::contiguous_memory_block ( contiguous_memory_block< Value, Storage, StaticSize, Space > && other)
inlinenoexcept

◆ ~contiguous_memory_block()

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::~contiguous_memory_block ( )
inline

Member Function Documentation

◆ assigns_in_place()

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
bool mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::assigns_in_place ( std::size_t other_size) const
inlinenoexcept

Whether assigning a block of other_size writes into this block's elements in place, rather than replacing its storage.

◆ begin() [1/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
Value * mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::begin ( )
inline

◆ begin() [2/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
const Value * mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::begin ( ) const
inline

◆ category()

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
memory_category mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::category ( ) const
inline

◆ data() [1/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
handle_type mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::data ( )
inline

The handle of the storage: a pointer in every space whose handle is one.

◆ data() [2/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
const_handle_type mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::data ( ) const
inline

◆ empty()

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
bool mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::empty ( ) const
inline

◆ end() [1/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
Value * mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::end ( )
inline

◆ end() [2/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
const Value * mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::end ( ) const
inline

◆ host_data() [1/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
Value * mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::host_data ( )
inline

The host address of the elements, in a host-accessible space.

An empty block's is nullptr: a handle that names no storage never reaches the space's address(), which need not accept one (review of issue 1017).

◆ host_data() [2/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
const Value * mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::host_data ( ) const
inline

◆ operator=() [1/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
contiguous_memory_block & mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::operator= ( const contiguous_memory_block< Value, Storage, StaticSize, Space > & other)
inline

◆ operator=() [2/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
contiguous_memory_block & mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::operator= ( contiguous_memory_block< Value, Storage, StaticSize, Space > && other)
inlinenoexcept

◆ operator[]() [1/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
Value & mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::operator[] ( std::size_t i)
inline

◆ operator[]() [2/2]

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
const Value & mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::operator[] ( std::size_t i) const
inline

◆ realloc()

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
void mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::realloc ( std::size_t n)
inline

◆ size()

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
std::size_t mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::size ( ) const
inline

◆ swap()

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
void mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::swap ( contiguous_memory_block< Value, Storage, StaticSize, Space > & other)
inlinenoexcept

Member Data Documentation

◆ fixed_heap

template<typename Value, typename Storage, std::size_t StaticSize = 0, typename Space = memory_space::host>
bool mtl::detail::contiguous_memory_block< Value, Storage, StaticSize, Space >::fixed_heap = !on_stack && StaticSize > 0
staticconstexpr

The documentation for this class was generated from the following file: