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"
13
14#include <vector>
15
16
17namespace NeoN
18{
38template<typename ValueType>
39class Vector
40{
41
42public:
43
44 using VectorValueType = ValueType;
45
53
63 const Executor& exec,
64 const ValueType* in,
66 Executor hostExec = SerialExecutor()
67 );
68
76 Vector(const Executor& exec, sizeType size, ValueType value);
77
84 Vector(const Executor& exec, std::vector<ValueType> in);
85
93
100
106 Vector(Vector<ValueType>&& rhs) noexcept;
107
112
121 template<typename func>
122 void apply(func f)
123 {
124 map(*this, f);
125 }
126
133 [[nodiscard]] Vector<ValueType> copyToExecutor(Executor dstExec) const;
134
140 [[nodiscard]] Vector<ValueType> copyToHost() const;
141
151
161 ValueType& operator[](const localIdx i) = delete;
162
163 const ValueType& operator[](const localIdx i) const = delete;
164
170 void operator=(const ValueType& rhs);
171
179 void operator=(const Vector<ValueType>& rhs);
180
194
202
210
224 requires requires(ValueType a, ValueType b) { a* b; };
225
238 [[nodiscard]] Vector<ValueType> operator*(const scalar rhs)
239 requires requires(ValueType a, scalar b) { a* b; };
240
254 requires requires(ValueType a, ValueType b) { a *= b; };
255
269 requires requires(ValueType a, scalar b) { a *= b; };
270
276 void resize(const sizeType size);
277
283 [[nodiscard]] ValueType* data() { return data_; }
284
290 [[nodiscard]] const ValueType* data() const { return data_; }
291
297 [[nodiscard]] ValueType* begin() { return data_; }
298
304 [[nodiscard]] const ValueType* begin() const { return data_; }
305
311 [[nodiscard]] ValueType* end() { return data_ + size(); }
312
318 [[nodiscard]] const ValueType* end() const { return data_ + size(); }
319
325 [[nodiscard]] const Executor& exec() const { return exec_; }
326
332 [[nodiscard]] sizeType size() const { return size_; }
333
339 [[nodiscard]] label ssize() const { return static_cast<label>(size_); }
340
346 [[nodiscard]] bool empty() const { return size() == 0; }
347
354 View<ValueType> view() && = delete;
355
356 View<const ValueType> view() const&& = delete;
357
363 [[nodiscard]] View<ValueType> view() & { return View<ValueType>(data_, size_); }
364
370 [[nodiscard]] View<const ValueType> view() const&
371 {
372 return View<const ValueType>(data_, size_);
373 }
374
381 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) && = delete;
382
383 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&& = delete;
384
391 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) &
392 {
394 return View<ValueType>(
395 data_ + range.first, static_cast<sizeType>(range.second - range.first)
396 );
397 }
398
405 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&
406 {
409 data_ + range.first, static_cast<sizeType>(range.second - range.first)
410 );
411 }
412
418 [[nodiscard]] inline std::pair<localIdx, localIdx> range() const
419 {
420 return {0, NeoN::toLocalIdx(size())};
421 }
422
423private:
424
425 sizeType size_ {0};
426 ValueType* data_ {nullptr};
427 const Executor exec_;
428
433 void validateOtherVector(const Vector<ValueType>& rhs) const;
434};
435
443template<typename ValueType>
445
453template<typename ValueType>
455
456} // namespace NeoN
Reference executor for serial CPU execution.
An executor-aware contiguous container for numerical data with vector-style arithmetic semantics.
Definition vector.hpp:40
Vector(const Executor &exec, sizeType size)
Creates an uninitialized Vector with the given size on an executor.
Vector(Vector< ValueType > &&rhs) noexcept
Move constructor.
ValueType & operator[](const localIdx i)=delete
Host-side element access is intentionally disabled.
Vector(const Vector< ValueType > &rhs)
Copy constructor.
const ValueType * begin() const
Returns a const pointer to the first element of the Vector.
Definition vector.hpp:304
Vector< ValueType > copyToExecutor(Executor dstExec) const
Creates a copy of the Vector on the specified executor.
Vector< ValueType > operator*(const Vector< ValueType > &rhs)
Multiplies two Vectors element-wise.
const ValueType * end() const
Returns a const pointer one past the last element of the Vector.
Definition vector.hpp:318
Vector(const Executor &exec, const Vector< ValueType > &in)
Creates a copy of another Vector on the specified executor.
View< ValueType > view(std::pair< localIdx, localIdx > range) &
Returns a non-owning view of a range of the Vector.
Definition vector.hpp:391
ValueType * data()
Returns a pointer to the underlying Vector data.
Definition vector.hpp:283
Vector< ValueType > & operator=(Vector< ValueType > &&rhs) noexcept
Move-assignment operator — transfers ownership of the data buffer.
std::pair< localIdx, localIdx > range() const
Definition vector.hpp:418
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &&=delete
label ssize() const
Returns the number of elements as a label.
Definition vector.hpp:339
void operator=(const ValueType &rhs)
Assigns a value to every element of the Vector.
View< const ValueType > view() const &
Returns a read-only, non-owning view of the Vector data.
Definition vector.hpp:370
Vector< ValueType > & operator*=(const scalar rhs)
Multiplies every element of the Vector by a scalar.
Vector(const Executor &exec, sizeType size, ValueType value)
Creates a Vector with the given size and initializes all elements to a value.
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &
Returns a read-only, non-owning view of a range of the Vector.
Definition vector.hpp:405
const ValueType * data() const
Returns a pointer to the underlying Vector data.
Definition vector.hpp:290
Vector< ValueType > & operator*=(const Vector< ValueType > &rhs)
Multiplies this Vector element-wise by another Vector.
bool empty() const
Checks whether the Vector contains no elements.
Definition vector.hpp:346
sizeType size() const
Definition vector.hpp:332
void apply(func f)
Applies a function to the elements of the Vector.
Definition vector.hpp:122
void resize(const sizeType size)
Resizes the Vector.
Vector< ValueType > operator*(const scalar rhs)
Multiplies every element of the Vector by a scalar.
const Executor & exec() const
Definition vector.hpp:325
Vector< ValueType > & operator+=(const Vector< ValueType > &rhs)
Adds another Vector element-wise.
~Vector()
Destroys the Vector object.
View< ValueType > view(std::pair< localIdx, localIdx > range) &&=delete
Prevents creating a view of a temporary Vector.
ValueType * end()
Returns a pointer one past the last element of the Vector.
Definition vector.hpp:311
ValueType VectorValueType
Definition vector.hpp:44
Vector(const Executor &exec, std::vector< ValueType > in)
Creates a Vector from a std::vector on the given executor.
void copyToHost(Vector< ValueType > &result)
Copies the Vector data to a host-resident Vector.
View< ValueType > view() &&=delete
Prevents creating a View from a temporary Vector.
Vector< ValueType > copyToHost() const
Creates a host-resident copy of the Vector.
Vector< ValueType > & operator-=(const Vector< ValueType > &rhs)
Subtracts another Vector element-wise.
const ValueType & operator[](const localIdx i) const =delete
void operator=(const Vector< ValueType > &rhs)
Assigns the contents of another Vector.
Vector(const Executor &exec, const ValueType *in, sizeType size, Executor hostExec=SerialExecutor())
Creates a Vector with the given size from existing data on an executor.
ValueType * begin()
Returns a pointer to the first element of the Vector.
Definition vector.hpp:297
View< const ValueType > view() const &&=delete
Integer types used throughout NeoN.
Definition array.hpp:18
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:21
localIdx toLocalIdx(sizeType value)
Vector< ValueType > operator-(Vector< ValueType > lhs, const Vector< ValueType > &rhs)
Subtracts two Vectors element-wise.
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)
Adds two Vectors element-wise.
void validateRange(std::pair< localIdx, localIdx > range, sizeType size)
float scalar
Definition scalar.hpp:17
std::size_t sizeType
Definition label.hpp:56
int32_t localIdx
Definition label.hpp:50
int32_t label
Definition label.hpp:44