Skip to content

Commit

Permalink
void
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed May 4, 2023
1 parent 2a591d9 commit fdadfa9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
6 changes: 4 additions & 2 deletions include/pmacc/memory/boxes/DataBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace pmacc
}
} // namespace detail

template<typename Base>
template<typename Base, typename SFINAE = void>
struct DataBox : Base
{
HDINLINE DataBox() = default;
Expand Down Expand Up @@ -115,7 +115,9 @@ namespace pmacc

// handle DataBox wrapping SharedBox with LLAMA
template<typename T_TYPE, class T_SizeVector, typename T_MemoryMapping, uint32_t T_id, uint32_t T_dim>
struct DataBox<SharedBox<T_TYPE, T_SizeVector, T_id, T_MemoryMapping, T_dim>>
struct DataBox<
SharedBox<T_TYPE, T_SizeVector, T_id, T_MemoryMapping, T_dim>,
std::enable_if_t<!std::is_void_v<T_MemoryMapping>>>
{
using SharedBoxBase = SharedBox<T_TYPE, T_SizeVector, T_id, T_MemoryMapping, T_dim>;

Expand Down
84 changes: 83 additions & 1 deletion include/pmacc/memory/boxes/SharedBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdint>

namespace pmacc
{
namespace detail
{
template<typename T_Vector, typename T_TYPE>
HDINLINE auto& subscript(T_TYPE* p, int const idx, std::integral_constant<uint32_t, 1>)
{
return p[idx];
}

template<typename T_Vector, typename T_TYPE>
HDINLINE auto* subscript(T_TYPE* p, int const idx, std::integral_constant<uint32_t, 2>)
{
return p + idx * T_Vector::x::value;
}

template<typename T_Vector, typename T_TYPE>
HDINLINE auto* subscript(T_TYPE* p, int const idx, std::integral_constant<uint32_t, 3>)
{
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
Expand All @@ -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<T_TYPE, typename math::CT::shrinkTo<T_Vector, 1>::type, T_id, T_MemoryMapping>;
using ReducedType3D = SharedBox<T_TYPE, typename math::CT::shrinkTo<T_Vector, 2>::type, T_id, T_MemoryMapping>;
using ReducedType
= std::conditional_t<Dim == 1, ReducedType1D, std::conditional_t<Dim == 2, ReducedType2D, ReducedType3D>>;

HDINLINE ReducedType operator[](const int idx) const
{
///@todo(bgruber): inline and replace this by if constexpr in C++17
return {detail::subscript<T_Vector>(fixedPointer, idx, std::integral_constant<uint32_t, T_dim>{})};
}

/*!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<typename T_Worker>
static DINLINE SharedBox init(T_Worker const& worker)
{
auto& mem_sh
= memory::shared::allocate<T_id, memory::Array<ValueType, math::CT::volume<Size>::type::value>>(
worker);
return {mem_sh.data()};
}

protected:
PMACC_ALIGN(fixedPointer, ValueType*);
};
} // namespace pmacc

0 comments on commit fdadfa9

Please sign in to comment.