NeoN
A framework for CFD software
Loading...
Searching...
No Matches
vector.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 <Kokkos_Core.hpp>
6
7#include "NeoN/core/error.hpp"
12#include "NeoN/core/view.hpp"
13
14#include <vector>
15
16
17namespace NeoN
18{
19
26template<typename ValueType>
27class Vector
28{
29
30public:
31
32 using VectorValueType = ValueType;
33
40
49 const Executor& exec,
50 const ValueType* in,
52 Executor hostExec = SerialExecutor()
53 );
54
61 Vector(const Executor& exec, localIdx size, ValueType value);
62
68 Vector(const Executor& exec, std::vector<ValueType> in);
69
76
82
87 Vector(Vector<ValueType>&& rhs) noexcept;
88
93
99 template<typename func>
100 void apply(func f)
101 {
102 map(*this, f);
103 }
104
110 [[nodiscard]] Vector<ValueType> copyToExecutor(Executor dstExec) const;
111
116 [[nodiscard]] Vector<ValueType> copyToHost() const;
117
127
128 // ensures no return of device address on host --> invalid memory access
129 ValueType& operator[](const localIdx i) = delete;
130
131 // ensures no return of device address on host --> invalid memory access
132 const ValueType& operator[](const localIdx i) const = delete;
133
138 void operator=(const ValueType& rhs);
139
146 void operator=(const Vector<ValueType>& rhs);
147
154
161
171 requires requires(ValueType a, ValueType b) { a* b; };
172
181 [[nodiscard]] Vector<ValueType> operator*(const scalar rhs)
182 requires requires(ValueType a, scalar b) { a* b; };
183
193 requires requires(ValueType a, ValueType b) { a *= b; };
194
203 requires requires(ValueType a, scalar b) { a *= b; };
204
209 void resize(const localIdx size);
210
215 [[nodiscard]] ValueType* data() { return data_; }
216
221 [[nodiscard]] const ValueType* data() const { return data_; }
222
227 [[nodiscard]] const Executor& exec() const { return exec_; }
228
233 [[nodiscard]] localIdx size() const { return size_; }
234
239 [[nodiscard]] label ssize() const { return static_cast<label>(size_); }
240
245 [[nodiscard]] bool empty() const { return size() == 0; }
246
247 // return of a temporary --> invalid memory access
248 View<ValueType> view() && = delete;
249
250 // return of a temporary --> invalid memory access
251 View<const ValueType> view() const&& = delete;
252
257 [[nodiscard]] View<ValueType> view() &
258 {
259 return View<ValueType>(data_, static_cast<size_t>(size_));
260 }
261
266 [[nodiscard]] View<const ValueType> view() const&
267 {
268 return View<const ValueType>(data_, static_cast<size_t>(size_));
269 }
270
271 // return of a temporary --> invalid memory access
272 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) && = delete;
273
274 // return of a temporary --> invalid memory access
275 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&& = delete;
276
281 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) &
282 {
283 return View<ValueType>(
284 data_ + range.first, static_cast<size_t>(range.second - range.first)
285 );
286 }
287
292 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&
293 {
295 data_ + range.first, static_cast<size_t>(range.second - range.first)
296 );
297 }
298
303 [[nodiscard]] std::pair<localIdx, localIdx> range() const { return {0, size()}; }
304
305private:
306
307 localIdx size_ {0};
308 ValueType* data_ {nullptr};
309 const Executor exec_;
310
315 void validateOtherVector(const Vector<ValueType>& rhs) const;
316};
317
324template<typename ValueType>
326
333template<typename ValueType>
335
336} // 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:28
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:233
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:281
ValueType * data()
Direct access to the underlying field data.
Definition vector.hpp:215
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:303
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &&=delete
label ssize() const
Gets the size of the field.
Definition vector.hpp:239
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:266
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:292
const ValueType * data() const
Direct access to the underlying field data.
Definition vector.hpp:221
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:245
void apply(func f)
applies a functor, transformation, to the field
Definition vector.hpp:100
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:227
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:32
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:19
int32_t localIdx
Definition label.hpp:30
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:16
float scalar
Definition scalar.hpp:14
int32_t label
Definition label.hpp:24