Skip to content

Commit

Permalink
aasdf
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed May 4, 2023
1 parent a73b645 commit e9f1a67
Showing 1 changed file with 34 additions and 41 deletions.
75 changes: 34 additions & 41 deletions include/pmacc/memory/boxes/DataBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@

namespace pmacc
{
namespace detail
{
template<typename DataBox>
HDINLINE decltype(auto) access(const DataBox& db, DataSpace<1> const& idx = {})
{
return db[idx.x()];
}

template<typename DataBox>
HDINLINE decltype(auto) access(const DataBox& db, DataSpace<2> const& idx = {})
{
return db[idx.y()][idx.x()];
}

template<typename DataBox>
HDINLINE decltype(auto) access(const DataBox& db, DataSpace<3> const& idx = {})
{
return db[idx.z()][idx.y()][idx.x()];
}
} // namespace detail

template<typename Base>
struct DataBox : Base
{
Expand All @@ -44,22 +65,9 @@ namespace pmacc

HDINLINE decltype(auto) operator()(DataSpace<Base::Dim> const& idx = {}) const
{
if constexpr(Base::Dim == 1)
{
return (*this)[idx.x()];
}
else if constexpr(Base::Dim == 2)
{
return (*this)[idx.y()][idx.x()];
}
else if constexpr(Base::Dim == 3)
{
return (*this)[idx.z()][idx.y()][idx.x()];
}
else
{
static_assert(sizeof(idx) == 0, "Dim must be 1, 2 or 3");
}
/// @TODO(bgruber): inline and replace this by if constexpr in C++17 at some point. however, nvcc generates
/// worse code with if constexpr. Ask Rene about it.
return detail::access(*this, idx);
}

HDINLINE DataBox shift(DataSpace<Base::Dim> const& offset) const
Expand All @@ -73,7 +81,7 @@ namespace pmacc
namespace internal
{
template<typename... Sizes>
HDINLINE auto toArrayExtents(math::CT::Vector<Sizes...>)
HDINLINE constexpr auto toArrayExtents(math::CT::Vector<Sizes...>)
{
using V = math::CT::Vector<Sizes...>;
using IndexType = typename math::CT::Vector<Sizes...>::type;
Expand All @@ -95,36 +103,21 @@ namespace pmacc
}
}

// LLAMA and DataSpace indices have the same semantic, fast moving index is first.
template<unsigned Dim>
HDINLINE auto toArrayIndex(DataSpace<Dim> idx)
{
using IndexType = typename DataSpace<Dim>::type;
using ArrayIndex = llama::ArrayIndex<IndexType, Dim>;
if constexpr(Dim == 1)
{
return ArrayIndex{idx[0]};
}
else if constexpr(Dim == 2)
{
return ArrayIndex{idx[1], idx[0]};
}
else if constexpr(Dim == 3)
{
return ArrayIndex{idx[2], idx[1], idx[0]};
}
else
{
static_assert(sizeof(idx) == 0, "Dim must be 1, 2 or 3");
}
llama::ArrayIndex<typename DataSpace<Dim>::type, Dim> ai;
for(int i = 0; i < Dim; i++)
ai[i] = idx[Dim - 1 - i];
return ai;
}
} // namespace internal

// 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>>
{
using SB = SharedBox<T_TYPE, T_SizeVector, T_id, T_MemoryMapping, T_dim>;
using SharedBoxBase = SharedBox<T_TYPE, T_SizeVector, T_id, T_MemoryMapping, T_dim>;

inline static constexpr std::uint32_t Dim = T_dim;
using ValueType = T_TYPE;
Expand All @@ -137,10 +130,11 @@ namespace pmacc
using View = llama::View<Mapping, std::byte*>;

View view;
DataSpace<T_dim> offset{};

HDINLINE DataBox() = default;

HDINLINE DataBox(SB sb)
HDINLINE DataBox(SharedBoxBase sb)
: view{
Mapping{{}},
llama::Array<std::byte*, 1>{
Expand All @@ -159,20 +153,19 @@ namespace pmacc

HDINLINE DataBox shift(const DataSpace<T_dim>& offset) const
{
// TODO(bgruber): can we enhance LLAMA to make this smarter than just keeping the offset?
DataBox result(*this);
result.offset += offset;
return result;
}

template<typename T_Worker>
static DINLINE SB init(T_Worker const& worker)
static DINLINE SharedBoxBase 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()};
}

DataSpace<T_dim> offset{};
};
} // namespace pmacc

0 comments on commit e9f1a67

Please sign in to comment.