Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow assigning values or virtual records to a One on construction #256

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/alpaka/asyncblur/asyncblur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ struct BlurKernel
LLAMA_INDEPENDENT_DATA
for (auto x = start[1]; x < end[1]; ++x)
{
llama::One<PixelOnAcc> sum;
sum = 0;
llama::One<PixelOnAcc> sum = 0;

using ItType = std::int64_t;
const ItType iBStart = SHARED ? ItType(y) - ItType(bi[0] * ElemsPerBlock) : y;
Expand Down
6 changes: 2 additions & 4 deletions examples/cuda/nbody/nbody.cu
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ __global__ void updateSM(View particles)
const auto ti = threadIdx.x + blockIdx.x * blockDim.x;
const auto tbi = blockIdx.x;

llama::One<Particle> pi;
pi = particles(ti);
llama::One<Particle> pi = particles(ti);
for (std::size_t blockOffset = 0; blockOffset < PROBLEM_SIZE; blockOffset += SHARED_ELEMENTS_PER_BLOCK)
{
LLAMA_INDEPENDENT_DATA
Expand All @@ -132,8 +131,7 @@ __global__ void update(View particles)
{
const auto ti = threadIdx.x + blockIdx.x * blockDim.x;

llama::One<Particle> pi;
pi = particles(ti);
llama::One<Particle> pi = particles(ti);
LLAMA_INDEPENDENT_DATA
for (auto j = std::size_t{0}; j < PROBLEM_SIZE; ++j)
pPInteraction(pi, particles(j));
Expand Down
3 changes: 1 addition & 2 deletions examples/nbody/nbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ namespace usellama
LLAMA_INDEPENDENT_DATA
for (std::size_t i = 0; i < PROBLEM_SIZE; i++)
{
llama::One<Particle> pi;
pi = particles(i);
llama::One<Particle> pi = particles(i);
for (std::size_t j = 0; j < PROBLEM_SIZE; ++j)
pPInteraction(pi, particles(j));
particles(i)(tag::Vel{}) = pi(tag::Vel{});
Expand Down
17 changes: 16 additions & 1 deletion include/llama/VirtualRecord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,13 @@ namespace llama
/// AccessibleRecordDim is the same as `Mapping::RecordDim`.
using AccessibleRecordDim = GetType<RecordDim, BoundRecordCoord>;

/// Creates an empty VirtualRecord. Only available for if the view is owned. Used by llama::One.
LLAMA_FN_HOST_ACC_INLINE VirtualRecord()
/* requires(OwnView) */
: arrayDimsCoord({})
, view{allocViewStack<1, RecordDim>()}
{
static_assert(OwnView, "The default constructor of VirtualRecord is only available if the ");
static_assert(OwnView, "The default constructor of VirtualRecord is only available if it owns the view.");
}

LLAMA_FN_HOST_ACC_INLINE
Expand All @@ -327,6 +328,20 @@ namespace llama
VirtualRecord(const VirtualRecord&) = default;
VirtualRecord(VirtualRecord&&) = default;

/// Create a VirtuaRecord from a single value or a different VirtualRecord. Only available for if the view is
/// owned. Used by llama::One.
template <typename T>
LLAMA_FN_HOST_ACC_INLINE VirtualRecord(const T& other)
/* requires(OwnView) */
: VirtualRecord()
{
static_assert(
OwnView,
"The copy constructor of VirtualRecord from a different VirtualRecord is only available if it owns the "
"view.");
*this = other;
}

/// Access a record in the record dimension underneath the current virtual
/// record using a \ref RecordCoord. If the access resolves to a leaf, a
/// reference to a variable inside the \ref View storage is returned,
Expand Down
19 changes: 19 additions & 0 deletions tests/virtualrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,3 +890,22 @@ TEST_CASE("VirtualRecord.loadAs.constref")
CHECK(pos.y == 1);
}
}

TEST_CASE("VirtualRecord.One_ctor_from_view")
{
auto view = llama::allocView(llama::mapping::AoS{llama::ArrayDims{5}, Name{}});
view(1u) = 1;

llama::One<Name> vr2 = view(1u);
llama::One<Name> vr3(view(1u));
llama::One<Name> vr4{view(1u)};

vr2 = 2;
vr3 = 3;
vr4 = 4;

CHECK(vr2 == 2);
CHECK(vr3 == 3);
CHECK(vr4 == 4);
CHECK(view(1u) == 1);
}