Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_OpenMPTargetSpace.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_OPENMPTARGETSPACE_HPP
46 #define KOKKOS_OPENMPTARGETSPACE_HPP
47 
48 #include <cstring>
49 #include <string>
50 #include <iosfwd>
51 #include <typeinfo>
52 
53 #include <Kokkos_Core_fwd.hpp>
54 
55 #ifdef KOKKOS_ENABLE_OPENMPTARGET
56 
57 #include <Kokkos_HostSpace.hpp>
58 #include <omp.h>
59 /*--------------------------------------------------------------------------*/
60 
61 namespace Kokkos {
62 namespace Impl {
63 
70 // void init_lock_array_host_space();
71 
77 // bool lock_address_host_space(void* ptr);
78 
85 // void unlock_address_host_space(void* ptr);
86 
87 } // namespace Impl
88 } // namespace Kokkos
89 
90 namespace Kokkos {
91 namespace Experimental {
92 
98 class OpenMPTargetSpace {
99  public:
101  typedef OpenMPTargetSpace memory_space;
102  typedef size_t size_type;
103 
110  typedef Kokkos::Experimental::OpenMPTarget execution_space;
111 
113  typedef Kokkos::Device<execution_space, memory_space> device_type;
114 
115  /*--------------------------------*/
116 
118  OpenMPTargetSpace();
119  OpenMPTargetSpace(OpenMPTargetSpace&& rhs) = default;
120  OpenMPTargetSpace(const OpenMPTargetSpace& rhs) = default;
121  OpenMPTargetSpace& operator=(OpenMPTargetSpace&&) = default;
122  OpenMPTargetSpace& operator=(const OpenMPTargetSpace&) = default;
123  ~OpenMPTargetSpace() = default;
124 
126  void* allocate(const size_t arg_alloc_size) const;
127 
129  void deallocate(void* const arg_alloc_ptr, const size_t arg_alloc_size) const;
130 
131  static constexpr const char* name() { return "OpenMPTargetSpace"; }
132 
133  private:
134  friend class Kokkos::Impl::SharedAllocationRecord<
135  Kokkos::Experimental::OpenMPTargetSpace, void>;
136 };
137 } // namespace Experimental
138 } // namespace Kokkos
139 
140 //----------------------------------------------------------------------------
141 //----------------------------------------------------------------------------
142 
143 namespace Kokkos {
144 namespace Impl {
145 
146 template <>
147 class SharedAllocationRecord<Kokkos::Experimental::OpenMPTargetSpace, void>
148  : public SharedAllocationRecord<void, void> {
149  private:
150  friend Kokkos::Experimental::OpenMPTargetSpace;
151 
152  typedef SharedAllocationRecord<void, void> RecordBase;
153 
154  SharedAllocationRecord(const SharedAllocationRecord&) = delete;
155  SharedAllocationRecord& operator=(const SharedAllocationRecord&) = delete;
156 
157  static void deallocate(RecordBase*);
158 
161  static RecordBase s_root_record;
162 
163  const Kokkos::Experimental::OpenMPTargetSpace m_space;
164 
165  protected:
166  ~SharedAllocationRecord();
167  SharedAllocationRecord() = default;
168 
169  SharedAllocationRecord(
170  const Kokkos::Experimental::OpenMPTargetSpace& arg_space,
171  const std::string& arg_label, const size_t arg_alloc_size,
172  const RecordBase::function_type arg_dealloc = &deallocate);
173 
174  public:
175  std::string get_label() const;
176 
177  KOKKOS_INLINE_FUNCTION static SharedAllocationRecord* allocate(
178  const Kokkos::Experimental::OpenMPTargetSpace& arg_space,
179  const std::string& arg_label, const size_t arg_alloc_size) {
180 #if defined(KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST)
181  return new SharedAllocationRecord(arg_space, arg_label, arg_alloc_size);
182 #else
183  return nullptr;
184 #endif
185  }
186 
188  static void* allocate_tracked(
189  const Kokkos::Experimental::OpenMPTargetSpace& arg_space,
190  const std::string& arg_label, const size_t arg_alloc_size);
191 
193  static void* reallocate_tracked(void* const arg_alloc_ptr,
194  const size_t arg_alloc_size);
195 
197  static void deallocate_tracked(void* const arg_alloc_ptr);
198 
199  static SharedAllocationRecord* get_record(void* arg_alloc_ptr);
200 
201  static void print_records(std::ostream&,
202  const Kokkos::Experimental::OpenMPTargetSpace&,
203  bool detail = false);
204 };
205 
206 } // namespace Impl
207 } // namespace Kokkos
208 
209 //----------------------------------------------------------------------------
210 //----------------------------------------------------------------------------
211 
212 namespace Kokkos {
213 namespace Impl {
214 
215 // TODO: implement all possible deep_copies
216 template <class ExecutionSpace>
217 struct DeepCopy<Kokkos::Experimental::OpenMPTargetSpace,
218  Kokkos::Experimental::OpenMPTargetSpace, ExecutionSpace> {
219  DeepCopy(void* dst, const void* src, size_t n) {
220  omp_target_memcpy(dst, const_cast<void*>(src), n, 0, 0,
221  omp_get_default_device(), omp_get_default_device());
222  }
223  DeepCopy(const ExecutionSpace& exec, void* dst, const void* src, size_t n) {
224  exec.fence();
225  omp_target_memcpy(dst, const_cast<void*>(src), n, 0, 0,
226  omp_get_default_device(), omp_get_default_device());
227  }
228 };
229 
230 template <class ExecutionSpace>
231 struct DeepCopy<Kokkos::Experimental::OpenMPTargetSpace, HostSpace,
232  ExecutionSpace> {
233  DeepCopy(void* dst, const void* src, size_t n) {
234  omp_target_memcpy(dst, const_cast<void*>(src), n, 0, 0,
235  omp_get_default_device(), omp_get_initial_device());
236  }
237  DeepCopy(const ExecutionSpace& exec, void* dst, const void* src, size_t n) {
238  exec.fence();
239  omp_target_memcpy(dst, const_cast<void*>(src), n, 0, 0,
240  omp_get_default_device(), omp_get_initial_device());
241  }
242 };
243 
244 template <class ExecutionSpace>
245 struct DeepCopy<HostSpace, Kokkos::Experimental::OpenMPTargetSpace,
246  ExecutionSpace> {
247  DeepCopy(void* dst, const void* src, size_t n) {
248  omp_target_memcpy(dst, const_cast<void*>(src), n, 0, 0,
249  omp_get_initial_device(), omp_get_default_device());
250  }
251  DeepCopy(const ExecutionSpace& exec, void* dst, const void* src, size_t n) {
252  exec.fence();
253  omp_target_memcpy(dst, const_cast<void*>(src), n, 0, 0,
254  omp_get_initial_device(), omp_get_default_device());
255  }
256 };
257 
258 template <>
259 struct VerifyExecutionCanAccessMemorySpace<
260  Kokkos::HostSpace, Kokkos::Experimental::OpenMPTargetSpace> {
261  enum { value = false };
262  inline static void verify(void) {}
263  inline static void verify(const void*) {}
264 };
265 
266 } // namespace Impl
267 } // namespace Kokkos
268 
269 #endif
270 #endif /* #define KOKKOS_OPENMPTARGETSPACE_HPP */