Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 Christian R. Trott (crtrott@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 <impl/Kokkos_AnalyzePolicy.hpp>
56 #include <Kokkos_CudaSpace.hpp>
57 
58 #include <Kokkos_Parallel.hpp>
59 #include <Kokkos_TaskScheduler.hpp>
60 #include <Kokkos_Layout.hpp>
61 #include <Kokkos_ScratchSpace.hpp>
62 #include <Kokkos_MemoryTraits.hpp>
63 #include <impl/Kokkos_Tags.hpp>
64 
65 
66 /*--------------------------------------------------------------------------*/
67 
68 namespace Kokkos {
69 namespace Impl {
70 class CudaExec ;
71 class CudaInternal ;
72 } // namespace Impl
73 } // namespace Kokkos
74 
75 /*--------------------------------------------------------------------------*/
76 
77 namespace Kokkos {
78 
79 namespace Impl {
80  namespace Experimental {
81  enum class CudaLaunchMechanism:unsigned{Default=0,ConstantMemory=1,GlobalMemory=2,LocalMemory=4};
82 
83  constexpr inline CudaLaunchMechanism operator | (CudaLaunchMechanism p1, CudaLaunchMechanism p2) {
84  return static_cast<CudaLaunchMechanism>(static_cast<unsigned>(p1) | static_cast<unsigned>(p2));
85  }
86  constexpr inline CudaLaunchMechanism operator & (CudaLaunchMechanism p1, CudaLaunchMechanism p2) {
87  return static_cast<CudaLaunchMechanism>(static_cast<unsigned>(p1) & static_cast<unsigned>(p2));
88  }
89 
90  template<CudaLaunchMechanism l>
91  struct CudaDispatchProperties {
92  CudaLaunchMechanism launch_mechanism = l;
93  };
94  }
95 }
106 class Cuda {
107 public:
109 
110 
112  typedef Cuda execution_space ;
113 
114 #if defined( KOKKOS_ENABLE_CUDA_UVM )
115  typedef CudaUVMSpace memory_space ;
117 #else
118  typedef CudaSpace memory_space ;
120 #endif
121 
123  typedef Kokkos::Device<execution_space,memory_space> device_type;
124 
126  typedef memory_space::size_type size_type ;
127 
129  typedef LayoutLeft array_layout ;
130 
132  typedef ScratchMemorySpace< Cuda > scratch_memory_space ;
133 
135  //--------------------------------------------------
137 
138 
141  KOKKOS_INLINE_FUNCTION static int in_parallel() {
142 #if defined( __CUDA_ARCH__ )
143  return true;
144 #else
145  return false;
146 #endif
147  }
148 
160  static bool sleep();
161 
167  static bool wake();
168 
175  static void impl_static_fence();
176 
177  #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
178  static void fence();
179  #else
180  void fence() const;
181  #endif
182 
184  static int concurrency();
185 
187  static void print_configuration( std::ostream & , const bool detail = false );
188 
190  //--------------------------------------------------
192 
193  KOKKOS_INLINE_FUNCTION
194  ~Cuda() {}
195 
196  Cuda();
197 
198  Cuda( Cuda && ) = default ;
199  Cuda( const Cuda & ) = default ;
200  Cuda & operator = ( Cuda && ) = default ;
201  Cuda & operator = ( const Cuda & ) = default ;
202 
203  Cuda(cudaStream_t stream);
204 
205  //--------------------------------------------------------------------------
207 
208 
209  struct SelectDevice {
210  int cuda_device_id ;
211  SelectDevice() : cuda_device_id(0) {}
212  explicit SelectDevice( int id ) : cuda_device_id( id ) {}
213  };
214 
215 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
216  static void finalize();
218 
220  static int is_initialized();
221 
223  static void initialize( const SelectDevice = SelectDevice()
224  , const size_t num_instances = 1 );
225 #else
226  static void impl_finalize();
228 
230  static int impl_is_initialized();
231 
233  static void impl_initialize( const SelectDevice = SelectDevice()
234  , const size_t num_instances = 1 );
235 #endif
236 
240  static size_type device_arch();
241 
243  static size_type detect_device_count();
244 
248  static std::vector<unsigned> detect_device_arch();
249 
250  cudaStream_t cuda_stream() const;
251  int cuda_device() const;
252 
254  //--------------------------------------------------------------------------
255 
256  static const char* name();
257 
258  inline Impl::CudaInternal* impl_internal_space_instance() const { return m_space_instance; }
259 private:
260 
261  Impl::CudaInternal* m_space_instance;
262 };
263 
264 } // namespace Kokkos
265 
266 /*--------------------------------------------------------------------------*/
267 /*--------------------------------------------------------------------------*/
268 
269 namespace Kokkos {
270 namespace Impl {
271 
272 template<>
273 struct MemorySpaceAccess
274  < Kokkos::CudaSpace
275  , Kokkos::Cuda::scratch_memory_space
276  >
277 {
278  enum { assignable = false };
279  enum { accessible = true };
280  enum { deepcopy = false };
281 };
282 
283 #if defined( KOKKOS_ENABLE_CUDA_UVM )
284 
285 // If forcing use of UVM everywhere
286 // then must assume that CudaUVMSpace
287 // can be a stand-in for CudaSpace.
288 // This will fail when a strange host-side execution space
289 // that defines CudaUVMSpace as its preferredmemory space.
290 
291 template<>
292 struct MemorySpaceAccess
293  < Kokkos::CudaUVMSpace
294  , Kokkos::Cuda::scratch_memory_space
295  >
296 {
297  enum { assignable = false };
298  enum { accessible = true };
299  enum { deepcopy = false };
300 };
301 
302 #endif
303 
304 
305 template<>
306 struct VerifyExecutionCanAccessMemorySpace
307  < Kokkos::CudaSpace
308  , Kokkos::Cuda::scratch_memory_space
309  >
310 {
311  enum { value = true };
312  KOKKOS_INLINE_FUNCTION static void verify( void ) { }
313  KOKKOS_INLINE_FUNCTION static void verify( const void * ) { }
314 };
315 
316 template<>
317 struct VerifyExecutionCanAccessMemorySpace
318  < Kokkos::HostSpace
319  , Kokkos::Cuda::scratch_memory_space
320  >
321 {
322  enum { value = false };
323  inline static void verify( void ) { CudaSpace::access_error(); }
324  inline static void verify( const void * p ) { CudaSpace::access_error(p); }
325 };
326 
327 } // namespace Impl
328 } // namespace Kokkos
329 
330 /*--------------------------------------------------------------------------*/
331 /*--------------------------------------------------------------------------*/
332 
333 #include <Cuda/Kokkos_Cuda_KernelLaunch.hpp>
334 #include <Cuda/Kokkos_Cuda_Instance.hpp>
335 #include <Cuda/Kokkos_Cuda_View.hpp>
336 #include <Cuda/Kokkos_Cuda_Team.hpp>
337 #include <Cuda/Kokkos_Cuda_Parallel.hpp>
338 #include <Cuda/Kokkos_Cuda_Task.hpp>
339 #include <Cuda/Kokkos_Cuda_UniqueToken.hpp>
340 
341 #include <KokkosExp_MDRangePolicy.hpp>
342 //----------------------------------------------------------------------------
343 
344 #endif /* #if defined( KOKKOS_ENABLE_CUDA ) */
345 #endif /* #ifndef KOKKOS_CUDA_HPP */
346 
void print_configuration(std::ostream &, const bool detail=false)
Print &quot;Bill of Materials&quot;.
Declaration of various MemoryLayout options.
Declaration of parallel operators.
void finalize()
Finalize the spaces that were initialized via Kokkos::initialize.