Kokkos Core Kernels Package Version of the Day
Kokkos_ScratchSpace.hpp
1/*
2//@HEADER
3// ************************************************************************
4//
5// Kokkos v. 3.0
6// Copyright (2020) National Technology & Engineering
7// Solutions of Sandia, LLC (NTESS).
8//
9// Under the terms of Contract DE-NA0003525 with NTESS,
10// the U.S. Government retains certain rights in this software.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38//
39// Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40//
41// ************************************************************************
42//@HEADER
43*/
44
45#ifndef KOKKOS_SCRATCHSPACE_HPP
46#define KOKKOS_SCRATCHSPACE_HPP
47
48#include <cstdio>
49#include <cstddef>
50#include <Kokkos_Core_fwd.hpp>
51#include <Kokkos_Concepts.hpp>
52
53/*--------------------------------------------------------------------------*/
54
55namespace Kokkos {
56
60template <class ExecSpace>
62 static_assert(
63 is_execution_space<ExecSpace>::value,
64 "Instantiating ScratchMemorySpace on non-execution-space type.");
65
66 public:
67 // Alignment of memory chunks returned by 'get'
68 // must be a power of two
69 enum { ALIGN = 8 };
70
71 private:
72 mutable char* m_iter_L0 = nullptr;
73 mutable char* m_iter_L1 = nullptr;
74 char* m_end_L0 = nullptr;
75 char* m_end_L1 = nullptr;
76
77 mutable int m_multiplier = 0;
78 mutable int m_offset = 0;
79 mutable int m_default_level = 0;
80
81 enum { MASK = ALIGN - 1 }; // Alignment used by View::shmem_size
82
83 public:
86 using execution_space = ExecSpace;
88 using device_type = Kokkos::Device<execution_space, memory_space>;
89
90 using array_layout = typename ExecSpace::array_layout;
91 using size_type = typename ExecSpace::size_type;
92
93 static constexpr const char* name() { return "ScratchMemorySpace"; }
94
95 template <typename IntType>
96 KOKKOS_INLINE_FUNCTION static IntType align(const IntType& size) {
97 return (size + MASK) & ~MASK;
98 }
99
100 template <typename IntType>
101 KOKKOS_INLINE_FUNCTION void* get_shmem(const IntType& size,
102 int level = -1) const {
103 return get_shmem_common</*aligned*/ false>(size, 1, level);
104 }
105
106 template <typename IntType>
107 KOKKOS_INLINE_FUNCTION void* get_shmem_aligned(const IntType& size,
108 const ptrdiff_t alignment,
109 int level = -1) const {
110 return get_shmem_common</*aligned*/ true>(size, alignment, level);
111 }
112
113 private:
114 template <bool aligned, typename IntType>
115 KOKKOS_INLINE_FUNCTION void* get_shmem_common(const IntType& size,
116 const ptrdiff_t alignment,
117 int level = -1) const {
118 if (level == -1) level = m_default_level;
119 auto& m_iter = (level == 0) ? m_iter_L0 : m_iter_L1;
120 auto& m_end = (level == 0) ? m_end_L0 : m_end_L1;
121 char* previous = m_iter;
122 const ptrdiff_t missalign = size_t(m_iter) % alignment;
123 if (missalign) m_iter += alignment - missalign;
124
125 void* tmp = m_iter + m_offset * (aligned ? size : align(size));
126 if (m_end < (m_iter += (aligned ? size : align(size)) * m_multiplier)) {
127 m_iter = previous; // put it back like it was
128#ifdef KOKKOS_ENABLE_DEBUG
129 // mfh 23 Jun 2015: printf call consumes 25 registers
130 // in a CUDA build, so only print in debug mode. The
131 // function still returns nullptr if not enough memory.
132 KOKKOS_IMPL_DO_NOT_USE_PRINTF(
133 "ScratchMemorySpace<...>::get_shmem: Failed to allocate "
134 "%ld byte(s); remaining capacity is %ld byte(s)\n",
135 long(size), long(m_end - m_iter));
136#endif // KOKKOS_ENABLE_DEBUG
137 tmp = nullptr;
138 }
139 return tmp;
140 }
141
142 public:
143 KOKKOS_DEFAULTED_FUNCTION
144 ScratchMemorySpace() = default;
145
146 template <typename IntType>
147 KOKKOS_INLINE_FUNCTION ScratchMemorySpace(void* ptr_L0,
148 const IntType& size_L0,
149 void* ptr_L1 = nullptr,
150 const IntType& size_L1 = 0)
151 : m_iter_L0((char*)ptr_L0),
152 m_iter_L1((char*)ptr_L1),
153 m_end_L0((char*)ptr_L0 + size_L0),
154 m_end_L1((char*)ptr_L1 + size_L1),
155 m_multiplier(1),
156 m_offset(0),
157 m_default_level(0) {}
158
159 KOKKOS_INLINE_FUNCTION
160 const ScratchMemorySpace& set_team_thread_mode(const int& level,
161 const int& multiplier,
162 const int& offset) const {
163 m_default_level = level;
164 m_multiplier = multiplier;
165 m_offset = offset;
166 return *this;
167 }
168};
169
170} // namespace Kokkos
171
172#endif /* #ifndef KOKKOS_SCRATCHSPACE_HPP */
Scratch memory space associated with an execution space.
Kokkos::Device< execution_space, memory_space > device_type
This execution space preferred device_type.