Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_ROCm.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_ROCM_HPP
46 #define KOKKOS_ROCM_HPP
47 
48 #include <Kokkos_Core_fwd.hpp>
49 
50 #if defined(KOKKOS_ENABLE_ROCM)
51 
52 class dim3 {
53  public:
54  int x, y, z;
55  dim3(int _x, int _y, int _z) : x(_x), y(_y), z(_z){};
56 };
57 
58 #include <ROCm/hc_math_std.hpp>
59 //----------------------------------------------------------------------------
60 //----------------------------------------------------------------------------
61 
62 #include <cstddef>
63 #include <iosfwd>
64 #include <Kokkos_HostSpace.hpp>
65 #include <Kokkos_ROCmSpace.hpp>
66 #include <ROCm/Kokkos_ROCm_Exec.hpp>
67 #include <Kokkos_ScratchSpace.hpp>
68 #include <Kokkos_Parallel.hpp>
69 #include <Kokkos_Layout.hpp>
70 #include <impl/Kokkos_Tags.hpp>
71 
72 /*--------------------------------------------------------------------------*/
73 
74 #include <hc.hpp>
75 #include <hc_am.hpp>
76 #include <amp_math.h>
77 
78 #if defined(__HCC_ACCELERATOR__)
79 
80 using namespace ::Concurrency::precise_math;
81 
82 #endif
83 
84 /*--------------------------------------------------------------------------*/
85 
86 namespace Kokkos {
87 namespace Impl {
88 class ROCmExec;
89 } // namespace Impl
90 } // namespace Kokkos
91 
92 /*--------------------------------------------------------------------------*/
93 
94 namespace Kokkos {
95 namespace Experimental {
98 class ROCm {
99  public:
100  //------------------------------------
102 
103 
105  typedef ROCm execution_space;
106  typedef ROCmSpace memory_space;
107  typedef Kokkos::Device<execution_space, memory_space> device_type;
108 
109  typedef LayoutLeft array_layout;
110  typedef HostSpace::size_type size_type;
111 
112  typedef ScratchMemorySpace<ROCm> scratch_memory_space;
113 
114  ~ROCm() {}
115  ROCm();
116  // explicit ROCm( const int instance_id );
117 
118  ROCm(ROCm&&) = default;
119  ROCm(const ROCm&) = default;
120  ROCm& operator=(ROCm&&) = default;
121  ROCm& operator=(const ROCm&) = default;
122 
124  //------------------------------------
126 
127 
128  KOKKOS_INLINE_FUNCTION static int in_parallel() {
129 #if defined(__HCC_ACCELERATOR__)
130  return true;
131 #else
132  return false;
133 #endif
134  }
135 
137  static bool sleep();
138 
140  static bool wake();
141 
143  static void impl_static_fence();
144 
145 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
146  static void fence();
147 #else
148  void fence() const;
149 #endif
150 
152  static void print_configuration(std::ostream&, const bool detail = false);
153 
155  static void finalize();
156 
160  struct SelectDevice {
161  int rocm_device_id;
162  SelectDevice() : rocm_device_id(1) {}
163  explicit SelectDevice(int id) : rocm_device_id(id + 1) {}
164  };
165 
166  int rocm_device() const { return m_device; }
167  bool isAPU();
168  bool isAPU(int device);
169 
170  static void initialize(const SelectDevice = SelectDevice());
171 
172  static int is_initialized();
173 
174  // static size_type device_arch();
175 
176  // static size_type detect_device_count();
177 
178  static int concurrency();
179  static const char* name();
180 
181  private:
182  int m_device;
183 };
184 } // namespace Experimental
185 } // namespace Kokkos
186 
187 namespace Kokkos {
188 namespace Impl {
189 
190 template <>
191 struct MemorySpaceAccess<Kokkos::Experimental::ROCmSpace,
192  Kokkos::Experimental::ROCm::scratch_memory_space> {
193  enum { assignable = false };
194  enum { accessible = true };
195  enum { deepcopy = false };
196 };
197 
198 template <>
199 struct VerifyExecutionCanAccessMemorySpace<
200  Kokkos::Experimental::ROCm::memory_space,
201  Kokkos::Experimental::ROCm::scratch_memory_space> {
202  enum { value = true };
203  KOKKOS_INLINE_FUNCTION static void verify(void) {}
204  KOKKOS_INLINE_FUNCTION static void verify(const void*) {}
205 };
206 
207 template <>
208 struct VerifyExecutionCanAccessMemorySpace<
209  Kokkos::HostSpace, Kokkos::Experimental::ROCm::scratch_memory_space> {
210  enum { value = false };
211  inline static void verify(void) {
212  Kokkos::Experimental::ROCmSpace::access_error();
213  }
214  inline static void verify(const void* p) {
215  Kokkos::Experimental::ROCmSpace::access_error(p);
216  }
217 };
218 
219 } // namespace Impl
220 } // namespace Kokkos
221 
222 #define threadIdx_x (hc_get_workitem_id(0))
223 #define threadIdx_y (hc_get_workitem_id(1))
224 #define threadIdx_z (hc_get_workitem_id(2))
225 
226 #define blockIdx_x (hc_get_group_id(0))
227 #define blockIdx_y (hc_get_group_id(1))
228 #define blockIdx_z (hc_get_group_id(2))
229 
230 #define blockDim_x (hc_get_group_size(0))
231 #define blockDim_y (hc_get_group_size(1))
232 #define blockDim_z (hc_get_group_size(2))
233 
234 #define gridDim_x (hc_get_num_groups(0))
235 #define gridDim_y (hc_get_num_groups(1))
236 #define gridDim_z (hc_get_num_groups(2))
237 
238 #include <ROCm/Kokkos_ROCm_Parallel.hpp>
239 #include <ROCm/Kokkos_ROCm_Task.hpp>
240 
241 #endif
242 #endif
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.