5#include <Kokkos_Core.hpp>
29template<
typename ValueType>
32 return [size, srcPtr, dstPtr](
const auto& srcExec,
const auto& dstExec)
35 dstExec.createKokkosView(dstPtr, size), srcExec.createKokkosView(srcPtr, size)
47template<
typename ValueType>
62 [&ptr,
size](
const auto& concreteExec)
63 { ptr = concreteExec.alloc(
size *
sizeof(ValueType)); },
66 data_ =
static_cast<ValueType*
>(ptr);
79 : size_(
size), data_(nullptr), exec_(
exec)
83 [&ptr,
size](
const auto& concreteExec)
84 { ptr = concreteExec.alloc(
size *
sizeof(ValueType)); },
87 data_ =
static_cast<ValueType*
>(ptr);
88 std::visit(detail::deepCopyVisitor<ValueType>(size_, in, data_), hostExec, exec_);
98 : size_(
size), data_(nullptr), exec_(
exec)
102 [&ptr,
size](
const auto& execu) { ptr = execu.alloc(
size *
sizeof(ValueType)); }, exec_
104 data_ =
static_cast<ValueType*
>(ptr);
149 std::visit([
this](
const auto&
exec) {
exec.free(data_); }, exec_);
158 template<
typename func>
171 if (dstExec == exec_)
return *
this;
196 result.
size() == size_,
"Parsed Field size not the same as current field size"
206 KOKKOS_INLINE_FUNCTION
214 KOKKOS_INLINE_FUNCTION
215 const ValueType&
operator[](
const size_t i)
const {
return data_[i]; }
222 KOKKOS_INLINE_FUNCTION
230 KOKKOS_INLINE_FUNCTION
231 const ValueType&
operator()(
const size_t i)
const {
return data_[i]; }
247 NF_ASSERT(exec_ == rhs.exec_,
"Executors are not the same");
262 validateOtherField(rhs);
274 validateOtherField(rhs);
286 validateOtherField(rhs);
317 [
this, &ptr,
size](
const auto&
exec)
318 { ptr =
exec.realloc(this->data_,
size *
sizeof(ValueType)); },
325 [&ptr,
size](
const auto&
exec) { ptr =
exec.alloc(
size *
sizeof(ValueType)); },
329 data_ =
static_cast<ValueType*
>(ptr);
337 [[nodiscard]] ValueType*
data() {
return data_; }
343 [[nodiscard]]
const ValueType*
data()
const {
return data_; }
355 [[nodiscard]]
size_t size()
const {
return size_; }
361 [[nodiscard]]
bool empty()
const {
return size() == 0; }
364 std::span<ValueType>
span() && =
delete;
367 std::span<const ValueType>
span() const&& = delete;
373 [[nodiscard]] std::
span<ValueType>
span() & {
return std::span<ValueType>(data_, size_); }
379 [[nodiscard]] std::span<const ValueType>
span() const&
381 return std::span<const ValueType>(data_, size_);
385 [[nodiscard]] std::span<ValueType>
span(std::pair<size_t, size_t>
range) && =
delete;
388 [[nodiscard]] std::span<const ValueType>
span(std::pair<size_t, size_t>
range)
const&& =
delete;
394 [[nodiscard]] std::span<ValueType>
span(std::pair<size_t, size_t>
range) &
396 return std::span<ValueType>(data_ +
range.first,
range.second -
range.first);
403 [[nodiscard]] std::span<const ValueType>
span(std::pair<size_t, size_t>
range)
const&
405 return std::span<const ValueType>(data_ +
range.first,
range.second -
range.first);
412 [[nodiscard]] std::pair<size_t, size_t>
range()
const {
return {0,
size()}; }
417 ValueType* data_ {
nullptr};
424 void validateOtherField(
const Field<ValueType>& rhs)
const
A class to contain the data and executors for a field and define some basic operations.
Field< ValueType > & operator-=(const Field< ValueType > &rhs)
Arithmetic subtraction operator, subtraction by a second field.
bool empty() const
Checks if the field is empty.
Field< ValueType > operator*(const scalar rhs)
Arithmetic multiply operator, multiplies every cell in the field by a scalar.
void resize(const size_t size)
Resizes the field to a new size.
std::pair< size_t, size_t > range() const
Gets the range of the field.
void operator=(const ValueType &rhs)
Assignment operator, Sets the field values to that of the passed value.
size_t size() const
Gets the size of the field.
const Executor & exec() const
Gets the executor associated with the field.
KOKKOS_INLINE_FUNCTION ValueType & operator()(const size_t i)
Function call operator.
Field< ValueType > copyToHost() const
Returns a copy of the field back to the host.
Field(const Executor &exec, const ValueType *in, size_t size, Executor hostExec=SerialExecutor())
Create a Field with a given size from existing memory on an executor.
KOKKOS_INLINE_FUNCTION ValueType & operator[](const size_t i)
Subscript operator.
void copyToHost(Field< ValueType > &result)
Copies the data (from anywhere) to a parsed host field.
std::span< ValueType > span(std::pair< size_t, size_t > range) &
Gets a sub view of the field as a span.
Field(const Executor &exec, size_t size)
Create an uninitialized Field with a given size on an executor.
ValueType * data()
Direct access to the underlying field data.
const ValueType * data() const
Direct access to the underlying field data.
KOKKOS_INLINE_FUNCTION const ValueType & operator[](const size_t i) const
Subscript operator.
~Field()
Destroy the Field object.
void operator=(const Field< ValueType > &rhs)
Assignment operator, Sets the field values to that of the parsed field.
std::span< const ValueType > span(std::pair< size_t, size_t > range) const &
Gets a sub view of the field as a span.
std::span< ValueType > span() &&=delete
std::span< const ValueType > span() const &&=delete
Field(const Field< ValueType > &rhs)
Copy constructor, creates a new field with the same size and data as the parsed field.
void apply(func f)
applies a functor, transformation, to the field
std::span< const ValueType > span(std::pair< size_t, size_t > range) const &&=delete
Field< ValueType > operator*(const Field< scalar > &rhs)
Arithmetic multiply operator, multiply by a second field.
Field(const Executor &exec, const Field< ValueType > &in)
Create a Field as a copy of a Field on a specified executor.
Field< ValueType > & operator+=(const Field< ValueType > &rhs)
Arithmetic add operator, addition of a second field.
std::span< const ValueType > span() const &
Gets the field as a span.
Field(const Executor &exec, size_t size, ValueType value)
Create a Field with a given size on an executor and uniform value.
Field(const Executor &exec, std::vector< ValueType > in)
Create a Field from a given vector of values on an executor.
std::span< ValueType > span(std::pair< size_t, size_t > range) &&=delete
KOKKOS_INLINE_FUNCTION const ValueType & operator()(const size_t i) const
Function call operator.
Field(Field< ValueType > &&rhs) noexcept
Move constructor, moves the data from the parsed field to the new field.
Field< ValueType > copyToExecutor(Executor dstExec) const
Copies the data to a new field on a specific executor.
Reference executor for serial CPU execution.
#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(size_t size, const ValueType *srcPtr, ValueType *dstPtr)
A helper function to simplify the common pattern of copying between and to executor.
void mul(Field< ValueType > &a, const Field< std::type_identity_t< ValueType > > &b)
void setField(Field< ValueType > &a, const std::span< const std::type_identity_t< ValueType > > b, std::pair< size_t, size_t > range={0, 0})
Set the field with a span of values using a specific executor.
void add(Field< ValueType > &a, const Field< std::type_identity_t< ValueType > > &b)
void map(Field< T > &a, const Inner inner, std::pair< size_t, size_t > range={0, 0})
Map a field using a specific executor.
Field< T > operator+(Field< T > lhs, const Field< T > &rhs)
Arithmetic add operator, addition of two fields.
Field< T > operator-(Field< T > lhs, const Field< T > &rhs)
Arithmetic subtraction operator, subtraction one field from another.
void sub(Field< ValueType > &a, const Field< std::type_identity_t< ValueType > > &b)
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
void scalarMul(Field< ValueType > &a, const std::type_identity_t< ValueType > value)
void fill(Field< ValueType > &a, const std::type_identity_t< ValueType > value, std::pair< size_t, size_t > range={0, 0})
Fill the field with a scalar value using a specific executor.