alpaka
Abstraction Library for Parallel Kernel Acceleration
BlockSharedMemDynMember.hpp
Go to the documentation of this file.
1 /* Copyright 2023 Jeffrey Kelling, Jan Stephan, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
9 #include "alpaka/core/Assert.hpp"
11 
12 #include <array>
13 #include <cstdint>
14 #include <type_traits>
15 
16 namespace alpaka
17 {
18  namespace detail
19  {
20  //! "namespace" for static constexpr members that should be in BlockSharedMemDynMember
21  //! but cannot be because having a static const member breaks GCC 10
22  //! OpenMP target: type not mappable.
23  template<std::size_t TStaticAllocKiB>
25  {
26  //! Storage size in bytes
27  static constexpr std::uint32_t staticAllocBytes = static_cast<std::uint32_t>(TStaticAllocKiB << 10u);
28  };
29  } // namespace detail
30 
31 #if BOOST_COMP_MSVC || defined(BOOST_COMP_MSVC_EMULATED)
32 # pragma warning(push)
33 # pragma warning(disable : 4324) // warning C4324: structure was padded due to alignment specifier
34 #endif
35  //! Dynamic block shared memory provider using fixed-size
36  //! member array to allocate memory on the stack or in shared
37  //! memory.
38  template<std::size_t TStaticAllocKiB = BlockSharedDynMemberAllocKiB>
39  class alignas(core::vectorization::defaultAlignment) BlockSharedMemDynMember
40  : public concepts::Implements<ConceptBlockSharedDyn, BlockSharedMemDynMember<TStaticAllocKiB>>
41  {
42  public:
43  BlockSharedMemDynMember(std::size_t sizeBytes) : m_dynPitch(getPitch(sizeBytes))
44  {
45  ALPAKA_ASSERT_ACC(static_cast<std::uint32_t>(sizeBytes) <= staticAllocBytes());
46  }
47 
48  auto dynMemBegin() const -> uint8_t*
49  {
50  return std::data(m_mem);
51  }
52 
53  /*! \return the pointer to the begin of data after the portion allocated as dynamical shared memory.
54  */
55  auto staticMemBegin() const -> uint8_t*
56  {
57  return std::data(m_mem) + m_dynPitch;
58  }
59 
60  /*! \return the remaining capacity for static block shared memory,
61  returns a 32-bit type for register efficiency on GPUs
62  */
63  auto staticMemCapacity() const -> std::uint32_t
64  {
65  return staticAllocBytes() - m_dynPitch;
66  }
67 
68  //! \return size of statically allocated memory available for both
69  //! dynamic and static shared memory. Value is of a 32-bit type
70  //! for register efficiency on GPUs
71  static constexpr auto staticAllocBytes() -> std::uint32_t
72  {
74  }
75 
76  private:
77  static auto getPitch(std::size_t sizeBytes) -> std::uint32_t
78  {
79  constexpr auto alignment = core::vectorization::defaultAlignment;
80  return static_cast<std::uint32_t>((sizeBytes / alignment + (sizeBytes % alignment > 0u)) * alignment);
81  }
82 
83  mutable std::array<uint8_t, detail::BlockSharedMemDynMemberStatic<TStaticAllocKiB>::staticAllocBytes> m_mem;
84  std::uint32_t m_dynPitch;
85  };
86 #if BOOST_COMP_MSVC || defined(BOOST_COMP_MSVC_EMULATED)
87 # pragma warning(pop)
88 #endif
89 
90  namespace trait
91  {
92  template<typename T, std::size_t TStaticAllocKiB>
93  struct GetDynSharedMem<T, BlockSharedMemDynMember<TStaticAllocKiB>>
94  {
95 #if BOOST_COMP_GNUC
96 # pragma GCC diagnostic push
97 # pragma GCC diagnostic ignored \
98  "-Wcast-align" // "cast from 'unsigned char*' to 'unsigned int*' increases required alignment of target type"
99 #endif
100  static auto getMem(BlockSharedMemDynMember<TStaticAllocKiB> const& mem) -> T*
101  {
102  static_assert(
104  "Unable to get block shared dynamic memory for types with alignment higher than "
105  "defaultAlignment!");
106  return reinterpret_cast<T*>(mem.dynMemBegin());
107  }
108 #if BOOST_COMP_GNUC
109 # pragma GCC diagnostic pop
110 #endif
111  };
112  } // namespace trait
113 } // namespace alpaka
#define ALPAKA_ASSERT_ACC(...)
ALPAKA_ASSERT_ACC is an assert-like macro.
Definition: Assert.hpp:52
Dynamic block shared memory provider using fixed-size member array to allocate memory on the stack or...
static constexpr auto staticAllocBytes() -> std::uint32_t
auto staticMemBegin() const -> uint8_t *
BlockSharedMemDynMember(std::size_t sizeBytes)
auto staticMemCapacity() const -> std::uint32_t
constexpr std::size_t defaultAlignment
Definition: Vectorize.hpp:34
The alpaka accelerator library.
Tag used in class inheritance hierarchies that describes that a specific concept (TConcept) is implem...
Definition: Concepts.hpp:15
"namespace" for static constexpr members that should be in BlockSharedMemDynMember but cannot be beca...
static constexpr std::uint32_t staticAllocBytes
Storage size in bytes.
static auto getMem(BlockSharedMemDynMember< TStaticAllocKiB > const &mem) -> T *
The block shared dynamic memory get trait.
Definition: Traits.hpp:23