17 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
18 #include <Kokkos_Macros.hpp>
20 "Including non-public Kokkos header files is not allowed.");
22 #ifndef KOKKOS_SCRATCHSPACE_HPP
23 #define KOKKOS_SCRATCHSPACE_HPP
27 #include <Kokkos_Core_fwd.hpp>
28 #include <Kokkos_Concepts.hpp>
37 template <
class ExecSpace>
38 class ScratchMemorySpace {
40 is_execution_space<ExecSpace>::value,
41 "Instantiating ScratchMemorySpace on non-execution-space type.");
45 constexpr
static int ALIGN = 8;
48 mutable char* m_iter_L0 =
nullptr;
49 mutable char* m_iter_L1 =
nullptr;
50 char* m_end_L0 =
nullptr;
51 char* m_end_L1 =
nullptr;
53 mutable int m_multiplier = 0;
54 mutable int m_offset = 0;
55 mutable int m_default_level = 0;
57 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
58 constexpr
static int DEFAULT_ALIGNMENT_MASK = ALIGN - 1;
64 using execution_space = ExecSpace;
66 using device_type = Kokkos::Device<execution_space, memory_space>;
68 using array_layout =
typename ExecSpace::array_layout;
69 using size_type =
typename ExecSpace::size_type;
71 static constexpr
const char* name() {
return "ScratchMemorySpace"; }
73 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
75 template <
typename IntType>
76 KOKKOS_DEPRECATED KOKKOS_INLINE_FUNCTION
static constexpr IntType align(
77 const IntType& size) {
78 return (size + DEFAULT_ALIGNMENT_MASK) & ~DEFAULT_ALIGNMENT_MASK;
82 template <
typename IntType>
83 KOKKOS_INLINE_FUNCTION
void* get_shmem(
const IntType& size,
84 int level = -1)
const {
85 return get_shmem_common<
false>(size, 1, level);
88 template <
typename IntType>
89 KOKKOS_INLINE_FUNCTION
void* get_shmem_aligned(
const IntType& size,
90 const ptrdiff_t alignment,
91 int level = -1)
const {
92 return get_shmem_common<
true>(size, alignment,
97 template <
bool alignment_requested,
typename IntType>
98 KOKKOS_INLINE_FUNCTION
void* get_shmem_common(
99 const IntType& size, [[maybe_unused]]
const ptrdiff_t alignment,
100 int level = -1)
const {
101 if (level == -1) level = m_default_level;
102 auto& m_iter = (level == 0) ? m_iter_L0 : m_iter_L1;
103 auto m_iter_old = m_iter;
104 if constexpr (alignment_requested) {
105 const ptrdiff_t missalign = size_t(m_iter) % alignment;
106 if (missalign) m_iter += alignment - missalign;
112 void* tmp = m_iter + m_offset * size;
113 uintptr_t increment =
static_cast<uintptr_t
>(size) * m_multiplier;
116 const auto end_iter =
117 reinterpret_cast<uintptr_t
>((level == 0) ? m_end_L0 : m_end_L1);
118 auto current_iter =
reinterpret_cast<uintptr_t
>(m_iter);
119 auto capacity = end_iter - current_iter;
121 if (increment > capacity) {
125 #ifdef KOKKOS_ENABLE_DEBUG
130 "ScratchMemorySpace<...>::get_shmem: Failed to allocate "
131 "%ld byte(s); remaining capacity is %ld byte(s)\n",
132 long(size),
long(capacity));
133 #endif // KOKKOS_ENABLE_DEBUG
141 KOKKOS_DEFAULTED_FUNCTION
142 ScratchMemorySpace() =
default;
144 template <
typename IntType>
145 KOKKOS_INLINE_FUNCTION ScratchMemorySpace(
void* ptr_L0,
146 const IntType& size_L0,
147 void* ptr_L1 =
nullptr,
148 const IntType& size_L1 = 0)
149 : m_iter_L0(static_cast<char*>(ptr_L0)),
150 m_iter_L1(static_cast<char*>(ptr_L1)),
151 m_end_L0(static_cast<char*>(ptr_L0) + size_L0),
152 m_end_L1(static_cast<char*>(ptr_L1) + size_L1),
155 m_default_level(0) {}
157 KOKKOS_INLINE_FUNCTION
158 const ScratchMemorySpace& set_team_thread_mode(
const int& level,
159 const int& multiplier,
160 const int& offset)
const {
161 m_default_level = level;
162 m_multiplier = multiplier;
ScratchMemorySpace< ExecSpace > memory_space
Kokkos::Device< execution_space, memory_space > device_type