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. 3.0
6 // Copyright (2020) National Technology & Engineering
7 // Solutions of Sandia, LLC (NTESS).
8 //
9 // Under the terms of Contract DE-NA0003525 with NTESS,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40 //
41 // ************************************************************************
42 //@HEADER
43 */
44 
45 #ifndef KOKKOS_CUDA_HPP
46 #define KOKKOS_CUDA_HPP
47 
48 #include <Kokkos_Macros.hpp>
49 #if defined(KOKKOS_ENABLE_CUDA)
50 
51 #include <Kokkos_Core_fwd.hpp>
52 
53 #include <iosfwd>
54 #include <vector>
55 
56 #include <impl/Kokkos_AnalyzePolicy.hpp>
57 #include <Kokkos_CudaSpace.hpp>
58 
59 #include <Kokkos_Parallel.hpp>
60 #include <Kokkos_TaskScheduler.hpp>
61 #include <Kokkos_Layout.hpp>
62 #include <Kokkos_ScratchSpace.hpp>
63 #include <Kokkos_MemoryTraits.hpp>
64 #include <impl/Kokkos_Tags.hpp>
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 {
82  Default = 0,
83  ConstantMemory = 1,
84  GlobalMemory = 2,
85  LocalMemory = 4
86 };
87 
88 constexpr inline CudaLaunchMechanism operator|(CudaLaunchMechanism p1,
89  CudaLaunchMechanism p2) {
90  return static_cast<CudaLaunchMechanism>(static_cast<unsigned>(p1) |
91  static_cast<unsigned>(p2));
92 }
93 constexpr inline CudaLaunchMechanism operator&(CudaLaunchMechanism p1,
94  CudaLaunchMechanism p2) {
95  return static_cast<CudaLaunchMechanism>(static_cast<unsigned>(p1) &
96  static_cast<unsigned>(p2));
97 }
98 
99 template <CudaLaunchMechanism l>
100 struct CudaDispatchProperties {
101  CudaLaunchMechanism launch_mechanism = l;
102 };
103 } // namespace Experimental
104 } // namespace Impl
115 class Cuda {
116  public:
118 
119 
121  typedef Cuda execution_space;
122 
123 #if defined(KOKKOS_ENABLE_CUDA_UVM)
124  typedef CudaUVMSpace memory_space;
126 #else
127  typedef CudaSpace memory_space;
129 #endif
130 
132  typedef Kokkos::Device<execution_space, memory_space> device_type;
133 
135  typedef memory_space::size_type size_type;
136 
138  typedef LayoutLeft array_layout;
139 
141  typedef ScratchMemorySpace<Cuda> scratch_memory_space;
142 
144  //--------------------------------------------------
146 
147 
150  KOKKOS_INLINE_FUNCTION static int in_parallel() {
151 #if defined(__CUDA_ARCH__)
152  return true;
153 #else
154  return false;
155 #endif
156  }
157 
169  static bool sleep();
170 
176  static bool wake();
177 
184  static void impl_static_fence();
185 
186 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
187  static void fence();
188 #else
189  void fence() const;
190 #endif
191 
193  static int concurrency();
194 
196  static void print_configuration(std::ostream&, const bool detail = false);
197 
199  //--------------------------------------------------
201 
202  ~Cuda() = default;
203 
204  Cuda();
205 
206  Cuda(Cuda&&) = default;
207  Cuda(const Cuda&) = default;
208  Cuda& operator=(Cuda&&) = default;
209  Cuda& operator=(const Cuda&) = default;
210 
211  Cuda(cudaStream_t stream);
212 
213  //--------------------------------------------------------------------------
215 
216 
217  struct SelectDevice {
218  int cuda_device_id;
219  SelectDevice() : cuda_device_id(0) {}
220  explicit SelectDevice(int id) : cuda_device_id(id) {}
221  };
222 
223 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
224  static void finalize();
226 
228  static int is_initialized();
229 
231  static void initialize(const SelectDevice = SelectDevice(),
232  const size_t num_instances = 1);
233 #else
234  static void impl_finalize();
236 
238  static int impl_is_initialized();
239 
241  static void impl_initialize(const SelectDevice = SelectDevice(),
242  const size_t num_instances = 1);
243 #endif
244 
248  static size_type device_arch();
249 
251  static size_type detect_device_count();
252 
256  static std::vector<unsigned> detect_device_arch();
257 
258  cudaStream_t cuda_stream() const;
259  int cuda_device() const;
260  const cudaDeviceProp& cuda_device_prop() const;
261 
263  //--------------------------------------------------------------------------
264 
265  static const char* name();
266 
267  inline Impl::CudaInternal* impl_internal_space_instance() const {
268  return m_space_instance;
269  }
270  uint32_t impl_instance_id() const noexcept { return 0; }
271 
272  private:
273  Impl::CudaInternal* m_space_instance;
274 };
275 
276 namespace Profiling {
277 namespace Experimental {
278 template <>
279 struct DeviceTypeTraits<Cuda> {
281  static constexpr DeviceType id = DeviceType::Cuda;
282 };
283 } // namespace Experimental
284 } // namespace Profiling
285 } // namespace Kokkos
286 
287 /*--------------------------------------------------------------------------*/
288 /*--------------------------------------------------------------------------*/
289 
290 namespace Kokkos {
291 namespace Impl {
292 
293 template <>
294 struct MemorySpaceAccess<Kokkos::CudaSpace,
295  Kokkos::Cuda::scratch_memory_space> {
296  enum { assignable = false };
297  enum { accessible = true };
298  enum { deepcopy = false };
299 };
300 
301 #if defined(KOKKOS_ENABLE_CUDA_UVM)
302 
303 // If forcing use of UVM everywhere
304 // then must assume that CudaUVMSpace
305 // can be a stand-in for CudaSpace.
306 // This will fail when a strange host-side execution space
307 // that defines CudaUVMSpace as its preferredmemory space.
308 
309 template <>
310 struct MemorySpaceAccess<Kokkos::CudaUVMSpace,
311  Kokkos::Cuda::scratch_memory_space> {
312  enum { assignable = false };
313  enum { accessible = true };
314  enum { deepcopy = false };
315 };
316 
317 #endif
318 
319 template <>
320 struct VerifyExecutionCanAccessMemorySpace<Kokkos::CudaSpace,
321  Kokkos::Cuda::scratch_memory_space> {
322  enum { value = true };
323  KOKKOS_INLINE_FUNCTION static void verify(void) {}
324  KOKKOS_INLINE_FUNCTION static void verify(const void*) {}
325 };
326 
327 template <>
328 struct VerifyExecutionCanAccessMemorySpace<Kokkos::HostSpace,
329  Kokkos::Cuda::scratch_memory_space> {
330  enum { value = false };
331  inline static void verify(void) { CudaSpace::access_error(); }
332  inline static void verify(const void* p) { CudaSpace::access_error(p); }
333 };
334 
335 } // namespace Impl
336 } // namespace Kokkos
337 
338 /*--------------------------------------------------------------------------*/
339 /*--------------------------------------------------------------------------*/
340 
341 #include <Cuda/Kokkos_Cuda_KernelLaunch.hpp>
342 #include <Cuda/Kokkos_Cuda_Instance.hpp>
343 #include <Cuda/Kokkos_Cuda_View.hpp>
344 #include <Cuda/Kokkos_Cuda_Team.hpp>
345 #include <Cuda/Kokkos_Cuda_Parallel.hpp>
346 #include <Cuda/Kokkos_Cuda_Task.hpp>
347 #include <Cuda/Kokkos_Cuda_UniqueToken.hpp>
348 
349 #include <KokkosExp_MDRangePolicy.hpp>
350 //----------------------------------------------------------------------------
351 
352 #endif /* #if defined( KOKKOS_ENABLE_CUDA ) */
353 #endif /* #ifndef KOKKOS_CUDA_HPP */
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.