NeoN
A framework for CFD software
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include "NeoN/core/error.hpp"
11#include "NeoN/core/view.hpp"
12
13#include <vector>
14
15
16namespace NeoN
17{
18
25template<typename ValueType>
26class Vector
27{
28
29public:
30
31 using VectorValueType = ValueType;
32
39
48 const Executor& exec,
49 const ValueType* in,
51 Executor hostExec = SerialExecutor()
52 );
53
60 Vector(const Executor& exec, localIdx size, ValueType value);
61
67 Vector(const Executor& exec, std::vector<ValueType> in);
68
75
81
86 Vector(Vector<ValueType>&& rhs) noexcept;
87
92
98 template<typename func>
99 void apply(func f)
100 {
101 map(*this, f);
102 }
103
109 [[nodiscard]] Vector<ValueType> copyToExecutor(Executor dstExec) const;
110
115 [[nodiscard]] Vector<ValueType> copyToHost() const;
116
126
127 // ensures no return of device address on host --> invalid memory access
128 ValueType& operator[](const localIdx i) = delete;
129
130 // ensures no return of device address on host --> invalid memory access
131 const ValueType& operator[](const localIdx i) const = delete;
132
137 void operator=(const ValueType& rhs);
138
145 void operator=(const Vector<ValueType>& rhs);
146
158
165
172
182 requires requires(ValueType a, ValueType b) { a* b; };
183
192 [[nodiscard]] Vector<ValueType> operator*(const scalar rhs)
193 requires requires(ValueType a, scalar b) { a* b; };
194
204 requires requires(ValueType a, ValueType b) { a *= b; };
205
214 requires requires(ValueType a, scalar b) { a *= b; };
215
220 void resize(const localIdx size);
221
226 [[nodiscard]] ValueType* data() { return data_; }
227
232 [[nodiscard]] const ValueType* data() const { return data_; }
233
238 [[nodiscard]] ValueType* begin() { return data_; }
239
244 [[nodiscard]] const ValueType* begin() const { return data_; }
245
250 [[nodiscard]] ValueType* end() { return data_ + size(); }
251
256 [[nodiscard]] const ValueType* end() const { return data_ + size(); }
257
262 [[nodiscard]] const Executor& exec() const { return exec_; }
263
268 [[nodiscard]] localIdx size() const { return size_; }
269
274 [[nodiscard]] label ssize() const { return static_cast<label>(size_); }
275
280 [[nodiscard]] bool empty() const { return size() == 0; }
281
282 // return of a temporary --> invalid memory access
283 View<ValueType> view() && = delete;
284
285 // return of a temporary --> invalid memory access
286 View<const ValueType> view() const&& = delete;
287
292 [[nodiscard]] View<ValueType> view() &
293 {
294 return View<ValueType>(data_, static_cast<size_t>(size_));
295 }
296
301 [[nodiscard]] View<const ValueType> view() const&
302 {
303 return View<const ValueType>(data_, static_cast<size_t>(size_));
304 }
305
306 // return of a temporary --> invalid memory access
307 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) && = delete;
308
309 // return of a temporary --> invalid memory access
310 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&& = delete;
311
316 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) &
317 {
318 return View<ValueType>(
319 data_ + range.first, static_cast<size_t>(range.second - range.first)
320 );
321 }
322
327 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&
328 {
330 data_ + range.first, static_cast<size_t>(range.second - range.first)
331 );
332 }
333
338 [[nodiscard]] std::pair<localIdx, localIdx> range() const { return {0, size()}; }
339
340private:
341
342 localIdx size_ {0};
343 ValueType* data_ {nullptr};
344 const Executor exec_;
345
350 void validateOtherVector(const Vector<ValueType>& rhs) const;
351};
352
359template<typename ValueType>
361
368template<typename ValueType>
370
371} // namespace NeoN
Reference executor for serial CPU execution.
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:27
Vector(const Executor &exec, const ValueType *in, localIdx size, Executor hostExec=SerialExecutor())
Create a Vector with a given size from existing memory on an executor.
Vector(Vector< ValueType > &&rhs) noexcept
Move constructor, moves the data from the parsed field to the new field.
ValueType & operator[](const localIdx i)=delete
Vector(const Vector< ValueType > &rhs)
Copy constructor, creates a new field with the same size and data as the parsed field.
const ValueType * begin() const
Direct access to the underlying field data.
Definition vector.hpp:244
localIdx size() const
Gets the size of the field.
Definition vector.hpp:268
Vector< ValueType > copyToExecutor(Executor dstExec) const
Copies the data to a new field on a specific executor.
Vector< ValueType > operator*(const Vector< ValueType > &rhs)
Arithmetic multiply operator, multiply by a second field.
const ValueType * end() const
Direct access to the underlying field data.
Definition vector.hpp:256
Vector(const Executor &exec, const Vector< ValueType > &in)
Create a Vector as a copy of a Vector on a specified executor.
View< ValueType > view(std::pair< localIdx, localIdx > range) &
Gets a sub view of the field as a view.
Definition vector.hpp:316
ValueType * data()
Direct access to the underlying field data.
Definition vector.hpp:226
Vector(const Executor &exec, localIdx size, ValueType value)
Create a Vector with a given size on an executor and uniform value.
Vector< ValueType > & operator=(Vector< ValueType > &&rhs) noexcept
Move-assignment operator — transfers ownership of the data buffer.
std::pair< localIdx, localIdx > range() const
Gets the range of the field.
Definition vector.hpp:338
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &&=delete
label ssize() const
Gets the size of the field.
Definition vector.hpp:274
Vector(const Executor &exec, localIdx size)
Create an uninitialized Vector with a given size on an executor.
void operator=(const ValueType &rhs)
Assignment operator, Sets the field values to that of the passed value.
View< const ValueType > view() const &
Gets the field as a view.
Definition vector.hpp:301
Vector< ValueType > & operator*=(const scalar rhs)
Assignment multiply operator, multiplies every cell in the field by a scalar.
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &
Gets a sub view of the field as a view.
Definition vector.hpp:327
const ValueType * data() const
Direct access to the underlying field data.
Definition vector.hpp:232
void resize(const localIdx size)
Resizes the field to a new size.
Vector< ValueType > & operator*=(const Vector< ValueType > &rhs)
Assignment multiply operator, multiplies this field by another field element-wise.
bool empty() const
Checks if the field is empty.
Definition vector.hpp:280
void apply(func f)
applies a functor, transformation, to the field
Definition vector.hpp:99
Vector< ValueType > operator*(const scalar rhs)
Arithmetic multiply operator, multiplies every cell in the field by a scalar.
const Executor & exec() const
Gets the executor associated with the field.
Definition vector.hpp:262
Vector< ValueType > & operator+=(const Vector< ValueType > &rhs)
Arithmetic add operator, addition of a second field.
~Vector()
Destroy the Vector object.
View< ValueType > view(std::pair< localIdx, localIdx > range) &&=delete
ValueType * end()
Direct access to the underlying field data.
Definition vector.hpp:250
ValueType VectorValueType
Definition vector.hpp:31
Vector(const Executor &exec, std::vector< ValueType > in)
Create a Vector from a given vector of values on an executor.
void copyToHost(Vector< ValueType > &result)
Copies the data (from anywhere) to a parsed host field.
View< ValueType > view() &&=delete
Vector< ValueType > copyToHost() const
Returns a copy of the field back to the host.
Vector< ValueType > & operator-=(const Vector< ValueType > &rhs)
Arithmetic subtraction operator, subtraction by a second field.
const ValueType & operator[](const localIdx i) const =delete
void operator=(const Vector< ValueType > &rhs)
Assignment operator, Sets the field values to that of the parsed field.
ValueType * begin()
Direct access to the underlying field data.
Definition vector.hpp:238
View< const ValueType > view() const &&=delete
Integer types used throughout NeoN.
Definition array.hpp:18
int32_t localIdx
Definition label.hpp:50
Vector< ValueType > operator-(Vector< ValueType > lhs, const Vector< ValueType > &rhs)
Arithmetic subtraction operator, subtraction one field from another.
void map(ContType< ValueType > &cont, const Inner inner, std::pair< localIdx, localIdx > range={0, 0})
Map a field using a specific executor.
Vector< ValueType > operator+(Vector< ValueType > lhs, const Vector< ValueType > &rhs)
Arithmetic add operator, addition of two fields.
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:20
float scalar
Definition scalar.hpp:17
int32_t label
Definition label.hpp:44