NeoN
A framework for CFD software
Loading...
Searching...
No Matches
vector.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 <Kokkos_Core.hpp>
8
9#include "NeoN/core/error.hpp"
14#include "NeoN/core/view.hpp"
15
16#include <vector>
17
18
19namespace NeoN
20{
21
28template<typename ValueType>
29class Vector
30{
31
32public:
33
34 using VectorValueType = ValueType;
35
42
51 const Executor& exec,
52 const ValueType* in,
54 Executor hostExec = SerialExecutor()
55 );
56
63 Vector(const Executor& exec, localIdx size, ValueType value);
64
70 Vector(const Executor& exec, std::vector<ValueType> in);
71
78
84
89 Vector(Vector<ValueType>&& rhs) noexcept;
90
95
101 template<typename func>
102 void apply(func f)
103 {
104 map(*this, f);
105 }
106
112 [[nodiscard]] Vector<ValueType> copyToExecutor(Executor dstExec) const;
113
118 [[nodiscard]] Vector<ValueType> copyToHost() const;
119
129
130 // ensures no return of device address on host --> invalid memory access
131 ValueType& operator[](const localIdx i) = delete;
132
133 // ensures no return of device address on host --> invalid memory access
134 const ValueType& operator[](const localIdx i) const = delete;
135
140 void operator=(const ValueType& rhs);
141
148 void operator=(const Vector<ValueType>& rhs);
149
156
163
173 requires requires(ValueType a, ValueType b) { a* b; };
174
183 [[nodiscard]] Vector<ValueType> operator*(const scalar rhs)
184 requires requires(ValueType a, scalar b) { a* b; };
185
195 requires requires(ValueType a, ValueType b) { a *= b; };
196
205 requires requires(ValueType a, scalar b) { a *= b; };
206
211 void resize(const localIdx size);
212
217 [[nodiscard]] ValueType* data() { return data_; }
218
223 [[nodiscard]] const ValueType* data() const { return data_; }
224
229 [[nodiscard]] const Executor& exec() const { return exec_; }
230
235 [[nodiscard]] localIdx size() const { return size_; }
236
241 [[nodiscard]] label ssize() const { return static_cast<label>(size_); }
242
247 [[nodiscard]] bool empty() const { return size() == 0; }
248
249 // return of a temporary --> invalid memory access
250 View<ValueType> view() && = delete;
251
252 // return of a temporary --> invalid memory access
253 View<const ValueType> view() const&& = delete;
254
259 [[nodiscard]] View<ValueType> view() &
260 {
261 return View<ValueType>(data_, static_cast<size_t>(size_));
262 }
263
268 [[nodiscard]] View<const ValueType> view() const&
269 {
270 return View<const ValueType>(data_, static_cast<size_t>(size_));
271 }
272
273 // return of a temporary --> invalid memory access
274 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) && = delete;
275
276 // return of a temporary --> invalid memory access
277 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&& = delete;
278
283 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) &
284 {
285 return View<ValueType>(
286 data_ + range.first, static_cast<size_t>(range.second - range.first)
287 );
288 }
289
294 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&
295 {
297 data_ + range.first, static_cast<size_t>(range.second - range.first)
298 );
299 }
300
305 [[nodiscard]] std::pair<localIdx, localIdx> range() const { return {0, size()}; }
306
307private:
308
309 localIdx size_ {0};
310 ValueType* data_ {nullptr};
311 const Executor exec_;
312
317 void validateOtherVector(const Vector<ValueType>& rhs) const;
318};
319
326template<typename ValueType>
328
335template<typename ValueType>
337
338} // 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:30
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.
localIdx size() const
Gets the size of the field.
Definition vector.hpp:235
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.
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:283
ValueType * data()
Direct access to the underlying field data.
Definition vector.hpp:217
Vector(const Executor &exec, localIdx size, ValueType value)
Create a Vector with a given size on an executor and uniform value.
std::pair< localIdx, localIdx > range() const
Gets the range of the field.
Definition vector.hpp:305
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &&=delete
label ssize() const
Gets the size of the field.
Definition vector.hpp:241
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:268
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:294
const ValueType * data() const
Direct access to the underlying field data.
Definition vector.hpp:223
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:247
void apply(func f)
applies a functor, transformation, to the field
Definition vector.hpp:102
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:229
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 VectorValueType
Definition vector.hpp:34
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.
View< const ValueType > view() const &&=delete
Definition array.hpp:20
int32_t localIdx
Definition label.hpp:32
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:18
float scalar
Definition scalar.hpp:16
int32_t label
Definition label.hpp:26