alpaka
Abstraction Library for Parallel Kernel Acceleration
DeviceGlobalUniformCudaHipBuiltIn.hpp
Go to the documentation of this file.
1 /* Copyright 2024 Aurora Perego
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
11 
12 #if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) || defined(ALPAKA_ACC_GPU_HIP_ENABLED)
13 
14 # if defined(ALPAKA_ACC_GPU_CUDA_ENABLED)
15 # include "alpaka/core/ApiCudaRt.hpp"
16 # endif
17 
18 # if defined(ALPAKA_ACC_GPU_HIP_ENABLED)
19 # include "alpaka/core/ApiHipRt.hpp"
20 # endif
21 
22 namespace alpaka
23 {
24 
25  namespace detail
26  {
27  template<typename T>
29  {
30  // CUDA implementation
32  };
33 
34  template<typename T>
36  {
37  // HIP/ROCm implementation
39  };
40  } // namespace detail
41 
42  // from device to host
43  template<
44  concepts::Tag TTag,
45  typename TApi,
46  bool TBlocking,
47  typename TViewDst,
48  typename TTypeSrc,
49  typename std::enable_if_t<
50 # if defined(ALPAKA_ACC_GPU_CUDA_ENABLED)
51  (std::is_same_v<TTag, TagGpuCudaRt> && std::is_same_v<TApi, ApiCudaRt>)
52 # else
53  (std::is_same_v<TTag, TagGpuHipRt> && std::is_same_v<TApi, ApiHipRt>)
54 # endif
55  ,
56  int>
57  = 0>
60  TViewDst& viewDst,
62  {
63  using Type = std::remove_const_t<std::remove_all_extents_t<TTypeSrc>>;
64  using TypeExt = std::remove_const_t<TTypeSrc>;
65  auto extent = getExtents(viewDst);
66  TypeExt* pMemAcc(nullptr);
68  TApi::getSymbolAddress(reinterpret_cast<void**>(&pMemAcc), *(const_cast<TypeExt*>(&viewSrc))));
69 
70  auto view = alpaka::ViewPlainPtr<
72  Type,
73  alpaka::Dim<decltype(extent)>,
74  alpaka::Idx<decltype(extent)>>(reinterpret_cast<Type*>(pMemAcc), alpaka::getDev(queue), extent);
75  enqueue(queue, createTaskMemcpy(std::forward<TViewDst>(viewDst), view, extent));
76  }
77 
78  // from host to device
79  template<
80  concepts::Tag TTag,
81  typename TApi,
82  bool TBlocking,
83  typename TTypeDst,
84  typename TViewSrc,
85  typename std::enable_if_t<
86 # if defined(ALPAKA_ACC_GPU_CUDA_ENABLED)
87  (std::is_same_v<TTag, TagGpuCudaRt> && std::is_same_v<TApi, ApiCudaRt>)
88 # else
89  (std::is_same_v<TTag, TagGpuHipRt> && std::is_same_v<TApi, ApiHipRt>)
90 # endif
91  ,
92  int>
93  = 0>
97  TViewSrc const& viewSrc)
98  {
99  using Type = std::remove_const_t<std::remove_all_extents_t<TTypeDst>>;
100  using TypeExt = std::remove_const_t<TTypeDst>;
101  auto extent = getExtents(viewSrc);
102  Type* pMemAcc(nullptr);
104  TApi::getSymbolAddress(reinterpret_cast<void**>(&pMemAcc), *(const_cast<TypeExt*>(&viewDst))));
105 
106  auto view = alpaka::ViewPlainPtr<
108  Type,
109  alpaka::Dim<decltype(extent)>,
110  alpaka::Idx<decltype(extent)>>(reinterpret_cast<Type*>(pMemAcc), alpaka::getDev(queue), extent);
111  enqueue(queue, createTaskMemcpy(std::forward<decltype(view)>(view), viewSrc, extent));
112  }
113 
114  // from device to host
115  template<
116  concepts::Tag TTag,
117  typename TApi,
118  bool TBlocking,
119  typename TViewDst,
120  typename TTypeSrc,
121  typename TExtent,
122  typename std::enable_if_t<
123 # if defined(ALPAKA_ACC_GPU_CUDA_ENABLED)
124  (std::is_same_v<TTag, TagGpuCudaRt> && std::is_same_v<TApi, ApiCudaRt>)
125 # else
126  (std::is_same_v<TTag, TagGpuHipRt> && std::is_same_v<TApi, ApiHipRt>)
127 # endif
128  ,
129  int>
130  = 0>
133  TViewDst& viewDst,
135  TExtent extent)
136  {
137  using Type = std::remove_const_t<std::remove_all_extents_t<TTypeSrc>>;
138  using TypeExt = std::remove_const_t<TTypeSrc>;
139  Type* pMemAcc(nullptr);
141  TApi::getSymbolAddress(reinterpret_cast<void**>(&pMemAcc), *(const_cast<TypeExt*>(&viewSrc))));
142 
144  reinterpret_cast<Type*>(pMemAcc),
145  alpaka::getDev(queue),
146  extent);
147  enqueue(queue, createTaskMemcpy(std::forward<TViewDst>(viewDst), view, extent));
148  }
149 
150  // from host to device
151  template<
152  concepts::Tag TTag,
153  typename TApi,
154  bool TBlocking,
155  typename TTypeDst,
156  typename TViewSrc,
157  typename TExtent,
158  typename std::enable_if_t<
159 # if defined(ALPAKA_ACC_GPU_CUDA_ENABLED)
160  (std::is_same_v<TTag, TagGpuCudaRt> && std::is_same_v<TApi, ApiCudaRt>)
161 # else
162  (std::is_same_v<TTag, TagGpuHipRt> && std::is_same_v<TApi, ApiHipRt>)
163 # endif
164  ,
165  int>
166  = 0>
170  TViewSrc const& viewSrc,
171  TExtent extent)
172  {
173  using Type = std::remove_const_t<std::remove_all_extents_t<TTypeDst>>;
174  using TypeExt = std::remove_const_t<TTypeDst>;
175  Type* pMemAcc(nullptr);
177  TApi::getSymbolAddress(reinterpret_cast<void**>(&pMemAcc), *(const_cast<TypeExt*>(&viewDst))));
178 
180  reinterpret_cast<Type*>(pMemAcc),
181  alpaka::getDev(queue),
182  extent);
183  enqueue(queue, createTaskMemcpy(std::forward<decltype(view)>(view), viewSrc, extent));
184  }
185 } // namespace alpaka
186 
187 #endif
#define ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cmd)
CUDA/HIP runtime error checking with log and exception.
The CUDA/HIP RT device handle.
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
concept Tag
Definition: Tag.hpp:46
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition: Traits.hpp:29
ALPAKA_FN_HOST auto memcpy(TQueue &queue, alpaka::detail::DevGlobalImplGeneric< TTag, TTypeDst > &viewDst, TViewSrc const &viewSrc) -> void
ALPAKA_FN_HOST auto createTaskMemcpy(TViewDstFwd &&viewDst, TViewSrc const &viewSrc, TExtent const &extent)
Creates a memory copy task.
Definition: Traits.hpp:253
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtents(T const &object) -> Vec< Dim< T >, Idx< T >>
Definition: Traits.hpp:59
ALPAKA_FN_HOST auto getDev(T const &t)
Definition: Traits.hpp:68
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
Definition: Traits.hpp:47
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition: Traits.hpp:19
The memory view to wrap plain pointers.