Skip to content

Commit

Permalink
Merge pull request #140 from bernhardmgruber/one
Browse files Browse the repository at this point in the history
Replace allocVirtualDatumStack<>() by One<>
  • Loading branch information
bernhardmgruber authored Dec 21, 2020
2 parents 7346d22 + 245e7df commit 4dfd971
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/alpaka/asyncblur/asyncblur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct BlurKernel
LLAMA_INDEPENDENT_DATA
for (auto x = start[1]; x < end[1]; ++x)
{
auto sum = llama::allocVirtualDatumStack<PixelOnAcc>();
llama::One<PixelOnAcc> sum;
sum = 0;

using ItType = long int;
Expand Down
18 changes: 9 additions & 9 deletions examples/alpaka/nbody/nbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ int main()
LLAMA_INDEPENDENT_DATA
for (std::size_t i = 0; i < PROBLEM_SIZE; ++i)
{
auto temp = llama::allocVirtualDatumStack<Particle>();
temp(tag::Pos(), tag::X()) = distribution(generator);
temp(tag::Pos(), tag::Y()) = distribution(generator);
temp(tag::Pos(), tag::Z()) = distribution(generator);
temp(tag::Vel(), tag::X()) = distribution(generator) / FP(10);
temp(tag::Vel(), tag::Y()) = distribution(generator) / FP(10);
temp(tag::Vel(), tag::Z()) = distribution(generator) / FP(10);
temp(tag::Mass()) = distribution(generator) / FP(100);
hostView(i) = temp;
llama::One<Particle> p;
p(tag::Pos(), tag::X()) = distribution(generator);
p(tag::Pos(), tag::Y()) = distribution(generator);
p(tag::Pos(), tag::Z()) = distribution(generator);
p(tag::Vel(), tag::X()) = distribution(generator) / FP(10);
p(tag::Vel(), tag::Y()) = distribution(generator) / FP(10);
p(tag::Vel(), tag::Z()) = distribution(generator) / FP(10);
p(tag::Mass()) = distribution(generator) / FP(100);
hostView(i) = p;
}

chrono.printAndReset("Init");
Expand Down
22 changes: 11 additions & 11 deletions examples/cuda/nbody/nbody.cu
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ __global__ void updateSM(View particles)
const auto ti = threadIdx.x + blockIdx.x * blockDim.x;
const auto tbi = blockIdx.x;

auto pi = llama::allocVirtualDatumStack<Particle>();
llama::One<Particle> pi;
if constexpr (UseAccumulator)
pi = particles(ti);
for (std::size_t blockOffset = 0; blockOffset < ProblemSize; blockOffset += BlockSize)
Expand Down Expand Up @@ -132,7 +132,7 @@ __global__ void update(View particles)
{
const auto ti = threadIdx.x + blockIdx.x * blockDim.x;

auto pi = llama::allocVirtualDatumStack<Particle>();
llama::One<Particle> pi;
if constexpr (UseAccumulator)
pi = particles(ti);
LLAMA_INDEPENDENT_DATA
Expand Down Expand Up @@ -218,15 +218,15 @@ try
std::normal_distribution<FP> distribution(FP(0), FP(1));
for (std::size_t i = 0; i < PROBLEM_SIZE; ++i)
{
auto temp = llama::allocVirtualDatumStack<Particle>();
temp(tag::Pos(), tag::X()) = distribution(generator);
temp(tag::Pos(), tag::Y()) = distribution(generator);
temp(tag::Pos(), tag::Z()) = distribution(generator);
temp(tag::Vel(), tag::X()) = distribution(generator) / FP(10);
temp(tag::Vel(), tag::Y()) = distribution(generator) / FP(10);
temp(tag::Vel(), tag::Z()) = distribution(generator) / FP(10);
temp(tag::Mass()) = distribution(generator) / FP(100);
hostView(i) = temp;
llama::One<Particle> p;
p(tag::Pos(), tag::X()) = distribution(generator);
p(tag::Pos(), tag::Y()) = distribution(generator);
p(tag::Pos(), tag::Z()) = distribution(generator);
p(tag::Vel(), tag::X()) = distribution(generator) / FP(10);
p(tag::Vel(), tag::Y()) = distribution(generator) / FP(10);
p(tag::Vel(), tag::Z()) = distribution(generator) / FP(10);
p(tag::Mass()) = distribution(generator) / FP(100);
hostView(i) = p;
}

watch.printAndReset("init");
Expand Down
2 changes: 1 addition & 1 deletion examples/nbody/nbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace usellama
LLAMA_INDEPENDENT_DATA
for (std::size_t i = 0; i < PROBLEM_SIZE; i++)
{
auto pi = llama::allocVirtualDatumStack<Particle>();
llama::One<Particle> pi;
if constexpr (UseAccumulator)
pi = particles(i);
LLAMA_INDEPENDENT_DATA
Expand Down
30 changes: 17 additions & 13 deletions include/llama/View.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ namespace llama
return allocView(Mapping{}, llama::allocator::Stack<sizeOf<DatumDomain>>{});
}

template <typename View>
inline constexpr auto IsView = false;

template <typename Mapping, typename BlobType>
inline constexpr auto IsView<View<Mapping, BlobType>> = true;

template <typename View, typename BoundDatumDomain = DatumCoord<>, bool OwnView = false>
struct VirtualDatum;

Expand All @@ -69,20 +63,16 @@ namespace llama
template <typename View, typename BoundDatumDomain, bool OwnView>
inline constexpr auto is_VirtualDatum<VirtualDatum<View, BoundDatumDomain, OwnView>> = true;

/// Creates a single \ref VirtualDatum owning a view with stack memory.
/// A \ref VirtualDatum that owns and holds a single value.
template <typename DatumDomain>
LLAMA_FN_HOST_ACC_INLINE auto allocVirtualDatumStack()
-> VirtualDatum<decltype(llama::allocViewStack<1, DatumDomain>()), DatumCoord<>, true>
{
return {ArrayDomain<1>{}, llama::allocViewStack<1, DatumDomain>()};
}
using One = VirtualDatum<decltype(allocViewStack<1, DatumDomain>()), DatumCoord<>, true>;

/// Creates a single \ref VirtualDatum owning a view with stack memory and
/// copies all values from an existing \ref VirtualDatum.
template <typename VirtualDatum>
LLAMA_FN_HOST_ACC_INLINE auto copyVirtualDatumStack(const VirtualDatum& vd) -> decltype(auto)
{
auto temp = allocVirtualDatumStack<typename VirtualDatum::AccessibleDatumDomain>();
One<typename VirtualDatum::AccessibleDatumDomain> temp;
temp = vd;
return temp;
}
Expand Down Expand Up @@ -259,6 +249,14 @@ namespace llama
/// AccessibleDatumDomain is the same as `Mapping::DatumDomain`.
using AccessibleDatumDomain = GetType<DatumDomain, BoundDatumDomain>;

LLAMA_FN_HOST_ACC_INLINE VirtualDatum()
/* requires(OwnView) */
: userDomainPos({})
, view{allocViewStack<1, DatumDomain>()}
{
static_assert(OwnView, "The default constructor of VirtualDatum is only available if the ");
}

LLAMA_FN_HOST_ACC_INLINE
VirtualDatum(ArrayDomain userDomainPos, std::conditional_t<OwnView, View&&, View&> view)
: userDomainPos(userDomainPos)
Expand Down Expand Up @@ -619,6 +617,12 @@ namespace llama
}
};

template <typename View>
inline constexpr auto IsView = false;

template <typename Mapping, typename BlobType>
inline constexpr auto IsView<View<Mapping, BlobType>> = true;

/// Acts like a \ref View, but shows only a smaller and/or shifted part of
/// another view it references, the parent view.
template <typename T_ParentViewType>
Expand Down
2 changes: 1 addition & 1 deletion tests/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ TEST_CASE("view.assign-one-datum")
Mapping mapping{arrayDomain};
auto view = allocView(mapping);

auto datum = llama::allocVirtualDatumStack<Particle>();
llama::One<Particle> datum;
datum(tag::Pos{}, tag::X{}) = 14.0f;
datum(tag::Pos{}, tag::Y{}) = 15.0f;
datum(tag::Pos{}, tag::Z{}) = 16.0f;
Expand Down
22 changes: 11 additions & 11 deletions tests/virtualdatum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using Name = llama::DS<

TEST_CASE("VirtualDatum.operator=")
{
auto datum = llama::allocVirtualDatumStack<Name>();
llama::One<Name> datum;

// scalar to multiple elements in virtual datum
datum(tag::Pos{}) = 1;
Expand Down Expand Up @@ -84,7 +84,7 @@ namespace
{
auto allocVc()
{
auto datum = llama::allocVirtualDatumStack<Name>();
llama::One<Name> datum;
datum(tag::Pos{}, tag::A{}) = 1;
datum(tag::Pos{}, tag::Y{}) = 2;
datum(tag::Vel{}, tag::X{}) = 3;
Expand Down Expand Up @@ -267,7 +267,7 @@ using Name2 = llama::DS<

TEST_CASE("VirtualDatum.operator=.propagation")
{
auto datum = llama::allocVirtualDatumStack<Name2>();
llama::One<Name2> datum;

datum(tag::Part1{}) = 1;
datum(tag::Part2{}) = 2;
Expand Down Expand Up @@ -295,8 +295,8 @@ TEST_CASE("VirtualDatum.operator=.propagation")

TEST_CASE("VirtualDatum.operator=.multiview")
{
auto datum1 = llama::allocVirtualDatumStack<Name>();
auto datum2 = llama::allocVirtualDatumStack<Name2>();
llama::One<Name> datum1;
llama::One<Name2> datum2;

datum2 = 1;
datum1 = datum2;
Expand All @@ -318,7 +318,7 @@ TEST_CASE("VirtualDatum.operator=.multiview")

TEST_CASE("VirtualDatum.operator==")
{
auto datum = llama::allocVirtualDatumStack<Name>();
llama::One<Name> datum;

datum = 1;

Expand All @@ -342,7 +342,7 @@ TEST_CASE("VirtualDatum.operator==")

TEST_CASE("VirtualDatum.operator<")
{
auto datum = llama::allocVirtualDatumStack<Name>();
llama::One<Name> datum;

datum = 1;

Expand Down Expand Up @@ -380,14 +380,14 @@ TEST_CASE("VirtualDatum.operator<")
TEST_CASE("VirtualDatum.asTuple.types")
{
{
auto datum = llama::allocVirtualDatumStack<Name>();
llama::One<Name> datum;

std::tuple<int&, int&> pos = datum(tag::Pos{}).asTuple();
std::tuple<int&, int&, int&> vel = datum(tag::Vel{}).asTuple();
std::tuple<int&, int&, int&, int&, int&, int&> name = datum.asTuple();
}
{
const auto datum = llama::allocVirtualDatumStack<Name>();
const llama::One<Name> datum;

std::tuple<const int&, const int&> pos = datum(tag::Pos{}).asTuple();
std::tuple<const int&, const int&, const int&> vel = datum(tag::Vel{}).asTuple();
Expand All @@ -397,7 +397,7 @@ TEST_CASE("VirtualDatum.asTuple.types")

TEST_CASE("VirtualDatum.asTuple.assign")
{
auto datum = llama::allocVirtualDatumStack<Name>();
llama::One<Name> datum;

datum(tag::Pos{}).asTuple() = std::tuple{1, 1};
CHECK(datum(tag::Pos{}, tag::A{}) == 1);
Expand Down Expand Up @@ -426,7 +426,7 @@ TEST_CASE("VirtualDatum.asTuple.assign")

TEST_CASE("VirtualDatum.asTuple.structuredBindings")
{
auto datum = llama::allocVirtualDatumStack<Name>();
llama::One<Name> datum;

{
auto [a, y] = datum(tag::Pos{}).asTuple();
Expand Down

0 comments on commit 4dfd971

Please sign in to comment.