NeoN
A framework for CFD software
Loading...
Searching...
No Matches
segmentedVector.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include "NeoN/core/view.hpp"
11
12namespace NeoN
13{
14
27template<typename IndexType>
28IndexType segmentsFromIntervals(const Vector<IndexType>& intervals, Vector<IndexType>& offsets)
29{
30 IndexType finalValue = 0;
31 const auto inView = intervals.view();
32 // skip the first element of the offsets
33 // assumed to be zero
34 auto offsView = offsets.view().subview(1);
35 // NOTE avoid compiler warning by static_casting to localIdx since offsView
36 // is a View
37 NF_ASSERT_EQUAL(inView.size(), offsView.size());
39 intervals.exec(),
40 {0, offsView.size()},
41 KOKKOS_LAMBDA(const localIdx i, IndexType& update, const bool final) {
42 update += inView[i];
43 if (final)
44 {
45 // offView is a view, thus [] takes unsigned idx
46 offsView[i] = update;
47 }
48 },
49 finalValue
50 );
51 return finalValue;
52}
53
59template<typename ValueType, typename IndexType = NeoN::localIdx>
61{
62public:
63
68
73
80 KOKKOS_INLINE_FUNCTION
81 Kokkos::pair<IndexType, IndexType> bounds(localIdx segI) const
82 {
83 return Kokkos::pair<IndexType, IndexType> {segments[segI], segments[segI + 1]};
84 }
85
92 KOKKOS_INLINE_FUNCTION
93 Kokkos::pair<IndexType, IndexType> range(localIdx segI) const
94 {
95 return Kokkos::pair<IndexType, IndexType> {
96 segments[segI], segments[segI + 1] - segments[segI]
97 };
98 }
99
107 KOKKOS_INLINE_FUNCTION View<ValueType> view(localIdx segI) const
108 {
109 auto [start, length] = range(segI);
110 return values.subview(start, length);
111 }
112
119 KOKKOS_INLINE_FUNCTION
120 IndexType operator[](localIdx i) const { return segments[i]; }
121};
122
129template<typename ValueType, typename IndexType>
131{
132public:
133
134
142 : values_(exec, size), segments_(exec, numSegments + 1)
143 {}
144
145 /*
146 * @brief Create a segmented vector from intervals.
147 * @param intervals The intervals to create the segmented vector from.
148 * @note The intervals are the lengths of each segment
149 */
151 : values_(intervals.exec(), 0),
152 segments_(intervals.exec(), intervals.size() + 1, IndexType(0))
153 {
154 IndexType valueSize = segmentsFromIntervals(intervals, segments_);
155 values_ = Vector<ValueType>(intervals.exec(), valueSize, ValueType(0));
156 }
157
158
165 : values_(values), segments_(segments)
166 {
167 NF_ASSERT(values.exec() == segments.exec(), "Executors are not the same.");
168 }
169
170
175 const Executor& exec() const { return values_.exec(); }
176
181 localIdx size() const { return values_.size(); }
182
187 localIdx numSegments() const { return segments_.size() - 1; }
188
194 {
195 SegmentedVector<ValueType, IndexType> result(values_.copyToHost(), segments_.copyToHost());
196 return result;
197 }
198
199
205 {
206 return SegmentedVectorView<ValueType, IndexType> {values_.view(), segments_.view()};
207 }
208
209 // ensures no return a view of a temporary object --> invalid memory access
211
216 [[nodiscard]] std::pair<View<ValueType>, View<IndexType>> views() &
217 {
218 return {values_.view(), segments_.view()};
219 }
220
221
222 // ensures not to return a view of a temporary object --> invalid memory access
223 [[nodiscard]] std::pair<View<ValueType>, View<IndexType>> views() && = delete;
224
225 const Vector<ValueType>& values() const { return values_; }
226
227 const Vector<IndexType>& segments() const { return segments_; }
228
229private:
230
231 Vector<ValueType> values_;
232 Vector<IndexType> segments_;
233};
234
235} // 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:30
localIdx size() const
Gets the size of the field.
Definition vector.hpp:235
const Executor & exec() const
Gets the executor associated with the field.
Definition vector.hpp:229
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:241
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:144
Definition array.hpp:20
int32_t localIdx
Definition label.hpp:32
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:18
void parallelScan(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel)