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 += block_used * block_size;
200 stats.reserved_blocks += block_count - block_used;
201 stats.reserved_bytes += (block_count - block_used) * block_size;
210 void print_state(std::ostream &s)
const {
213 const size_t alloc_size = m_hint_offset *
sizeof(uint32_t);
215 uint32_t *
const sb_state_array =
216 accessible ? m_sb_state_array : (uint32_t *)host.
allocate(alloc_size);
219 Kokkos::Impl::DeepCopy<Kokkos::HostSpace, base_memory_space>(
220 sb_state_array, m_sb_state_array, alloc_size);
222 "MemoryPool::print_state(): fence after copying state array to "
226 Impl::_print_memory_pool_state(s, sb_state_array, m_sb_count, m_sb_size_lg2,
227 m_sb_state_size, state_shift,
237 KOKKOS_DEFAULTED_FUNCTION MemoryPool(MemoryPool &&) =
default;
238 KOKKOS_DEFAULTED_FUNCTION MemoryPool(
const MemoryPool &) =
default;
239 KOKKOS_DEFAULTED_FUNCTION MemoryPool &operator=(MemoryPool &&) =
default;
240 KOKKOS_DEFAULTED_FUNCTION MemoryPool &operator=(
const MemoryPool &) =
default;
242 KOKKOS_INLINE_FUNCTION MemoryPool()
244 m_sb_state_array(nullptr),
247 m_max_block_size_lg2(0),
248 m_min_block_size_lg2(0),
252 m_unused_padding(0) {}
268 MemoryPool(
const base_memory_space &memspace,
269 const size_t min_total_alloc_size,
size_t min_block_alloc_size = 0,
270 size_t max_block_alloc_size = 0,
size_t min_superblock_size = 0)
272 m_sb_state_array(nullptr),
275 m_max_block_size_lg2(0),
276 m_min_block_size_lg2(0),
280 m_unused_padding(0) {
281 const uint32_t int_align_lg2 = 3;
282 const uint32_t int_align_mask = (1u << int_align_lg2) - 1;
283 const uint32_t default_min_block_size = 1u << 6;
284 const uint32_t default_max_block_size = 1u << 12;
285 const uint32_t default_min_superblock_size = 1u << 20;
290 if (0 == min_block_alloc_size) {
293 min_superblock_size =
294 std::min(
size_t(default_min_superblock_size), min_total_alloc_size);
296 min_block_alloc_size =
297 std::min(
size_t(default_min_block_size), min_superblock_size);
299 max_block_alloc_size =
300 std::min(
size_t(default_max_block_size), min_superblock_size);
301 }
else if (0 == min_superblock_size) {
307 const size_t max_superblock =
308 min_block_alloc_size * max_block_per_superblock;
310 min_superblock_size =
311 std::min(max_superblock,
312 std::min(
size_t(max_superblock_size), min_total_alloc_size));
315 if (0 == max_block_alloc_size) {
316 max_block_alloc_size = min_superblock_size;
330 Kokkos::Impl::memory_pool_bounds_verification(
331 min_block_alloc_size, max_block_alloc_size, min_superblock_size,
332 max_superblock_size, max_block_per_superblock, min_total_alloc_size);
338 m_min_block_size_lg2 =
339 Kokkos::Impl::integral_power_of_two_that_contains(min_block_alloc_size);
341 m_max_block_size_lg2 =
342 Kokkos::Impl::integral_power_of_two_that_contains(max_block_alloc_size);
345 Kokkos::Impl::integral_power_of_two_that_contains(min_superblock_size);
351 const uint64_t sb_size_mask = (1LU << m_sb_size_lg2) - 1;
353 m_sb_count = (min_total_alloc_size + sb_size_mask) >> m_sb_size_lg2;
360 const uint32_t max_block_count_lg2 = m_sb_size_lg2 - m_min_block_size_lg2;
363 (CB::buffer_bound_lg2(max_block_count_lg2) + int_align_mask) &
369 const size_t all_sb_state_size =
370 (m_sb_count * m_sb_state_size + int_align_mask) & ~int_align_mask;
374 const int32_t number_block_sizes =
375 1 + m_max_block_size_lg2 - m_min_block_size_lg2;
380 const int32_t block_size_array_size =
381 (number_block_sizes + int_align_mask) & ~int_align_mask;
383 m_hint_offset = all_sb_state_size;
384 m_data_offset = m_hint_offset + block_size_array_size * HINT_PER_BLOCK_SIZE;
388 const size_t header_size = m_data_offset *
sizeof(uint32_t);
389 const size_t alloc_size =
390 header_size + (size_t(m_sb_count) << m_sb_size_lg2);
392 Record *rec = Record::allocate(memspace,
"Kokkos::MemoryPool", alloc_size);
394 m_tracker.assign_allocated_record_to_uninitialized(rec);
396 m_sb_state_array = (uint32_t *)rec->data();
400 uint32_t *
const sb_state_array =
401 accessible ? m_sb_state_array : (uint32_t *)host.
allocate(header_size);
403 for (int32_t i = 0; i < m_data_offset; ++i) sb_state_array[i] = 0;
407 for (int32_t i = 0; i < number_block_sizes; ++i) {
408 const uint32_t block_size_lg2 = i + m_min_block_size_lg2;
409 const uint32_t block_count_lg2 = m_sb_size_lg2 - block_size_lg2;
410 const uint32_t block_state = block_count_lg2 << state_shift;
411 const uint32_t hint_begin = m_hint_offset + i * HINT_PER_BLOCK_SIZE;
417 const int32_t jbeg = (i * m_sb_count) / number_block_sizes;
418 const int32_t jend = ((i + 1) * m_sb_count) / number_block_sizes;
420 sb_state_array[hint_begin] = uint32_t(jbeg);
421 sb_state_array[hint_begin + 1] = uint32_t(jbeg);
423 for (int32_t j = jbeg; j < jend; ++j) {
424 sb_state_array[j * m_sb_state_size] = block_state;
431 Kokkos::Impl::DeepCopy<base_memory_space, Kokkos::HostSpace>(
432 m_sb_state_array, sb_state_array, header_size);
434 "MemoryPool::MemoryPool(): fence after copying state array from "
439 Kokkos::memory_fence();
449 KOKKOS_FORCEINLINE_FUNCTION
450 uint32_t get_block_size_lg2(uint32_t n)
const noexcept {
451 const unsigned i = Kokkos::Impl::integral_power_of_two_that_contains(n);
453 return i < m_min_block_size_lg2 ? m_min_block_size_lg2 : i;
458 KOKKOS_INLINE_FUNCTION
459 uint32_t allocate_block_size(uint64_t alloc_size)
const noexcept {
460 return alloc_size <= (1UL << m_max_block_size_lg2)
461 ? (1UL << get_block_size_lg2(uint32_t(alloc_size)))
476 void *allocate(
size_t alloc_size, int32_t attempt_limit = 1) const noexcept {
477 if (
size_t(1LU << m_max_block_size_lg2) < alloc_size) {
479 "Kokkos MemoryPool allocation request exceeded specified maximum "
483 if (0 == alloc_size)
return nullptr;
487 const uint32_t block_size_lg2 = get_block_size_lg2(alloc_size);
492 const uint32_t block_count_lg2 = m_sb_size_lg2 - block_size_lg2;
493 const uint32_t block_state = block_count_lg2 << state_shift;
494 const uint32_t block_count = 1u << block_count_lg2;
500 volatile uint32_t *
const hint_sb_id_ptr =
503 + HINT_PER_BLOCK_SIZE
504 * (block_size_lg2 - m_min_block_size_lg2);
506 const int32_t sb_id_begin = int32_t(hint_sb_id_ptr[1]);
511 #if defined(KOKKOS_ENABLE_SYCL) && !defined(KOKKOS_ARCH_INTEL_GPU)
512 const uint32_t block_id_hint = alloc_size;
514 const uint32_t block_id_hint =
515 (uint32_t)(Kokkos::Impl::clock_tic()
516 #ifdef __CUDA_ARCH__ // FIXME_CUDA
519 + (threadIdx.x + blockDim.x * threadIdx.y)
525 uint32_t sb_state = block_state;
529 volatile uint32_t *sb_state_array =
nullptr;
531 while (attempt_limit) {
532 int32_t hint_sb_id = -1;
537 sb_id = hint_sb_id = int32_t(*hint_sb_id_ptr);
539 sb_state_array = m_sb_state_array + (sb_id * m_sb_state_size);
546 if (sb_state == (state_header_mask & *sb_state_array)) {
551 const uint32_t count_lg2 = sb_state >> state_shift;
552 const uint32_t mask = (1u << count_lg2) - 1;
555 sb_state_array, count_lg2, block_id_hint & mask, sb_state);
562 if (0 <= result.
first) {
564 const uint32_t size_lg2 = m_sb_size_lg2 - count_lg2;
568 p = ((
char *)(m_sb_state_array + m_data_offset)) +
569 (uint64_t(sb_id) << m_sb_size_lg2)
570 + (uint64_t(result.
first) << size_lg2);
585 sb_state = block_state;
588 bool update_hint =
false;
589 int32_t sb_id_empty = -1;
590 int32_t sb_id_large = -1;
591 uint32_t sb_state_large = 0;
593 sb_state_array = m_sb_state_array + sb_id_begin * m_sb_state_size;
595 for (int32_t i = 0,
id = sb_id_begin; i < m_sb_count; ++i) {
600 const uint32_t full_state = *sb_state_array;
601 const uint32_t used = full_state & state_used_mask;
602 const uint32_t state = full_state & state_header_mask;
604 if (state == block_state) {
607 if (used < block_count) {
614 update_hint = used + 1 < block_count;
618 }
else if (0 == used) {
621 if (-1 == sb_id_empty) {
628 }
else if ((-1 == sb_id_empty ) &&
629 (-1 == sb_id_large ) &&
630 (state < block_state ) &&
632 (used < (1u << (state >> state_shift)))) {
638 sb_state_large = state;
643 if (++
id < m_sb_count) {
644 sb_state_array += m_sb_state_size;
647 sb_state_array = m_sb_state_array;
657 if (0 <= sb_id_empty) {
666 sb_state_array = m_sb_state_array + (sb_id * m_sb_state_size);
671 const uint32_t state_empty = state_header_mask & *sb_state_array;
675 state_empty == Kokkos::atomic_compare_exchange(
676 sb_state_array, state_empty, block_state);
677 }
else if (0 <= sb_id_large) {
681 sb_state = sb_state_large;
683 sb_state_array = m_sb_state_array + (sb_id * m_sb_state_size);
691 Kokkos::atomic_compare_exchange(hint_sb_id_ptr, uint32_t(hint_sb_id),
708 KOKKOS_INLINE_FUNCTION
709 void deallocate(
void *p,
size_t ) const noexcept {
710 if (
nullptr == p)
return;
714 static_cast<char *
>(p) -
715 reinterpret_cast<char *>(m_sb_state_array + m_data_offset);
718 const int ok_contains =
719 (0 <= d) && (
size_t(d) < (size_t(m_sb_count) << m_sb_size_lg2));
721 int ok_block_aligned = 0;
722 int ok_dealloc_once = 0;
725 const int sb_id = d >> m_sb_size_lg2;
728 volatile uint32_t *
const sb_state_array =
729 m_sb_state_array + (sb_id * m_sb_state_size);
731 const uint32_t block_state = (*sb_state_array) & state_header_mask;
732 const uint32_t block_size_lg2 =
733 m_sb_size_lg2 - (block_state >> state_shift);
735 ok_block_aligned = 0 == (d & ((1UL << block_size_lg2) - 1));
737 if (ok_block_aligned) {
742 (d & (ptrdiff_t(1LU << m_sb_size_lg2) - 1)) >> block_size_lg2;
744 const int result = CB::release(sb_state_array, bit, block_state);
746 ok_dealloc_once = 0 <= result;
750 if (!ok_contains || !ok_block_aligned || !ok_dealloc_once) {
751 Kokkos::abort(
"Kokkos MemoryPool::deallocate given erroneous pointer");
757 KOKKOS_INLINE_FUNCTION
758 int number_of_superblocks() const noexcept {
return m_sb_count; }
760 KOKKOS_INLINE_FUNCTION
761 void superblock_state(
int sb_id,
int &block_size,
int &block_count_capacity,
762 int &block_count_used)
const noexcept {
764 block_count_capacity = 0;
765 block_count_used = 0;
767 bool can_access_state_array = []() {
769 (
return SpaceAccessibility<DefaultHostExecutionSpace,
770 base_memory_space>::accessible;))
772 (return SpaceAccessibility<DefaultExecutionSpace,
773 base_memory_space>::accessible;))
776 if (can_access_state_array) {
779 const uint32_t state =
780 ((uint32_t
volatile *)m_sb_state_array)[sb_id * m_sb_state_size];
782 const uint32_t block_count_lg2 = state >> state_shift;
783 const uint32_t block_used = state & state_used_mask;
785 block_size = 1LU << (m_sb_size_lg2 - block_count_lg2);
786 block_count_capacity = 1LU << block_count_lg2;
787 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.