NeoN
A framework for CFD software
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include "NeoN/core/error.hpp"
10#include "NeoN/core/view.hpp"
13
14#include <variant>
15#include <vector>
16
17namespace NeoN
18{
19
36template<typename ValueType>
37class Array
38{
39
40public:
41
42 using ArrayValueType = ValueType;
43
50 Array(const Executor& exec, sizeType size) : size_(size), data_(nullptr), exec_(exec)
51 {
52 void* ptr = nullptr;
53 std::visit(
54 [&ptr, size](const auto& concreteExec)
55 { ptr = concreteExec.template alloc<ValueType>(size); },
56 exec_
57 );
58 data_ = static_cast<ValueType*>(ptr);
59 }
60
70 const Executor& exec,
71 const ValueType* in,
73 Executor hostExec = SerialExecutor()
74 )
75 : size_(size), data_(nullptr), exec_(exec)
76 {
77 void* ptr = nullptr;
78 std::visit(
79 [&ptr, size](const auto& concreteExec)
80 { ptr = concreteExec.template alloc<ValueType>(size); },
81 exec_
82 );
83 data_ = static_cast<ValueType*>(ptr);
84 std::visit(detail::deepCopyVisitor<ValueType>(size_, in, data_), hostExec, exec_);
85 }
86
87
95 Array(const Executor& exec, sizeType size, ValueType value)
96 : size_(size), data_(nullptr), exec_(exec)
97 {
98 void* ptr = nullptr;
99 std::visit(
100 [&ptr, size](const auto& execu) { ptr = execu.template alloc<ValueType>(size); }, exec_
101 );
102 data_ = static_cast<ValueType*>(ptr);
103 NeoN::fill(*this, value);
104 }
105
111 Array(const Executor& exec, std::vector<ValueType> in) : Array(exec, in.data(), in.size()) {}
112
113
121 : Array(exec, in.data(), in.size(), in.exec())
122 {}
123
129 Array(const Array<ValueType>& rhs) : Array(rhs.exec(), rhs.data(), rhs.size(), rhs.exec()) {}
130
131
137 Array(Array<ValueType>&& rhs) noexcept : size_(rhs.size_), data_(rhs.data_), exec_(rhs.exec_)
138 {
139 rhs.data_ = nullptr;
140 rhs.size_ = 0;
141 };
142
147 {
148 std::visit([this](const auto& exec) { exec.free(data_); }, exec_);
149 data_ = nullptr;
150 }
151
160 template<typename func>
161 void apply(func f)
162 {
163 map(*this, f);
164 }
165
172 [[nodiscard]] Array<ValueType> copyToExecutor(Executor dstExec) const
173 {
174 if (dstExec == exec_) return *this;
175
176 Array<ValueType> result(dstExec, size_);
177 std::visit(detail::deepCopyVisitor(size_, data_, result.data()), exec_, dstExec);
178
179 return result;
180 }
181
187 [[nodiscard]] Array<ValueType> copyToHost() const { return copyToExecutor(SerialExecutor()); }
188
198 {
200 result.size() == size_, "Parsed Array size not the same as current field size"
201 );
202 result = copyToExecutor(SerialExecutor());
203 }
204
205 // ensures no return of device address on host --> invalid memory access
206 Array& operator[](const localIdx i) = delete;
207
208 // ensures no return of device address on host --> invalid memory access
209 const Array& operator[](const localIdx i) const = delete;
210
216 void operator=(const ValueType& rhs)
217 {
218 NF_ERROR_EXIT("Not implemented");
219 fill(*this, rhs);
220 }
221
231 {
232 NF_ASSERT(exec_ == rhs.exec_, "Executors are not the same");
233 if (this->size() != rhs.size())
234 {
235 this->resize(rhs.size());
236 }
237 setContainer(*this, rhs.view());
238 }
239
245 void resize(const sizeType size)
246 {
247 void* ptr = nullptr;
248 if (!empty())
249 {
250 std::visit(
251 [this, &ptr, size](const auto& exec)
252 { ptr = exec.template realloc<ValueType>(this->data_, size); },
253 exec_
254 );
255 }
256 else
257 {
258 std::visit(
259 [&ptr, size](const auto& exec) { ptr = exec.template alloc<ValueType>(size); },
260 exec_
261 );
262 }
263 data_ = static_cast<ValueType*>(ptr);
264 size_ = size;
265 }
266
272 [[nodiscard]] inline ValueType* data() { return data_; }
273
279 [[nodiscard]] inline const ValueType* data() const { return data_; }
280
286 [[nodiscard]] inline const Executor& exec() const { return exec_; }
287
293 [[nodiscard]] inline sizeType size() const { return size_; }
294
300 [[nodiscard]] inline label ssize() const { return static_cast<label>(size_); }
301
307 [[nodiscard]] inline bool empty() const { return size() == 0; }
308
309 // return of a temporary --> invalid memory access
310 View<ValueType> view() && = delete;
311
312 // return of a temporary --> invalid memory access
313 View<const ValueType> view() const&& = delete;
314
320 [[nodiscard]] inline View<ValueType> view() & { return View<ValueType>(data_, size_); }
321
327 [[nodiscard]] inline View<const ValueType> view() const&
328 {
329 return View<const ValueType>(data_, size_);
330 }
331
332 // return of a temporary --> invalid memory access
333 [[nodiscard]] View<ValueType> view(std::pair<localIdx, localIdx> range) && = delete;
334
335 // return of a temporary --> invalid memory access
336 [[nodiscard]] View<const ValueType> view(std::pair<localIdx, localIdx> range) const&& = delete;
337
350 [[nodiscard]] inline View<ValueType> view(std::pair<localIdx, localIdx> range) &
351 {
353 return View<ValueType>(
354 data_ + range.first, static_cast<sizeType>(range.second - range.first)
355 );
356 }
357
364 [[nodiscard]] inline View<const ValueType> view(std::pair<localIdx, localIdx> range) const&
365 {
368 data_ + range.first, static_cast<sizeType>(range.second - range.first)
369 );
370 }
371
377 [[nodiscard]] inline std::pair<localIdx, localIdx> range() const
378 {
379 return {0, NeoN::toLocalIdx(size())};
380 }
381
382private:
383
384 sizeType size_ {0};
385 ValueType* data_ {nullptr};
386 const Executor exec_;
387
392 void validateOtherArray(const Array<ValueType>& rhs) const
393 {
394 NF_DEBUG_ASSERT(size() == rhs.size(), "Arrays are not the same size.");
395 NF_DEBUG_ASSERT(exec() == rhs.exec(), "Executors are not the same.");
396 }
397};
398
399} // namespace NeoN
sizeType size() const
Returns the number of elements in the Array.
Definition array.hpp:293
const ValueType * data() const
Returns a pointer to the underlying array data.
Definition array.hpp:279
View< ValueType > view() &&=delete
Array< ValueType > copyToHost() const
Creates a host-resident copy of the Array.
Definition array.hpp:187
void copyToHost(Array< ValueType > &result)
Copies the Array data to a host-resident Array.
Definition array.hpp:197
View< ValueType > view(std::pair< localIdx, localIdx > range) &
Returns a view of a range of the Array.
Definition array.hpp:350
std::pair< localIdx, localIdx > range() const
Returns the index range of the Array.
Definition array.hpp:377
Array(Array< ValueType > &&rhs) noexcept
Move constructor.
Definition array.hpp:137
Array< ValueType > copyToExecutor(Executor dstExec) const
Creates a copy of the Array on a specified executor.
Definition array.hpp:172
View< const ValueType > view() const &
Returns a view of the Array data.
Definition array.hpp:327
Array(const Executor &exec, sizeType size)
Creates an uninitialized Array with the given size on an executor.
Definition array.hpp:50
label ssize() const
Returns the number of elements in the Array.
Definition array.hpp:300
Array(const Executor &exec, const ValueType *in, sizeType size, Executor hostExec=SerialExecutor())
Creates an Array from existing data on the given executor.
Definition array.hpp:69
void operator=(const ValueType &rhs)
Assigns a value to all elements of the Array.
Definition array.hpp:216
const Array & operator[](const localIdx i) const =delete
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &
Returns a view of a range of the Array.
Definition array.hpp:364
ValueType ArrayValueType
Definition array.hpp:42
bool empty() const
Checks whether the Array contains no elements.
Definition array.hpp:307
const Executor & exec() const
Returns the executor associated with the Array.
Definition array.hpp:286
View< const ValueType > view() const &&=delete
void resize(const sizeType size)
Resizes the Array.
Definition array.hpp:245
Array(const Executor &exec, std::vector< ValueType > in)
Create an Array from a std::vector on a given executor.
Definition array.hpp:111
Array & operator[](const localIdx i)=delete
Array(const Executor &exec, sizeType size, ValueType value)
Creates an Array with the given size and initializes all elements to a value.
Definition array.hpp:95
~Array()
Destroy the Array object.
Definition array.hpp:146
Array(const Executor &exec, const Array< ValueType > &in)
Creates a copy of an Array on the specified executor.
Definition array.hpp:120
Array(const Array< ValueType > &rhs)
Creates a copy of another Array on the same executor.
Definition array.hpp:129
View< ValueType > view(std::pair< localIdx, localIdx > range) &&=delete
void operator=(const Array< ValueType > &rhs)
Copies the contents of another Array.
Definition array.hpp:230
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &&=delete
void apply(func f)
Applies a function to each element of the Array.
Definition array.hpp:161
ValueType * data()
Returns a pointer to the underlying array data.
Definition array.hpp:272
Reference executor for serial CPU execution.
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
Definition error.hpp:90
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:118
#define NF_DEBUG_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false (only in debu...
Definition error.hpp:190
auto deepCopyVisitor(sizeType size, const ValueType *srcPtr, ValueType *dstPtr)
A helper function to simplify the common pattern of copying between and to executor.
Integer types used throughout NeoN.
Definition array.hpp:18
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:21
localIdx toLocalIdx(sizeType value)
void map(ContType< ValueType > &cont, const Inner inner, std::pair< localIdx, localIdx > range={0, 0})
Map a field using a specific executor.
void validateRange(std::pair< localIdx, localIdx > range, sizeType size)
std::size_t sizeType
Definition label.hpp:56
void setContainer(ContType< ValueType > &cont, const View< const std::type_identity_t< ValueType > > view, std::pair< localIdx, localIdx > range={0, 0})
Set the container with a view of values using a specific executor.
int32_t localIdx
Definition label.hpp:50
void fill(ContType< ValueType > &cont, const std::type_identity_t< ValueType > value, std::pair< localIdx, localIdx > range={0, 0})
Fill the field with a vector value using a specific executor.
int32_t label
Definition label.hpp:44