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_Core_fwd.hpp>
48 
49 // If CUDA execution space is enabled then use this header file.
50 
51 #if defined( KOKKOS_HAVE_CUDA )
52 
53 #include <iosfwd>
54 #include <vector>
55 
56 #include <Kokkos_CudaSpace.hpp>
57 
58 #include <Kokkos_Parallel.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 namespace Kokkos {
67 namespace Impl {
68 class CudaExec ;
69 } // namespace Impl
70 } // namespace Kokkos
71 
72 /*--------------------------------------------------------------------------*/
73 
74 namespace Kokkos {
75 
86 class Cuda {
87 public:
89 
90 
92  typedef Cuda execution_space ;
93 
94 #if defined( KOKKOS_USE_CUDA_UVM )
95  typedef CudaUVMSpace memory_space ;
97 #else
98  typedef CudaSpace memory_space ;
100 #endif
101 
104 
106  typedef memory_space::size_type size_type ;
107 
109  typedef LayoutLeft array_layout ;
110 
112  typedef ScratchMemorySpace< Cuda > scratch_memory_space ;
113 
115  //--------------------------------------------------
117 
118 
121  KOKKOS_INLINE_FUNCTION static int in_parallel() {
122 #if defined( __CUDA_ARCH__ )
123  return true;
124 #else
125  return false;
126 #endif
127  }
128 
140  static bool sleep();
141 
147  static bool wake();
148 
155  static void fence();
156 
158  static void finalize();
159 
161  static int is_initialized();
162 
164  static void print_configuration( std::ostream & , const bool detail = false );
165 
167  //--------------------------------------------------
169 
170  ~Cuda() {}
171  Cuda();
172  explicit Cuda( const int instance_id );
173 
174  Cuda( const Cuda & ) = default ;
175  Cuda( Cuda && ) = default ;
176  Cuda & operator = ( const Cuda & ) = default ;
177  Cuda & operator = ( Cuda && ) = default ;
178 
179  //--------------------------------------------------------------------------
181 
182 
183  struct SelectDevice {
184  int cuda_device_id ;
185  SelectDevice() : cuda_device_id(0) {}
186  explicit SelectDevice( int id ) : cuda_device_id( id ) {}
187  };
188 
190  static void initialize( const SelectDevice = SelectDevice()
191  , const size_t num_instances = 1 );
192 
196  static size_type device_arch();
197 
199  static size_type detect_device_count();
200 
204  static std::vector<unsigned> detect_device_arch();
205 
206  cudaStream_t cuda_stream() const { return m_stream ; }
207  int cuda_device() const { return m_device ; }
208 
210  //--------------------------------------------------------------------------
211 
212 private:
213 
214  cudaStream_t m_stream ;
215  int m_device ;
216 };
217 
218 } // namespace Kokkos
219 
220 /*--------------------------------------------------------------------------*/
221 /*--------------------------------------------------------------------------*/
222 
223 namespace Kokkos {
224 namespace Impl {
225 
226 template<>
227 struct VerifyExecutionCanAccessMemorySpace
228  < Kokkos::CudaSpace
229  , Kokkos::Cuda::scratch_memory_space
230  >
231 {
232  enum { value = true };
233  KOKKOS_INLINE_FUNCTION static void verify( void ) { }
234  KOKKOS_INLINE_FUNCTION static void verify( const void * ) { }
235 };
236 
237 template<>
238 struct VerifyExecutionCanAccessMemorySpace
239  < Kokkos::HostSpace
240  , Kokkos::Cuda::scratch_memory_space
241  >
242 {
243  enum { value = false };
244  inline static void verify( void ) { CudaSpace::access_error(); }
245  inline static void verify( const void * p ) { CudaSpace::access_error(p); }
246 };
247 
248 } // namespace Impl
249 } // namespace Kokkos
250 
251 /*--------------------------------------------------------------------------*/
252 /*--------------------------------------------------------------------------*/
253 
254 #include <Cuda/Kokkos_CudaExec.hpp>
255 #include <Cuda/Kokkos_Cuda_View.hpp>
256 
257 #include <KokkosExp_View.hpp>
258 #include <Cuda/KokkosExp_Cuda_View.hpp>
259 
260 #include <Cuda/Kokkos_Cuda_Parallel.hpp>
261 
262 //----------------------------------------------------------------------------
263 
264 #endif /* #if defined( KOKKOS_HAVE_CUDA ) */
265 #endif /* #ifndef KOKKOS_CUDA_HPP */
266 
267 
268 
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.