alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
ConstBufUniformCudaHipRtTraits.hpp
Go to the documentation of this file.
1/* Copyright 2025 Alexander Matthes, Benjamin Worpitz, Matthias Werner, René Widera, Andrea Bocci, Jan Stephan,
2 * Bernhard Manfred Gruber, Antonio Di Pilato, Anton Reinhard
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
6#pragma once
7
12
13#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) || defined(ALPAKA_ACC_GPU_HIP_ENABLED)
14
15namespace alpaka::trait
16{
17 //! The CUDA/HIP RT device memory const-buffer type trait specialization.
18 template<typename TApi, typename TElem, typename TDim, typename TIdx>
23
24 //! The ConstBufUniformCudaHipRt device type trait specialization.
25 template<typename TApi, typename TElem, typename TDim, typename TIdx>
26 struct DevType<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
27 {
29 };
30
31 //! The ConstBufUniformCudaHipRt device get trait specialization.
32 template<typename TApi, typename TElem, typename TDim, typename TIdx>
33 struct GetDev<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
34 {
37 {
38 return buf.m_spBufImpl->m_dev;
39 }
40 };
41
42 //! The ConstBufUniformCudaHipRt dimension getter trait specialization.
43 template<typename TApi, typename TElem, typename TDim, typename TIdx>
44 struct DimType<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
45 {
46 using type = TDim;
47 };
48
49 //! The ConstBufUniformCudaHipRt memory element type get trait specialization.
50 template<typename TApi, typename TElem, typename TDim, typename TIdx>
51 struct ElemType<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
52 {
53 using type = TElem const;
54 };
55
56 //! The ConstBufUniformCudaHipRt extent get trait specialization.
57 template<typename TApi, typename TElem, typename TDim, typename TIdx>
58 struct GetExtents<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
59 {
61 {
62 return buf.m_spBufImpl->m_extentElements;
63 }
64 };
65
66 //! The ConstBufUniformCudaHipRt native pointer get trait specialization.
67 template<typename TApi, typename TElem, typename TDim, typename TIdx>
68 struct GetPtrNative<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
69 {
71 -> TElem const*
72 {
73 return buf.m_spBufImpl->m_pMem;
74 }
75 };
76
77 //! The ConstBufUniformCudaHipRt pointer on device get trait specialization.
78 template<typename TApi, typename TElem, typename TDim, typename TIdx>
79 struct GetPtrDev<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>, DevUniformCudaHipRt<TApi>>
80 {
83 DevUniformCudaHipRt<TApi> const& dev) -> TElem const*
84 {
85 if(dev == getDev(buf))
86 {
87 return buf.m_spBufImpl->m_pMem;
88 }
89 else
90 {
91 throw std::runtime_error("The buffer is not accessible from the given device!");
92 }
93 }
94 };
95
96 template<typename TApi, typename TElem, typename TDim, typename TIdx>
97 struct GetPitchesInBytes<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
98 {
101 {
102 Vec<TDim, TIdx> v{};
103 if constexpr(TDim::value > 0)
104 {
105 v.back() = sizeof(TElem);
106 if constexpr(TDim::value > 1)
107 {
108 v[TDim::value - 2] = static_cast<TIdx>(buf.m_spBufImpl->m_rowPitchInBytes);
109 for(TIdx i = TDim::value - 2; i > 0; i--)
110 v[i - 1] = buf.m_spBufImpl->m_extentElements[i] * v[i];
111 }
112 }
113 return v;
114 }
115 };
116
117 //! The ConstBufUniformCudaHipRt offset get trait specialization.
118 template<typename TApi, typename TElem, typename TDim, typename TIdx>
119 struct GetOffsets<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
120 {
126 };
127
128 //! The ConstBufUniformCudaHipRt idx type trait specialization.
129 template<typename TApi, typename TElem, typename TDim, typename TIdx>
130 struct IdxType<ConstBufUniformCudaHipRt<TApi, TElem, TDim, TIdx>>
131 {
132 using type = TIdx;
133 };
134
135 //! The ConstBufCpu pointer on CUDA/HIP device get trait specialization.
136 template<typename TApi, typename TElem, typename TDim, typename TIdx>
137 struct GetPtrDev<ConstBufCpu<TElem, TDim, TIdx>, DevUniformCudaHipRt<TApi>>
138 {
141 DevUniformCudaHipRt<TApi> const&) -> TElem const*
142 {
143 // TODO: Check if the memory is mapped at all!
144 TElem* pDev(nullptr);
145
146 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(TApi::hostGetDevicePointer(
147 &pDev,
148 const_cast<void*>(reinterpret_cast<void const*>(getPtrNative(buf))),
149 0));
150
151 return pDev;
152 }
153 };
154
155 //! The MakeConstBuf trait for constant CUDA/HIP buffers.
156 template<typename TApi, typename TElem, typename TDim, typename TIdx>
171
172} // namespace alpaka::trait
173
174#endif
#define ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cmd)
CUDA/HIP runtime error checking with log and exception.
The CPU memory buffer.
The CUDA/HIP RT device handle.
A n-dimensional vector.
Definition Vec.hpp:38
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto zeros() -> Vec< TDim, TVal >
Zero value constructor.
Definition Vec.hpp:99
ALPAKA_FN_HOST_ACC constexpr auto back() -> TVal &
Definition Vec.hpp:141
#define ALPAKA_FN_HOST
Definition Common.hpp:40
The accelerator traits.
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition Traits.hpp:136
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:68
The memory const-buffer type trait.
Definition Traits.hpp:27
The device type trait.
Definition Traits.hpp:23
The dimension getter type trait.
Definition Traits.hpp:14
The element type trait.
Definition Traits.hpp:16
static ALPAKA_FN_HOST auto getDev(ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx > const &buf) -> DevUniformCudaHipRt< TApi >
The device get trait.
Definition Traits.hpp:27
ALPAKA_FN_HOST auto operator()(ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx > const &buf) const
The GetExtents trait for getting the extents of an object as an alpaka::Vec.
Definition Traits.hpp:37
ALPAKA_FN_HOST auto operator()(ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx > const &) const -> Vec< TDim, TIdx >
The GetOffsets trait for getting the offsets of an object as an alpaka::Vec.
Definition Traits.hpp:33
ALPAKA_FN_HOST auto operator()(ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx > const &buf) const -> Vec< TDim, TIdx >
Customization point for getPitchesInBytes. The default implementation uses the extent to calculate th...
Definition Traits.hpp:103
static ALPAKA_FN_HOST auto getPtrDev(ConstBufCpu< TElem, TDim, TIdx > const &buf, DevUniformCudaHipRt< TApi > const &) -> TElem const *
static ALPAKA_FN_HOST auto getPtrDev(ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx > const &buf, DevUniformCudaHipRt< TApi > const &dev) -> TElem const *
The pointer on device get trait.
Definition Traits.hpp:58
static ALPAKA_FN_HOST auto getPtrNative(ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx > const &buf) -> TElem const *
The native pointer get trait.
Definition Traits.hpp:54
The idx type trait.
Definition Traits.hpp:25
static ALPAKA_FN_HOST auto makeConstBuf(ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx > &&buf) -> ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx >
static ALPAKA_FN_HOST auto makeConstBuf(ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx > const &buf) -> ConstBufUniformCudaHipRt< TApi, TElem, TDim, TIdx >
The trait to transform a mutable buffer into a constant one.
Definition Traits.hpp:55