2222#pragma once
2323
2424#include " pmacc/mappings/kernel/MappingDescription.hpp"
25+ #include " pmacc/math/Vector.hpp"
26+ #include " pmacc/memory/Array.hpp"
27+ #include " pmacc/memory/shared/Allocate.hpp"
28+ #include " pmacc/types.hpp"
2529
2630#include < cstdint>
2731
2832namespace pmacc
2933{
34+ namespace detail
35+ {
36+ template <typename T_Vector, typename T_TYPE>
37+ HDINLINE auto & subscript (T_TYPE* p, int const idx, std::integral_constant<uint32_t , 1 >)
38+ {
39+ return p[idx];
40+ }
41+
42+ template <typename T_Vector, typename T_TYPE>
43+ HDINLINE auto * subscript (T_TYPE* p, int const idx, std::integral_constant<uint32_t , 2 >)
44+ {
45+ return p + idx * T_Vector::x::value;
46+ }
47+
48+ template <typename T_Vector, typename T_TYPE>
49+ HDINLINE auto * subscript (T_TYPE* p, int const idx, std::integral_constant<uint32_t , 3 >)
50+ {
51+ return p + idx * (T_Vector::x::value * T_Vector::y::value);
52+ }
53+ } // namespace detail
54+
3055 /* * A shared memory on gpu. Used in conjunction with \ref pmacc::DataBox.
3156 *
3257 * @tparam T_TYPE type of memory objects
@@ -43,6 +68,63 @@ namespace pmacc
4368 uint32_t T_dim = T_Vector::dim>
4469 struct SharedBox
4570 {
46- T_TYPE* fixedPointer;
71+ static constexpr std::uint32_t Dim = T_dim;
72+
73+ using ValueType = T_TYPE;
74+ using RefValueType = ValueType&;
75+ using Size = T_Vector;
76+
77+ HDINLINE
78+ SharedBox (ValueType* pointer = nullptr ) : fixedPointer(pointer)
79+ {
80+ }
81+
82+ HDINLINE SharedBox (SharedBox const &) = default;
83+
84+ using ReducedType1D = T_TYPE&;
85+ using ReducedType2D = SharedBox<T_TYPE, typename math::CT::shrinkTo<T_Vector, 1 >::type, T_id, T_MemoryMapping>;
86+ using ReducedType3D = SharedBox<T_TYPE, typename math::CT::shrinkTo<T_Vector, 2 >::type, T_id, T_MemoryMapping>;
87+ using ReducedType
88+ = std::conditional_t <Dim == 1 , ReducedType1D, std::conditional_t <Dim == 2 , ReducedType2D, ReducedType3D>>;
89+
90+ HDINLINE ReducedType operator [](const int idx) const
91+ {
92+ // /@todo(bgruber): inline and replace this by if constexpr in C++17
93+ return {detail::subscript<T_Vector>(fixedPointer, idx, std::integral_constant<uint32_t , T_dim>{})};
94+ }
95+
96+ /* !return the first value in the box (list)
97+ * @return first value
98+ */
99+ HDINLINE RefValueType operator *()
100+ {
101+ return *fixedPointer;
102+ }
103+
104+ HDINLINE ValueType const * getPointer () const
105+ {
106+ return fixedPointer;
107+ }
108+ HDINLINE ValueType* getPointer ()
109+ {
110+ return fixedPointer;
111+ }
112+
113+ /* * create a shared memory box
114+ *
115+ * This call synchronizes a block and must be called from all threads and
116+ * not inside a if clauses
117+ */
118+ template <typename T_Worker>
119+ static DINLINE SharedBox init (T_Worker const & worker)
120+ {
121+ auto & mem_sh
122+ = memory::shared::allocate<T_id, memory::Array<ValueType, math::CT::volume<Size>::type::value>>(
123+ worker);
124+ return {mem_sh.data ()};
125+ }
126+
127+ protected:
128+ PMACC_ALIGN (fixedPointer, ValueType*);
47129 };
48130} // namespace pmacc
0 commit comments