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  using execution_space = ROCm;
106  using memory_space = ROCmSpace;
107  using device_type = Kokkos::Device<execution_space, memory_space>;
108 
109  using array_layout = LayoutLeft;
110  using size_type = HostSpace::size_type;
111 
112  using scratch_memory_space = ScratchMemorySpace<ROCm>;
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  void fence() const;
146 
148  static void print_configuration(std::ostream&, const bool detail = false);
149 
151  static void finalize();
152 
156  struct SelectDevice {
157  int rocm_device_id;
158  SelectDevice() : rocm_device_id(1) {}
159  explicit SelectDevice(int id) : rocm_device_id(id + 1) {}
160  };
161 
162  int rocm_device() const { return m_device; }
163  bool isAPU();
164  bool isAPU(int device);
165 
166  static void initialize(const SelectDevice = SelectDevice());
167 
168  static int is_initialized();
169 
170  // static size_type device_arch();
171 
172  // static size_type detect_device_count();
173 
174  static int concurrency();
175  static const char* name();
176 
177  private:
178  int m_device;
179 };
180 } // namespace Experimental
181 } // namespace Kokkos
182 
183 namespace Kokkos {
184 namespace Impl {
185 
186 template <>
187 struct MemorySpaceAccess<Kokkos::Experimental::ROCmSpace,
188  Kokkos::Experimental::ROCm::scratch_memory_space> {
189  enum { assignable = false };
190  enum { accessible = true };
191  enum { deepcopy = false };
192 };
193 
194 template <>
195 struct VerifyExecutionCanAccessMemorySpace<
196  Kokkos::Experimental::ROCm::memory_space,
197  Kokkos::Experimental::ROCm::scratch_memory_space> {
198  enum { value = true };
199  KOKKOS_INLINE_FUNCTION static void verify(void) {}
200  KOKKOS_INLINE_FUNCTION static void verify(const void*) {}
201 };
202 
203 template <>
204 struct VerifyExecutionCanAccessMemorySpace<
205  Kokkos::HostSpace, Kokkos::Experimental::ROCm::scratch_memory_space> {
206  enum { value = false };
207  inline static void verify(void) {
208  Kokkos::Experimental::ROCmSpace::access_error();
209  }
210  inline static void verify(const void* p) {
211  Kokkos::Experimental::ROCmSpace::access_error(p);
212  }
213 };
214 
215 } // namespace Impl
216 } // namespace Kokkos
217 
218 #define threadIdx_x (hc_get_workitem_id(0))
219 #define threadIdx_y (hc_get_workitem_id(1))
220 #define threadIdx_z (hc_get_workitem_id(2))
221 
222 #define blockIdx_x (hc_get_group_id(0))
223 #define blockIdx_y (hc_get_group_id(1))
224 #define blockIdx_z (hc_get_group_id(2))
225 
226 #define blockDim_x (hc_get_group_size(0))
227 #define blockDim_y (hc_get_group_size(1))
228 #define blockDim_z (hc_get_group_size(2))
229 
230 #define gridDim_x (hc_get_num_groups(0))
231 #define gridDim_y (hc_get_num_groups(1))
232 #define gridDim_z (hc_get_num_groups(2))
233 
234 #include <ROCm/Kokkos_ROCm_Parallel.hpp>
235 #include <ROCm/Kokkos_ROCm_Task.hpp>
236 
237 #endif
238 #endif
Declaration of various MemoryLayout options.
Declaration of parallel operators.