FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
memory_arbiter.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
12#include <kernel/util/random.hpp>
13#include <kernel/util/memory_aux.hpp>
14#include <kernel/backend.hpp>
15
16#include <memory>
17#include <deque>
18
19namespace FEAT
20{
21 namespace Memory
22 {
23 // forward declarations
24 class Arbiter;
25 class Block;
26 class Manager;
27 class View;
28
37 enum class Location : std::uint32_t
38 {
40 none = 0,
42 main = 0x0001,
44 cuda = 0x0002
45 }; // enum class Location
46
49 {
50 return static_cast<Location>(static_cast<std::uint32_t>(a) & static_cast<std::uint32_t>(b));
51 }
52
54 {
55 return static_cast<Location>(static_cast<std::uint32_t>(a) | static_cast<std::uint32_t>(b));
56 }
57
58 inline Location& operator|=(Location& a, Location b)
59 {
60 return a = (a | b);
61 }
62
63 inline Location operator~(Location a)
64 {
65 return static_cast<Location>(~static_cast<std::uint32_t>(a));
66 }
67
68 inline bool operator*(Location a)
69 {
70 // only check the relevant bits
71 return (static_cast<std::uint32_t>(a) & 0x3u) != 0u;
72 }
73
74 inline Location to_location(PreferredBackend backend)
75 {
76 switch (backend)
77 {
80 return Location::main;
82 return Location::cuda;
83 }
84 return Location::main;
85 }
86
88
97 enum class Access : std::uint32_t
98 {
100 none = 0,
102 read = 0x1,
104 write = 0x2,
106 read_write = 0x3,
108 overlap = 0x4
109 }; // enum class Access
110
112 inline Access operator&(Access a, Access b)
113 {
114 return static_cast<Access>(static_cast<std::uint32_t>(a) & static_cast<std::uint32_t>(b));
115 }
116
117 inline Access operator|(Access a, Access b)
118 {
119 return static_cast<Access>(static_cast<std::uint32_t>(a) | static_cast<std::uint32_t>(b));
120 }
121
122 inline bool operator*(Access a)
123 {
124 // only check the relevant bits
125 return (static_cast<std::uint32_t>(a) & 0x7u) != 0u;
126 }
128
137 enum class Init : std::uint32_t
138 {
140 dont_care = 0,
142 uninitialized = 1,
144 format_to_zero = 2,
146 format_to_one = 3
147 }; // enum class Init
148
153 public Exception
154 {
155 public:
156 explicit MemoryException(const String& msg) :
157 Exception(msg)
158 {
159 }
160 }; // class MemoryException
161
168 public MemoryException
169 {
170 public:
171 explicit AllocException(std::size_t bytes, const String& loc) :
172 MemoryException(String("Failed to allocate ") + stringify(bytes) + " in " + loc + " memory")
173 {
174 }
175 }; // class AllocException
176
184 public MemoryException
185 {
186 public:
187 explicit AccessException(const String& msg) :
188 MemoryException(msg)
189 {
190 }
191 }; // class AccessException
192
200 public MemoryException
201 {
202 public:
203 explicit RangeException(const String& msg) :
204 MemoryException(msg)
205 {
206 }
207 }; // class RangeException
208
212
228 class View
229 {
230 // I've got friends!
231 friend class Arbiter;
232 friend class Block;
233
234 protected:
242 std::size_t _bytes;
244 void* _ptr;
245
247 explicit View(Block* blk, Location l, Access a, std::size_t b, void* p);
248
250 void _reset();
251
252 public:
254 View();
255
257 virtual ~View();
258
260 View(const View&) = delete;
262 View& operator=(const View&) = delete;
263
265 View(View&& other);
267 View& operator=(View&& other);
268
272 bool empty() const
273 {
274 return _bytes <= std::size_t(0u);
275 }
276
280 bool is_nullptr() const
281 {
282 return _ptr == nullptr;
283 }
284
286 std::size_t bytes() const
287 {
288 return _bytes;
289 }
290
292 void release() const;
293
301 void* raw_w()
302 {
303 if(!this->writeable())
304 throw AccessException("Memory::View::raw_w(): write access required");
305 return _ptr;
306 }
307
311 const void* raw_r() const
312 {
313 //XASSERT(this->readable());
314 return _ptr;
315 }
316
319 {
320 return _location;
321 }
322
325 {
326 return _access;
327 }
328
330 bool readable() const
331 {
332 return *(this->_access & Access::read);
333 }
334
336 bool writeable() const
337 {
338 return *(this->_access & Access::write);
339 }
340
342 void format_to_zero();
343
345 void format_to_one();
346
356 void copy_raw_from(const void* src_ptr, Location src_loc);
357
369 void copy_from(const View& src)
370 {
371 if(src.bytes() < this->bytes())
372 throw RangeException("Memory::View::copy_from(): source memory view is smaller than the destination memory view");
373 if(this != &src)
374 this->copy_raw_from(src.raw_r(), src.location());
375 }
376 }; // class View
377
381
393 template<typename T_>
394 class TypedView :
395 public View
396 {
397 public:
399 TypedView() = default;
400
402 virtual ~TypedView() = default;
403
405 TypedView(const TypedView&) = delete;
407 TypedView& operator=(const TypedView&) = delete;
408
412 explicit TypedView(View&& other) :
413 View(std::forward<View>(other))
414 {
415 }
416
421 {
422 static_cast<View&>(*this) = std::forward<View>(other);
423 return *this;
424 }
425
429 explicit TypedView(TypedView&& other) :
430 View(std::forward<View>(other))
431 {
432 }
433
438 {
439 static_cast<View&>(*this) = std::forward<View>(other);
440 return *this;
441 }
442
454 template<typename Tother_>
456
468 template<typename Tother_>
470
474 Index size() const
475 {
476 return Index(this->bytes() / sizeof(T_));
477 }
478
486 T_* get_w()
487 {
488 if(!this->writeable())
489 throw AccessException("Memory::TypedView::get_w(): write access required");
490 return reinterpret_cast<T_*>(this->_ptr);
491 }
492
496 const T_* get_r() const
497 {
498 //XASSERT(this->readable());
499 return reinterpret_cast<const T_*>(this->_ptr);
500 }
501
512 {
513 ASSERT(i < size());
514 return get_w()[i];
515 }
516
526 const T_& operator[](Index i) const
527 {
528 ASSERT(i < size());
529 return get_r()[i];
530 }
531
541 const T_& operator()(Index i) const
542 {
543 ASSERT(i < size());
544 return get_r()[i];
545 }
546
548 void convert_from(const T_* ptr_src)
549 {
550 this->copy_raw_from(ptr_src, this->location());
551 }
552
561 template<typename Tsrc_>
562 void convert_from(const Tsrc_* ptr_src)
563 {
564 switch(this->location())
565 {
566 case Location::main:
567 Memory::convert_main(this->get_w(), ptr_src, this->size());
568 break;
569
570 case Location::cuda:
571 Memory::convert_cuda(this->get_w(), ptr_src, this->size());
572 break;
573
574 default:
575 XABORTM("TypedView::convert_from: unknown memory location");
576 break;
577 }
578 }
579
587 template<typename Tsrc_>
588 void convert_from(const TypedView<Tsrc_>& view_src)
589 {
590 if((const void*)this != (const void*)&view_src)
591 {
592 XASSERTM(this->location() == view_src.location(), "cannot convert between different memory locations!");
593 this->convert_from(view_src.get_r());
594 }
595 }
596
603 void format(const T_& value)
604 {
605 switch(this->location())
606 {
607 case Location::main:
608 Memory::format_main(this->get_w(), value, this->size());
609 break;
610
611 case Location::cuda:
612 Memory::format_cuda(this->get_w(), value, this->size());
613 break;
614
615 default:
616 XABORTM("TypedView::format: unknown memory location");
617 break;
618 }
619 }
620 }; // class TypedView<T_>
621
625
637 {
638 public:
640 static constexpr std::size_t full_size = ~std::size_t(0);
641
642 private:
644 mutable Block* _block;
645
647 std::size_t _offset;
648
650 std::size_t _size;
651
653 explicit Arbiter(Block* block, std::size_t offset, std::size_t size);
654
656 void _move(Arbiter&& other);
657
658 public:
661 _block(nullptr),
662 _offset(0u),
663 _size(0u)
664 {
665 }
666
692 explicit Arbiter(std::size_t bytes, Location mem_loc = Location::none, Init mem_ini = Init::dont_care);
693
695 Arbiter(const Arbiter& other) = delete;
697 Arbiter& operator=(const Arbiter& other) = delete;
699 Arbiter(Arbiter&& other);
701 Arbiter& operator=(Arbiter&& other);
702
704 ~Arbiter();
705
718 Arbiter attach(std::size_t offset = 0u, std::size_t size = full_size) const;
719
737 void attach(const Arbiter& other, std::size_t offset = 0u, std::size_t size = full_size);
738
763 Arbiter clone(std::size_t offset = 0u, std::size_t size = full_size, bool clone_block = false) const;
764
794 void clone(const Arbiter& source, std::size_t offset = 0u, std::size_t size = full_size, bool clone_block = false);
795
805 void release();
806
810 std::size_t bytes() const;
811
815 std::size_t block_bytes() const;
816
820 Location location() const;
821
831 void alloc(Location mem_loc, Init mem_ini = Init::dont_care);
832
839 void free(Location mem_loc);
840
848 void format(Location mem_loc = Location::none);
849
867 template<typename T_>
868 void format(const T_& value, Location mem_loc)
869 {
870 if(mem_loc == Location::none)
871 mem_loc = this->location();
872
873 if(*(mem_loc & Location::main))
874 this->template typed_view<T_>(Location::main, Access::write).format(value);
875 if(*(mem_loc & Location::cuda))
876 this->template typed_view<T_>(Location::cuda, Access::write).format(value);
877 }
878
886 void format_random(Location mem_loc = Location::none);
887
903 void copy(const Arbiter& source, Location mem_loc = Location::none);
904
923 void copy(const void* src_ptr, Location src_loc, Location mem_loc = Location::none);
924
938 void copy_to(void* dst_ptr, Location dst_loc) const;
939
961 template<typename Tdst_, typename Tsrc_>
962 void convert(const Arbiter& source, Location mem_loc)
963 {
964 TypedView<Tdst_> view_dst = this->template typed_view<Tdst_>(mem_loc, Access::write);
965 TypedView<Tsrc_> view_src = source.template typed_view<Tsrc_>(mem_loc, Access::read);
966 view_dst.convert_from(view_src);
967 }
968
986 template<typename Tdst_, typename Tsrc_>
987 void convert(const Arbiter& source)
988 {
989 Location mem_loc = this->location() | source.location();
990 if(*(mem_loc & Location::main))
991 {
992 TypedView<Tdst_> view_dst = this->template typed_view<Tdst_>(Location::main, Access::write);
993 TypedView<Tsrc_> view_src = source.template typed_view<Tsrc_>(Location::main, Access::read);
994 view_dst.convert_from(view_src);
995 view_dst.release();
996 // synchronize view by creating write view instead of overwriting location flag
997 if(*(mem_loc & Location::cuda))
998 {
999 TypedView<Tdst_> view_d = this->template typed_view<Tdst_>(Location::cuda, Access::read);
1000 (void) view_d;
1001 }
1002 }
1003 else if(*(mem_loc & Location::cuda))
1004 {
1005 TypedView<Tdst_> view_dst = this->template typed_view<Tdst_>(Location::cuda, Access::write);
1006 TypedView<Tsrc_> view_src = source.template typed_view<Tsrc_>(Location::cuda, Access::read);
1007 view_dst.convert_from(view_src);
1008 }
1009 }
1010
1024 View view(Location mem_loc, Access access);
1025
1044 View view(Location mem_loc, Access access = Memory::Access::read) const;
1045
1062 template<typename T_>
1064 {
1065 return TypedView<T_>(this->view(mem_loc, access));
1066 }
1067
1085 template<typename T_>
1087 {
1088 return TypedView<T_>(this->view(mem_loc, access));
1089 }
1090
1094 operator bool() const
1095 {
1096 return this->_block != nullptr;
1097 }
1098
1100 bool operator==(const Arbiter& other) const
1101 {
1102 return (this->_block == other._block) && (this->_offset == other._offset) && (this->_size == other._size);
1103 }
1104
1106 bool operator!=(const Arbiter& other) const
1107 {
1108 return (this->_block != other._block) || (this->_offset != other._offset) || (this->_size != other._size);
1109 }
1110
1112 bool same_block(const Arbiter& other) const
1113 {
1114 return (this->_block == other._block);
1115 }
1116 }; // class Arbiter
1117
1121
1133 {
1134 public:
1140 static void initialize(int argc, char** argv);
1141
1147 static void finalize();
1148
1158 static void free_all_unmapped(Location mem_loc);
1159
1163 static std::size_t bytes_reserved();
1164
1168 static std::size_t bytes_allocated_main();
1169
1173 static std::size_t bytes_allocated_cuda();
1174 }; // class Manager
1175 } // namespace Memory
1176} // namespace FEAT
#define ASSERT(expr)
Debug-Assertion macro definition.
Definition: assertion.hpp:229
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Base exception class.
Definition: exception.hpp:27
Memory access exception.
Memory allocation exception.
Memory arbiter class.
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.
Memory range exception.
Typed memory view 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.
Raw memory view class.
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.
bool writeable() const
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
Access access() const
bool empty() const
Checks whether the memory view has a size of 0 bytes.
Location _location
the current memory location
String class implementation.
Definition: string.hpp:47
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)
@ read
read-only access
@ 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
Definition: memory_aux.hpp:99
void format_main(T_ *dst, const T_ &value, const Index count)
Sets all entries of a main memory array to a given value.
Definition: memory_aux.hpp:154
Init
Memory initialization enumeration.
@ uninitialized
leave memory uninitialized
@ format_to_zero
allocate and format all bits to zero
@ dont_care
don't care
@ format_to_one
allocate and format all bits to one
FEAT namespace.
Definition: adjactor.hpp:12
PreferredBackend
The backend that shall be used in all compute heavy calculations.
Definition: backend.hpp:18
static constexpr bool operator*(TrafoTags a)
bool conversion operator
Definition: eval_tags.hpp:81
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
static constexpr TrafoTags operator|(TrafoTags a, TrafoTags b)
Binary OR operator for TrafoTags values.
Definition: eval_tags.hpp:53
static constexpr TrafoTags operator&(TrafoTags a, TrafoTags b)
Binary AND operator for TrafoTags values.
Definition: eval_tags.hpp:67
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.