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 <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 
104  typedef Kokkos::Device<execution_space,memory_space> device_type;
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 int concurrency();
160 
162  static void print_configuration( std::ostream & , const bool detail = false );
163 
165  //--------------------------------------------------
167 
168  ~Cuda() {}
169  Cuda();
170  explicit Cuda( const int instance_id );
171 
172  Cuda( Cuda && ) = default ;
173  Cuda( const Cuda & ) = default ;
174  Cuda & operator = ( Cuda && ) = default ;
175  Cuda & operator = ( const Cuda & ) = default ;
176 
177  //--------------------------------------------------------------------------
179 
180 
181  struct SelectDevice {
182  int cuda_device_id ;
183  SelectDevice() : cuda_device_id(0) {}
184  explicit SelectDevice( int id ) : cuda_device_id( id ) {}
185  };
186 
187 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
188  static void finalize();
190 
192  static int is_initialized();
193 
195  static void initialize( const SelectDevice = SelectDevice()
196  , const size_t num_instances = 1 );
197 #else
198  static void impl_finalize();
200 
202  static int impl_is_initialized();
203 
205  static void impl_initialize( const SelectDevice = SelectDevice()
206  , const size_t num_instances = 1 );
207 #endif
208 
212  static size_type device_arch();
213 
215  static size_type detect_device_count();
216 
220  static std::vector<unsigned> detect_device_arch();
221 
222  cudaStream_t cuda_stream() const { return m_stream ; }
223  int cuda_device() const { return m_device ; }
224 
226  //--------------------------------------------------------------------------
227 
228  static const char* name();
229 
230 private:
231 
232  int m_device ;
233  cudaStream_t m_stream ;
234 };
235 
236 } // namespace Kokkos
237 
238 /*--------------------------------------------------------------------------*/
239 /*--------------------------------------------------------------------------*/
240 
241 namespace Kokkos {
242 namespace Impl {
243 
244 template<>
245 struct MemorySpaceAccess
246  < Kokkos::CudaSpace
247  , Kokkos::Cuda::scratch_memory_space
248  >
249 {
250  enum { assignable = false };
251  enum { accessible = true };
252  enum { deepcopy = false };
253 };
254 
255 #if defined( KOKKOS_ENABLE_CUDA_UVM )
256 
257 // If forcing use of UVM everywhere
258 // then must assume that CudaUVMSpace
259 // can be a stand-in for CudaSpace.
260 // This will fail when a strange host-side execution space
261 // that defines CudaUVMSpace as its preferredmemory space.
262 
263 template<>
264 struct MemorySpaceAccess
265  < Kokkos::CudaUVMSpace
266  , Kokkos::Cuda::scratch_memory_space
267  >
268 {
269  enum { assignable = false };
270  enum { accessible = true };
271  enum { deepcopy = false };
272 };
273 
274 #endif
275 
276 
277 template<>
278 struct VerifyExecutionCanAccessMemorySpace
279  < Kokkos::CudaSpace
280  , Kokkos::Cuda::scratch_memory_space
281  >
282 {
283  enum { value = true };
284  KOKKOS_INLINE_FUNCTION static void verify( void ) { }
285  KOKKOS_INLINE_FUNCTION static void verify( const void * ) { }
286 };
287 
288 template<>
289 struct VerifyExecutionCanAccessMemorySpace
290  < Kokkos::HostSpace
291  , Kokkos::Cuda::scratch_memory_space
292  >
293 {
294  enum { value = false };
295  inline static void verify( void ) { CudaSpace::access_error(); }
296  inline static void verify( const void * p ) { CudaSpace::access_error(p); }
297 };
298 
299 } // namespace Impl
300 } // namespace Kokkos
301 
302 /*--------------------------------------------------------------------------*/
303 /*--------------------------------------------------------------------------*/
304 
305 #include <Cuda/Kokkos_CudaExec.hpp>
306 #include <Cuda/Kokkos_Cuda_View.hpp>
307 #include <Cuda/Kokkos_Cuda_Team.hpp>
308 #include <Cuda/Kokkos_Cuda_Parallel.hpp>
309 #include <Cuda/Kokkos_Cuda_Task.hpp>
310 #include <Cuda/Kokkos_Cuda_UniqueToken.hpp>
311 
312 #include <KokkosExp_MDRangePolicy.hpp>
313 //----------------------------------------------------------------------------
314 
315 #endif /* #if defined( KOKKOS_ENABLE_CUDA ) */
316 #endif /* #ifndef KOKKOS_CUDA_HPP */
317 
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.