Kokkos Core Kernels Package  Version of the Day
Kokkos_Cuda.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 H. Carter Edwards (hcedwar@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_CUDA_HPP
45 #define KOKKOS_CUDA_HPP
46 
47 #include <Kokkos_Macros.hpp>
48 #if defined( KOKKOS_ENABLE_CUDA )
49 
50 #include <Kokkos_Core_fwd.hpp>
51 
52 #include <iosfwd>
53 #include <vector>
54 
55 #include <Kokkos_CudaSpace.hpp>
56 
57 #include <Kokkos_Parallel.hpp>
58 #include <Kokkos_TaskScheduler.hpp>
59 #include <Kokkos_Layout.hpp>
60 #include <Kokkos_ScratchSpace.hpp>
61 #include <Kokkos_MemoryTraits.hpp>
62 #include <impl/Kokkos_Tags.hpp>
63 
64 
65 /*--------------------------------------------------------------------------*/
66 
67 namespace Kokkos {
68 namespace Impl {
69 class CudaExec ;
70 } // namespace Impl
71 } // namespace Kokkos
72 
73 /*--------------------------------------------------------------------------*/
74 
75 namespace Kokkos {
76 
87 class Cuda {
88 public:
90 
91 
93  typedef Cuda execution_space ;
94 
95 #if defined( KOKKOS_ENABLE_CUDA_UVM )
96  typedef CudaUVMSpace memory_space ;
98 #else
99  typedef CudaSpace memory_space ;
101 #endif
102 
105 
107  typedef memory_space::size_type size_type ;
108 
110  typedef LayoutLeft array_layout ;
111 
113  typedef ScratchMemorySpace< Cuda > scratch_memory_space ;
114 
116  //--------------------------------------------------
118 
119 
122  KOKKOS_INLINE_FUNCTION static int in_parallel() {
123 #if defined( __CUDA_ARCH__ )
124  return true;
125 #else
126  return false;
127 #endif
128  }
129 
141  static bool sleep();
142 
148  static bool wake();
149 
156  static void fence();
157 
159  static void finalize();
160 
162  static int is_initialized();
163 
165  static int concurrency();
166 
168  static void print_configuration( std::ostream & , const bool detail = false );
169 
171  //--------------------------------------------------
173 
174  ~Cuda() {}
175  Cuda();
176  explicit Cuda( const int instance_id );
177 
178  Cuda( Cuda && ) = default ;
179  Cuda( const Cuda & ) = default ;
180  Cuda & operator = ( Cuda && ) = default ;
181  Cuda & operator = ( const Cuda & ) = default ;
182 
183  //--------------------------------------------------------------------------
185 
186 
187  struct SelectDevice {
188  int cuda_device_id ;
189  SelectDevice() : cuda_device_id(0) {}
190  explicit SelectDevice( int id ) : cuda_device_id( id ) {}
191  };
192 
194  static void initialize( const SelectDevice = SelectDevice()
195  , const size_t num_instances = 1 );
196 
200  static size_type device_arch();
201 
203  static size_type detect_device_count();
204 
208  static std::vector<unsigned> detect_device_arch();
209 
210  cudaStream_t cuda_stream() const { return m_stream ; }
211  int cuda_device() const { return m_device ; }
212 
214  //--------------------------------------------------------------------------
215 
216  static const char* name();
217 
218 private:
219 
220  cudaStream_t m_stream ;
221  int m_device ;
222 };
223 
224 } // namespace Kokkos
225 
226 /*--------------------------------------------------------------------------*/
227 /*--------------------------------------------------------------------------*/
228 
229 namespace Kokkos {
230 namespace Impl {
231 
232 template<>
233 struct MemorySpaceAccess
234  < Kokkos::CudaSpace
235  , Kokkos::Cuda::scratch_memory_space
236  >
237 {
238  enum { assignable = false };
239  enum { accessible = true };
240  enum { deepcopy = false };
241 };
242 
243 #if defined( KOKKOS_ENABLE_CUDA_UVM )
244 
245 // If forcing use of UVM everywhere
246 // then must assume that CudaUVMSpace
247 // can be a stand-in for CudaSpace.
248 // This will fail when a strange host-side execution space
249 // that defines CudaUVMSpace as its preferredmemory space.
250 
251 template<>
252 struct MemorySpaceAccess
253  < Kokkos::CudaUVMSpace
254  , Kokkos::Cuda::scratch_memory_space
255  >
256 {
257  enum { assignable = false };
258  enum { accessible = true };
259  enum { deepcopy = false };
260 };
261 
262 #endif
263 
264 
265 template<>
266 struct VerifyExecutionCanAccessMemorySpace
267  < Kokkos::CudaSpace
268  , Kokkos::Cuda::scratch_memory_space
269  >
270 {
271  enum { value = true };
272  KOKKOS_INLINE_FUNCTION static void verify( void ) { }
273  KOKKOS_INLINE_FUNCTION static void verify( const void * ) { }
274 };
275 
276 template<>
277 struct VerifyExecutionCanAccessMemorySpace
278  < Kokkos::HostSpace
279  , Kokkos::Cuda::scratch_memory_space
280  >
281 {
282  enum { value = false };
283  inline static void verify( void ) { CudaSpace::access_error(); }
284  inline static void verify( const void * p ) { CudaSpace::access_error(p); }
285 };
286 
287 } // namespace Impl
288 } // namespace Kokkos
289 
290 /*--------------------------------------------------------------------------*/
291 /*--------------------------------------------------------------------------*/
292 
293 #include <Cuda/Kokkos_CudaExec.hpp>
294 #include <Cuda/Kokkos_Cuda_View.hpp>
295 #include <Cuda/Kokkos_Cuda_Team.hpp>
296 #include <Cuda/Kokkos_Cuda_Parallel.hpp>
297 #include <Cuda/Kokkos_Cuda_Task.hpp>
298 
299 #include <KokkosExp_MDRangePolicy.hpp>
300 //----------------------------------------------------------------------------
301 
302 #endif /* #if defined( KOKKOS_ENABLE_CUDA ) */
303 #endif /* #ifndef KOKKOS_CUDA_HPP */
304 
void print_configuration(std::ostream &, const bool detail=false)
Print "Bill of Materials".
Memory space for main process and CPU execution spaces.
Declaration of various MemoryLayout options.
Declaration of parallel operators.
void finalize()
Finalize the spaces that were initialized via Kokkos::initialize.