diff --git a/include/pmacc/memory/boxes/DataBox.hpp b/include/pmacc/memory/boxes/DataBox.hpp index 93a5d0e199..bb6d84fca1 100644 --- a/include/pmacc/memory/boxes/DataBox.hpp +++ b/include/pmacc/memory/boxes/DataBox.hpp @@ -52,7 +52,7 @@ namespace pmacc } } // namespace detail - template + template struct DataBox : Base { HDINLINE DataBox() = default; @@ -115,7 +115,9 @@ namespace pmacc // handle DataBox wrapping SharedBox with LLAMA template - struct DataBox> + struct DataBox< + SharedBox, + std::enable_if_t>> { using SharedBoxBase = SharedBox; diff --git a/include/pmacc/memory/boxes/SharedBox.hpp b/include/pmacc/memory/boxes/SharedBox.hpp index 06f3b3551a..4349eaf5b3 100644 --- a/include/pmacc/memory/boxes/SharedBox.hpp +++ b/include/pmacc/memory/boxes/SharedBox.hpp @@ -22,11 +22,36 @@ #pragma once #include "pmacc/mappings/kernel/MappingDescription.hpp" +#include "pmacc/math/Vector.hpp" +#include "pmacc/memory/Array.hpp" +#include "pmacc/memory/shared/Allocate.hpp" +#include "pmacc/types.hpp" #include namespace pmacc { + namespace detail + { + template + HDINLINE auto& subscript(T_TYPE* p, int const idx, std::integral_constant) + { + return p[idx]; + } + + template + HDINLINE auto* subscript(T_TYPE* p, int const idx, std::integral_constant) + { + return p + idx * T_Vector::x::value; + } + + template + HDINLINE auto* subscript(T_TYPE* p, int const idx, std::integral_constant) + { + return p + idx * (T_Vector::x::value * T_Vector::y::value); + } + } // namespace detail + /** A shared memory on gpu. Used in conjunction with \ref pmacc::DataBox. * * @tparam T_TYPE type of memory objects @@ -43,6 +68,63 @@ namespace pmacc uint32_t T_dim = T_Vector::dim> struct SharedBox { - T_TYPE* fixedPointer; + static constexpr std::uint32_t Dim = T_dim; + + using ValueType = T_TYPE; + using RefValueType = ValueType&; + using Size = T_Vector; + + HDINLINE + SharedBox(ValueType* pointer = nullptr) : fixedPointer(pointer) + { + } + + HDINLINE SharedBox(SharedBox const&) = default; + + using ReducedType1D = T_TYPE&; + using ReducedType2D = SharedBox::type, T_id, T_MemoryMapping>; + using ReducedType3D = SharedBox::type, T_id, T_MemoryMapping>; + using ReducedType + = std::conditional_t>; + + HDINLINE ReducedType operator[](const int idx) const + { + ///@todo(bgruber): inline and replace this by if constexpr in C++17 + return {detail::subscript(fixedPointer, idx, std::integral_constant{})}; + } + + /*!return the first value in the box (list) + * @return first value + */ + HDINLINE RefValueType operator*() + { + return *fixedPointer; + } + + HDINLINE ValueType const* getPointer() const + { + return fixedPointer; + } + HDINLINE ValueType* getPointer() + { + return fixedPointer; + } + + /** create a shared memory box + * + * This call synchronizes a block and must be called from all threads and + * not inside a if clauses + */ + template + static DINLINE SharedBox init(T_Worker const& worker) + { + auto& mem_sh + = memory::shared::allocate::type::value>>( + worker); + return {mem_sh.data()}; + } + + protected: + PMACC_ALIGN(fixedPointer, ValueType*); }; } // namespace pmacc