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_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 #include <KokkosExp_MDRangePolicy.hpp>
66 
67 /*--------------------------------------------------------------------------*/
68 
69 namespace Kokkos {
70 namespace Impl {
71 class CudaExec ;
72 } // namespace Impl
73 } // namespace Kokkos
74 
75 /*--------------------------------------------------------------------------*/
76 
77 namespace Kokkos {
78 
89 class Cuda {
90 public:
92 
93 
95  typedef Cuda execution_space ;
96 
97 #if defined( KOKKOS_USE_CUDA_UVM )
98  typedef CudaUVMSpace memory_space ;
100 #else
101  typedef CudaSpace memory_space ;
103 #endif
104 
107 
109  typedef memory_space::size_type size_type ;
110 
112  typedef LayoutLeft array_layout ;
113 
115  typedef ScratchMemorySpace< Cuda > scratch_memory_space ;
116 
118  //--------------------------------------------------
120 
121 
124  KOKKOS_INLINE_FUNCTION static int in_parallel() {
125 #if defined( __CUDA_ARCH__ )
126  return true;
127 #else
128  return false;
129 #endif
130  }
131 
143  static bool sleep();
144 
150  static bool wake();
151 
158  static void fence();
159 
161  static void finalize();
162 
164  static int is_initialized();
165 
167  static int concurrency();
168 
170  static void print_configuration( std::ostream & , const bool detail = false );
171 
173  //--------------------------------------------------
175 
176  ~Cuda() {}
177  Cuda();
178  explicit Cuda( const int instance_id );
179 
180  Cuda( Cuda && ) = default ;
181  Cuda( const Cuda & ) = default ;
182  Cuda & operator = ( Cuda && ) = default ;
183  Cuda & operator = ( const Cuda & ) = default ;
184 
185  //--------------------------------------------------------------------------
187 
188 
189  struct SelectDevice {
190  int cuda_device_id ;
191  SelectDevice() : cuda_device_id(0) {}
192  explicit SelectDevice( int id ) : cuda_device_id( id ) {}
193  };
194 
196  static void initialize( const SelectDevice = SelectDevice()
197  , const size_t num_instances = 1 );
198 
202  static size_type device_arch();
203 
205  static size_type detect_device_count();
206 
210  static std::vector<unsigned> detect_device_arch();
211 
212  cudaStream_t cuda_stream() const { return m_stream ; }
213  int cuda_device() const { return m_device ; }
214 
216  //--------------------------------------------------------------------------
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 VerifyExecutionCanAccessMemorySpace
234  < Kokkos::CudaSpace
235  , Kokkos::Cuda::scratch_memory_space
236  >
237 {
238  enum { value = true };
239  KOKKOS_INLINE_FUNCTION static void verify( void ) { }
240  KOKKOS_INLINE_FUNCTION static void verify( const void * ) { }
241 };
242 
243 template<>
244 struct VerifyExecutionCanAccessMemorySpace
245  < Kokkos::HostSpace
246  , Kokkos::Cuda::scratch_memory_space
247  >
248 {
249  enum { value = false };
250  inline static void verify( void ) { CudaSpace::access_error(); }
251  inline static void verify( const void * p ) { CudaSpace::access_error(p); }
252 };
253 
254 } // namespace Impl
255 } // namespace Kokkos
256 
257 /*--------------------------------------------------------------------------*/
258 /*--------------------------------------------------------------------------*/
259 
260 #include <Cuda/Kokkos_CudaExec.hpp>
261 #include <Cuda/Kokkos_Cuda_View.hpp>
262 #include <Cuda/Kokkos_Cuda_Parallel.hpp>
263 #include <Cuda/Kokkos_Cuda_Task.hpp>
264 
265 //----------------------------------------------------------------------------
266 
267 #endif /* #if defined( KOKKOS_HAVE_CUDA ) */
268 #endif /* #ifndef KOKKOS_CUDA_HPP */
269 
270 
271 
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.