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

Add Iterator to make the STL available to 1D Views #158

Merged
merged 1 commit into from
Mar 3, 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
102 changes: 102 additions & 0 deletions include/llama/Iterator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "View.hpp"

#include <boost/iterator/iterator_facade.hpp>
//#include <boost/stl_interfaces/iterator_interface.hpp>

namespace llama
{
// requires Boost 1.74 which is quite new
#if 0
template <typename View>
struct Iterator
: boost::stl_interfaces::proxy_iterator_interface<
Iterator<View>,
std::random_access_iterator_tag,
decltype(std::declval<View>()(ArrayDomain<1>{}))>
{
constexpr decltype(auto) operator*() const
{
return (*view)(coord);
}

constexpr auto operator+=(std::ptrdiff_t n) -> Iterator&
{
coord[0] += n;
return *this;
}

friend constexpr auto operator-(const Iterator& a, const Iterator& b)
{
return a.coord[0] - b.coord[0];
}

friend constexpr bool operator==(const Iterator& a, const Iterator& b)
{
return a.coord == b.coord;
}

ArrayDomain<1> coord;
View* view;
};
#endif

template <typename View>
struct Iterator
: boost::iterators::iterator_facade<
Iterator<View>,
typename View::VirtualDatumType,
std::random_access_iterator_tag,
typename View::VirtualDatumType,
std::ptrdiff_t>
{
constexpr decltype(auto) dereference() const
{
return (*view)(coord);
}

constexpr bool equal(const Iterator& other) const
{
return coord == other.coord;
}

constexpr auto increment() -> Iterator&
{
coord[0]++;
return *this;
}

constexpr auto decrement() -> Iterator&
{
coord[0]--;
return *this;
}

constexpr auto advance(std::ptrdiff_t n) -> Iterator&
{
coord[0] += n;
return *this;
}

constexpr auto distance_to(const Iterator& other) const -> std::ptrdiff_t
{
return static_cast<std::ptrdiff_t>(other.coord[0]) - static_cast<std::ptrdiff_t>(coord[0]);
}

ArrayDomain<1> coord;
View* view;
};

template <typename View>
auto begin(View& view) -> Iterator<View>
{
static_assert(View::ArrayDomain::rank == 1, "Iterators for non-1D views are not implemented");
return {{}, ArrayDomain<1>{}, &view};
}

template <typename View>
auto end(View& view) -> Iterator<View>
{
static_assert(View::ArrayDomain::rank == 1, "Iterators for non-1D views are not implemented");
return {{}, view.mapping.arrayDomainSize, &view};
}
} // namespace llama
1 change: 1 addition & 0 deletions include/llama/llama.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "Allocators.hpp"
#include "ArrayDomainRange.hpp"
#include "Core.hpp"
#include "Iterator.hpp"
#include "View.hpp"
#include "macros.hpp"
#include "mapping/AoS.hpp"
Expand Down
60 changes: 60 additions & 0 deletions tests/iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <algorithm>
#include <catch2/catch.hpp>
#include <llama/llama.hpp>
#include <numeric>

// clang-format off
namespace tag {
struct X {};
struct Y {};
struct Z {};
}

using Position = llama::DS<
llama::DE<tag::X, int>,
llama::DE<tag::Y, int>,
llama::DE<tag::Z, int>
>;
// clang-format on

TEST_CASE("iterator")
{
using ArrayDomain = llama::ArrayDomain<1>;
constexpr auto arrayDomain = ArrayDomain{32};
constexpr auto mapping = llama::mapping::AoS<ArrayDomain, Position>{arrayDomain};
auto view = llama::allocView(mapping);

for (auto vd : view)
{
vd(tag::X{}) = 1;
vd(tag::Y{}) = 2;
vd(tag::Z{}) = 3;
}
std::transform(begin(view), end(view), begin(view), [](auto vd) { return vd * 2; });
const int sumY = std::accumulate(begin(view), end(view), 0, [](int acc, auto vd) { return acc + vd(tag::Y{}); });
CHECK(sumY == 128);
}

TEST_CASE("iterator.std_copy")
{
using ArrayDomain = llama::ArrayDomain<1>;
constexpr auto arrayDomain = ArrayDomain{32};
auto aosView = llama::allocView(llama::mapping::AoS<ArrayDomain, Position>{arrayDomain});
auto soaView = llama::allocView(llama::mapping::SoA<ArrayDomain, Position>{arrayDomain});

int i = 0;
for (auto vd : aosView)
{
vd(tag::X{}) = ++i;
vd(tag::Y{}) = ++i;
vd(tag::Z{}) = ++i;
}
std::copy(begin(aosView), end(aosView), begin(soaView));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this line work is super crazy! This means we can use std::copy to copy between ANY LLAMA layouts. This will always do an element-wise copy, which is sometimes suboptimal. But it correctly shuffles the layouts through the iterators and virtual datums.

i = 0;
for (auto vd : soaView)
{
CHECK(vd(tag::X{}) == ++i);
CHECK(vd(tag::Y{}) == ++i);
CHECK(vd(tag::Z{}) == ++i);
}
}