33#ifndef GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
34#define GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
44#include <ginkgo/core/base/exception.hpp>
45#include <ginkgo/core/base/exception_helpers.hpp>
46#include <ginkgo/core/base/executor.hpp>
47#include <ginkgo/core/base/types.hpp>
48#include <ginkgo/core/base/utils.hpp>
54template <
typename ValueType>
67template <
typename SourceType,
typename TargetType>
68void convert_data(std::shared_ptr<const Executor> exec,
size_type size,
81template <
typename ValueType>
82class const_array_view {
87 using value_type = ValueType;
97 const ValueType* data)
105 const_array_view& operator=(
const const_array_view&) =
delete;
106 const_array_view& operator=(const_array_view&&) =
delete;
107 const_array_view(
const const_array_view&) =
delete;
112 const_array_view(const_array_view&&
other)
115 other.num_elems_ = 0;
116 other.data_ =
nullptr;
131 const value_type* get_const_data()
const noexcept {
return data_; }
138 std::shared_ptr<const Executor> get_executor()
const noexcept
156 std::shared_ptr<const Executor> exec_;
158 const ValueType* data_;
162template <
typename ValueType>
163using ConstArrayView GKO_DEPRECATED(
"please use const_array_view") =
164 const_array_view<ValueType>;
167template <
typename ValueType>
186template <
typename ValueType>
231 exec_(std::move(exec))
244 exec_(std::
move(exec))
269 template <
typename DeleterType>
300 template <
typename RandomAccessIterator>
305 array tmp(exec->get_master(), std::distance(begin, end));
306 std::copy(begin, end, tmp.data_.get());
307 *
this = std::move(tmp);
320 template <
typename T>
321 array(std::shared_ptr<const Executor> exec,
362 *
this = std::move(
other);
452 if (&
other ==
this) {
455 if (exec_ ==
nullptr) {
456 exec_ =
other.get_executor();
457 data_ = data_manager{
nullptr,
other.data_.get_deleter()};
459 if (
other.get_executor() ==
nullptr) {
467 GKO_ENSURE_COMPATIBLE_BOUNDS(
other.get_num_elems(),
470 exec_->copy_from(
other.get_executor(),
other.get_num_elems(),
471 other.get_const_data(),
this->get_data());
506 if (&
other ==
this) {
509 if (exec_ ==
nullptr) {
510 exec_ =
other.get_executor();
513 if (
other.get_executor() ==
nullptr) {
517 if (exec_ ==
other.get_executor()) {
519 data_ = std::exchange(
520 other.data_, data_manager{nullptr, default_deleter{exec_}});
521 num_elems_ = std::exchange(
other.num_elems_, 0);
547 template <
typename OtherValueType>
548 std::enable_if_t<!std::is_same<ValueType, OtherValueType>::value,
array>&
551 if (this->exec_ ==
nullptr) {
552 this->exec_ =
other.get_executor();
555 if (
other.get_executor() ==
nullptr) {
560 if (this->is_owning()) {
561 this->resize_and_reset(
other.get_num_elems());
563 GKO_ENSURE_COMPATIBLE_BOUNDS(
other.get_num_elems(),
569 if (this->exec_ !=
other.get_executor()) {
571 source = tmp.get_const_data();
573 detail::convert_data(this->exec_,
other.get_num_elems(),
source,
588 data_.reset(
nullptr);
608 if (exec_ ==
nullptr) {
610 "gko::Executor (nullptr)");
612 if (!this->is_owning()) {
614 "Non owning gko::array cannot be resized.");
617 if (
num_elems > 0 && this->is_owning()) {
679 array tmp(std::move(exec));
681 exec_ = std::move(tmp.exec_);
682 data_ = std::move(tmp.data_);
704 template <
typename OtherValueType>
708 std::unique_ptr<value_type[], std::function<
void(value_type[])>>;
712 std::shared_ptr<const Executor> exec_;
716template <
typename ValueType>
717using Array GKO_DEPRECATED(
"please use array") = array<ValueType>;
730template <
typename ValueType>
732 const ValueType init_val = 0);
744template <
typename ValueType>
759template <
typename ValueType>
778template <
typename ValueType>
780 std::shared_ptr<const Executor> exec,
size_type size,
const ValueType* data)
790struct temporary_clone_helper<array<T>> {
791 static std::unique_ptr<array<T>> create(
792 std::shared_ptr<const Executor> exec, array<T>* ptr,
bool copy_data)
795 return std::make_unique<array<T>>(std::move(exec), *ptr);
797 return std::make_unique<array<T>>(std::move(exec),
798 ptr->get_num_elems());
804struct temporary_clone_helper<const
array<T>> {
805 static std::unique_ptr<const array<T>> create(
806 std::shared_ptr<const Executor> exec,
const array<T>* ptr,
bool)
808 return std::make_unique<const array<T>>(std::move(exec), *ptr);
815class copy_back_deleter<
array<T>> {
817 using pointer = array<T>*;
825 copy_back_deleter(pointer original) : original_{original} {}
832 void operator()(pointer ptr)
const
855template <
typename ValueType>
856array<ValueType> array_const_cast(const_array_view<ValueType> view)
858 return array<ValueType>::view(
859 view.get_executor(), view.get_num_elems(),
860 const_cast<ValueType*
>(view.get_const_data()));
864template <
typename ValueType>
865array<ValueType> const_array_view<ValueType>::copy_to_array()
const
867 array<ValueType> result(this->get_executor(), this->get_num_elems());
868 result.get_executor()->copy_from(this->get_executor(),
869 this->get_num_elems(),
870 this->get_const_data(), result.get_data());
NotSupported is thrown in case it is not possible to perform the requested operation on the given obj...
Definition exception.hpp:156
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
std::enable_if_t<!std::is_same< ValueType, OtherValueType >::value, array > & operator=(const array< OtherValueType > &other)
Copies and converts data from another array with another data type.
Definition array.hpp:549
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:646
array(array &&other)
Moves another array.
Definition array.hpp:373
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition array.hpp:662
array & operator=(array &&other)
Moves data from another array or view.
Definition array.hpp:504
void clear() noexcept
Deallocates all data used by the array.
Definition array.hpp:585
static detail::const_array_view< ValueType > const_view(std::shared_ptr< const Executor > exec, size_type num_elems, const value_type *data)
Creates a constant (immutable) array from existing memory.
Definition array.hpp:407
array(std::shared_ptr< const Executor > exec, size_type num_elems)
Creates an array on the specified Executor.
Definition array.hpp:241
array(std::shared_ptr< const Executor > exec, const array &other)
Creates a copy of another array on a different executor.
Definition array.hpp:335
const value_type * get_const_data() const noexcept
Returns a constant pointer to the block of memory used to store the elements of the array.
Definition array.hpp:655
bool is_owning()
Tells whether this array owns its data or not.
Definition array.hpp:696
array(std::shared_ptr< const Executor > exec) noexcept
Creates an empty array tied to the specified Executor.
Definition array.hpp:228
void resize_and_reset(size_type num_elems)
Resizes the array so it is able to hold the specified number of elements.
Definition array.hpp:603
void fill(const value_type value)
Fill the array with the given value.
array< ValueType > as_view()
Returns a non-owning view of the memory owned by this array.
Definition array.hpp:418
ValueType value_type
The type of elements stored in the array.
Definition array.hpp:192
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:637
static array view(std::shared_ptr< const Executor > exec, size_type num_elems, value_type *data)
Creates an array from existing memory.
Definition array.hpp:388
detail::const_array_view< ValueType > as_const_view() const
Returns a non-owning constant view of the memory owned by this array.
Definition array.hpp:428
array & operator=(const array &other)
Copies data from another array or view.
Definition array.hpp:450
array() noexcept
Creates an empty array not tied to any executor.
Definition array.hpp:217
void set_executor(std::shared_ptr< const Executor > exec)
Changes the Executor of the array, moving the allocated data to the new Executor.
Definition array.hpp:673
array(std::shared_ptr< const Executor > exec, size_type num_elems, value_type *data)
Creates an array from existing memory.
Definition array.hpp:285
array(std::shared_ptr< const Executor > exec, size_type num_elems, value_type *data, DeleterType deleter)
Creates an array from existing memory.
Definition array.hpp:270
array(std::shared_ptr< const Executor > exec, std::initializer_list< T > init_list)
Creates an array on the specified Executor and initializes it with values.
Definition array.hpp:321
array(const array &other)
Creates a copy of another array.
Definition array.hpp:349
array(std::shared_ptr< const Executor > exec, RandomAccessIterator begin, RandomAccessIterator end)
Creates an array on the specified Executor and initializes it with values.
Definition array.hpp:301
array(std::shared_ptr< const Executor > exec, array &&other)
Moves another array to a different executor.
Definition array.hpp:360
This is a deleter that uses an executor's free method to deallocate the data.
Definition executor.hpp:1199
This is a deleter that does not delete the object.
Definition utils_helper.hpp:495
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
ValueType reduce_add(const array< ValueType > &input_arr, const ValueType init_val=0)
Reduce (sum) the values in the array.
detail::const_array_view< ValueType > make_const_array_view(std::shared_ptr< const Executor > exec, size_type size, const ValueType *data)
Helper function to create a const array view deducing the value type.
Definition array.hpp:779
array< ValueType > make_array_view(std::shared_ptr< const Executor > exec, size_type size, ValueType *data)
Helper function to create an array view deducing the value type.
Definition array.hpp:760
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
@ array
The matrix should be written as dense matrix in column-major order.