12#include <kernel/util/random.hpp>
13#include <kernel/util/memory_aux.hpp>
14#include <kernel/backend.hpp>
50 return static_cast<Location>(
static_cast<std::uint32_t
>(a) &
static_cast<std::uint32_t
>(b));
55 return static_cast<Location>(
static_cast<std::uint32_t
>(a) |
static_cast<std::uint32_t
>(b));
65 return static_cast<Location>(~static_cast<std::uint32_t>(a));
71 return (
static_cast<std::uint32_t
>(a) & 0x3u) != 0u;
114 return static_cast<Access>(
static_cast<std::uint32_t
>(a) &
static_cast<std::uint32_t
>(b));
119 return static_cast<Access>(
static_cast<std::uint32_t
>(a) |
static_cast<std::uint32_t
>(b));
125 return (
static_cast<std::uint32_t
>(a) & 0x7u) != 0u;
137 enum class Init : std::uint32_t
274 return _bytes <= std::size_t(0u);
282 return _ptr ==
nullptr;
371 if(src.
bytes() < this->bytes())
372 throw RangeException(
"Memory::View::copy_from(): source memory view is smaller than the destination memory view");
393 template<
typename T_>
422 static_cast<View&
>(*this) = std::forward<View>(other);
439 static_cast<View&
>(*this) = std::forward<View>(other);
454 template<
typename Tother_>
468 template<
typename Tother_>
489 throw AccessException(
"Memory::TypedView::get_w(): write access required");
490 return reinterpret_cast<T_*
>(this->
_ptr);
499 return reinterpret_cast<const T_*
>(this->
_ptr);
561 template<
typename Tsrc_>
575 XABORTM(
"TypedView::convert_from: unknown memory location");
587 template<
typename Tsrc_>
590 if((
const void*)
this != (
const void*)&view_src)
616 XABORTM(
"TypedView::format: unknown memory location");
640 static constexpr std::size_t
full_size = ~std::size_t(0);
653 explicit Arbiter(
Block* block, std::size_t offset, std::size_t size);
763 Arbiter clone(std::size_t offset = 0u, std::size_t size =
full_size,
bool clone_block =
false)
const;
794 void clone(
const Arbiter& source, std::size_t offset = 0u, std::size_t size =
full_size,
bool clone_block =
false);
810 std::size_t
bytes()
const;
867 template<
typename T_>
961 template<
typename Tdst_,
typename Tsrc_>
986 template<
typename Tdst_,
typename Tsrc_>
1062 template<
typename T_>
1085 template<
typename T_>
1094 operator bool()
const
1096 return this->_block !=
nullptr;
1102 return (this->_block == other._block) && (this->_offset == other._offset) && (this->_size == other._size);
1108 return (this->_block != other._block) || (this->_offset != other._offset) || (this->_size != other._size);
1114 return (this->_block == other._block);
1140 static void initialize(
int argc,
char** argv);
1147 static void finalize();
1158 static void free_all_unmapped(
Location mem_loc);
1163 static std::size_t bytes_reserved();
1168 static std::size_t bytes_allocated_main();
1173 static std::size_t bytes_allocated_cuda();
#define ASSERT(expr)
Debug-Assertion macro definition.
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Memory allocation exception.
void copy(const Arbiter &source, Location mem_loc=Location::none)
Copies of the contents of one memory block into another one.
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.
void _move(Arbiter &&other)
moves another arbiter into this one
void release()
Releases this arbiter from its associated memory block and frees it if necessary.
void free(Location mem_loc)
Frees memory for a given memory block.
void format(const T_ &value, Location mem_loc)
Formats the memory block to a given value.
TypedView< T_ > typed_view(Location mem_loc, Access access=Access::read) const
Maps a read-only typed view to a memory block.
void alloc(Location mem_loc, Init mem_ini=Init::dont_care)
Allocates memory for a given memory block.
void convert(const Arbiter &source, Location mem_loc)
Converts the contents of one memory block into another one by casting the underlying data types.
Arbiter()
standard constructor
static constexpr std::size_t full_size
full size constant for size parameters
std::size_t bytes() const
Returns the size of the arbiter's range in the attached memory block in bytes.
void copy_to(void *dst_ptr, Location dst_loc) const
Copies the contents of this memory block into a raw memory region.
Location location() const
Returns a combination of all up-to-date memory locations.
std::size_t _offset
the offset of this arbiter's memory region within the block
Block * _block
a pointer to the memory block associated with this arbiter
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.
Arbiter(const Arbiter &other)=delete
deleted copy constructor
bool operator==(const Arbiter &other) const
Checks whether two arbiters refer to the same memory region.
bool operator!=(const Arbiter &other) const
Checks whether two arbiters refer to the different memory regions.
bool same_block(const Arbiter &other) const
Checks whether two arbiters refer to the same memory block ignoring offset and size.
View view(Location mem_loc, Access access)
Maps a raw view to a memory block.
std::size_t _size
size of this arbiter's memory region within the block
Arbiter & operator=(const Arbiter &other)=delete
deleted copy-assign operator
TypedView< T_ > typed_view(Location mem_loc, Access access)
Maps a typed view to a memory block.
void format_random(Location mem_loc=Location::none)
Formats the memory block to random values.
void format(Location mem_loc=Location::none)
Formats the memory block to zero.
void convert(const Arbiter &source)
Converts the contents of one memory block into another one by casting the underlying data types.
std::size_t block_bytes() const
Returns the size of the entire attached memory block in bytes.
Memory Arbiter memory block class.
Memory management interface class.
Memory related exception base class.
TypedView & operator=(View &&other)
View move-assign operator.
T_ & operator[](Index i)
Returns a mutable reference to an array element that grants write (and read) access.
Index size() const
Returns the number of array elements mapped to this view.
TypedView(TypedView &&other)
View move constructor.
void convert_from(const T_ *ptr_src)
overload of convert_from for the identical type
const T_ & operator[](Index i) const
Returns a const reference to an array elements that grants read access.
const T_ * get_r() const
Returns a const typed pointer to the array that grants read access.
void convert_from(const TypedView< Tsrc_ > &view_src)
Converts the array elements of another view to this view's array elements.
TypedView & operator=(TypedView< Tother_ > &&)=delete
Deleted type-conversion move-assign operator.
TypedView(View &&other)
View move constructor.
TypedView & operator=(TypedView &&other)
View move-assign operator.
TypedView()=default
default constructor
void convert_from(const Tsrc_ *ptr_src)
Converts the array elements of another type to this view's array elements.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
TypedView(TypedView< Tother_ > &&)=delete
Deleted type-conversion move constructor.
TypedView(const TypedView &)=delete
no copy
const T_ & operator()(Index i) const
Returns a const reference to an array elements that grants read access.
virtual ~TypedView()=default
default destructor
TypedView & operator=(const TypedView &)=delete
no copy-assign
void format(const T_ &value)
Formats the array elements to a given value, i.e. sets all elements to the same value.
void format_to_one()
Formats all bits of the memory view to one (yields NaN for floating point types)
void * _ptr
the actual memory pointer (may be a main or cuda or whatever pointer)
void format_to_zero()
Formats all bits of the memory view to zero.
void release() const
Releases this view.
Block * _block
the block that this view belongs to
View(const View &)=delete
no copy allowed
bool is_nullptr() const
Checks whether the memory pointer is a nullptr.
void copy_raw_from(const void *src_ptr, Location src_loc)
Copies the contents of a given memory region into the memory of this memory view.
const void * raw_r() const
Returns a const raw pointer to the memory block that grants read access.
View()
default constructor
void copy_from(const View &src)
Copies the contents of another view's memory region into the memory of this memory view.
View & operator=(const View &)=delete
no copy-assign allowed
void * raw_w()
Returns a raw pointer to the memory block that grants write (and read) access.
Location location() const
std::size_t _bytes
the size of the memory block in bytes
Access _access
the granted memory access
std::size_t bytes() const
void _reset()
reset all attributes to null
virtual ~View()
virtual destructor
bool empty() const
Checks whether the memory view has a size of 0 bytes.
Location _location
the current memory location
String class implementation.
void format_cuda(T_ *dst, const T_ &value, const Index count)
Sets all entries of a cuda memory array to a given value.
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.
Access
Memory access enumeration.
@ read_write
read-write access
@ overlap
explicitly allow overlapping writes (see Overlapping Memory Views)
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
@ none
no or unknown or unspecified location
void convert_main(T_ *dst, const T_ *src, const Index count)
overload for Tdst_ = Tsrc_; performs a memcopy internally
void format_main(T_ *dst, const T_ &value, const Index count)
Sets all entries of a main memory array to a given value.
Init
Memory initialization enumeration.
@ uninitialized
leave memory uninitialized
@ format_to_zero
allocate and format all bits to zero
@ format_to_one
allocate and format all bits to one
PreferredBackend
The backend that shall be used in all compute heavy calculations.
static constexpr bool operator*(TrafoTags a)
bool conversion operator
String stringify(const T_ &item)
Converts an item into a String.
static constexpr TrafoTags operator|(TrafoTags a, TrafoTags b)
Binary OR operator for TrafoTags values.
static constexpr TrafoTags operator&(TrafoTags a, TrafoTags b)
Binary AND operator for TrafoTags values.
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.