5#include <Kokkos_Core.hpp>
32template<
typename ValueType>
35 size_t size =
static_cast<size_t>(ssize);
36 return [size, srcPtr, dstPtr](
const auto& srcExec,
const auto& dstExec)
39 dstExec.createKokkosView(dstPtr, size), srcExec.createKokkosView(srcPtr, size)
51template<
typename ValueType>
68 [&ptr,
size](
const auto& concreteExec)
69 { ptr = concreteExec.alloc(
static_cast<size_t>(
size) *
sizeof(ValueType)); },
72 data_ =
static_cast<ValueType*
>(ptr);
88 : size_(
size), data_(nullptr), exec_(
exec)
92 [&ptr,
size](
const auto& concreteExec)
93 { ptr = concreteExec.alloc(
static_cast<size_t>(
size) *
sizeof(ValueType)); },
96 data_ =
static_cast<ValueType*
>(ptr);
97 std::visit(detail::deepCopyVisitor<ValueType>(size_, in, data_), hostExec, exec_);
107 : size_(
size), data_(nullptr), exec_(
exec)
111 [&ptr,
size](
const auto& execu)
112 { ptr = execu.alloc(
static_cast<size_t>(
size) *
sizeof(ValueType)); },
115 data_ =
static_cast<ValueType*
>(ptr);
158 std::visit([
this](
const auto&
exec) {
exec.free(data_); }, exec_);
167 template<
typename func>
180 if (dstExec == exec_)
return *
this;
205 result.
size() == size_,
"Parsed Vector size not the same as current field size"
230 NF_ASSERT(exec_ == rhs.exec_,
"Executors are not the same");
245 validateOtherVector(rhs);
257 validateOtherVector(rhs);
269 validateOtherVector(rhs);
297 validateOtherVector(rhs);
325 [
this, &ptr,
size](
const auto&
exec)
326 { ptr =
exec.realloc(this->data_,
static_cast<size_t>(
size) *
sizeof(ValueType)); },
334 { ptr =
exec.alloc(
static_cast<size_t>(
size) *
sizeof(ValueType)); },
338 data_ =
static_cast<ValueType*
>(ptr);
346 [[nodiscard]] ValueType*
data() {
return data_; }
352 [[nodiscard]]
const ValueType*
data()
const {
return data_; }
376 [[nodiscard]]
bool empty()
const {
return size() == 0; }
434 [[nodiscard]] std::pair<localIdx, localIdx>
range()
const {
return {0,
size()}; }
439 ValueType* data_ {
nullptr};
446 void validateOtherVector(
const Vector<ValueType>& rhs)
const
Reference executor for serial CPU execution.
A class to contain the data and executors for a field and define some basic operations.
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.
Vector< ValueType > & operator*=(const scalar rhs)
Arithmetic multiply-assignment operator, multiplies every cell in the field by a scalar and updates t...
localIdx size() const
Gets the size of the field.
Vector< ValueType > copyToExecutor(Executor dstExec) const
Copies the data to a new field on a specific executor.
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.
ValueType * data()
Direct access to the underlying field data.
Vector(const Executor &exec, localIdx size, ValueType value)
Create a Vector with a given size on an executor and uniform value.
Vector< ValueType > & operator*=(const Vector< scalar > &rhs)
Arithmetic multiply operator, multiplies this field by another field element-wise.
std::pair< localIdx, localIdx > range() const
Gets the range of the field.
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &&=delete
label ssize() const
Gets the size of the field.
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.
View< const ValueType > view(std::pair< localIdx, localIdx > range) const &
Gets a sub view of the field as a view.
const ValueType * data() const
Direct access to the underlying field data.
void resize(const localIdx size)
Resizes the field to a new size.
bool empty() const
Checks if the field is empty.
void apply(func f)
applies a functor, transformation, to the field
const Executor & exec() const
Gets the executor associated with the field.
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
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.
Vector< ValueType > operator*(const Vector< scalar > &rhs)
Arithmetic multiply operator, multiply by a second 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.
Vector< ValueType > operator*(const scalar rhs)
Arithmetic multiply operator, multiplies every cell in the field by a scalar.
View< const ValueType > view() const &&=delete
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
#define NF_DEBUG_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false (only in debu...
auto deepCopyVisitor(localIdx ssize, const ValueType *srcPtr, ValueType *dstPtr)
A helper function to simplify the common pattern of copying between and to executor.
void map(Vector< T > &a, const Inner inner, std::pair< localIdx, localIdx > range={0, 0})
Map a field using a specific executor.
void mul(Vector< ValueType > &a, const Vector< std::type_identity_t< ValueType > > &b)
void add(Vector< ValueType > &a, const Vector< std::type_identity_t< ValueType > > &b)
void fill(Vector< ValueType > &a, const std::type_identity_t< ValueType > value, std::pair< localIdx, localIdx > range={0, 0})
Fill the field with a scalar value using a specific executor.
Vector< T > operator+(Vector< T > lhs, const Vector< T > &rhs)
Arithmetic add operator, addition of two fields.
Vector< T > operator-(Vector< T > lhs, const Vector< T > &rhs)
Arithmetic subtraction operator, subtraction one field from another.
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
void setVector(Vector< ValueType > &a, const View< const std::type_identity_t< ValueType > > b, std::pair< localIdx, localIdx > range={0, 0})
Set the field with a view of values using a specific executor.
void sub(Vector< ValueType > &a, const Vector< std::type_identity_t< ValueType > > &b)
void scalarMul(Vector< ValueType > &a, const scalar value)