FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
FEAT::Memory::Arbiter Class Reference

Memory arbiter class. More...

#include <memory_arbiter.hpp>

Public Member Functions

 Arbiter ()
 standard constructor More...
 
 Arbiter (Arbiter &&other)
 move constructor More...
 
 Arbiter (const Arbiter &other)=delete
 deleted copy constructor
 
 Arbiter (std::size_t bytes, Location mem_loc=Location::none, Init mem_ini=Init::dont_care)
 Creates a new memory arbiter block of a given size. More...
 
 ~Arbiter ()
 destructor More...
 
void alloc (Location mem_loc, Init mem_ini=Init::dont_care)
 Allocates memory for a given memory block. More...
 
void attach (const Arbiter &other, std::size_t offset=0u, std::size_t size=full_size)
 Attaches this arbiter to another arbiter's memory block. More...
 
Arbiter attach (std::size_t offset=0u, std::size_t size=full_size) const
 Creates a new arbiter that is attached to the same memory block as this arbiter. More...
 
std::size_t block_bytes () const
 Returns the size of the entire attached memory block in bytes. More...
 
std::size_t bytes () const
 Returns the size of the arbiter's range in the attached memory block in bytes. More...
 
void clone (const Arbiter &source, std::size_t offset=0u, std::size_t size=full_size, bool clone_block=false)
 Sets this arbiter to a deep copy of (a sub-region of) another arbiter's memory block. More...
 
Arbiter clone (std::size_t offset=0u, std::size_t size=full_size, bool clone_block=false) const
 Creates a new arbiter that contains a deep copy to (a sub-region of) this arbiters memory block. More...
 
template<typename Tdst_ , typename Tsrc_ >
void convert (const Arbiter &source)
 Converts the contents of one memory block into another one by casting the underlying data types. More...
 
template<typename Tdst_ , typename Tsrc_ >
void convert (const Arbiter &source, Location mem_loc)
 Converts the contents of one memory block into another one by casting the underlying data types. More...
 
void copy (const Arbiter &source, Location mem_loc=Location::none)
 Copies of the contents of one memory block into another one. More...
 
void copy (const void *src_ptr, Location src_loc, Location mem_loc=Location::none)
 Copies of the contents of a raw memory region into this memory block. More...
 
void copy_to (void *dst_ptr, Location dst_loc) const
 Copies the contents of this memory block into a raw memory region. More...
 
template<typename T_ >
void format (const T_ &value, Location mem_loc)
 Formats the memory block to a given value. More...
 
void format (Location mem_loc=Location::none)
 Formats the memory block to zero. More...
 
void format_random (Location mem_loc=Location::none)
 Formats the memory block to random values. More...
 
void free (Location mem_loc)
 Frees memory for a given memory block. More...
 
Location location () const
 Returns a combination of all up-to-date memory locations. More...
 
 operator bool () const
 Checks whether this arbiter is not the null arbiter. More...
 
bool operator!= (const Arbiter &other) const
 Checks whether two arbiters refer to the different memory regions. More...
 
Arbiter & operator= (Arbiter &&other)
 move-assign operator More...
 
Arbiter & operator= (const Arbiter &other)=delete
 deleted copy-assign operator
 
bool operator== (const Arbiter &other) const
 Checks whether two arbiters refer to the same memory region. More...
 
void release ()
 Releases this arbiter from its associated memory block and frees it if necessary. More...
 
bool same_block (const Arbiter &other) const
 Checks whether two arbiters refer to the same memory block ignoring offset and size. More...
 
template<typename T_ >
TypedView< T_ > typed_view (Location mem_loc, Access access)
 Maps a typed view to a memory block. More...
 
template<typename T_ >
TypedView< T_ > typed_view (Location mem_loc, Access access=Access::read) const
 Maps a read-only typed view to a memory block. More...
 
View view (Location mem_loc, Access access)
 Maps a raw view to a memory block. More...
 
View view (Location mem_loc, Access access=Memory::Access::read) const
 Maps a raw read-only view to the arbiters memory block. More...
 

Static Public Attributes

static constexpr std::size_t full_size = ~std::size_t(0)
 full size constant for size parameters More...
 

Private Member Functions

 Arbiter (Block *block, std::size_t offset, std::size_t size)
 private constructor More...
 
void _move (Arbiter &&other)
 moves another arbiter into this one More...
 

Private Attributes

Block * _block
 a pointer to the memory block associated with this arbiter More...
 
std::size_t _offset
 the offset of this arbiter's memory region within the block More...
 
std::size_t _size
 size of this arbiter's memory region within the block More...
 

Detailed Description

Memory arbiter class.

This class implements a memory-location-aware reference-counted smart pointer, which is capable of automatically transferring memory between host and device memory when needed without explicit synchronization by the user.

See The Memory Arbiter and its View classes for more information.

Author
Peter Zajac

Definition at line 636 of file memory_arbiter.hpp.

Constructor & Destructor Documentation

◆ Arbiter() [1/4]

FEAT::Memory::Arbiter::Arbiter ( Block *  block,
std::size_t  offset,
std::size_t  size 
)
explicitprivate

private constructor

Definition at line 1153 of file memory_arbiter.cpp.

References FEAT::Memory::Block::bytes, full_size, and XASSERT.

◆ Arbiter() [2/4]

FEAT::Memory::Arbiter::Arbiter ( )
inline

standard constructor

Definition at line 660 of file memory_arbiter.hpp.

Referenced by attach(), and clone().

◆ Arbiter() [3/4]

FEAT::Memory::Arbiter::Arbiter ( std::size_t  bytes,
Location  mem_loc = Location::none,
Init  mem_ini = Init::dont_care 
)
explicit

Creates a new memory arbiter block of a given size.

Note
Although there is little point in doing that, it is perfectly legal to create an arbiter with bytes = 0, in which case a new but empty memory block is created. It is also possible to create views for this memory block and the pointer stored in these views will be a valid non-null pointer in the requested memory location, but the mapped memory block will have a size of 0 bytes (as requested), so no reasonable operations can be performed on that memory block. In consequence, it is also necessary to release the memory block of size 0, as the memory manager will report a memory leak otherwise.
Parameters
[in]bytesThe size of the memory block to be created in bytes.
[in]mem_locA combination of memory locations on which the memory block has to be allocated at time of creation, i.e. during this function call. If set to Location::none, then no memory is allocated initially and the allocation is performed when the first view to this memory block is mapped.
[in]mem_iniSpecifies how the memory of this block is to be initialized if memory is to be allocated during this function call, i.e. if mem_loc != Location::none.

Definition at line 1175 of file memory_arbiter.cpp.

References _block, and bytes().

◆ Arbiter() [4/4]

FEAT::Memory::Arbiter::Arbiter ( Arbiter &&  other)

move constructor

Definition at line 1183 of file memory_arbiter.cpp.

References _move().

◆ ~Arbiter()

FEAT::Memory::Arbiter::~Arbiter ( )

destructor

Definition at line 1200 of file memory_arbiter.cpp.

References _block.

Member Function Documentation

◆ _move()

void FEAT::Memory::Arbiter::_move ( Arbiter &&  other)
private

moves another arbiter into this one

Definition at line 1205 of file memory_arbiter.cpp.

References _block, _offset, _size, and XASSERT.

Referenced by Arbiter(), attach(), clone(), and operator=().

◆ alloc()

void FEAT::Memory::Arbiter::alloc ( Location  mem_loc,
Init  mem_ini = Init::dont_care 
)

Allocates memory for a given memory block.

Parameters
[in]mem_locA combination of memory locations on which the memory block has to be allocated
[in]mem_iniSpecifies how the memory of this block is to be initialized if memory is to be allocated

Definition at line 1394 of file memory_arbiter.cpp.

References _block.

◆ attach() [1/2]

void FEAT::Memory::Arbiter::attach ( const Arbiter &  other,
std::size_t  offset = 0u,
std::size_t  size = full_size 
)

Attaches this arbiter to another arbiter's memory block.

This function is effectively equivalent to

this->release();
(*this) = other.attach(offset, size);
void release()
Releases this arbiter from its associated memory block and frees it if necessary.
Note
This function effectively performs a shallow copy of the memory block, i.e. both the input arbiter other as well as this arbiter will point to the same memory block. If you indent to perform a deep copy instead, use the clone() function instead.
Parameters
[in]otherA transient reference to another arbiter to whose block this arbiter should attach to.

Definition at line 1249 of file memory_arbiter.cpp.

References _block, _move(), _offset, _size, attach(), FEAT::Memory::Block::bytes, and full_size.

◆ attach() [2/2]

Arbiter FEAT::Memory::Arbiter::attach ( std::size_t  offset = 0u,
std::size_t  size = full_size 
) const

Creates a new arbiter that is attached to the same memory block as this arbiter.

Note
This function effectively performs a shallow copy of the memory block, i.e. both this arbiter as well as the returned arbiter will point to the same memory block. If you indent to perform a deep copy of the memory block instead, use the clone() function instead.
Returns
A new arbiter that is attached to this arbiters memory block.

Definition at line 1216 of file memory_arbiter.cpp.

References Arbiter(), _block, _offset, _size, FEAT::Memory::Block::bytes, and full_size.

Referenced by FEAT::LAFEM::MatrixMirrorBuffer< DT_, IT_ >::MatrixMirrorBuffer(), FEAT::LAFEM::SparseMatrixBanded< DT_, IT_ >::SparseMatrixBanded(), FEAT::LAFEM::SparseMatrixBCSR< DT_, IT_, block_height_, block_width_ >::SparseMatrixBCSR(), FEAT::LAFEM::SparseMatrixCSCR< DT_, IT_ >::SparseMatrixCSCR(), FEAT::LAFEM::SparseMatrixCSR< DT_, IT_ >::SparseMatrixCSR(), FEAT::LAFEM::SparseVector< DT_, IT_ >::SparseVector(), FEAT::LAFEM::SparseVectorBlocked< DT_, IT_, block_size_ >::SparseVectorBlocked(), and attach().

◆ block_bytes()

std::size_t FEAT::Memory::Arbiter::block_bytes ( ) const

Returns the size of the entire attached memory block in bytes.

Definition at line 1384 of file memory_arbiter.cpp.

References _block, and FEAT::Memory::Block::bytes.

◆ bytes()

std::size_t FEAT::Memory::Arbiter::bytes ( ) const

Returns the size of the arbiter's range in the attached memory block in bytes.

Definition at line 1374 of file memory_arbiter.cpp.

References _block, _offset, _size, and FEAT::Memory::Block::bytes.

Referenced by Arbiter().

◆ clone() [1/2]

void FEAT::Memory::Arbiter::clone ( const Arbiter &  source,
std::size_t  offset = 0u,
std::size_t  size = full_size,
bool  clone_block = false 
)

Sets this arbiter to a deep copy of (a sub-region of) another arbiter's memory block.

This function is effectively equivalent to

this->release();
(*this) = other.clone(offset, size);
Note
This function performs a deep copy of the memory block, i.e. the input arbiter other and this arbiter will point to independent memory blocks. If you indent to perform a shallow copy instead, use the attach() function instead.
Parameters
[in]sourceA transient reference to another arbiter to whose block this arbiter should clone from.
[in]offsetSpecifies the offset of the memory sub-region in bytes that is to be copied.
[in]sizeSpecifies the size of the memory sub-region in bytes that is to be copied. Set to Arbiter::full_size if the memory sub-region should extend to the end of the input memory block (sub-region).
[in]clone_blockSpecifies if the entire memory block is to be cloned or only the sub-region of the block that is defined by the range [offset, offset+size).

Definition at line 1329 of file memory_arbiter.cpp.

References _block, _move(), _offset, _size, FEAT::Memory::Block::bytes, clone(), and full_size.

◆ clone() [2/2]

Arbiter FEAT::Memory::Arbiter::clone ( std::size_t  offset = 0u,
std::size_t  size = full_size,
bool  clone_block = false 
) const

Creates a new arbiter that contains a deep copy to (a sub-region of) this arbiters memory block.

Note
This function performs a deep copy of (a sub-region of) the memory block, i.e. this arbiter and the returned arbiter will point to independent memory blocks. If you indent to perform a shallow copy of the memory block instead, use the attach() function instead.
Parameters
[in]offsetSpecifies the offset of the memory sub-region in bytes that is to be copied.
[in]sizeSpecifies the size of the memory sub-region in bytes that is to be copied. Set to Arbiter::full_size if the memory sub-region should extend to the end of the input memory block (sub-region).
[in]clone_blockSpecifies if the entire memory block is to be cloned or only the sub-region of the block that is defined by the range [offset, offset+size).
Returns
A new arbiter that contains a deep copy of (a sub-region of) this arbiters memory block.

Definition at line 1287 of file memory_arbiter.cpp.

References Arbiter(), _block, _offset, _size, FEAT::Memory::Block::bytes, full_size, FEAT::Memory::none, and XASSERTM.

Referenced by clone().

◆ convert() [1/2]

template<typename Tdst_ , typename Tsrc_ >
void FEAT::Memory::Arbiter::convert ( const Arbiter &  source)
inline

Converts the contents of one memory block into another one by casting the underlying data types.

This function interprets the contents of the destination memory block as an array of elements of type Tdst_ and the contents of the source memory block as an array of elements of type Tsrc_ and it converts each element of the source array into its corresponding element of the destination array by performing a static cast to the destination data type.

Template Parameters
Tdst_Specifies the underlying type of the destination.
Tsrc_Specifies the underlying type of the source array; must be convertible to Tdst_.
Parameters
[in]sourceThe arbiter of the source memory block that is to be converted.

Definition at line 987 of file memory_arbiter.hpp.

References FEAT::Memory::TypedView< T_ >::convert_from(), FEAT::Memory::cuda, location(), FEAT::Memory::main, FEAT::Memory::read, FEAT::Memory::View::release(), and FEAT::Memory::write.

◆ convert() [2/2]

template<typename Tdst_ , typename Tsrc_ >
void FEAT::Memory::Arbiter::convert ( const Arbiter &  source,
Location  mem_loc 
)
inline

Converts the contents of one memory block into another one by casting the underlying data types.

This function interprets the contents of the destination memory block as an array of elements of type Tdst_ and the contents of the source memory block as an array of elements of type Tsrc_ and it converts each element of the source array into its corresponding element of the destination array by performing a static cast to the destination data type.

Template Parameters
Tdst_Specifies the underlying type of the destination array.
Tsrc_Specifies the underlying type of the source array; must be convertible to Tdst_.
Parameters
[in]sourceThe arbiter of the source memory block that is to be converted.
[in]mem_locSpecifies which memory location of the destination memory block should receive the copy. Must be a single memory location and must not be Location::none.

Definition at line 962 of file memory_arbiter.hpp.

References FEAT::Memory::TypedView< T_ >::convert_from(), FEAT::Memory::read, and FEAT::Memory::write.

◆ copy() [1/2]

void FEAT::Memory::Arbiter::copy ( const Arbiter &  source,
Location  mem_loc = Location::none 
)

Copies of the contents of one memory block into another one.

This function performs a deep copy of the currently up-to-date memory location(s) of the source memory block into the memory locations of the destination memory block that are specified by mem_loc.

Parameters
[in]sourceThe handle of the source memory block that is to be copied.
[in]mem_locSpecifies which memory location of the destination memory block should receive the copy. If set to Location::none, then all up-to-date locations of the source memory block are copied into the destination memory block.

Definition at line 1406 of file memory_arbiter.cpp.

References _block, _offset, _size, FEAT::Memory::Block::bytes, full_size, XASSERT, and XASSERTM.

Referenced by FEAT::LAFEM::SparseMatrixBanded< DT_, IT_ >::extract_diag(), and FEAT::VoxelAssembly::DeviceCubature< DT_, PointType_ >::update().

◆ copy() [2/2]

void FEAT::Memory::Arbiter::copy ( const void *  src_ptr,
Location  src_loc,
Location  mem_loc = Location::none 
)

Copies of the contents of a raw memory region into this memory block.

This function performs a deep copy of the given source memory region into the memory locations of the destination memory block that are specified by mem_loc.

Parameters
[in]src_ptrA transient pointer to the source memory region that is to be deep copied.
[in]src_locThe memory location of the source memory region. Must be a single memory location and must not be Location::none.
[in]mem_locSpecifies which memory location of the destination memory block should receive the copy. If set to Location::none, then the destination memory location will be equal to the source memory location src_loc

Definition at line 1421 of file memory_arbiter.cpp.

References _block, _offset, _size, and XASSERT.

◆ copy_to()

void FEAT::Memory::Arbiter::copy_to ( void *  dst_ptr,
Location  dst_loc 
) const

Copies the contents of this memory block into a raw memory region.

This function performs a deep copy of this memory block's contents into the given memory region. This function automatically chooses a valid source memory location in this block.

Parameters
[out]dst_ptrA transient pointer to the destination memory region that is to be deep copied to.
[in]dst_locThe memory location of the source memory region. Must be a single memory location and must not be Location::none.

Definition at line 1427 of file memory_arbiter.cpp.

References _block, _offset, _size, and XASSERT.

◆ format() [1/2]

template<typename T_ >
void FEAT::Memory::Arbiter::format ( const T_ &  value,
Location  mem_loc 
)
inline

Formats the memory block to a given value.

This function interprets the memory block as an array of elements of type T_ and sets all elements of the array to the given value

Template Parameters
T_Specifies the type of the array elements that the memory block's contents are to be interpreted as
Parameters
[in]value_The value that all array elements are to be set to.
[in]mem_locA combination of memory locations which have to be formatted. If set to Location::none, then all memory locations that are currently allocated will be formatted.

Definition at line 868 of file memory_arbiter.hpp.

References FEAT::Memory::cuda, location(), FEAT::Memory::main, FEAT::Memory::none, FEAT::value, and FEAT::Memory::write.

◆ format() [2/2]

◆ format_random()

void FEAT::Memory::Arbiter::format_random ( Location  mem_loc = Location::none)

Formats the memory block to random values.

Parameters
[in]mem_locA combination of memory locations which have to be formatted. If set to Location::none, then all memory locations that are currently allocated will be formatted.

Definition at line 1439 of file memory_arbiter.cpp.

References _block, _offset, and _size.

◆ free()

void FEAT::Memory::Arbiter::free ( Location  mem_loc)

Frees memory for a given memory block.

Parameters
[in]mem_locA combination of memory locations on which the memory block has to be freed

Definition at line 1400 of file memory_arbiter.cpp.

References _block.

◆ location()

Location FEAT::Memory::Arbiter::location ( ) const

Returns a combination of all up-to-date memory locations.

Definition at line 1389 of file memory_arbiter.cpp.

References _block, FEAT::Memory::none, and FEAT::Memory::Block::valid_locations.

Referenced by convert(), and format().

◆ operator bool()

FEAT::Memory::Arbiter::operator bool ( ) const
inline

Checks whether this arbiter is not the null arbiter.

Definition at line 1094 of file memory_arbiter.hpp.

◆ operator!=()

bool FEAT::Memory::Arbiter::operator!= ( const Arbiter &  other) const
inline

Checks whether two arbiters refer to the different memory regions.

Definition at line 1106 of file memory_arbiter.hpp.

◆ operator=()

Arbiter & FEAT::Memory::Arbiter::operator= ( Arbiter &&  other)

move-assign operator

Definition at line 1189 of file memory_arbiter.cpp.

References _block, and _move().

◆ operator==()

bool FEAT::Memory::Arbiter::operator== ( const Arbiter &  other) const
inline

Checks whether two arbiters refer to the same memory region.

Definition at line 1100 of file memory_arbiter.hpp.

◆ release()

void FEAT::Memory::Arbiter::release ( )

Releases this arbiter from its associated memory block and frees it if necessary.

This function decrements the reference counter of this arbiters memory block by one and, if the reference counter reaches zero, frees all memory allocated within the memory block as well as the block itself.

This function has no effect if the arbiter is not attached to a memory block.

Definition at line 1368 of file memory_arbiter.cpp.

References _block.

Referenced by FEAT::Solver::VoxelAmaVanka< Matrix_, Filter_, pol_threading_, macro_type_ >::_free_device().

◆ same_block()

bool FEAT::Memory::Arbiter::same_block ( const Arbiter &  other) const
inline

Checks whether two arbiters refer to the same memory block ignoring offset and size.

Definition at line 1112 of file memory_arbiter.hpp.

◆ typed_view() [1/2]

template<typename T_ >
TypedView< T_ > FEAT::Memory::Arbiter::typed_view ( Location  mem_loc,
Access  access 
)
inline

Maps a typed view to a memory block.

Template Parameters
T_The type that is to be used for the typed view.
Parameters
[in]mem_locThe memory location that the view is to be mapped for. Must be a single location and must not be Location::none.
[in]accessThe access rights that are to be granted for the view.
Returns
A typed view for the (sub-region of the) memory block of the requested type T_.

Definition at line 1063 of file memory_arbiter.hpp.

References view().

◆ typed_view() [2/2]

template<typename T_ >
TypedView< T_ > FEAT::Memory::Arbiter::typed_view ( Location  mem_loc,
Access  access = Access::read 
) const
inline

Maps a read-only typed view to a memory block.

Template Parameters
T_The type that is to be used for the typed view.
Parameters
[in]mem_locThe memory location that the view is to be mapped for. Must be a single location and must not be Location::none.
[in]accessThe access rights that are to be granted for the view. Must be equal to Memory::Access:read for this overload.
Returns
A typed view for the (sub-region of the) memory block of the requested type T_.

Definition at line 1086 of file memory_arbiter.hpp.

References view().

◆ view() [1/2]

◆ view() [2/2]

View FEAT::Memory::Arbiter::view ( Location  mem_loc,
Access  access = Memory::Access::read 
) const

Maps a raw read-only view to the arbiters memory block.

Attention
For this const-overload the access must be equal to Memory::access::read, otherwise an assertion will fire.
Parameters
[in]mem_locThe memory location that the view is to be mapped for. Must be a single location and must not be Location::none.
[in]accessThe access rights that are to be granted for the view. Must be equal to Memory::Access:read for this overload.
Returns
A raw view for the memory block.

Definition at line 1451 of file memory_arbiter.cpp.

References _block, _offset, _size, FEAT::Memory::write, XASSERT, and XASSERTM.

Member Data Documentation

◆ _block

Block* FEAT::Memory::Arbiter::_block
mutableprivate

a pointer to the memory block associated with this arbiter

Definition at line 644 of file memory_arbiter.hpp.

Referenced by Arbiter(), ~Arbiter(), _move(), alloc(), attach(), block_bytes(), bytes(), clone(), copy(), copy_to(), format(), format_random(), free(), location(), operator=(), release(), and view().

◆ _offset

std::size_t FEAT::Memory::Arbiter::_offset
private

the offset of this arbiter's memory region within the block

Definition at line 647 of file memory_arbiter.hpp.

Referenced by _move(), attach(), bytes(), clone(), copy(), copy_to(), format(), format_random(), and view().

◆ _size

std::size_t FEAT::Memory::Arbiter::_size
private

size of this arbiter's memory region within the block

Definition at line 650 of file memory_arbiter.hpp.

Referenced by _move(), attach(), bytes(), clone(), copy(), copy_to(), format(), format_random(), and view().

◆ full_size

constexpr std::size_t FEAT::Memory::Arbiter::full_size = ~std::size_t(0)
staticconstexpr

full size constant for size parameters

Definition at line 640 of file memory_arbiter.hpp.

Referenced by Arbiter(), attach(), clone(), and copy().


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