17 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
18 #include <Kokkos_Macros.hpp>
20 "Including non-public Kokkos header files is not allowed.");
22 #ifndef KOKKOS_MEMORYPOOL_HPP
23 #define KOKKOS_MEMORYPOOL_HPP
25 #include <Kokkos_Core_fwd.hpp>
28 #include <impl/Kokkos_ConcurrentBitset.hpp>
29 #include <impl/Kokkos_Error.hpp>
30 #include <impl/Kokkos_SharedAlloc.hpp>
42 void memory_pool_bounds_verification(
size_t min_block_alloc_size,
43 size_t max_block_alloc_size,
44 size_t min_superblock_size,
45 size_t max_superblock_size,
46 size_t max_block_per_superblock,
47 size_t min_total_alloc_size);
55 void _print_memory_pool_state(std::ostream &s, uint32_t
const *sb_state_ptr,
56 int32_t sb_count, uint32_t sb_size_lg2,
57 uint32_t sb_state_size, uint32_t state_shift,
58 uint32_t state_used_mask);
62 template <
typename DeviceType>
65 using CB = Kokkos::Impl::concurrent_bitset;
67 enum : uint32_t { bits_per_int_lg2 = CB::bits_per_int_lg2 };
68 enum : uint32_t { state_shift = CB::state_shift };
69 enum : uint32_t { state_used_mask = CB::state_used_mask };
70 enum : uint32_t { state_header_mask = CB::state_header_mask };
71 enum : uint32_t { max_bit_count_lg2 = CB::max_bit_count_lg2 };
72 enum : uint32_t { max_bit_count = CB::max_bit_count };
74 enum : uint32_t { HINT_PER_BLOCK_SIZE = 2 };
96 using base_memory_space =
typename DeviceType::memory_space;
100 base_memory_space>::accessible
103 using Tracker = Kokkos::Impl::SharedAllocationTracker;
104 using Record = Kokkos::Impl::SharedAllocationRecord<base_memory_space>;
107 uint32_t *m_sb_state_array;
108 uint32_t m_sb_state_size;
109 uint32_t m_sb_size_lg2;
110 uint32_t m_max_block_size_lg2;
111 uint32_t m_min_block_size_lg2;
113 int32_t m_hint_offset;
114 int32_t m_data_offset;
115 int32_t m_unused_padding;
118 using memory_space =
typename DeviceType::memory_space;
121 enum : uint32_t { max_superblock_size = 1LU << 31 };
122 enum : uint32_t { max_block_per_superblock = max_bit_count };
126 KOKKOS_INLINE_FUNCTION
127 bool operator==(MemoryPool
const &other)
const {
128 return m_sb_state_array == other.m_sb_state_array;
131 KOKKOS_INLINE_FUNCTION
132 size_t capacity() const noexcept {
133 return size_t(m_sb_count) << m_sb_size_lg2;
136 KOKKOS_INLINE_FUNCTION
137 size_t min_block_size() const noexcept {
138 return (1LU << m_min_block_size_lg2);
141 KOKKOS_INLINE_FUNCTION
142 size_t max_block_size() const noexcept {
143 return (1LU << m_max_block_size_lg2);
146 struct usage_statistics {
147 size_t capacity_bytes;
148 size_t superblock_bytes;
149 size_t max_block_bytes;
150 size_t min_block_bytes;
151 size_t capacity_superblocks;
152 size_t consumed_superblocks;
153 size_t consumed_blocks;
154 size_t consumed_bytes;
155 size_t reserved_blocks;
156 size_t reserved_bytes;
159 void get_usage_statistics(usage_statistics &stats)
const {
162 const size_t alloc_size = m_hint_offset *
sizeof(uint32_t);
164 uint32_t *
const sb_state_array =
165 accessible ? m_sb_state_array : (uint32_t *)host.
allocate(alloc_size);
168 Kokkos::Impl::DeepCopy<Kokkos::HostSpace, base_memory_space>(
169 sb_state_array, m_sb_state_array, alloc_size);
171 "MemoryPool::get_usage_statistics(): fence after copying state "
172 "array to HostSpace");
175 stats.superblock_bytes = (1LU << m_sb_size_lg2);
176 stats.max_block_bytes = (1LU << m_max_block_size_lg2);
177 stats.min_block_bytes = (1LU << m_min_block_size_lg2);
178 stats.capacity_bytes = stats.superblock_bytes * m_sb_count;
179 stats.capacity_superblocks = m_sb_count;
180 stats.consumed_superblocks = 0;
181 stats.consumed_blocks = 0;
182 stats.consumed_bytes = 0;
183 stats.reserved_blocks = 0;
184 stats.reserved_bytes = 0;
186 const uint32_t *sb_state_ptr = sb_state_array;
188 for (int32_t i = 0; i < m_sb_count; ++i, sb_state_ptr += m_sb_state_size) {
189 const uint32_t block_count_lg2 = (*sb_state_ptr) >> state_shift;
191 if (block_count_lg2) {
192 const uint32_t block_count = 1u << block_count_lg2;
193 const uint32_t block_size_lg2 = m_sb_size_lg2 - block_count_lg2;
194 const uint32_t block_size = 1u << block_size_lg2;
195 const uint32_t block_used = (*sb_state_ptr) & state_used_mask;
197 stats.consumed_superblocks++;
198 stats.consumed_blocks += block_used;
199 stats.consumed_bytes +=
static_cast<size_t>(block_used) * block_size;
200 stats.reserved_blocks += block_count - block_used;
201 stats.reserved_bytes +=
202 static_cast<size_t>(block_count - block_used) * block_size;
211 void print_state(std::ostream &s)
const {
214 const size_t alloc_size = m_hint_offset *
sizeof(uint32_t);
216 uint32_t *
const sb_state_array =
217 accessible ? m_sb_state_array : (uint32_t *)host.
allocate(alloc_size);
220 Kokkos::Impl::DeepCopy<Kokkos::HostSpace, base_memory_space>(
221 sb_state_array, m_sb_state_array, alloc_size);
223 "MemoryPool::print_state(): fence after copying state array to "
227 Impl::_print_memory_pool_state(s, sb_state_array, m_sb_count, m_sb_size_lg2,
228 m_sb_state_size, state_shift,
238 KOKKOS_DEFAULTED_FUNCTION MemoryPool(MemoryPool &&) =
default;
239 KOKKOS_DEFAULTED_FUNCTION MemoryPool(
const MemoryPool &) =
default;
240 KOKKOS_DEFAULTED_FUNCTION MemoryPool &operator=(MemoryPool &&) =
default;
241 KOKKOS_DEFAULTED_FUNCTION MemoryPool &operator=(
const MemoryPool &) =
default;
243 KOKKOS_INLINE_FUNCTION MemoryPool()
245 m_sb_state_array(nullptr),
248 m_max_block_size_lg2(0),
249 m_min_block_size_lg2(0),
253 m_unused_padding(0) {}
269 MemoryPool(
const base_memory_space &memspace,
270 const size_t min_total_alloc_size,
size_t min_block_alloc_size = 0,
271 size_t max_block_alloc_size = 0,
size_t min_superblock_size = 0)
273 m_sb_state_array(nullptr),
276 m_max_block_size_lg2(0),
277 m_min_block_size_lg2(0),
281 m_unused_padding(0) {
282 const uint32_t int_align_lg2 = 3;
283 const uint32_t int_align_mask = (1u << int_align_lg2) - 1;
284 const uint32_t default_min_block_size = 1u << 6;
285 const uint32_t default_max_block_size = 1u << 12;
286 const uint32_t default_min_superblock_size = 1u << 20;
291 if (0 == min_block_alloc_size) {
294 min_superblock_size =
295 std::min(
size_t(default_min_superblock_size), min_total_alloc_size);
297 min_block_alloc_size =
298 std::min(
size_t(default_min_block_size), min_superblock_size);
300 max_block_alloc_size =
301 std::min(
size_t(default_max_block_size), min_superblock_size);
302 }
else if (0 == min_superblock_size) {
308 const size_t max_superblock =
309 min_block_alloc_size * max_block_per_superblock;
311 min_superblock_size =
312 std::min(max_superblock,
313 std::min(
size_t(max_superblock_size), min_total_alloc_size));
316 if (0 == max_block_alloc_size) {
317 max_block_alloc_size = min_superblock_size;
331 Kokkos::Impl::memory_pool_bounds_verification(
332 min_block_alloc_size, max_block_alloc_size, min_superblock_size,
333 max_superblock_size, max_block_per_superblock, min_total_alloc_size);
339 m_min_block_size_lg2 =
340 Kokkos::Impl::integral_power_of_two_that_contains(min_block_alloc_size);
342 m_max_block_size_lg2 =
343 Kokkos::Impl::integral_power_of_two_that_contains(max_block_alloc_size);
346 Kokkos::Impl::integral_power_of_two_that_contains(min_superblock_size);
352 const uint64_t sb_size_mask = (1LU << m_sb_size_lg2) - 1;
354 m_sb_count = (min_total_alloc_size + sb_size_mask) >> m_sb_size_lg2;
361 const uint32_t max_block_count_lg2 = m_sb_size_lg2 - m_min_block_size_lg2;
364 (CB::buffer_bound_lg2(max_block_count_lg2) + int_align_mask) &
370 const size_t all_sb_state_size =
371 (m_sb_count * m_sb_state_size + int_align_mask) & ~int_align_mask;
375 const int32_t number_block_sizes =
376 1 + m_max_block_size_lg2 - m_min_block_size_lg2;
381 const int32_t block_size_array_size =
382 (number_block_sizes + int_align_mask) & ~int_align_mask;
384 m_hint_offset = all_sb_state_size;
385 m_data_offset = m_hint_offset + block_size_array_size * HINT_PER_BLOCK_SIZE;
389 const size_t header_size = m_data_offset *
sizeof(uint32_t);
390 const size_t alloc_size =
391 header_size + (size_t(m_sb_count) << m_sb_size_lg2);
393 Record *rec = Record::allocate(memspace,
"Kokkos::MemoryPool", alloc_size);
395 m_tracker.assign_allocated_record_to_uninitialized(rec);
397 m_sb_state_array = (uint32_t *)rec->data();
401 uint32_t *
const sb_state_array =
402 accessible ? m_sb_state_array : (uint32_t *)host.
allocate(header_size);
404 for (int32_t i = 0; i < m_data_offset; ++i) sb_state_array[i] = 0;
408 for (int32_t i = 0; i < number_block_sizes; ++i) {
409 const uint32_t block_size_lg2 = i + m_min_block_size_lg2;
410 const uint32_t block_count_lg2 = m_sb_size_lg2 - block_size_lg2;
411 const uint32_t block_state = block_count_lg2 << state_shift;
412 const uint32_t hint_begin = m_hint_offset + i * HINT_PER_BLOCK_SIZE;
418 const int32_t jbeg = (i * m_sb_count) / number_block_sizes;
419 const int32_t jend = ((i + 1) * m_sb_count) / number_block_sizes;
421 sb_state_array[hint_begin] = uint32_t(jbeg);
422 sb_state_array[hint_begin + 1] = uint32_t(jbeg);
424 for (int32_t j = jbeg; j < jend; ++j) {
425 sb_state_array[j * m_sb_state_size] = block_state;
432 Kokkos::Impl::DeepCopy<base_memory_space, Kokkos::HostSpace>(
433 m_sb_state_array, sb_state_array, header_size);
435 "MemoryPool::MemoryPool(): fence after copying state array from "
440 Kokkos::memory_fence();
450 KOKKOS_FORCEINLINE_FUNCTION
451 uint32_t get_block_size_lg2(uint32_t n)
const noexcept {
452 const unsigned i = Kokkos::Impl::integral_power_of_two_that_contains(n);
454 return i < m_min_block_size_lg2 ? m_min_block_size_lg2 : i;
459 KOKKOS_INLINE_FUNCTION
460 uint32_t allocate_block_size(uint64_t alloc_size)
const noexcept {
461 return alloc_size <= (1UL << m_max_block_size_lg2)
462 ? (1UL << get_block_size_lg2(uint32_t(alloc_size)))
477 void *allocate(
size_t alloc_size, int32_t attempt_limit = 1) const noexcept {
478 if (
size_t(1LU << m_max_block_size_lg2) < alloc_size) {
480 "Kokkos MemoryPool allocation request exceeded specified maximum "
484 if (0 == alloc_size)
return nullptr;
488 const uint32_t block_size_lg2 = get_block_size_lg2(alloc_size);
493 const uint32_t block_count_lg2 = m_sb_size_lg2 - block_size_lg2;
494 const uint32_t block_state = block_count_lg2 << state_shift;
495 const uint32_t block_count = 1u << block_count_lg2;
501 volatile uint32_t *
const hint_sb_id_ptr =
504 + HINT_PER_BLOCK_SIZE
505 * (block_size_lg2 - m_min_block_size_lg2);
507 const int32_t sb_id_begin = int32_t(hint_sb_id_ptr[1]);
512 #if defined(KOKKOS_ENABLE_SYCL) && !defined(KOKKOS_ARCH_INTEL_GPU)
513 const uint32_t block_id_hint = alloc_size;
515 const uint32_t block_id_hint =
516 (uint32_t)(Kokkos::Impl::clock_tic()
517 #ifdef __CUDA_ARCH__ // FIXME_CUDA
520 + (threadIdx.x + blockDim.x * threadIdx.y)
526 uint32_t sb_state = block_state;
530 volatile uint32_t *sb_state_array =
nullptr;
532 while (attempt_limit) {
533 int32_t hint_sb_id = -1;
538 sb_id = hint_sb_id = int32_t(*hint_sb_id_ptr);
540 sb_state_array = m_sb_state_array + (sb_id * m_sb_state_size);
547 if (sb_state == (state_header_mask & *sb_state_array)) {
552 const uint32_t count_lg2 = sb_state >> state_shift;
553 const uint32_t mask = (1u << count_lg2) - 1;
556 sb_state_array, count_lg2, block_id_hint & mask, sb_state);
563 if (0 <= result.
first) {
565 const uint32_t size_lg2 = m_sb_size_lg2 - count_lg2;
569 p = ((
char *)(m_sb_state_array + m_data_offset)) +
570 (uint64_t(sb_id) << m_sb_size_lg2)
571 + (uint64_t(result.
first) << size_lg2);
586 sb_state = block_state;
589 bool update_hint =
false;
590 int32_t sb_id_empty = -1;
591 int32_t sb_id_large = -1;
592 uint32_t sb_state_large = 0;
594 sb_state_array = m_sb_state_array + sb_id_begin * m_sb_state_size;
596 for (int32_t i = 0,
id = sb_id_begin; i < m_sb_count; ++i) {
601 const uint32_t full_state = *sb_state_array;
602 const uint32_t used = full_state & state_used_mask;
603 const uint32_t state = full_state & state_header_mask;
605 if (state == block_state) {
608 if (used < block_count) {
615 update_hint = used + 1 < block_count;
619 }
else if (0 == used) {
622 if (-1 == sb_id_empty) {
629 }
else if ((-1 == sb_id_empty ) &&
630 (-1 == sb_id_large ) &&
631 (state < block_state ) &&
633 (used < (1u << (state >> state_shift)))) {
639 sb_state_large = state;
644 if (++
id < m_sb_count) {
645 sb_state_array += m_sb_state_size;
648 sb_state_array = m_sb_state_array;
658 if (0 <= sb_id_empty) {
667 sb_state_array = m_sb_state_array + (sb_id * m_sb_state_size);
672 const uint32_t state_empty = state_header_mask & *sb_state_array;
676 state_empty == Kokkos::atomic_compare_exchange(
677 sb_state_array, state_empty, block_state);
678 }
else if (0 <= sb_id_large) {
682 sb_state = sb_state_large;
684 sb_state_array = m_sb_state_array + (sb_id * m_sb_state_size);
692 Kokkos::atomic_compare_exchange(hint_sb_id_ptr, uint32_t(hint_sb_id),
709 KOKKOS_INLINE_FUNCTION
710 void deallocate(
void *p,
size_t ) const noexcept {
711 if (
nullptr == p)
return;
715 static_cast<char *
>(p) -
716 reinterpret_cast<char *>(m_sb_state_array + m_data_offset);
719 const int ok_contains =
720 (0 <= d) && (
size_t(d) < (size_t(m_sb_count) << m_sb_size_lg2));
722 int ok_block_aligned = 0;
723 int ok_dealloc_once = 0;
726 const int sb_id = d >> m_sb_size_lg2;
729 volatile uint32_t *
const sb_state_array =
730 m_sb_state_array + (sb_id * m_sb_state_size);
732 const uint32_t block_state = (*sb_state_array) & state_header_mask;
733 const uint32_t block_size_lg2 =
734 m_sb_size_lg2 - (block_state >> state_shift);
736 ok_block_aligned = 0 == (d & ((1UL << block_size_lg2) - 1));
738 if (ok_block_aligned) {
743 (d & (ptrdiff_t(1LU << m_sb_size_lg2) - 1)) >> block_size_lg2;
745 const int result = CB::release(sb_state_array, bit, block_state);
747 ok_dealloc_once = 0 <= result;
751 if (!ok_contains || !ok_block_aligned || !ok_dealloc_once) {
752 Kokkos::abort(
"Kokkos MemoryPool::deallocate given erroneous pointer");
758 KOKKOS_INLINE_FUNCTION
759 int number_of_superblocks() const noexcept {
return m_sb_count; }
761 KOKKOS_INLINE_FUNCTION
762 void superblock_state(
int sb_id,
int &block_size,
int &block_count_capacity,
763 int &block_count_used)
const noexcept {
765 block_count_capacity = 0;
766 block_count_used = 0;
768 bool can_access_state_array = []() {
770 (
return SpaceAccessibility<DefaultHostExecutionSpace,
771 base_memory_space>::accessible;))
773 (return SpaceAccessibility<DefaultExecutionSpace,
774 base_memory_space>::accessible;))
777 if (can_access_state_array) {
780 const uint32_t state =
781 ((uint32_t
volatile *)m_sb_state_array)[sb_id * m_sb_state_size];
783 const uint32_t block_count_lg2 = state >> state_shift;
784 const uint32_t block_used = state & state_used_mask;
786 block_size = 1LU << (m_sb_size_lg2 - block_count_lg2);
787 block_count_capacity = 1LU << block_count_lg2;
788 block_count_used = block_used;
void * allocate(const ExecutionSpace &, const size_t arg_alloc_size) const
Allocate untracked memory in the space.
Replacement for std::pair that works on CUDA devices.
void deallocate(void *const arg_alloc_ptr, const size_t arg_alloc_size) const
Deallocate untracked memory in the space.
first_type first
The first element of the pair.
Memory management for host memory.
Declaration of parallel operators.
Access relationship between DstMemorySpace and SrcMemorySpace.