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

Memory namespace containing functions and classes related to memory management. More...

Classes

class  AccessException
 Memory access exception. More...
 
class  AllocException
 Memory allocation exception. More...
 
class  Arbiter
 Memory arbiter class. More...
 
class  Block
 Memory Arbiter memory block class. More...
 
class  Manager
 Memory management interface class. More...
 
class  MemoryException
 Memory related exception base class. More...
 
class  RangeException
 Memory range exception. More...
 
class  TypedView
 Typed memory view class. More...
 
class  View
 Raw memory view class. More...
 

Enumerations

enum class  Access : std::uint32_t {
  none = 0 , read = 0x1 , write = 0x2 , read_write = 0x3 ,
  overlap = 0x4
}
 Memory access enumeration. More...
 
enum class  Init : std::uint32_t { dont_care = 0 , uninitialized = 1 , format_to_zero = 2 , format_to_one = 3 }
 Memory initialization enumeration. More...
 
enum class  Location : std::uint32_t { none = 0 , main = 0x0001 , cuda = 0x0002 }
 Memory location enumeration. More...
 

Functions

void * alloc_cuda (std::size_t bytes)
 Allocate a memory block in cuda heap memory. More...
 
void * alloc_cuda_async (std::size_t bytes, const Util::StreamWrapper &stream=Util::StreamWrapper{})
 Allocates stream ordered memory block in cuda heap memory. More...
 
void * alloc_main (std::size_t bytes)
 Allocate a memory block in main heap memory. More...
 
template<typename T_ >
void convert_cuda (T_ *dst, const T_ *src, const Index count)
 overload for Tdst_ = Tsrc_; performs a memcopy internally More...
 
template<typename Tdst_ , typename Tsrc_ >
void convert_cuda (Tdst_ *dst, const Tsrc_ *src, const Index count)
 Converts the elements of an array into element of another array of a different data type. More...
 
template<typename T_ >
void convert_main (T_ *dst, const T_ *src, const Index count)
 overload for Tdst_ = Tsrc_; performs a memcopy internally More...
 
template<typename Tdst_ , typename Tsrc_ >
void convert_main (Tdst_ *dst, const Tsrc_ *src, const Index count)
 Converts the elements of an array into element of another array of a different data type. More...
 
template<typename T_ >
void format_cuda (T_ *dst, const T_ &value, const Index count)
 Sets all entries of a cuda memory array to a given value. More...
 
template<typename T_ >
void format_main (T_ *dst, const T_ &value, const Index count)
 Sets all entries of a main memory array to a given value. More...
 
void free_cuda (void *ptr)
 Frees a memory block in cuda heap memory allocated by alloc_cuda. More...
 
void free_cuda_async (void *ptr, const Util::StreamWrapper &stream=Util::StreamWrapper{})
 Frees a memory block in cuda heap memory allocated by alloc_cuda. More...
 
void free_main (void *ptr)
 Frees a memory block in main heap memory allocated by alloc_main. More...
 
void memcopy_cuda (void *dst, const void *src, std::size_t bytes, const Util::StreamWrapper &stream=Util::StreamWrapper{})
 Copies a region of a cuda memory block into another cuda memory block. More...
 
void memcopy_cuda_to_main (void *dst, const void *src, std::size_t bytes)
 Copies a region of a cuda memory block into a main memory block. More...
 
void memcopy_main (void *dst, const void *src, std::size_t bytes)
 Copies a region of a main memory block into another main memory block. More...
 
void memcopy_main_to_cuda (void *dst, const void *src, std::size_t bytes, const Util::StreamWrapper &stream=Util::StreamWrapper{})
 Copies a region of a main memory block into a cuda memory block. More...
 
void memset_cuda (void *ptr, int value, std::size_t bytes, const Util::StreamWrapper &stream=Util::StreamWrapper{})
 Formats all bytes of a memory block in cuda memory to a given value. More...
 
void memset_main (void *ptr, int value, std::size_t bytes)
 Formats all bytes of a memory block in main memory to a given value. More...
 
void memset_random_main (void *ptr, std::size_t bytes, std::uint64_t seed=0u)
 Formats all bytes of a memory block in main memory to random values. More...
 

Detailed Description

Memory namespace containing functions and classes related to memory management.

Enumeration Type Documentation

◆ Access

enum class FEAT::Memory::Access : std::uint32_t
strong

Memory access enumeration.

This enum is used by the Memory::Arbiter class and its related classes to specify the access rights requested/granted to a view of a memory block.

See Memory Access Rights for more details.

Enumerator
none 

no access

read 

read-only access

write 

write-only access (see attention note above)

read_write 

read-write access

overlap 

explicitly allow overlapping writes (see Overlapping Memory Views)

Definition at line 97 of file memory_arbiter.hpp.

◆ Init

enum class FEAT::Memory::Init : std::uint32_t
strong

Memory initialization enumeration.

This enum is used by the Memory::Arbiter class and its related classes to specify how newly allocated memory arrays are to be initialized.

See Allocation and Initialization for more information.

Enumerator
dont_care 

don't care

uninitialized 

leave memory uninitialized

format_to_zero 

allocate and format all bits to zero

format_to_one 

allocate and format all bits to one

Definition at line 137 of file memory_arbiter.hpp.

◆ Location

enum class FEAT::Memory::Location : std::uint32_t
strong

Memory location enumeration.

This enum is used by the Memory::Arbiter class and its related classes to specify the locations of a memory block and views that are mapped to it.

See Memory Locations for more information.

Enumerator
none 

no or unknown or unspecified location

main 

main memory

cuda 

cuda memory

Definition at line 37 of file memory_arbiter.hpp.

Function Documentation

◆ alloc_cuda()

void * FEAT::Memory::alloc_cuda ( std::size_t  bytes)

Allocate a memory block in cuda heap memory.

This is effectively a wrapper around ::cudaMalloc.

Parameters
[in]bytesThe size of the memory block in bytes.
Returns
A pointer to the newly allocated cuda memory block or nullptr, if the memory allocation failed.

◆ alloc_cuda_async()

void * FEAT::Memory::alloc_cuda_async ( std::size_t  bytes,
const Util::StreamWrapper &  stream = Util::StreamWrapper{} 
)

Allocates stream ordered memory block in cuda heap memory.

This is effectively a wrapper around ::cudaMallocAsync. Note, async capabilities are not available on all devices, so this falls back to standard cudaMalloc if its not available. You can query the availability with the Util function cuda_get_async...

Parameters
[in]bytesThe size of the memory block in bytes.
Returns
A pointer to the newly allocated cuda memory block or nullptr, if the memory allocation failed.

◆ alloc_main()

void * FEAT::Memory::alloc_main ( std::size_t  bytes)

Allocate a memory block in main heap memory.

This is effectively a wrapper around ::malloc.

Parameters
[in]bytesThe size of the memory block in bytes.
Returns
A pointer to the newly allocated main memory block or nullptr, if the memory allocation failed.

Definition at line 19 of file memory_aux_main.cpp.

◆ convert_cuda() [1/2]

template<typename T_ >
void FEAT::Memory::convert_cuda ( T_ *  dst,
const T_ *  src,
const Index  count 
)
inline

overload for Tdst_ = Tsrc_; performs a memcopy internally

Definition at line 320 of file memory_aux.hpp.

References memcopy_cuda().

◆ convert_cuda() [2/2]

template<typename Tdst_ , typename Tsrc_ >
void FEAT::Memory::convert_cuda ( Tdst_ *  dst,
const Tsrc_ *  src,
const Index  count 
)

Converts the elements of an array into element of another array of a different data type.

Attention
The memory regions [dst, ..., dst + count*sizeof(Tdst_)] and [src, ..., src + count*sizeof(Tsrc_)] must not overlap.
Note
This template is explicitly instantiated for the cross-product of the following types: float, double, signed int, signed long, signed long long, unsigned int, unsigned long, unsigned long long
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,out]dstA device pointer to the cuda memory block that receives the converted elements; must not be nullptr if count > 0.
[in]srcA device pointer to the cuda memory block that contains the source array; must not be nullptr if count > 0.
[in]countThe number of arrays to be converted from *src to *dst

Referenced by FEAT::Memory::TypedView< T_ >::convert_from().

◆ convert_main() [1/2]

template<typename T_ >
void FEAT::Memory::convert_main ( T_ *  dst,
const T_ *  src,
const Index  count 
)
inline

overload for Tdst_ = Tsrc_; performs a memcopy internally

Definition at line 99 of file memory_aux.hpp.

References memcopy_main().

Referenced by FEAT::Memory::TypedView< T_ >::convert_from().

◆ convert_main() [2/2]

template<typename Tdst_ , typename Tsrc_ >
void FEAT::Memory::convert_main ( Tdst_ *  dst,
const Tsrc_ *  src,
const Index  count 
)
inline

Converts the elements of an array into element of another array of a different data type.

Attention
The memory regions [dst, ..., dst + count*sizeof(Tdst_)] and [src, ..., src + count*sizeof(Tsrc_)] must not overlap.
Parameters
[in,out]dstA pointer to the main memory block that receives the converted elements; must not be nullptr if count > 0.
[in]srcA pointer to the main memory block that contains the source array; must not be nullptr if count > 0.
[in]countThe number of arrays to be converted from *src to *dst

Definition at line 121 of file memory_aux.hpp.

References XASSERT, and XASSERTM.

◆ format_cuda()

template<typename T_ >
void FEAT::Memory::format_cuda ( T_ *  dst,
const T_ &  value,
const Index  count 
)

Sets all entries of a cuda memory array to a given value.

Parameters
[out]dstA transient pointer to the cuda memory array whose elements are to be formatted
[in]valueA transient reference to the value that is to be copied to the array elements
[in]countThe number of elements in the array dst

Referenced by FEAT::Memory::TypedView< T_ >::format().

◆ format_main()

template<typename T_ >
void FEAT::Memory::format_main ( T_ *  dst,
const T_ &  value,
const Index  count 
)
inline

Sets all entries of a main memory array to a given value.

Parameters
[out]dstA transient pointer to the main memory array whose elements are to be formatted
[in]valueA transient reference to the value that is to be copied to the array elements
[in]countThe number of elements in the array dst

Definition at line 154 of file memory_aux.hpp.

References FEAT::value, and XASSERT.

Referenced by FEAT::Memory::TypedView< T_ >::format().

◆ free_cuda()

void FEAT::Memory::free_cuda ( void *  ptr)

Frees a memory block in cuda heap memory allocated by alloc_cuda.

This is effectively a wrapper around ::cudaFree.

Parameters
[in]ptrA pointer to the cuda memory block that is to be freed; must not be nullptr.

◆ free_cuda_async()

void FEAT::Memory::free_cuda_async ( void *  ptr,
const Util::StreamWrapper &  stream = Util::StreamWrapper{} 
)

Frees a memory block in cuda heap memory allocated by alloc_cuda.

This is effectively a wrapper around ::cudaFree.

Parameters
[in]ptrA pointer to the cuda memory block that is to be freed; must not be nullptr.

◆ free_main()

void FEAT::Memory::free_main ( void *  ptr)

Frees a memory block in main heap memory allocated by alloc_main.

This is effectively a wrapper around ::free.

Parameters
[in]ptrA pointer to the main memory block that is to be freed; must not be nullptr.

Definition at line 26 of file memory_aux_main.cpp.

◆ memcopy_cuda()

void FEAT::Memory::memcopy_cuda ( void *  dst,
const void *  src,
std::size_t  bytes,
const Util::StreamWrapper &  stream = Util::StreamWrapper{} 
)

Copies a region of a cuda memory block into another cuda memory block.

Attention
The memory regions [dst, ..., dst + bytes] and [src, ..., src + bytes] must not overlap, however, the trivial case of dst = src is allowed and it will not perform any action.

This is effectively a wrapper around ::cudaMemcpy for cudaMemcpyDeviceToDevice.

Parameters
[in,out]dstA device pointer to the cuda memory block that receives the copy; must not be nullptr if bytes > 0.
[in]srcA device pointer to the cuda memory block that acts as the source for the copy; must not be nullptr if bytes > 0.
[in]bytesThe number of bytes to be copied from *src to *dst

Referenced by convert_cuda(), and FEAT::Memory::View::copy_raw_from().

◆ memcopy_cuda_to_main()

void FEAT::Memory::memcopy_cuda_to_main ( void *  dst,
const void *  src,
std::size_t  bytes 
)

Copies a region of a cuda memory block into a main memory block.

This is effectively a wrapper around ::cudaMemcpy for cudaMemcpyDeviceToHost.

Parameters
[in,out]dstA host pointer to the main memory block that receives the copy; must not be nullptr if bytes > 0.
[in]srcA device pointer to the cuda memory block that acts as the source for the copy; must not be nullptr if bytes > 0.
[in]bytesThe number of bytes to be copied from *src to *dst

Referenced by FEAT::Memory::View::copy_raw_from().

◆ memcopy_main()

void FEAT::Memory::memcopy_main ( void *  dst,
const void *  src,
std::size_t  bytes 
)

Copies a region of a main memory block into another main memory block.

Attention
The memory regions [dst, ..., dst + bytes] and [src, ..., src + bytes] must not overlap, however, the trivial case of dst = src is allowed and it will not perform any action.

This is effectively a wrapper around ::memcpy.

Parameters
[in,out]dstA pointer to the main memory block that receives the copy; must not be nullptr if bytes > 0.
[in]srcA pointer to the main memory block that acts as the source for the copy; must not be nullptr if bytes > 0.
[in]bytesThe number of bytes to be copied from *src to *dst

Definition at line 71 of file memory_aux_main.cpp.

References XASSERT, and XASSERTM.

Referenced by FEAT::Solver::VoxelAmaVanka< Matrix_, Filter_, pol_threading_, macro_type_ >::_alloc_device(), FEAT::LAFEM::Container< DT_, IT_ >::_serialize(), convert_main(), FEAT::Memory::View::copy_raw_from(), FEAT::LAFEM::Arch::TransposeDense::exec_generic_impl(), FEAT::Adjacency::ColoringDataHandler::fill_color(), FEAT::LAFEM::SparseMatrixBCSR< DT_, IT_, block_height_, block_width_ >::permute(), and FEAT::LAFEM::SparseMatrixCSR< DT_, IT_ >::permute().

◆ memcopy_main_to_cuda()

void FEAT::Memory::memcopy_main_to_cuda ( void *  dst,
const void *  src,
std::size_t  bytes,
const Util::StreamWrapper &  stream = Util::StreamWrapper{} 
)

Copies a region of a main memory block into a cuda memory block.

This is effectively a wrapper around ::cudaMemcpy for cudaMemcpyHostToDevice.

Parameters
[in,out]dstA device pointer to the cuda memory block that receives the copy; must not be nullptr if bytes > 0.
[in]srcA host pointer to the main memory block that acts as the source for the copy; must not be nullptr if bytes > 0.
[in]bytesThe number of bytes to be copied from *src to *dst

Referenced by FEAT::Memory::View::copy_raw_from().

◆ memset_cuda()

void FEAT::Memory::memset_cuda ( void *  ptr,
int  value,
std::size_t  bytes,
const Util::StreamWrapper &  stream = Util::StreamWrapper{} 
)

Formats all bytes of a memory block in cuda memory to a given value.

This is effectively a wrapper around ::cudaMemset.

Parameters
[in,out]ptrA device pointer to the cuda memory block that is to be formatted; must not be nullptr if bytes > 0.
[in]valueThe value that all bytes are to be set to. Set this to 0 to format all bits to 0 and set it to 0xFF (= 255) to format all bits to 1.
[in]bytesThe size of the memory block in bytes.

Referenced by FEAT::Memory::View::format_to_one(), and FEAT::Memory::View::format_to_zero().

◆ memset_main()

void FEAT::Memory::memset_main ( void *  ptr,
int  value,
std::size_t  bytes 
)

Formats all bytes of a memory block in main memory to a given value.

This is effectively a wrapper around ::memset.

Parameters
[in,out]ptrA pointer to the main memory block that is to be formatted; must not be nullptr if bytes > 0.
[in]valueThe value that all bytes are to be set to. Set this to 0 to format all bits to 0 and set it to 0xFF (= 255) to format all bits to 1.
[in]bytesThe size of the memory block in bytes.

Definition at line 32 of file memory_aux_main.cpp.

References FEAT::value, and XASSERT.

Referenced by FEAT::Solver::VoxelAmaVanka< Matrix_, Filter_, pol_threading_, macro_type_ >::_alloc_device(), FEAT::Solver::Vanka< LAFEM::SaddlePointMatrix< MatrixA_, MatrixB_, MatrixD_ >, Filter_ >::_factor_diag(), FEAT::Solver::Vanka< LAFEM::SaddlePointMatrix< MatrixA_, MatrixB_, MatrixD_ >, Filter_ >::_factor_full(), FEAT::Memory::View::format_to_one(), FEAT::Memory::View::format_to_zero(), and FEAT::Global::AlgDofPartiSystem< LocalMatrix_, LocalFilter_, Mirror_ >::upload_matrix_numeric().

◆ memset_random_main()

void FEAT::Memory::memset_random_main ( void *  ptr,
std::size_t  bytes,
std::uint64_t  seed = 0u 
)

Formats all bytes of a memory block in main memory to random values.

Parameters
[in,out]ptrA pointer to the main memory block that is to be formatted; must not be nullptr if bytes > 0.
[in]bytesThe size of the memory block in bytes.
[in]seedThe seed for the RNG. If set to zero, then the address stored in ptr is used as the seed.

Definition at line 42 of file memory_aux_main.cpp.

References FEAT::Random::next(), and XASSERT.