Argon 0.1.0
Loading...
Searching...
No Matches
interleaved.hpp
1#pragma once
2#include <cstddef>
3#include <cstdint>
4#include <iterator>
5#include <ranges>
6#include <span>
7#include "argon.hpp"
9#include "arm_simd/helpers/vec128.hpp"
10
11#ifdef __ARM_FEATURE_MVE
12#define simd mve
13#else
14#define simd neon
15#endif
16
17namespace argon::vectorize {
18
19template <size_t Stride, typename ScalarType>
20struct interleaved : public std::ranges::view_interface<interleaved<Stride, ScalarType>> {
21 using intrinsic_type = simd::Vec128_t<ScalarType>;
22 static constexpr size_t lanes = sizeof(intrinsic_type) / sizeof(ScalarType);
23 // Round down to a whole number of iterations; each iteration spans lanes*Stride elements.
24 static constexpr size_t vectorizeable_size(size_t size) { return size - (size % (lanes * Stride)); }
25
26 public:
27 struct Iterator {
28 using iterator_category = std::forward_iterator_tag;
29 using argon_type = Argon<ScalarType>;
30 using value_type = std::array<argon_type, Stride>;
31 using difference_type = std::ptrdiff_t;
32
33 Iterator() = default;
34 Iterator(ScalarType* ptr) : ptr{ptr} {}
35
36 value_type& operator*() {
37 load();
38 dirty_ = true;
39 return vec;
40 }
41 value_type* operator->() { return &**this; }
42 const value_type& operator*() const {
43 load();
44 return vec;
45 }
46 const value_type* operator->() const { return &**this; }
47 Iterator& operator++() {
48 if (dirty_) {
49 argon::store_interleaved(ptr, vec); // store before increment
50 }
51 ptr += lanes * Stride;
52 loaded_ = dirty_ = false; // load lazily: the end position is never read
53 return *this;
54 }
55
56 Iterator operator++(int) {
57 Iterator tmp = *this;
58 ++(*this);
59 return tmp;
60 }
61
62 friend bool operator==(const Iterator& a, const Iterator& b) { return a.ptr == b.ptr; }
63 friend bool operator==(const Iterator& a, const ScalarType* ptr) { return a.ptr == ptr; }
64 friend bool operator!=(const Iterator& a, const Iterator& b) { return a.ptr != b.ptr; }
65 friend bool operator!=(const Iterator& a, const ScalarType* ptr) { return a.ptr != ptr; }
66
67 private:
68 void load() const {
69 if (!loaded_) {
70 vec = argon_type::template LoadInterleaved<Stride>(ptr);
71 loaded_ = true;
72 }
73 }
74
75 ScalarType* ptr = nullptr;
76 mutable value_type vec;
77 mutable bool loaded_ = false;
78 bool dirty_ = false;
79 };
80 static_assert(std::input_or_output_iterator<Iterator>);
81 struct ConstIterator {
82 using iterator_category = std::forward_iterator_tag;
83 using argon_type = Argon<ScalarType>;
84 using value_type = std::array<argon_type, Stride>;
85 using difference_type = std::ptrdiff_t;
86
87 ConstIterator() = default;
88 ConstIterator(const ScalarType* ptr) : ptr{ptr} {}
89
90 const value_type operator*() const { return argon_type::template LoadInterleaved<Stride>(ptr); }
91 ConstIterator& operator++() {
92 ptr += lanes * Stride;
93 return *this;
94 }
95 ConstIterator operator++(int) {
96 ConstIterator tmp = *this;
97 ++(*this);
98 return tmp;
99 }
100 friend bool operator==(const ConstIterator& a, const ConstIterator& b) { return a.ptr == b.ptr; }
101 friend bool operator!=(const ConstIterator& a, const ConstIterator& b) { return a.ptr != b.ptr; }
102 friend bool operator==(const ConstIterator& a, const ScalarType* ptr) { return a.ptr == ptr; }
103
104 private:
105 const ScalarType* ptr = nullptr;
106 };
107 static_assert(std::input_iterator<ConstIterator>);
108
109 using iterator = Iterator;
110 using const_iterator = ConstIterator;
111
112 interleaved(ScalarType* start, ScalarType* end)
113 : start_{start}, size_{vectorizeable_size(end - start)}, count_{static_cast<size_t>(end - start)} {};
114 interleaved(ScalarType* start, const size_t size) : start_{start}, size_{vectorizeable_size(size)}, count_{size} {};
115 interleaved(const std::span<ScalarType> span)
116 : start_{span.data()}, size_{vectorizeable_size(span.size())}, count_{span.size()} {};
117
118 iterator begin() const { return Iterator(start_); }
119 const ScalarType* end() const { return start_ + size_; }
120 const_iterator cbegin() const { return ConstIterator(start_); }
121 const ScalarType* cend() const { return start_ + size_; }
123 size_t size() const { return size_ / (lanes * Stride); }
124
129 load_store_tail<ScalarType, Stride> with_tail() const { return {start_, count_ / Stride}; }
130
131 private:
132 ScalarType* start_;
133 size_t size_;
134 size_t count_;
135};
136
137// template <size_t stride, std::ranges::contiguous_range R>
138// interleaved(R&& r) -> interleaved<stride, std::ranges::range_value_t<R>>;
139
140// MSVC's ranges implementation does not accept this view as a std::ranges::range
141// (it works on GCC and Clang). TODO: revisit MSVC support for the interleaved
142// read-modify-write view; until then, skip the concept checks there.
143#if !defined(_MSC_VER) || defined(__clang__)
144static_assert(std::ranges::range<interleaved<3, int32_t>>);
145static_assert(std::ranges::view<interleaved<3, int32_t>>);
146static_assert(std::movable<interleaved<3, int32_t>>);
147static_assert(std::ranges::viewable_range<interleaved<3, int32_t>>);
148#endif
149
150} // namespace argon::vectorize
151
152#undef simd
Definition argon_full.hpp:44
In-place read/write view of a range as vectors, including a final partial vector.
Definition tail.hpp:275
ace void store_interleaved(scalar_type *ptr, simd::MultiVector_t< intrinsic_type, stride > multi_vec)
Store a multi-vector to a location in memory with interleaving.
Definition store.hpp:32
Definition interleaved.hpp:27
Definition interleaved.hpp:20
load_store_tail< ScalarType, Stride > with_tail() const
A view of the same range that also visits the final, partial group of vectors.
Definition interleaved.hpp:129
size_t size() const
The number of iterations (groups of Stride vectors).
Definition interleaved.hpp:123
Vectorized views that also visit the final, partial vector of a range (see with_tail() on the views).