NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
segmentedVector.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoN authors
3#pragma once
4
5#include "NeoN/core/view.hpp"
8
9namespace NeoN
10{
11
24template<typename IndexType>
25IndexType segmentsFromIntervals(const Vector<IndexType>& intervals, Vector<IndexType>& offsets)
26{
27 IndexType finalValue = 0;
28 const auto inSpan = intervals.view();
29 // skip the first element of the offsets
30 // assumed to be zero
31 auto offsSpan = offsets.view().subspan(1);
32 // NOTE avoid compiler warning by static_casting to localIdx since offsSpan
33 // is a std::span
34 NF_ASSERT_EQUAL(inSpan.size(), offsSpan.size());
36 intervals.exec(),
37 {0, offsSpan.size()},
38 KOKKOS_LAMBDA(const localIdx i, IndexType& update, const bool final) {
39 update += inSpan[i];
40 if (final)
41 {
42 // offsSpan is a std::span, thus [] takes unsigned idx
43 offsSpan[i] = update;
44 }
45 },
46 finalValue
47 );
48 return finalValue;
49}
50
56template<typename ValueType, typename IndexType = NeoN::localIdx>
58{
59public:
60
65
70
77 KOKKOS_INLINE_FUNCTION
78 Kokkos::pair<IndexType, IndexType> bounds(localIdx segI) const
79 {
80 return Kokkos::pair<IndexType, IndexType> {segments[segI], segments[segI + 1]};
81 }
82
89 KOKKOS_INLINE_FUNCTION
90 Kokkos::pair<IndexType, IndexType> range(localIdx segI) const
91 {
92 return Kokkos::pair<IndexType, IndexType> {
93 segments[segI], segments[segI + 1] - segments[segI]
94 };
95 }
96
104 KOKKOS_INLINE_FUNCTION View<ValueType> span(localIdx segI) const
105 {
106 auto [start, length] = range(segI);
107 return values.subspan(start, length);
108 }
109
116 KOKKOS_INLINE_FUNCTION
117 IndexType operator[](localIdx i) const { return segments[i]; }
118};
119
126template<typename ValueType, typename IndexType>
128{
129public:
130
131
139 : values_(exec, size), segments_(exec, numSegments + 1)
140 {}
141
142 /*
143 * @brief Create a segmented field from intervals.
144 * @param intervals The intervals to create the segmented field from.
145 * @note The intervals are the lengths of each segment
146 */
148 : values_(intervals.exec(), 0),
149 segments_(intervals.exec(), intervals.size() + 1, IndexType(0))
150 {
151 IndexType valueSize = segmentsFromIntervals(intervals, segments_);
152 values_ = Vector<ValueType>(intervals.exec(), valueSize);
153 }
154
155
162 : values_(values), segments_(segments)
163 {
164 NF_ASSERT(values.exec() == segments.exec(), "Executors are not the same.");
165 }
166
167
172 const Executor& exec() const { return values_.exec(); }
173
178 localIdx size() const { return values_.size(); }
179
184 localIdx numSegments() const { return segments_.size() - 1; }
185
186
192 {
193 return SegmentedVectorView<ValueType, IndexType> {values_.view(), segments_.view()};
194 }
195
196 // ensures no return a span of a temporary object --> invalid memory access
198
203 [[nodiscard]] std::pair<View<ValueType>, View<IndexType>> spans() &
204 {
205 return {values_.view(), segments_.view()};
206 }
207
208 // ensures not to return a span of a temporary object --> invalid memory access
209 [[nodiscard]] std::pair<View<ValueType>, View<IndexType>> spans() && = delete;
210
211 const Vector<ValueType>& values() const { return values_; }
212
213 const Vector<IndexType>& segments() const { return segments_; }
214
215private:
216
217 Vector<ValueType> values_;
218 Vector<IndexType> segments_;
219};
220
221} // namespace NeoN
A class representing a segment of indices.
KOKKOS_INLINE_FUNCTION View< ValueType > span(localIdx segI) const
Get a subspan of values corresponding to a segment.
View< IndexType > segments
A span of indices representing the segments.
KOKKOS_INLINE_FUNCTION Kokkos::pair< IndexType, IndexType > range(localIdx segI) const
Get the range, ie. [start,end), of a segment.
KOKKOS_INLINE_FUNCTION IndexType operator[](localIdx i) const
Access an element of the segments.
View< ValueType > values
A span with the values.
KOKKOS_INLINE_FUNCTION Kokkos::pair< IndexType, IndexType > bounds(localIdx segI) const
Get the bounds of a segment.
Data structure that stores a segmented fields or a vector of vectors.
SegmentedVector(const Vector< ValueType > &values, const Vector< IndexType > &segments)
Constructor to create a segmentedVector from values and the segments.
const Vector< ValueType > & values() const
SegmentedVector(const Vector< IndexType > &intervals)
const Vector< IndexType > & segments() const
SegmentedVectorView< ValueType, IndexType > view() &
get a view of the segmented field
SegmentedVector(const Executor &exec, localIdx size, localIdx numSegments)
Create a segmented field with a given size and number of segments.
localIdx numSegments() const
Get the number of segments in the segmented field.
const Executor & exec() const
Get the executor associated with the segmented field.
std::pair< View< ValueType >, View< IndexType > > spans() &&=delete
std::pair< View< ValueType >, View< IndexType > > spans() &
get the combined value and range spans of the segmented field
SegmentedVectorView< ValueType, IndexType > view() &&=delete
localIdx size() const
Get the size of the segmented field.
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:53
localIdx size() const
Gets the size of the field.
Definition vector.hpp:364
const Executor & exec() const
Gets the executor associated with the field.
Definition vector.hpp:358
View< ValueType > view() &&=delete
#define NF_ASSERT_EQUAL(a, b)
Macro for asserting that two values are equal and printing an error message if they are not.
Definition error.hpp:239
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:142
int32_t localIdx
Definition label.hpp:30
IndexType segmentsFromIntervals(const Vector< IndexType > &intervals, Vector< IndexType > &offsets)
Compute segment offsets from an input field corresponding to lengths by computing a prefix sum.
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16
void parallelScan(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel)