alpaka
Abstraction Library for Parallel Kernel Acceleration
Traits.hpp
Go to the documentation of this file.
1 /* Copyright 2023 Alexander Matthes, Benjamin Worpitz, Andrea Bocci, Bernhard Manfred Gruber, Jan Stephan,
2  * Christian Kaever
3  * SPDX-License-Identifier: MPL-2.0
4  */
5 
6 #pragma once
7 
9 #include "alpaka/core/Common.hpp"
12 
13 namespace alpaka
14 {
15  //! The CPU device handle.
16  class DevCpu;
17 
18  //! The buffer traits.
19  namespace trait
20  {
21  //! The memory buffer type trait.
22  template<typename TDev, typename TElem, typename TDim, typename TIdx, typename TSfinae = void>
23  struct BufType;
24 
25  //! The memory allocator trait.
26  template<typename TElem, typename TDim, typename TIdx, typename TDev, typename TSfinae = void>
27  struct BufAlloc;
28 
29  //! The stream-ordered memory allocator trait.
30  template<typename TElem, typename TDim, typename TIdx, typename TDev, typename TSfinae = void>
31  struct AsyncBufAlloc;
32 
33  //! The stream-ordered memory allocation capability trait.
34  template<typename TDim, typename TDev>
35  struct HasAsyncBufSupport : public std::false_type
36  {
37  };
38 
39  //! The pinned/mapped memory allocator trait.
40  template<typename TPlatform, typename TElem, typename TDim, typename TIdx>
42 
43  //! The pinned/mapped memory allocation capability trait.
44  template<typename TPlatform>
45  struct HasMappedBufSupport : public std::false_type
46  {
47  };
48  } // namespace trait
49 
50  //! The memory buffer type trait alias template to remove the ::type.
51  template<typename TDev, typename TElem, typename TDim, typename TIdx>
52  using Buf = typename trait::BufType<alpaka::Dev<TDev>, TElem, TDim, TIdx>::type;
53 
54  //! Allocates memory on the given device.
55  //!
56  //! \tparam TElem The element type of the returned buffer.
57  //! \tparam TIdx The linear index type of the buffer.
58  //! \tparam TExtent The extent type of the buffer.
59  //! \tparam TDev The type of device the buffer is allocated on.
60  //! \param dev The device to allocate the buffer on.
61  //! \param extent The extent of the buffer.
62  //! \return The newly allocated buffer.
63  template<typename TElem, typename TIdx, typename TExtent, typename TDev>
64  ALPAKA_FN_HOST auto allocBuf(TDev const& dev, TExtent const& extent = TExtent())
65  {
66  return trait::BufAlloc<TElem, Dim<TExtent>, TIdx, TDev>::allocBuf(dev, extent);
67  }
68 
69  //! Allocates stream-ordered memory on the given device.
70  //!
71  //! \tparam TElem The element type of the returned buffer.
72  //! \tparam TIdx The linear index type of the buffer.
73  //! \tparam TExtent The extent type of the buffer.
74  //! \tparam TQueue The type of queue used to order the buffer allocation.
75  //! \param queue The queue used to order the buffer allocation.
76  //! \param extent The extent of the buffer.
77  //! \return The newly allocated buffer.
78  template<typename TElem, typename TIdx, typename TExtent, typename TQueue>
79  ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const& extent = TExtent())
80  {
82  }
83 
84  /* TODO: Remove this pragma block once support for clang versions <= 13 is removed. These versions are unable to
85  figure out that the template parameters are attached to a C++17 inline variable. */
86 #if BOOST_COMP_CLANG
87 # pragma clang diagnostic push
88 # pragma clang diagnostic ignored "-Wdocumentation"
89 #endif
90  //! Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
91  //!
92  //! \tparam TDev The type of device to allocate the buffer on.
93  //! \tparam TDim The dimensionality of the buffer to allocate.
94  template<typename TDev, typename TDim>
96 #if BOOST_COMP_CLANG
97 # pragma clang diagnostic pop
98 #endif
99 
100  //! If supported, allocates stream-ordered memory on the given queue and the associated device.
101  //! Otherwise, allocates regular memory on the device associated to the queue.
102  //! Please note that stream-ordered and regular memory have different semantics:
103  //! this function is provided for convenience in the cases where the difference is not relevant,
104  //! and the stream-ordered memory is only used as a performance optimisation.
105  //!
106  //! \tparam TElem The element type of the returned buffer.
107  //! \tparam TIdx The linear index type of the buffer.
108  //! \tparam TExtent The extent type of the buffer.
109  //! \tparam TQueue The type of queue used to order the buffer allocation.
110  //! \param queue The queue used to order the buffer allocation.
111  //! \param extent The extent of the buffer.
112  //! \return The newly allocated buffer.
113  template<typename TElem, typename TIdx, typename TExtent, typename TQueue>
114  ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const& extent = TExtent())
115  {
117  {
118  return allocAsyncBuf<TElem, TIdx>(queue, extent);
119  }
120  else
121  {
122  return allocBuf<TElem, TIdx>(getDev(queue), extent);
123  }
124 
125  ALPAKA_UNREACHABLE(allocBuf<TElem, TIdx>(getDev(queue), extent));
126  }
127 
128  //! Allocates pinned/mapped host memory, accessible by all devices in the given platform.
129  //!
130  //! \tparam TPlatform The platform from which the buffer is accessible.
131  //! \tparam TElem The element type of the returned buffer.
132  //! \tparam TIdx The linear index type of the buffer.
133  //! \tparam TExtent The extent type of the buffer.
134  //! \param host The host device to allocate the buffer on.
135  //! \param extent The extent of the buffer.
136  //! \return The newly allocated buffer.
137  template<typename TPlatform, typename TElem, typename TIdx, typename TExtent>
139  DevCpu const& host,
140  TPlatform const& platform,
141  TExtent const& extent = TExtent())
142  {
143  return trait::BufAllocMapped<TPlatform, TElem, Dim<TExtent>, TIdx>::allocMappedBuf(host, platform, extent);
144  }
145 
146  /* TODO: Remove this pragma block once support for clang versions <= 13 is removed. These versions are unable to
147  figure out that the template parameters are attached to a C++17 inline variable. */
148 #if BOOST_COMP_CLANG
149 # pragma clang diagnostic push
150 # pragma clang diagnostic ignored "-Wdocumentation"
151 #endif
152  //! Checks if the host can allocate a pinned/mapped host memory, accessible by all devices in the given platform.
153  //!
154  //! \tparam TPlatform The platform from which the buffer is accessible.
155  template<typename TPlatform>
157 #if BOOST_COMP_CLANG
158 # pragma clang diagnostic pop
159 #endif
160 
161  //! If supported, allocates pinned/mapped host memory, accessible by all devices in the given platform.
162  //! Otherwise, allocates regular host memory.
163  //! Please note that pinned/mapped and regular memory may have different semantics:
164  //! this function is provided for convenience in the cases where the difference is not relevant,
165  //! and the pinned/mapped memory is only used as a performance optimisation.
166  //!
167  //! \tparam TElem The element type of the returned buffer.
168  //! \tparam TIdx The linear index type of the buffer.
169  //! \tparam TExtent The extent type of the buffer.
170  //! \tparam TPlatform The platform from which the buffer is accessible.
171  //! \param host The host device to allocate the buffer on.
172  //! \param extent The extent of the buffer.
173  //! \return The newly allocated buffer.
174  template<typename TElem, typename TIdx, typename TExtent, typename TPlatform>
176  DevCpu const& host,
177  TPlatform const& platform,
178  TExtent const& extent = TExtent())
179  {
181  if constexpr(hasMappedBufSupport<Platform>)
182  {
183  return allocMappedBuf<Platform, TElem, TIdx>(host, platform, extent);
184  }
185  else
186  {
187  return allocBuf<TElem, TIdx>(host, extent);
188  }
189 
190  ALPAKA_UNREACHABLE(allocBuf<TElem, TIdx>(host, extent));
191  }
192 } // namespace alpaka
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
Definition: Unreachable.hpp:24
The CPU device handle.
Definition: DevCpu.hpp:56
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
The alpaka accelerator library.
constexpr bool hasMappedBufSupport
Checks if the host can allocate a pinned/mapped host memory, accessible by all devices in the given p...
Definition: Traits.hpp:156
ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const &extent=TExtent())
If supported, allocates stream-ordered memory on the given queue and the associated device....
Definition: Traits.hpp:114
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
Definition: Traits.hpp:56
ALPAKA_FN_HOST auto allocMappedBuf(DevCpu const &host, TPlatform const &platform, TExtent const &extent=TExtent())
Allocates pinned/mapped host memory, accessible by all devices in the given platform.
Definition: Traits.hpp:138
ALPAKA_FN_HOST auto allocBuf(TDev const &dev, TExtent const &extent=TExtent())
Allocates memory on the given device.
Definition: Traits.hpp:64
constexpr bool hasAsyncBufSupport
Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
Definition: Traits.hpp:95
ALPAKA_FN_HOST auto getDev(T const &t)
Definition: Traits.hpp:68
ALPAKA_FN_HOST auto allocMappedBufIfSupported(DevCpu const &host, TPlatform const &platform, TExtent const &extent=TExtent())
If supported, allocates pinned/mapped host memory, accessible by all devices in the given platform....
Definition: Traits.hpp:175
typename trait::BufType< alpaka::Dev< TDev >, TElem, TDim, TIdx >::type Buf
The memory buffer type trait alias template to remove the ::type.
Definition: Traits.hpp:52
ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const &extent=TExtent())
Allocates stream-ordered memory on the given device.
Definition: Traits.hpp:79
typename trait::PlatformType< T >::type Platform
The platform type trait alias template to remove the ::type.
Definition: Traits.hpp:51
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition: Traits.hpp:19
The stream-ordered memory allocator trait.
Definition: Traits.hpp:31
The pinned/mapped memory allocator trait.
Definition: Traits.hpp:41
The memory allocator trait.
Definition: Traits.hpp:27
The memory buffer type trait.
Definition: Traits.hpp:23
The stream-ordered memory allocation capability trait.
Definition: Traits.hpp:36
The pinned/mapped memory allocation capability trait.
Definition: Traits.hpp:46