FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
memory_arbiter.cpp
1#include <kernel/util/memory_arbiter.hpp>
2#include <kernel/util/cuda_util.hpp>
4#include <kernel/util/os_windows.hpp>
5
6#include <cstdint>
7#include <mutex>
8#include <unordered_set>
9
10namespace FEAT
11{
12 namespace Memory
13 {
15
25 static std::mutex manager_mutex;
26
27 static constexpr std::size_t arbiter_mutex_shift = 6;
28
30 static constexpr std::size_t arbiter_mutex_mask = 0x7;
31
35 static std::mutex arbiter_mutex[arbiter_mutex_mask + 1u];
36
37 typedef std::unique_lock<std::mutex> Lock;
38
42 static std::unordered_set<Block*> manager_block_set;
43
45
51
60 class Block
61 {
62 public:
64 static constexpr std::size_t full_size = ~std::size_t(0);
65
69 std::size_t ref_count;
70
74 std::size_t bytes;
75
79 void* mem_main;
80
84 void* mem_cuda;
85
92
99
103 std::size_t write_views;
104
108 std::size_t read_views_main;
109
113 std::size_t read_views_cuda;
114
119
120 public:
121 Block() :
122 ref_count(0u),
123 bytes(0u),
124 mem_main(nullptr),
125 mem_cuda(nullptr),
128 write_views(0u),
129 read_views_main(0u),
130 read_views_cuda(0u),
131 write_overlap(false)
132 {
133 }
134
135 ~Block()
136 {
137 XASSERT(ref_count == 0u);
138 XASSERT(mem_main == nullptr);
139 XASSERT(mem_cuda == nullptr);
140 }
141
142 // no move constructor
143 Block(Block&& other) = delete;
144 // no move-assign operator
145 Block& operator=(Block&& other) = delete;
147 Block(const Block&) = delete;
149 Block& operator=(const Block&) = delete;
150
152 Lock lock_mutex() const
153 {
154 return Lock(arbiter_mutex[(reinterpret_cast<std::size_t>(this) >> arbiter_mutex_shift) & arbiter_mutex_mask]);
155 }
156
158 bool uses_same_lock(const Block& other) const
159 {
160 return
161 ((reinterpret_cast<std::size_t>(this) >> arbiter_mutex_shift) & arbiter_mutex_mask) ==
162 ((reinterpret_cast<std::size_t>(&other) >> arbiter_mutex_shift) & arbiter_mutex_mask);
163 }
164
165 void create(std::size_t bytes_, Location loc_, Init all_)
166 {
167 std::unique_lock<std::mutex> lock(lock_mutex());
168
169 // make sure that this block is empty
170 if(this->bytes > 0u)
171 throw MemoryException("Memory::Block::create: memory block is already allocated");
172
173 // save byte count and try to allocate
174 this->bytes = bytes_;
175 if(loc_ != Location::none)
176 this->_alloc(loc_, all_);
177 ++this->ref_count;
178
179 // finally, register this block with the manager
180 {
181 Lock manager_lock(manager_mutex);
182 manager_block_set.insert(this);
183 }
184 }
185
186 static Block* attach(Block* block)
187 {
188 if(block == nullptr)
189 return nullptr;
190
191 Lock lock(block->lock_mutex());
192 //XASSERTM(block != nullptr, "cannot attach to null block");
193 ++block->ref_count;
194 return block;
195 }
196
197 static bool detach(Block* block)
198 {
199 if(block == nullptr)
200 return false;
201
202 Lock lock(block->lock_mutex());
203
204 //XASSERTM(block != nullptr, "cannot detach from null block");
205 if(block->ref_count == 0u)
206 throw MemoryException("Memory::Block::detach: reference count is already zero");
207
208 if(0u < --block->ref_count)
209 return true;
210
211 // unregister this block with the manager
212 {
213 Lock manager_lock(manager_mutex);
214 manager_block_set.erase(block);
215 }
216
217 // all references have been detached, so let's free the block
218 block->_free_main();
219 block->_free_cuda();
220
221 // finally, free the block itself
222 delete block;
223
224 return false;
225 }
226
227 void alloc(Location mem_loc, Init mem_ini)
228 {
229 std::unique_lock<std::mutex> lock(lock_mutex());
230 _alloc(mem_loc, mem_ini);
231 }
232
233 void free(Location mem_loc)
234 {
235 std::unique_lock<std::mutex> lock(lock_mutex());
236 if(*(mem_loc & Location::main))
237 _free_main();
238 if(*(mem_loc & Location::cuda))
239 _free_cuda();
240 }
241
242 void free_unmapped(Location mem_loc)
243 {
244 std::unique_lock<std::mutex> lock(lock_mutex());
245
246 // ensure that no view is currently mapped, otherwise scram
247 if(_is_mapped_any())
248 return;
249
250 // ensure that a valid copy remains in main memory
251 _copy_main_from_uptodate();
252
253 // free requested memory locations (but never free main memory)
254 if(*(mem_loc & Location::cuda))
255 _free_cuda();
256 }
257
258 void copy_from(const Block& source, Location mem_loc, std::size_t this_offset, std::size_t src_offset, std::size_t size)
259 {
260 // first lock the mutex for this memory block
261 std::unique_lock<std::mutex> lock(lock_mutex());
262
263 // make sure that this block is not mapped
264 if(this->_is_mapped_any())
265 throw AccessException("Memory::Block::copy_from: cannot copy to mapped memory block");
266
267 // now lock the mutex for the source block, but only if it actually uses another mutex than ours
268 std::unique_lock<std::mutex> source_lock;
269 if(!this->uses_same_lock(source))
270 source_lock = source.lock_mutex();
271
272 // ensure that the source block is not mapped for writing
273 if(source._is_mapped_any_write())
274 throw AccessException("Memory::Block::copy_from: cannot copy from write-mapped memory block");
275
276 // if no target location is specified, then simply copy the existing source location
277 if(mem_loc == Location::none)
278 mem_loc = source.valid_locations;
279
280 // ensure the ranges are valid
281 if(size == full_size)
282 throw RangeException("Memory::Block::copy_from: memory size must be given explicitly");
283 if(this->bytes < this_offset + size)
284 throw RangeException("Memory::Block::copy_from: invalid destination memory range");
285 if(source.bytes < src_offset + size)
286 throw RangeException("Memory::Block::copy_from: invalid source memory range");
287
288 // perform the actual copy
289 if(size > std::size_t(0))
290 {
291 this->_copy_from(source, mem_loc, this_offset, src_offset, size);
292 }
293
294 // store our up-to-date locations
295 this->valid_locations = mem_loc;
296 }
297
298 void copy_from(const void* src_ptr, Location src_loc, Location mem_loc, std::size_t this_offset, std::size_t size)
299 {
300 // first lock the mutex for this memory block
301 std::unique_lock<std::mutex> lock(lock_mutex());
302
303 // make sure that this block is not mapped
304 if(this->_is_mapped_any())
305 throw AccessException("Memory::Block::copy_from: cannot copy to mapped memory block");
306
307 // if no target location is specified, then simply copy the existing source location
308 if(mem_loc == Location::none)
309 mem_loc = src_loc;
310
311 // ensure the ranges are valid
312 if(this->bytes < this_offset)
313 throw RangeException("Memory::Block::copy_from: invalid destination memory offset");
314 if(size == full_size)
315 size = this->bytes - this_offset;
316 if(this->bytes < this_offset + size)
317 throw RangeException("Memory::Block::copy_from: invalid destination memory range");
318
319 // perform the actual copy
320 if(size > std::size_t(0))
321 {
322 this->_copy_from(src_ptr, src_loc, mem_loc, this_offset, size);
323 }
324
325 // store our up-to-date locations
326 this->valid_locations = mem_loc;
327 }
328
329 void copy_to(void* dst_ptr, Location dst_loc, std::size_t this_offset, std::size_t size) const
330 {
331 // first lock the mutex for this memory block
332 std::unique_lock<std::mutex> lock(lock_mutex());
333
334 // make sure that this block is not mapped
335 if(this->_is_mapped_any_write())
336 throw AccessException("Memory::Block::copy_to: cannot copy to mapped memory block");
337
338 // ensure the ranges are valid
339 if(this->bytes < this_offset)
340 throw RangeException("Memory::Block::copy_from: invalid destination memory offset");
341 if(size == full_size)
342 size = this->bytes - this_offset;
343 if(this->bytes < this_offset + size)
344 throw RangeException("Memory::Block::copy_from: invalid destination memory range");
345
346 // perform the actual copy
347 if(size > std::size_t(0))
348 {
349 this->_copy_to(dst_ptr, dst_loc, this_offset, size);
350 }
351 }
352
353 void format(Location mem_loc, std::size_t this_offset, std::size_t size)
354 {
355 std::unique_lock<std::mutex> lock(lock_mutex());
356 if(this->_is_mapped_any())
357 throw AccessException("Memory::Block::format: cannot format mapped memory block");
358
359 // ensure the ranges are valid
360 if(this->bytes < this_offset)
361 throw RangeException("Memory::Block::format: invalid destination memory offset");
362 if(size == full_size)
363 size = this->bytes - this_offset;
364 if(this->bytes < this_offset + size)
365 throw RangeException("Memory::Block::format: invalid destination memory range");
366
367 // if no target location is specified, then simply format the existing location
368 if(mem_loc == Location::none)
369 mem_loc = this->valid_locations;
370
371 // if there was no existing location, then let's continue with main memory
372 if(mem_loc == Location::none)
373 mem_loc = Location::main;
374
375 // reset memory location
376 this->valid_locations = Location::none;
377
378 // format cuda memory?
379 if(*(mem_loc & Location::cuda))
380 {
381 if(this->mem_cuda != nullptr)
382 Memory::memset_cuda(reinterpret_cast<char*>(this->mem_cuda) + this_offset, 0, size);
383 else
384 this->_alloc_cuda(Init::format_to_zero);
385 this->valid_locations = this->valid_locations | Location::cuda;
386 }
387
388 // format main memory?
389 if(*(mem_loc & Location::main))
390 {
391 if(this->mem_main != nullptr)
392 Memory::memset_main(reinterpret_cast<char*>(this->mem_main) + this_offset, 0, size);
393 else
394 this->_alloc_main(Init::format_to_zero);
395 this->valid_locations = this->valid_locations | Location::main;
396 }
397
398 }
399
400 void format_random(Location mem_loc, std::size_t this_offset, std::size_t size)
401 {
402 std::unique_lock<std::mutex> lock(lock_mutex());
403 if(this->_is_mapped_any())
404 throw AccessException("Memory::Block::format_random: cannot format mapped memory block");
405
406 // ensure the ranges are valid
407 if(this->bytes < this_offset)
408 throw RangeException("Memory::Block::format_random: invalid destination memory offset");
409 if(size == full_size)
410 size = this->bytes - this_offset;
411 if(this->bytes < this_offset + size)
412 throw RangeException("Memory::Block::format: invalid destination memory range");
413
414 // ensure that main memory location is allocated
415 this->_alloc(Location::main, Init::uninitialized);
416
417 // format main memory block to random
418 if(size > 0u)
419 memset_random_main(reinterpret_cast<char*>(this->mem_main) + this_offset, size);
420
421 // remember that we wrote to main memory last and all other memory locations are out-of-date now
422 this->valid_locations = Location::main;
423
424 // copy from main to whatever the user asked for
425 if(*(mem_loc & Location::cuda))
426 {
427 _copy_cuda_from_main(reinterpret_cast<const char*>(this->mem_main) + this_offset, this_offset, size);
428 }
429 }
430
431 View map_view(Location mem_loc, Access access, std::size_t offset, std::size_t size)
432 {
433 std::unique_lock<std::mutex> lock(lock_mutex());
434
435 // do we need to create a view for a sub-region?
436 if((offset != std::size_t(0)) || (size != Arbiter::full_size))
437 {
438 // We are trying to map a write-accessible view to a sub-region of the memory block, so we need to add the
439 // read access flag independently of whether the user asked for it, because this will ensure that the
440 // entire memory block is up-to-date at the target memory location. If the read access flag was missing and
441 // the memory block was not already up-to-date on the target memory location, then the user's write
442 // operations would only overwrite the sub-region memory, but the region outside of the mapped view would
443 // remain in an undefined state.
444 if(*(access & Access::write))
445 access = access | Access::read;
446 }
447
448 // ensure that the offset is not out-of-bounds
449 if(this->bytes < offset)
450 throw RangeException("Memory::Block::map_view: memory offset is out of bounds");
451
452 // set size to entire (rest of) memory block if desired (which may be zero, which is fine)
453 if(size == full_size)
454 size = this->bytes - offset;
455 else if(this->bytes < offset + size)
456 throw RangeException("Memory::Block::map_view: memory range is out of bounds");
457
458 // do we need write access?
459 if(*(access & Access::write))
460 {
461 // do we allow overlapping write access?
462 if(*(access & Access::overlap))
463 {
464 // yes, overlap is allowed, but we must not have any mapped exclusive write views
465 if(this->_is_mapped_any_write())
466 {
467 // we can live with overlapping access, but some other write view is already mapped to this block,
468 // so ensure that a) it also allows overlapped access and b) it is mapped to the same location
469 if(!this->write_overlap)
470 throw AccessException("Memory::Block::map_view: cannot map exclusively write-mapped memory block for overlapping write access");
471 if(this->write_location != mem_loc)
472 throw AccessException("Memory::Block::map_view: memory block is write-mapped onto a different memory location");
473 }
474 // if no write overlapping write view was opened yet, then we also must not have mapped any read views yet;
475 // note that this if is never executed if an overlapping write view was already opened, which is intentional
476 // as one might not be able to create overlapping read-write views which are used in multi-threaded assembly
477 else if(this->_is_mapped_any_read())
478 {
479 throw AccessException("Memory::Block::map_view: cannot map read-mapped memory block for overlapping write access");
480 }
481 }
482 else if(this->_is_mapped_any())
483 {
484 // we require exclusive write access, but there is some other view mapped to this block
485 throw AccessException("Memory::Block::map_view: cannot map locked memory block for exclusive write access");
486 }
487 }
488
489 // do we need read access? if so, then make sure that there is no exclusive write lock on the memory block
490 if(*(access & Access::read) && !this->write_overlap)
491 {
492 if(this->_is_mapped_any_write())
493 throw AccessException("Memory::Block::map_view: cannot map write-locked memory block for read access");
494 }
495
496 // zero size? create an empty view in this case
497 if(size == std::size_t(0))
498 return View(this, mem_loc, access, size, nullptr);
499
500 // ensure that the target memory location is allocated
501 this->_alloc(mem_loc, Init::dont_care);
502
503 // read access requested?
504 if(*(access & Access::read))
505 {
506 // do we have an up-to-date memory location?
507 if(this->valid_locations == Location::none)
508 {
509 // emit a warning
510 // std::cerr << "\n>>> MEMORY ARBITER: map_view: trying to map uninitialized memory for reading!\n";
511 throw AccessException("Memory::Block::map_view: trying to map uninitialized memory for reading");
512 // XABORTM("\n>>> MEMORY ARBITER: map_view: trying to map uninitialized memory for reading!\n");
513#ifdef _WIN32
515#endif
516 }
517 // check whether the location that we want to map is out-of-date
518 else if(!*(this->valid_locations & mem_loc))
519 {
520 // the desired target memory location is out of date, so first make sure that
521 // main memory is up-to-date and then copy from main to target location
522 _copy_main_from_uptodate();
523 _copy_main_to(mem_loc);
524 }
525
526 // increase read lock counters
527 if(*(mem_loc & Location::main))
528 ++this->read_views_main;
529 if(*(mem_loc & Location::cuda))
530 ++this->read_views_cuda;
531 }
532
533 // write access requested?
534 if(*(access & Access::write))
535 {
536 if(this->write_views == std::size_t(0))
537 {
538 // remember whether we asked for overlapping or exclusive access
539 this->write_overlap = *(access & Access::overlap);
540
541 // remember memory write location
542 this->write_location = mem_loc;
543 }
544
545 // increase number of write views
546 ++this->write_views;
547 }
548
549 // create view for the requested memory location
550 if(mem_loc == Location::main)
551 {
552 return View(this, Location::main, access, size, reinterpret_cast<char*>(this->mem_main) + offset);
553 }
554 if(mem_loc == Location::cuda)
555 {
556 return View(this, Location::cuda, access, size, reinterpret_cast<char*>(this->mem_cuda) + offset);
557 }
558
559 // we should never arrive here
560 XABORTM("Memory::Block::map_view: invalid memory location");
561 }
562
563 void release_view(const View& view)
564 {
565 std::unique_lock<std::mutex> lock(lock_mutex());
566
567 // if the view had a write lock, then remove it
568 if(*(view.access() & Access::write))
569 {
570 if(--this->write_views == std::size_t(0))
571 {
572 // ok, this was the last open write view
573 this->valid_locations = this->write_location;
574 this->write_location = Memory::Location::none;
575 this->write_overlap = false;
576 }
577 }
578
579 // if the view had a read lock, then decrease the number of read views by one
580 if(*(view.access() & Access::read))
581 {
582 if(view.location() == Location::main)
583 {
584 XASSERT(this->read_views_main > 0u);
585 --this->read_views_main;
586 }
587 if(view.location() == Location::cuda)
588 {
589 XASSERT(this->read_views_cuda > 0u);
590 --this->read_views_cuda;
591 }
592 }
593 }
594
595 private:
596 // ATTENTION: none of the private functions are thread-safe, so they have to be called from
597 // within a mutex-locked scope!
598 void _reset()
599 {
600 this->bytes = 0u;
601 this->ref_count = 0u;
602 this->mem_main = nullptr;
603 this->mem_cuda = nullptr;
604 this->valid_locations = Location::none;
605 this->write_location = Location::none;
606 this->write_views = 0u;
607 this->read_views_main = 0u;
608 this->read_views_cuda = 0u;
609 this->write_overlap = false;
610 }
611
612 bool _is_mapped_any() const
613 {
614 return _is_mapped_any_read() || _is_mapped_any_write();
615 }
616
617 bool _is_mapped_any_read() const
618 {
619 return this->read_views_main + this->read_views_cuda > 0u;
620 }
621
622 bool _is_mapped_any_write() const
623 {
624 return this->write_views > 0u;
625 }
626
627 bool _is_mapped_cuda() const
628 {
629 return _is_mapped_cuda_read() || _is_mapped_cuda_write();
630 }
631
632 bool _is_mapped_cuda_read() const
633 {
634 return read_views_cuda > 0u;
635 }
636
637 bool _is_mapped_cuda_write() const
638 {
640 }
641
642 bool _is_mapped_main() const
643 {
644 return _is_mapped_main_read() || _is_mapped_main_write();
645 }
646
647 bool _is_mapped_main_read() const
648 {
649 return read_views_main > 0u;
650 }
651
652 bool _is_mapped_main_write() const
653 {
655 }
656
657 bool _is_write_overlapped() const
658 {
659 return write_overlap;
660 }
661
662 // not thread-safe; must be called in a mutex-locked region!
663 void _alloc_main(Init mem_ini)
664 {
665 // try to allocate memory
666 if((this->mem_main = Memory::alloc_main(this->bytes)) == nullptr)
667 throw AllocException(this->bytes, "main");
668
669 // pre-format memory to ones if required
670 switch(mem_ini)
671 {
672 case Init::dont_care:
673#ifdef DEBUG
674 Memory::memset_main(this->mem_main, 0xFF, this->bytes);
675#endif
676 break;
677
679 // nothing to do here
680 break;
681
683 Memory::memset_main(this->mem_main, 0, this->bytes);
684 this->valid_locations = this->valid_locations | Location::main;
685 break;
686
688 Memory::memset_main(this->mem_main, 0xFF, this->bytes);
689 this->valid_locations = this->valid_locations | Location::main;
690 break;
691 }
692 }
693
694 // not thread-safe; must be called in a mutex-locked region!
695 void _alloc_cuda(Init mem_ini)
696 {
697 if((this->mem_cuda = Memory::alloc_cuda(this->bytes)) == nullptr)
698 throw AllocException(this->bytes, "cuda");
699
700 // pre-format memory to ones if required
701 switch(mem_ini)
702 {
703 case Init::dont_care:
704#ifdef DEBUG
705 Memory::memset_cuda(this->mem_cuda, 0xFF, this->bytes);
706#endif
707 break;
708
710 // nothing to do here
711 break;
712
714 Memory::memset_cuda(this->mem_cuda, 0, this->bytes);
715 this->valid_locations = this->valid_locations | Location::cuda;
716 break;
717
719 Memory::memset_cuda(this->mem_cuda, 0xFF, this->bytes);
720 this->valid_locations = this->valid_locations | Location::cuda;
721 break;
722 }
723 }
724
725 // not thread-safe; must be called in a mutex-locked region!
726 void _alloc(Location mem_loc, Init mem_ini)
727 {
728 // nothing to allocate?
729 if(this->bytes <= 0u)
730 return;
731
732 // allocate main memory?
733 if(*(mem_loc & Location::main) && (this->mem_main == nullptr))
734 this->_alloc_main(mem_ini);
735
736 // allocate cuda memory?
737 if(*(mem_loc & Location::cuda) && (this->mem_cuda == nullptr))
738 this->_alloc_cuda(mem_ini);
739 }
740
741 // not thread-safe; must be called in a mutex-locked region!
742 void _free_main()
743 {
744 if(this->mem_main)
745 {
746 XASSERTM(this->read_views_main == std::size_t(0), "Memory::Block::_free_main: Cannot free main memory block that has a read view attached!");
747 XASSERTM(this->write_location != Memory::Location::main, "Memory::Block::_free_main: Cannot free main memory block that has a write view attached!");
748 Memory::free_main(this->mem_main);
749 this->mem_main = nullptr;
750 this->valid_locations = this->valid_locations & ~Memory::Location::main;
751 }
752 }
753
754 // not thread-safe; must be called in a mutex-locked region!
755 void _free_cuda()
756 {
757 if(this->mem_cuda)
758 {
759 XASSERTM(this->read_views_cuda == std::size_t(0), "Memory::Block::_free_cuda: Cannot free cuda memory block that has a read view attached!");
760 XASSERTM(this->write_location != Memory::Location::cuda, "Memory::Block::_free_cuda: Cannot free cuda memory block that has a write view attached!");
761 Memory::free_cuda(this->mem_cuda);
762 this->mem_cuda = nullptr;
763 this->valid_locations = this->valid_locations & ~Memory::Location::cuda;
764 }
765 }
766
767 // not thread-safe; must be called in a mutex-locked region!
768 void _copy_main_from_main(const void* mem_main_src, std::size_t this_offset, std::size_t size)
769 {
770 //XASSERT(!this->_is_mapped_main());
771 XASSERT(mem_main_src != nullptr);
772 XASSERT(this_offset + size <= this->bytes);
773 if(this->mem_main == nullptr)
774 this->_alloc_main(Init::uninitialized);
775 Memory::memcopy_main(reinterpret_cast<char*>(this->mem_main) + this_offset, mem_main_src, size);
776
777 // remember that main is an up-to-date location now
778 this->valid_locations = this->valid_locations | Location::main;
779 }
780
781 // not thread-safe; must be called in a mutex-locked region!
782 void _copy_cuda_from_main(const void* mem_main_src, std::size_t this_offset, std::size_t size)
783 {
784 //XASSERT(!this->_is_mapped_cuda());
785 XASSERT(mem_main_src != nullptr);
786 XASSERT(this_offset + size <= this->bytes);
787 if(this->mem_cuda == nullptr)
788 this->_alloc_cuda(Init::uninitialized);
789 Memory::memcopy_main_to_cuda(reinterpret_cast<char*>(this->mem_cuda) + this_offset, mem_main_src, size);
790
791 // remember that cuda is an up-to-date location now
792 this->valid_locations = this->valid_locations | Location::cuda;
793 }
794
795 // not thread-safe; must be called in a mutex-locked region!
796 void _copy_main_from_cuda(const void* mem_cuda_src, std::size_t this_offset, std::size_t size)
797 {
798 //XASSERT(!this->_is_mapped_cuda_write());
799 //XASSERT(!this->_is_mapped_main());
800 XASSERT(mem_cuda_src != nullptr);
801 XASSERT(this_offset + size <= this->bytes);
802 if(this->mem_main == nullptr)
803 this->_alloc_main(Init::uninitialized);
804 Memory::memcopy_cuda_to_main(reinterpret_cast<char*>(this->mem_main) + this_offset, mem_cuda_src, size);
805
806 // remember that main is an up-to-date location now
807 this->valid_locations = this->valid_locations | Location::main;
808 }
809
810 // not thread-safe; must be called in a mutex-locked region!
811 void _copy_cuda_from_cuda(const void* mem_cuda_src, std::size_t this_offset, std::size_t size)
812 {
813 //XASSERT(!this->_is_mapped_cuda());
814 XASSERT(mem_cuda_src != nullptr);
815 XASSERT(this_offset + size <= this->bytes);
816 if(this->mem_cuda == nullptr)
817 this->_alloc_cuda(Init::uninitialized);
818 Memory::memcopy_cuda(reinterpret_cast<char*>(this->mem_cuda) + this_offset, mem_cuda_src, size);
819
820 // remember that cuda is an up-to-date location now
821 this->valid_locations = this->valid_locations | Location::cuda;
822 }
823
824 // not thread-safe; must be called in a mutex-locked region!
825 void _copy_main_from_uptodate()
826 {
827 // is main memory already up to date?
828 if(*(this->valid_locations & Location::main))
829 {
830 // there's nothing left to do then
831 return;
832 }
833
834 // is cuda memory up-to-date?
835 if(*(this->valid_locations & Location::cuda))
836 {
837 // copy cuda to main then
838 XASSERT(!_is_mapped_cuda_write());
839 _copy_main_from_cuda(this->mem_cuda, std::size_t(0), this->bytes);
840 return;
841 }
842
843 // if we come out here, then there is no up-to-date memory to copy
844 }
845
846 // not thread-safe; must be called in a mutex-locked region!
847 void _copy_main_to(Location mem_loc)
848 {
849 if(*(mem_loc & Location::cuda))
850 {
851 XASSERT(!_is_mapped_main_write());
852 _copy_cuda_from_main(this->mem_main, std::size_t(0), this->bytes);
853 }
854 }
855
856 //void _copy(const Block& other, Location mem_loc, std::size_t offset)
857 void _copy_from(const Block& source, Location mem_loc, std::size_t this_offset, std::size_t src_offset, std::size_t size)
858 {
859 if(*(mem_loc & Location::cuda))
860 {
861 if(*(source.valid_locations & Location::cuda))
862 {
863 XASSERT(!source._is_mapped_cuda_write());
864 _copy_cuda_from_cuda(reinterpret_cast<char*>(source.mem_cuda) + src_offset, this_offset, size);
865 }
866 else if(*(source.valid_locations & Location::main))
867 {
868 XASSERT(!source._is_mapped_main_write());
869 _copy_cuda_from_main(reinterpret_cast<char*>(source.mem_main) + src_offset, this_offset, size);
870 }
871 }
872
873 if(*(mem_loc & Location::main))
874 {
875 if(*(source.valid_locations & Location::main))
876 {
877 XASSERT(!source._is_mapped_main_write());
878 _copy_main_from_main(reinterpret_cast<char*>(source.mem_main) + src_offset, this_offset, size);
879 }
880 else if(*(source.valid_locations & Location::cuda))
881 {
882 XASSERT(!source._is_mapped_cuda_write());
883 _copy_main_from_cuda(reinterpret_cast<char*>(source.mem_cuda) + src_offset, this_offset, size);
884 }
885 }
886
887 }
888
889 void _copy_from(const void* src_ptr, Location src_loc, Location mem_loc, std::size_t this_offset, std::size_t size)
890 {
891 if(*(mem_loc & Location::main))
892 {
893 switch(src_loc)
894 {
895 case Location::main:
896 _copy_main_from_main(src_ptr, this_offset, size);
897 break;
898
899 case Location::cuda:
900 _copy_main_from_cuda(src_ptr, this_offset, size);
901 break;
902
903 default:
904 XABORTM("Memory::Block::_copy_from: invalid source memory location");
905 break;
906 }
907 }
908
909 if(*(mem_loc & Location::cuda))
910 {
911 switch(src_loc)
912 {
913 case Location::cuda:
914 _copy_cuda_from_cuda(src_ptr, this_offset, size);
915 break;
916
917 case Location::main:
918 _copy_cuda_from_main(src_ptr, this_offset, size);
919 break;
920
921 default:
922 XABORTM("Memory::Block::_copy_from: invalid source memory location");
923 break;
924 }
925 }
926 }
927
928 // not thread-safe; must be called in a mutex-locked region!
929 void _copy_main_to_main(void* mem_main_dst, std::size_t this_offset, std::size_t size) const
930 {
931 XASSERT(!this->_is_mapped_main_write());
932 XASSERT(mem_main_dst != nullptr);
933 XASSERT(this->mem_main != nullptr);
934 XASSERT(this_offset + size <= this->bytes);
935 Memory::memcopy_main(mem_main_dst, reinterpret_cast<char*>(this->mem_main) + this_offset, size);
936 }
937
938 // not thread-safe; must be called in a mutex-locked region!
939 void _copy_cuda_to_main(void* mem_main_dst, std::size_t this_offset, std::size_t size) const
940 {
941 XASSERT(!this->_is_mapped_cuda_write());
942 XASSERT(mem_main_dst != nullptr);
943 XASSERT(this->mem_cuda != nullptr);
944 XASSERT(this_offset + size <= this->bytes);
945 Memory::memcopy_cuda_to_main(mem_main_dst, reinterpret_cast<char*>(this->mem_cuda) + this_offset, size);
946 }
947
948 // not thread-safe; must be called in a mutex-locked region!
949 void _copy_main_to_cuda(void* mem_cuda_dst, std::size_t this_offset, std::size_t size) const
950 {
951 XASSERT(!this->_is_mapped_cuda_write());
952 XASSERT(mem_cuda_dst != nullptr);
953 XASSERT(this->mem_main != nullptr);
954 XASSERT(this_offset + size <= this->bytes);
955 Memory::memcopy_main_to_cuda(mem_cuda_dst, reinterpret_cast<char*>(this->mem_main) + this_offset, size);
956 }
957
958 // not thread-safe; must be called in a mutex-locked region!
959 void _copy_cuda_to_cuda(void* mem_cuda_dst, std::size_t this_offset, std::size_t size) const
960 {
961 XASSERT(!this->_is_mapped_cuda_write());
962 XASSERT(mem_cuda_dst != nullptr);
963 XASSERT(this->mem_cuda != nullptr);
964 XASSERT(this_offset + size <= this->bytes);
965 Memory::memcopy_cuda(mem_cuda_dst, reinterpret_cast<char*>(this->mem_cuda) + this_offset, size);
966 }
967
968 void _copy_to(void* dst_ptr, Location dst_loc, std::size_t this_offset, std::size_t size) const
969 {
970 switch(dst_loc)
971 {
972 case Location::main:
973 if(*(this->valid_locations & Location::main))
974 _copy_main_to_main(dst_ptr, this_offset, size);
975 else if(*(this->valid_locations & Location::cuda))
976 _copy_cuda_to_main(dst_ptr, this_offset, size);
977 else
978 XABORTM("Memory::Block::_copy_to: invalid destination memory location");
979 break;
980
981 case Location::cuda:
982 if(*(this->valid_locations & Location::cuda))
983 _copy_cuda_to_cuda(dst_ptr, this_offset, size);
984 else if(*(this->valid_locations & Location::main))
985 _copy_cuda_to_main(dst_ptr, this_offset, size);
986 else
987 XABORTM("Memory::Block::_copy_to: invalid destination memory location");
988 break;
989
990 default:
991 XABORTM("Memory::Block::_copy_to: no up-to-date source memory location");
992 break;
993 }
994 }
995 }; // class Block
996
1002
1004 _block(nullptr),
1005 _location(Location::none),
1006 _access(Access::none),
1007 _bytes(0u),
1008 _ptr(nullptr)
1009 {
1010 }
1011
1012 View::View(Block* blk, Location l, Access a, std::size_t b, void* p) :
1013 _block(blk),
1014 _location(l),
1015 _access(a),
1016 _bytes(b),
1017 _ptr(p)
1018 {
1019 }
1020
1022 {
1023 if((this->_block != nullptr) && (this->_ptr != nullptr))
1024 {
1025 this->_block->release_view(*this);
1026 }
1027 }
1028
1030 View::View(View&& other) :
1031 _block(other._block),
1032 _location(other._location),
1033 _access(other._access),
1034 _bytes(other._bytes),
1035 _ptr(other._ptr)
1036 {
1037 other._reset();
1038 }
1039
1042 {
1043 if(this != &other)
1044 {
1045 this->release();
1046 this->_block = other._block;
1047 this->_location = other._location;
1048 this->_access = other._access;
1049 this->_bytes = other._bytes;
1050 this->_ptr = other._ptr;
1051 other._reset();
1052 }
1053 return *this;
1054 }
1055
1056 void View::release() const
1057 {
1058 if(this->_block)
1059 this->_block->release_view(*this);
1060 const_cast<View*>(this)->_reset();
1061 }
1062
1064 {
1065 this->_block = nullptr;
1066 this->_location = Location::none;
1067 this->_access = Access::none;
1068 this->_bytes = 0u;
1069 this->_ptr = nullptr;
1070 }
1071
1073 {
1074 if(!this->writeable())
1075 throw AccessException("Memory::View::format_to_zero(): write access required for format");
1076 if(this->empty())
1077 return;
1078
1079 if(this->_location == Location::main)
1080 Memory::memset_main(this->_ptr, 0, this->_bytes);
1081 else if(this->_location == Location::cuda)
1082 Memory::memset_cuda(this->_ptr, 0, this->_bytes);
1083 }
1084
1086 {
1087 if(!this->writeable())
1088 throw AccessException("Memory::View::format_to_zero(): write access required for format");
1089 if(this->empty())
1090 return;
1091
1092 if(this->_location == Location::main)
1093 Memory::memset_main(this->_ptr, 0xFF, this->_bytes);
1094 else if(this->_location == Location::cuda)
1095 Memory::memset_cuda(this->_ptr, 0xFF, this->_bytes);
1096 }
1097
1098 void View::copy_raw_from(const void* src_ptr, Location src_loc)
1099 {
1100 XASSERT(src_ptr != nullptr);
1101 if(!this->writeable())
1102 throw AccessException("Memory::View::format_to_zero(): write access required for copy");
1103
1104 // where do we copy to?
1105 switch(this->_location)
1106 {
1107 case Location::main:
1108 switch(src_loc)
1109 {
1110 case Location::main:
1111 Memory::memcopy_main(this->_ptr, src_ptr, this->_bytes);
1112 break;
1113
1114 case Location::cuda:
1115 Memory::memcopy_cuda_to_main(this->_ptr, src_ptr, this->_bytes);
1116 break;
1117
1118 default:
1119 XABORTM("Memory::View::copy_raw_from: unknown source memory view location");
1120 break;
1121 }
1122 break;
1123
1124 case Location::cuda:
1125 switch(src_loc)
1126 {
1127 case Location::main:
1128 Memory::memcopy_main_to_cuda(this->_ptr, src_ptr, this->_bytes);
1129 break;
1130
1131 case Location::cuda:
1132 Memory::memcopy_cuda(this->_ptr, src_ptr, this->_bytes);
1133 break;
1134
1135 default:
1136 XABORTM("Memory::View::copy_raw_from: unknown source memory view location");
1137 break;
1138 }
1139 break;
1140
1141 default:
1142 XABORTM("Memory::View::copy_raw_from: unknown destination memory view location");
1143 break;
1144 }
1145 }
1146
1152
1153 Arbiter::Arbiter(Block* block, std::size_t offset, std::size_t size) :
1154 _block(block),
1155 _offset(offset),
1156 _size(size)
1157 {
1158 if(block != nullptr)
1159 {
1160 if(block->bytes < offset)
1161 throw RangeException("Memory::Arbiter: invalid block memory offset");
1162 if(size != full_size)
1163 {
1164 if(block->bytes < offset + size)
1165 throw RangeException("Memory::Arbiter: invalid block memory range");
1166 }
1167 }
1168 else
1169 {
1170 XASSERT(offset == std::size_t(0));
1171 XASSERT(size == std::size_t(0));
1172 }
1173 }
1174
1175 Arbiter::Arbiter(std::size_t bytes, Location mem_loc, Init mem_ini) :
1176 _block(new Block()),
1177 _offset(0),
1178 _size(full_size)
1179 {
1180 _block->create(bytes, mem_loc, mem_ini);
1181 }
1182
1184 Arbiter()
1185 {
1186 this->_move(std::forward<Arbiter>(other));
1187 }
1188
1190 {
1191 if(this->_block != other._block)
1192 {
1193 Block::detach(this->_block);
1194 this->_block = nullptr;
1195 this->_move(std::forward<Arbiter>(other));
1196 }
1197 return *this;
1198 }
1199
1201 {
1202 Block::detach(this->_block);
1203 }
1204
1206 {
1207 XASSERT(this->_block == nullptr);
1208 this->_block = other._block;
1209 this->_offset = other._offset;
1210 this->_size = other._size;
1211 other._block = nullptr;
1212 other._offset = 0u;
1213 other._size = 0u;
1214 }
1215
1216 Arbiter Arbiter::attach(std::size_t offset, std::size_t size) const
1217 {
1218 if(this->_block == nullptr)
1219 {
1220 if(offset != std::size_t(0))
1221 throw RangeException("Memory::Arbiter::attach(): invalid block memory offset");
1222 if((size != full_size) && (size != std::size_t(0)))
1223 throw RangeException("Memory::Arbiter::attach(): invalid block memory size");
1224 return Arbiter();
1225 }
1226
1227 if(this->_size != full_size)
1228 {
1229 if(this->_size < this->_offset + offset)
1230 throw RangeException("Memory::Arbiter::attach(): invalid block memory offset");
1231 if(size == full_size)
1232 return Arbiter(Block::attach(this->_block), this->_offset + offset, this->_size);
1233 if(this->_size < size)
1234 throw RangeException("Memory::Arbiter::attach(): invalid block memory range");
1235 return Arbiter(Block::attach(this->_block), this->_offset + offset, size);
1236 }
1237 else // this->_size == full_size
1238 {
1239 if(this->_block->bytes < this->_offset + offset)
1240 throw RangeException("Memory::Arbiter::attach(): invalid block memory offset");
1241 if(size == full_size)
1242 return Arbiter(Block::attach(this->_block), this->_offset + offset, full_size);
1243 if(this->_block->bytes < this->_offset + offset + size)
1244 throw RangeException("Memory::Arbiter::attach(): invalid block memory range");
1245 return Arbiter(Block::attach(this->_block), this->_offset + offset, size);
1246 }
1247 }
1248
1249 void Arbiter::attach(const Arbiter& other, std::size_t offset, std::size_t size)
1250 {
1251 // self-attach?
1252 if(this->_block == other._block)
1253 {
1254 if(this->_size != full_size)
1255 {
1256 if(this->_size < this->_offset + offset)
1257 throw RangeException("Memory::Arbiter::attach(): invalid block memory offset");
1258 if(size != full_size)
1259 {
1260 if(this->_size < size)
1261 throw RangeException("Memory::Arbiter::attach(): invalid block memory range");
1262 this->_size = size;
1263 }
1264 }
1265 else // this->_size == full_size
1266 {
1267 if(this->_block->bytes < this->_offset + offset)
1268 throw RangeException("Memory::Arbiter::attach(): invalid block memory offset");
1269 if(size != full_size)
1270 {
1271 if(this->_block->bytes < this->_offset + offset + size)
1272 throw RangeException("Memory::Arbiter::attach(): invalid block memory range");
1273 this->_size = size;
1274 }
1275 }
1276 this->_offset += offset;
1277 return;
1278 }
1279
1280 // detach old block
1281 Block::detach(this->_block);
1282
1283 // attach new block
1284 this->_move(other.attach(offset, size));
1285 }
1286
1287 Arbiter Arbiter::clone(std::size_t offset, std::size_t size, bool clone_block) const
1288 {
1289 // no block assigned?
1290 if(this->_block == nullptr)
1291 {
1292 XASSERTM(offset == std::size_t(0), "arbiter clone range offset is out-of-bounds");
1293 XASSERTM((size == std::size_t(0)) || (size == full_size), "arbiter clone range size is out-of-bounds");
1294 return Arbiter();
1295 }
1296
1297 // validate range offset and size
1298 if(this->_block->bytes < offset)
1299 throw RangeException("Memory::Arbiter::clone(): invalid block memory offset");
1300 if((size != full_size) && (this->_block->bytes < offset + size))
1301 throw RangeException("Memory::Arbiter::clone(): invalid block memory range");
1302
1303 // create new block and copy content
1304 if(clone_block)
1305 {
1306 // clone entire block
1307 Arbiter arb(this->_block->bytes);
1308 arb._block->copy_from(*this->_block, Memory::Location::none, std::size_t(0), std::size_t(0), this->_block->bytes);
1309 arb._offset = this->_offset + offset;
1310 arb._size = size;
1311 return arb;
1312 }
1313 else
1314 {
1315 // clone selected memory range
1316 if(size == full_size)
1317 {
1318 if(this->_size == full_size)
1319 size = this->_block->bytes - offset;
1320 else
1321 size = this->_size - offset;
1322 }
1323 Arbiter arb(size);
1324 arb._block->copy_from(*this->_block, Memory::Location::none, std::size_t(0), this->_offset + offset, size);
1325 return arb;
1326 }
1327 }
1328
1329 void Arbiter::clone(const Arbiter& source, std::size_t offset, std::size_t size, bool clone_block)
1330 {
1331 // self-clone?
1332 if((this->_block == source._block) && clone_block)
1333 {
1334 // in this case, we only need to update the offset and size, if necessary
1335 if(this->_size != full_size)
1336 {
1337 if(this->_size < this->_offset + offset)
1338 throw RangeException("Memory::Arbiter::attach(): invalid block memory offset");
1339 if(size != full_size)
1340 {
1341 if(this->_size < size)
1342 throw RangeException("Memory::Arbiter::attach(): invalid block memory range");
1343 this->_size = size;
1344 }
1345 }
1346 else // this->_size == full_size
1347 {
1348 if(this->_block->bytes < this->_offset + offset)
1349 throw RangeException("Memory::Arbiter::attach(): invalid block memory offset");
1350 if(size != full_size)
1351 {
1352 if(this->_block->bytes < this->_offset + offset + size)
1353 throw RangeException("Memory::Arbiter::attach(): invalid block memory range");
1354 this->_size = size;
1355 }
1356 }
1357 this->_offset += offset;
1358 return;
1359 }
1360
1361 // clone to temporary, detach and move temporary to properly handle self-clone
1362 Arbiter temp(source.clone(offset, size, clone_block));
1363 Block::detach(this->_block);
1364 this->_block = nullptr;
1365 this->_move(std::move(temp));
1366 }
1367
1369 {
1370 Block::detach(this->_block);
1371 this->_block = nullptr;
1372 }
1373
1374 std::size_t Arbiter::bytes() const
1375 {
1376 if(_block == nullptr)
1377 return std::size_t(0u);
1378 else if(this->_size == full_size)
1379 return _block->bytes - this->_offset;
1380 else
1381 return this->_size;
1382 }
1383
1384 std::size_t Arbiter::block_bytes() const
1385 {
1386 return (_block != nullptr ? _block->bytes : std::size_t(0u));
1387 }
1388
1390 {
1391 return (this->_block ? this->_block->valid_locations : Location::none);
1392 }
1393
1394 void Arbiter::alloc(Location mem_loc, Init mem_ini)
1395 {
1396 if(this->_block)
1397 this->_block->alloc(mem_loc, mem_ini);
1398 }
1399
1401 {
1402 if(this->_block)
1403 this->_block->free(mem_loc);
1404 }
1405
1406 void Arbiter::copy(const Arbiter& source, Location mem_loc)
1407 {
1408 if(this->_block == source._block)
1409 return;
1410
1411 XASSERT(this->_block != nullptr);
1412 XASSERT(source._block != nullptr);
1413
1414 std::size_t dst_size = (this->_size == full_size ? this->_block->bytes : this->_size);
1415 std::size_t src_size = (source._size == full_size ? source._block->bytes : source._size);
1416 XASSERTM(dst_size == src_size, "source and destination arbiters have different size");
1417
1418 this->_block->copy_from(*source._block, mem_loc, this->_offset, source._offset, dst_size);
1419 }
1420
1421 void Arbiter::copy(const void* src_ptr, Location src_loc, Location mem_loc)
1422 {
1423 XASSERT(this->_block != nullptr);
1424 this->_block->copy_from(src_ptr, src_loc, mem_loc, this->_offset, this->_size);
1425 }
1426
1427 void Arbiter::copy_to(void* dst_ptr, Location dst_loc) const
1428 {
1429 XASSERT(this->_block != nullptr);
1430 this->_block->copy_to(dst_ptr, dst_loc, this->_offset, this->_size);
1431 }
1432
1434 {
1435 if(this->_block)
1436 this->_block->format(mem_loc, this->_offset, this->_size);
1437 }
1438
1440 {
1441 if(this->_block)
1442 this->_block->format_random(mem_loc, this->_offset, this->_size);
1443 }
1444
1446 {
1447 XASSERT(this->_block != nullptr);
1448 return this->_block->map_view(mem_loc, access, this->_offset, this->_size);
1449 }
1450
1451 View Arbiter::view(Location mem_loc, Access access) const
1452 {
1453 XASSERT(this->_block != nullptr);
1454 XASSERTM(!*(access & Memory::Access::write), "Memory::Arbiter::view(): const overload does not offer write access");
1455 return this->_block->map_view(mem_loc, access, this->_offset, this->_size);
1456 }
1457
1463
1464 void Manager::initialize(int, char**)
1465 {
1466 // nothing to do here (for now)
1467 }
1468
1470 {
1471 // all memory blocks should have been freed by now
1472 if(manager_block_set.empty())
1473 return;
1474
1475 // print a warning
1476 std::cout << "\nWARNING: Memory::Manager: " << manager_block_set.size()
1477 << " memory arbiter blocks are still allocated upon program termination\n\n";
1478 }
1479
1481 {
1482 std::unique_lock<std::mutex> lock(manager_mutex);
1483
1484 for(auto& mbi : manager_block_set)
1485 {
1486 mbi->free_unmapped(mem_loc);
1487 }
1488 }
1489
1491 {
1492 std::unique_lock<std::mutex> lock(manager_mutex);
1493
1494 std::size_t r = 0u;
1495 for(const auto& mbi : manager_block_set)
1496 {
1497 r += mbi->bytes;
1498 }
1499
1500 return r;
1501 }
1502
1504 {
1505 std::unique_lock<std::mutex> lock(manager_mutex);
1506
1507 std::size_t r = 0u;
1508 for(const auto& mbi : manager_block_set)
1509 {
1510 if(mbi->mem_main != nullptr)
1511 r += mbi->bytes;
1512 }
1513
1514 return r;
1515 }
1516
1518 {
1519 std::unique_lock<std::mutex> lock(manager_mutex);
1520
1521 std::size_t r = 0u;
1522 for(const auto& mbi : manager_block_set)
1523 {
1524 if(mbi->mem_cuda != nullptr)
1525 r += mbi->bytes;
1526 }
1527
1528 return r;
1529 }
1530
1531 } // namespace Memory
1532} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Memory access 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 alloc(Location mem_loc, Init mem_ini=Init::dont_care)
Allocates memory for a given memory block.
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.
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
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.
std::size_t block_bytes() const
Returns the size of the entire attached memory block in bytes.
Memory Arbiter memory block class.
void * mem_main
memory block pointer to main (host) memory
std::size_t read_views_cuda
number of currently active cuda memory views with read access
std::size_t read_views_main
number of currently active main memory views with read access
Block(const Block &)=delete
no copy constructor
Block & operator=(const Block &)=delete
no copy-assign operator
Lock lock_mutex() const
locks the arbiter mutex assigned to this memory block
Location write_location
location of current write view
Location valid_locations
combination of all currently up-to-date memory locations
void * mem_cuda
memory block pointer to cuda device memory
bool uses_same_lock(const Block &other) const
checks wether another block is assigned to the same mutex as this block
bool write_overlap
specifies whether the write view(s) can be overlapped
static constexpr std::size_t full_size
full size
std::size_t write_views
number of currently active write views
std::size_t ref_count
total number of references to this memory block
std::size_t bytes
total size of memory block in bytes
static void initialize(int argc, char **argv)
Initializes FEAT's memory management.
static std::size_t bytes_reserved()
Returns the total number of bytes reserved over all memory blocks.
static std::size_t bytes_allocated_main()
Returns the total number of bytes currently allocated in main memory over all blocks.
static void finalize()
Finalizes FEAT's memory management.
static std::size_t bytes_allocated_cuda()
Returns the total number of bytes currently allocated in cuda memory over all blocks.
static void free_all_unmapped(Location mem_loc)
Frees all memory blocks for a given set of memory location that are currently not mapped to a view.
Memory related exception base class.
Memory range exception.
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
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.
View()
default constructor
View & operator=(const View &)=delete
no copy-assign allowed
std::size_t _bytes
the size of the memory block in bytes
Access _access
the granted memory access
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
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.
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.
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.
Access
Memory access enumeration.
@ overlap
explicitly allow overlapping writes (see Overlapping Memory Views)
@ read
read-only access
@ write
write-only access (see attention note above)
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.
Location
Memory location enumeration.
@ none
no or unknown or unspecified location
void * alloc_cuda(std::size_t bytes)
Allocate a memory block in cuda heap memory.
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.
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.
void free_main(void *ptr)
Frees a memory block in main heap memory allocated by alloc_main.
void free_cuda(void *ptr)
Frees a memory block in cuda heap memory allocated by alloc_cuda.
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.
void * alloc_main(std::size_t bytes)
Allocate a memory block in main heap memory.
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
void dump_call_stack_to_stderr()
Dumps the call-stack to stderr.
Definition: os_windows.cpp:152
FEAT namespace.
Definition: adjactor.hpp:12