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. 2.0
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_OPENMPTARGETSPACE_HPP
45 #define KOKKOS_OPENMPTARGETSPACE_HPP
46 
47 #include <cstring>
48 #include <string>
49 #include <iosfwd>
50 #include <typeinfo>
51 
52 #include <Kokkos_Core_fwd.hpp>
53 
54 #ifdef KOKKOS_ENABLE_OPENMPTARGET
55 
56 #include <Kokkos_HostSpace.hpp>
57 #include <omp.h>
58 /*--------------------------------------------------------------------------*/
59 
60 namespace Kokkos {
61 namespace Impl {
62 
69 //void init_lock_array_host_space();
70 
76 //bool lock_address_host_space(void* ptr);
77 
84 //void unlock_address_host_space(void* ptr);
85 
86 } // namespace Impl
87 } // namespace Kokkos
88 
89 namespace Kokkos {
90 namespace Experimental {
91 
97 class OpenMPTargetSpace {
98 public:
99 
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
130  , const size_t arg_alloc_size ) const ;
131 
132 private:
133 
134  friend class Kokkos::Impl::SharedAllocationRecord< Kokkos::Experimental::OpenMPTargetSpace , void > ;
135 };
136 } // namespace Experimental
137 } // namespace Kokkos
138 
139 //----------------------------------------------------------------------------
140 //----------------------------------------------------------------------------
141 
142 namespace Kokkos {
143 namespace Impl {
144 
145 template<>
146 class SharedAllocationRecord< Kokkos::Experimental::OpenMPTargetSpace , void >
147  : public SharedAllocationRecord< void , void >
148 {
149 private:
150 
151  friend Kokkos::Experimental::OpenMPTargetSpace ;
152 
153  typedef SharedAllocationRecord< void , void > RecordBase ;
154 
155  SharedAllocationRecord( const SharedAllocationRecord & ) = delete ;
156  SharedAllocationRecord & operator = ( const SharedAllocationRecord & ) = delete ;
157 
158  static void deallocate( RecordBase * );
159 
161  static RecordBase s_root_record ;
162 
163  const Kokkos::Experimental::OpenMPTargetSpace m_space ;
164 
165 protected:
166 
167  ~SharedAllocationRecord();
168  SharedAllocationRecord() = default ;
169 
170  SharedAllocationRecord( const Kokkos::Experimental::OpenMPTargetSpace & arg_space
171  , const std::string & arg_label
172  , const size_t arg_alloc_size
173  , const RecordBase::function_type arg_dealloc = & deallocate
174  );
175 
176 public:
177 
178  std::string get_label() const;
179 
180  KOKKOS_INLINE_FUNCTION static
181  SharedAllocationRecord * allocate( const Kokkos::Experimental::OpenMPTargetSpace & arg_space
182  , const std::string & arg_label
183  , const size_t arg_alloc_size
184  );
185 
187  static
188  void * allocate_tracked( const Kokkos::Experimental::OpenMPTargetSpace & arg_space
189  , const std::string & arg_label
190  , const size_t arg_alloc_size );
191 
193  static
194  void * reallocate_tracked( void * const arg_alloc_ptr
195  , const size_t arg_alloc_size );
196 
198  static
199  void deallocate_tracked( void * const arg_alloc_ptr );
200 
201 
202  static SharedAllocationRecord * get_record( void * arg_alloc_ptr );
203 
204  static void print_records( std::ostream & , const Kokkos::Experimental::OpenMPTargetSpace & , bool detail = false );
205 };
206 
207 } // namespace Impl
208 } // namespace Kokkos
209 
210 //----------------------------------------------------------------------------
211 //----------------------------------------------------------------------------
212 
213 namespace Kokkos {
214 namespace Impl {
215 
216 //TODO: implement all possible deep_copies
217 template<class ExecutionSpace>
218 struct DeepCopy<Kokkos::Experimental::OpenMPTargetSpace,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, omp_get_default_device(), omp_get_default_device());
221  }
222  DeepCopy( const ExecutionSpace& exec, void * dst , const void * src , size_t n ) {
223  exec.fence();
224  omp_target_memcpy( dst , const_cast<void*> (src) , n, 0, 0, omp_get_default_device(), omp_get_default_device());
225  }
226 };
227 
228 
229 template<class ExecutionSpace>
230 struct DeepCopy<Kokkos::Experimental::OpenMPTargetSpace,HostSpace,ExecutionSpace> {
231  DeepCopy( void * dst , const void * src , size_t n ) {
232  omp_target_memcpy( dst , const_cast<void*> (src) , n, 0, 0, omp_get_default_device(), omp_get_initial_device());
233  }
234  DeepCopy( const ExecutionSpace& exec, void * dst , const void * src , size_t n ) {
235  exec.fence();
236  omp_target_memcpy( dst , const_cast<void*> (src) , n, 0, 0, omp_get_default_device(), omp_get_initial_device());
237  }
238 };
239 
240 template<class ExecutionSpace>
241 struct DeepCopy<HostSpace,Kokkos::Experimental::OpenMPTargetSpace,ExecutionSpace> {
242  DeepCopy( void * dst , const void * src , size_t n ) {
243  omp_target_memcpy( dst , const_cast<void*> (src) , n, 0, 0, omp_get_initial_device(), omp_get_default_device());
244  }
245  DeepCopy( const ExecutionSpace& exec, void * dst , const void * src , size_t n ) {
246  exec.fence();
247  omp_target_memcpy( dst , const_cast<void*> (src) , n, 0, 0, omp_get_initial_device(), omp_get_default_device());
248  }
249 };
250 
251 
252 template<>
253 struct VerifyExecutionCanAccessMemorySpace< Kokkos::HostSpace , Kokkos::Experimental::OpenMPTargetSpace >
254 {
255  enum { value = false };
256  inline static void verify( void ) { }
257  inline static void verify( const void * ) { }
258 };
259 
260 } // namespace Impl
261 } // namespace Kokkos
262 
263 #endif
264 #endif /* #define KOKKOS_OPENMPTARGETSPACE_HPP */
265