NeoN
A framework for CFD software
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"
9
10namespace NeoN
11{
12
25template<typename IndexType>
26IndexType segmentsFromIntervals(const Vector<IndexType>& intervals, Vector<IndexType>& offsets)
27{
28 IndexType finalValue = 0;
29 const auto inView = intervals.view();
30 // skip the first element of the offsets
31 // assumed to be zero
32 auto offsView = offsets.view().subview(1);
33 // NOTE avoid compiler warning by static_casting to localIdx since offsView
34 // is a View
35 NF_ASSERT_EQUAL(inView.size(), offsView.size());
37 intervals.exec(),
38 {0, offsView.size()},
39 KOKKOS_LAMBDA(const localIdx i, IndexType& update, const bool final) {
40 update += inView[i];
41 if (final)
42 {
43 // offView is a view, thus [] takes unsigned idx
44 offsView[i] = update;
45 }
46 },
47 finalValue
48 );
49 return finalValue;
50}
51
57template<typename ValueType, typename IndexType = NeoN::localIdx>
59{
60public:
61
66
71
78 KOKKOS_INLINE_FUNCTION
79 Kokkos::pair<IndexType, IndexType> bounds(localIdx segI) const
80 {
81 return Kokkos::pair<IndexType, IndexType> {segments[segI], segments[segI + 1]};
82 }
83
90 KOKKOS_INLINE_FUNCTION
91 Kokkos::pair<IndexType, IndexType> range(localIdx segI) const
92 {
93 return Kokkos::pair<IndexType, IndexType> {
94 segments[segI], segments[segI + 1] - segments[segI]
95 };
96 }
97
105 KOKKOS_INLINE_FUNCTION View<ValueType> view(localIdx segI) const
106 {
107 auto [start, length] = range(segI);
108 return values.subview(start, length);
109 }
110
117 KOKKOS_INLINE_FUNCTION
118 IndexType operator[](localIdx i) const { return segments[i]; }
119};
120
127template<typename ValueType, typename IndexType>
129{
130public:
131
132
140 : values_(exec, size), segments_(exec, numSegments + 1)
141 {}
142
143 /*
144 * @brief Create a segmented vector from intervals.
145 * @param intervals The intervals to create the segmented vector from.
146 * @note The intervals are the lengths of each segment
147 */
149 : values_(intervals.exec(), 0),
150 segments_(intervals.exec(), intervals.size() + 1, IndexType(0))
151 {
152 IndexType valueSize = segmentsFromIntervals(intervals, segments_);
153 values_ = Vector<ValueType>(intervals.exec(), valueSize, ValueType(0));
154 }
155
156
163 : values_(values), segments_(segments)
164 {
165 NF_ASSERT(values.exec() == segments.exec(), "Executors are not the same.");
166 }
167
168
173 const Executor& exec() const { return values_.exec(); }
174
179 localIdx size() const { return values_.size(); }
180
185 localIdx numSegments() const { return segments_.size() - 1; }
186
192 {
193 SegmentedVector<ValueType, IndexType> result(values_.copyToHost(), segments_.copyToHost());
194 return result;
195 }
196
197
203 {
204 return SegmentedVectorView<ValueType, IndexType> {values_.view(), segments_.view()};
205 }
206
207 // ensures no return a view of a temporary object --> invalid memory access
209
214 [[nodiscard]] std::pair<View<ValueType>, View<IndexType>> views() &
215 {
216 return {values_.view(), segments_.view()};
217 }
218
219
220 // ensures not to return a view of a temporary object --> invalid memory access
221 [[nodiscard]] std::pair<View<ValueType>, View<IndexType>> views() && = delete;
222
223 const Vector<ValueType>& values() const { return values_; }
224
225 const Vector<IndexType>& segments() const { return segments_; }
226
227private:
228
229 Vector<ValueType> values_;
230 Vector<IndexType> segments_;
231};
232
233} // namespace NeoN
A class representing a segment of indices.
View< IndexType > segments
A View of indices representing the segments.
KOKKOS_INLINE_FUNCTION View< ValueType > view(localIdx segI) const
Get a subview of values corresponding to a segment.
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 View 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)
std::pair< View< ValueType >, View< IndexType > > views() &
get the combined value and range views of the segmented vector
const Vector< IndexType > & segments() const
SegmentedVector< ValueType, IndexType > copyToHost() const
Returns a copy of the segmentedVector on the host.
std::pair< View< ValueType >, View< IndexType > > views() &&=delete
SegmentedVectorView< ValueType, IndexType > view() &
get a view of the segmented vector
SegmentedVector(const Executor &exec, localIdx size, localIdx numSegments)
Create a segmented vector with a given size and number of segments.
localIdx numSegments() const
Get the number of segments in the segmented vector.
const Executor & exec() const
Get the executor associated with the segmented vector.
SegmentedVectorView< ValueType, IndexType > view() &&=delete
localIdx size() const
Get the size of the segmented vector.
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:28
localIdx size() const
Gets the size of the field.
Definition vector.hpp:233
const Executor & exec() const
Gets the executor associated with the field.
Definition vector.hpp:227
View< ValueType > view() &&=delete
Vector< ValueType > copyToHost() const
Returns a copy of the field back to the host.
#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
Definition array.hpp:19
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)