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

Extend ArrayDomainIndexIterator to support random access and std::ranges #199

Merged
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (BUILD_TESTING)
source_group(TREE "${CMAKE_CURRENT_LIST_DIR}/tests" FILES ${testSources})
target_compile_features(tests PRIVATE cxx_std_20)
if (MSVC)
target_compile_options(tests PRIVATE /permissive-)
target_compile_options(tests PRIVATE /permissive- /constexpr:steps10000000)
endif()
target_link_libraries(tests PRIVATE Catch2::Catch2 llama::llama)

Expand Down
175 changes: 158 additions & 17 deletions include/llama/ArrayDomainRange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

#include "Core.hpp"

#include <algorithm>
#include <iterator>
#if CAN_USE_RANGES
# include <ranges>
#endif

namespace llama
{
Expand All @@ -13,50 +17,151 @@ namespace llama
using value_type = ArrayDomain<Dim>;
using difference_type = std::ptrdiff_t;
using reference = value_type;
using pointer = value_type*;
using iterator_category = std::forward_iterator_tag;
using pointer = internal::IndirectValue<value_type>;
using iterator_category = std::random_access_iterator_tag;

constexpr ArrayDomainIndexIterator() noexcept = default;

constexpr ArrayDomainIndexIterator(ArrayDomain<Dim> size, ArrayDomain<Dim> current) noexcept
: size(size)
: lastIndex([size]() mutable {
for (auto i = 0; i < Dim; i++)
size[i]--;
return size;
}())
, current(current)
{
}

constexpr ArrayDomainIndexIterator(const ArrayDomainIndexIterator&) noexcept = default;
constexpr ArrayDomainIndexIterator(ArrayDomainIndexIterator&&) noexcept = default;
constexpr auto operator=(const ArrayDomainIndexIterator&) noexcept -> ArrayDomainIndexIterator& = default;
constexpr auto operator=(ArrayDomainIndexIterator&&) noexcept -> ArrayDomainIndexIterator& = default;

constexpr auto operator*() const noexcept -> ArrayDomain<Dim>
constexpr auto operator*() const noexcept -> value_type
{
return current;
}

constexpr auto operator->() const noexcept -> pointer
{
return internal::IndirectValue{**this};
}

constexpr auto operator++() noexcept -> ArrayDomainIndexIterator&
{
for (auto i = (int) Dim - 1; i >= 0; i--)
{
current[i]++;
if (current[i] != size[i])
if (current[i] < lastIndex[i])
{
current[i]++;
return *this;
}
current[i] = 0;
}
// we reached the end, the iterator now needs to compare equal to a value initialized one
current = {};
size = {};
current[0] = lastIndex[0] + 1;
return *this;
}

constexpr auto operator++(int) noexcept -> ArrayDomainIndexIterator
{
auto tmp = *this;
++*this;
return tmp;
}

constexpr auto operator--() noexcept -> ArrayDomainIndexIterator&
{
for (auto i = (int) Dim - 1; i >= 0; i--)
{
if (current[i] > 0)
{
current[i]--;
return *this;
}
current[i] = lastIndex[i];
}
// decrementing beyond [0, 0, ..., 0] is UB
return *this;
}

constexpr auto operator++(int) noexcept -> ArrayDomainIndexIterator&
constexpr auto operator--(int) noexcept -> ArrayDomainIndexIterator
{
auto tmp = *this;
--*this;
return tmp;
}

constexpr auto operator[](difference_type i) const noexcept -> reference
{
return *(*this + i);
}

constexpr auto operator+=(difference_type n) noexcept -> ArrayDomainIndexIterator&
{
for (auto i = (int) Dim - 1; i >= 0 && n != 0; i--)
{
n += static_cast<difference_type>(current[i]);
const auto size = static_cast<difference_type>(lastIndex[i]) + 1;
auto mod = n % size;
n /= size;
if (mod < 0)
{
mod += size;
n--;
}
current[i] = mod;
}
assert(n == 0);
return *this;
}

friend constexpr auto operator+(ArrayDomainIndexIterator it, difference_type n) noexcept
-> ArrayDomainIndexIterator
{
it += n;
return it;
}

friend constexpr auto operator+(difference_type n, ArrayDomainIndexIterator it) noexcept
-> ArrayDomainIndexIterator
{
return it + n;
}

constexpr auto operator-=(difference_type n) noexcept -> ArrayDomainIndexIterator&
{
return operator+=(-n);
}

friend constexpr auto operator-(ArrayDomainIndexIterator it, difference_type n) noexcept
-> ArrayDomainIndexIterator
{
return ++*this;
it -= n;
return it;
}

friend constexpr auto operator-(const ArrayDomainIndexIterator& a, const ArrayDomainIndexIterator& b) noexcept
-> difference_type
{
assert(a.lastIndex == b.lastIndex);

difference_type n = a.current[Dim - 1] - b.current[Dim - 1];
difference_type size = a.lastIndex[Dim - 1] + 1;
for (auto i = (int) Dim - 2; i >= 0; i--)
{
n += (a.current[i] - b.current[i]) * size;
size *= a.lastIndex[i] + 1;
}

return n;
}

friend constexpr auto operator==(
const ArrayDomainIndexIterator<Dim>& a,
const ArrayDomainIndexIterator<Dim>& b) noexcept -> bool
{
return a.size == b.size && a.current == b.current;
assert(a.lastIndex == b.lastIndex);
return a.current == b.current;
}

friend constexpr auto operator!=(
Expand All @@ -66,27 +171,63 @@ namespace llama
return !(a == b);
}

friend constexpr auto operator<(const ArrayDomainIndexIterator& a, const ArrayDomainIndexIterator& b) noexcept
-> bool
{
assert(a.lastIndex == b.lastIndex);
return std::lexicographical_compare(
std::begin(a.current),
std::end(a.current),
std::begin(b.current),
std::end(b.current));
}

friend constexpr auto operator>(const ArrayDomainIndexIterator& a, const ArrayDomainIndexIterator& b) noexcept
-> bool
{
return b < a;
}

friend constexpr auto operator<=(const ArrayDomainIndexIterator& a, const ArrayDomainIndexIterator& b) noexcept
-> bool
{
return !(a > b);
}

friend constexpr auto operator>=(const ArrayDomainIndexIterator& a, const ArrayDomainIndexIterator& b) noexcept
-> bool
{
return !(a < b);
}

private:
ArrayDomain<Dim> size;
ArrayDomain<Dim> lastIndex;
ArrayDomain<Dim> current;
};

/// Range allowing to iterate over all indices in a \ref ArrayDomain.
template <std::size_t Dim>
struct ArrayDomainIndexRange
#if CAN_USE_RANGES
: std::ranges::view_base
#endif
{
constexpr ArrayDomainIndexRange(ArrayDomain<Dim> size) : size(size)
constexpr ArrayDomainIndexRange() noexcept = default;

constexpr ArrayDomainIndexRange(ArrayDomain<Dim> size) noexcept : size(size)
{
}

constexpr auto begin() const -> ArrayDomainIndexIterator<Dim>
constexpr auto begin() const noexcept -> ArrayDomainIndexIterator<Dim>
{
return {size, ArrayDomain<Dim>{}};
}

constexpr auto end() const -> ArrayDomainIndexIterator<Dim>
constexpr auto end() const noexcept -> ArrayDomainIndexIterator<Dim>
{
return {};
auto endPos = ArrayDomain<Dim>{};
endPos[0] = size[0];
return {size, endPos};
}

private:
Expand Down
19 changes: 19 additions & 0 deletions include/llama/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,23 @@ namespace llama
std::forward<Func>(func)(ArrayDomain<sizeof...(outerIndices) + 1>{outerIndices..., i});
}
}

namespace internal
{
template <typename T>
struct IndirectValue
{
T value;

auto operator->() -> T*
{
return &value;
}

auto operator->() const -> const T*
{
return &value;
}
};
} // namespace internal
} // namespace llama
19 changes: 0 additions & 19 deletions include/llama/View.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,25 +720,6 @@ namespace llama
};
#endif

namespace internal
{
template <typename T>
struct IndirectValue
{
T value;

auto operator->() -> T*
{
return &value;
}

auto operator->() const -> const T*
{
return &value;
}
};
} // namespace internal

// Currently, only 1D iterators are supported, becaues higher dimensional iterators are difficult if we also
// want to preserve good codegen. Multiple nested loops seem to be superior to a single iterator over multiple
// dimensions. At least compilers are able to produce better code. std::mdspan also discovered similar
Expand Down
6 changes: 6 additions & 0 deletions include/llama/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@
/// Forces a copy of a value. This is useful to prevent ODR usage of constants
/// when compiling for GPU targets.
#define LLAMA_COPY(x) decltype(x)(x)

// TODO: clang 10 and 11 fail to compile this currently with the issue described here:
// https://stackoverflow.com/questions/64300832/why-does-clang-think-gccs-subrange-does-not-satisfy-gccs-ranges-begin-functi
// let's try again with clang 12
// Intel LLVM compiler is also using the clang frontend
#define CAN_USE_RANGES (__has_include(<ranges>) && defined(__cpp_concepts) && !defined(__clang__) && !defined(__INTEL_LLVM_COMPILER))
Loading