5#ifndef GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
6#define GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
15#include <ginkgo/core/base/exception.hpp>
16#include <ginkgo/core/base/exception_helpers.hpp>
17#include <ginkgo/core/base/executor.hpp>
18#include <ginkgo/core/base/types.hpp>
19#include <ginkgo/core/base/utils.hpp>
25template <
typename ValueType>
38template <
typename SourceType,
typename TargetType>
39void convert_data(std::shared_ptr<const Executor> exec,
size_type size,
40 const SourceType* src, TargetType* dst);
52template <
typename ValueType>
53class const_array_view {
58 using value_type = ValueType;
67 const_array_view(std::shared_ptr<const Executor> exec,
size_type size,
68 const ValueType* data)
69 : exec_{std::move(exec)}, size_{size}, data_{data}
76 const_array_view& operator=(
const const_array_view&) =
delete;
77 const_array_view& operator=(const_array_view&&) =
delete;
78 const_array_view(
const const_array_view&) =
delete;
83 const_array_view(const_array_view&& other)
84 : const_array_view{other.exec_, other.size_, other.data_}
87 other.data_ =
nullptr;
95 size_type get_size() const noexcept {
return size_; }
102 GKO_DEPRECATED(
"use get_size() instead")
103 size_type get_num_elems() const noexcept {
return get_size(); }
110 const value_type* get_const_data() const noexcept {
return data_; }
117 std::shared_ptr<const Executor> get_executor() const noexcept
125 bool is_owning() const noexcept {
return false; }
135 std::shared_ptr<const Executor> exec_;
137 const ValueType* data_;
141template <
typename ValueType>
142using ConstArrayView GKO_DEPRECATED(
"please use const_array_view") =
143 const_array_view<ValueType>;
146template <
typename ValueType>
165template <
typename ValueType>
205 explicit array(std::shared_ptr<const Executor> exec) noexcept
208 exec_(std::move(exec))
221 exec_(
std::move(exec))
246 template <
typename DeleterType>
249 : size_{size}, data_(data, deleter), exec_{exec}
277 template <
typename RandomAccessIterator>
278 array(std::shared_ptr<const Executor> exec, RandomAccessIterator begin,
279 RandomAccessIterator end)
282 array tmp(exec->get_master(), std::distance(begin, end));
283 std::copy(begin, end, tmp.data_.get());
284 *
this = std::move(tmp);
297 template <
typename T>
298 array(std::shared_ptr<const Executor> exec,
299 std::initializer_list<T> init_list)
300 : array(exec, begin(init_list), end(init_list))
312 array(std::shared_ptr<const Executor> exec,
const array& other)
337 array(std::shared_ptr<const Executor> exec, array&& other) : array(exec)
339 *
this = std::move(other);
385 std::shared_ptr<const Executor> exec,
size_type size,
388 return {exec, size, data};
428 if (&other ==
this) {
431 if (exec_ ==
nullptr) {
433 data_ = data_manager{
nullptr, other.data_.get_deleter()};
443 GKO_ENSURE_COMPATIBLE_BOUNDS(other.
get_size(), this->get_size());
481 if (&other ==
this) {
484 if (exec_ ==
nullptr) {
485 exec_ = other.get_executor();
488 if (other.get_executor() ==
nullptr) {
492 if (exec_ == other.get_executor()) {
494 data_ = std::exchange(
495 other.data_, data_manager{nullptr, default_deleter{exec_}});
496 size_ = std::exchange(other.size_, 0);
522 template <
typename OtherValueType>
523 std::enable_if_t<!std::is_same<ValueType, OtherValueType>::value,
array>&
526 if (this->exec_ ==
nullptr) {
538 GKO_ENSURE_COMPATIBLE_BOUNDS(other.
get_size(), this->get_size());
540 array<OtherValueType> tmp{this->exec_};
547 detail::convert_data(this->exec_, other.
get_size(), source,
569 array&
operator=(
const detail::const_array_view<ValueType>& other)
571 if (this->exec_ ==
nullptr) {
572 this->exec_ = other.get_executor();
575 if (other.get_executor() ==
nullptr) {
583 GKO_ENSURE_COMPATIBLE_BOUNDS(other.get_size(), this->get_size());
585 array tmp{this->exec_};
586 const ValueType* source = other.get_const_data();
588 if (this->exec_ != other.get_executor()) {
589 tmp = other.copy_to_array();
590 source = tmp.get_const_data();
592 exec_->copy_from(other.get_executor(), other.get_size(), source,
607 data_.reset(
nullptr);
627 if (exec_ ==
nullptr) {
629 "gko::Executor (nullptr)");
633 "Non owning gko::array cannot be resized.");
663 GKO_DEPRECATED(
"use get_size() instead")
706 array tmp(std::move(exec));
708 exec_ = std::move(tmp.exec_);
709 data_ = std::move(tmp.data_);
731 template <
typename OtherValueType>
735 std::unique_ptr<value_type[], std::function<void(value_type[])>>;
739 std::shared_ptr<const Executor> exec_;
743template <
typename ValueType>
744using Array GKO_DEPRECATED(
"please use array") = array<ValueType>;
757template <
typename ValueType>
759 const ValueType init_val = 0);
771template <
typename ValueType>
786template <
typename ValueType>
805template <
typename ValueType>
807 std::shared_ptr<const Executor> exec,
size_type size,
const ValueType* data)
817struct temporary_clone_helper<array<T>> {
818 static std::unique_ptr<array<T>> create(
819 std::shared_ptr<const Executor> exec, array<T>* ptr,
bool copy_data)
822 return std::make_unique<array<T>>(std::move(exec), *ptr);
824 return std::make_unique<array<T>>(std::move(exec), ptr->get_size());
830struct temporary_clone_helper<const
array<T>> {
831 static std::unique_ptr<const array<T>> create(
832 std::shared_ptr<const Executor> exec,
const array<T>* ptr,
bool)
834 return std::make_unique<const array<T>>(std::move(exec), *ptr);
841class copy_back_deleter<
array<T>>
842 :
public copy_back_deleter_from_assignment<array<T>> {
844 using copy_back_deleter_from_assignment<
845 array<T>>::copy_back_deleter_from_assignment;
861template <
typename ValueType>
862array<ValueType> array_const_cast(const_array_view<ValueType> view)
864 return array<ValueType>::view(
865 view.get_executor(), view.get_size(),
866 const_cast<ValueType*
>(view.get_const_data()));
870template <
typename ValueType>
871array<ValueType> const_array_view<ValueType>::copy_to_array()
const
873 array<ValueType> result(this->get_executor(), this->get_size());
874 result.get_executor()->copy_from(this->get_executor(), this->get_size(),
875 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:127
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
void resize_and_reset(size_type size)
Resizes the array so it is able to hold the specified number of elements.
Definition array.hpp:622
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:524
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:673
array(array &&other)
Moves another array.
Definition array.hpp:350
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition array.hpp:689
array & operator=(array &&other)
Moves data from another array or view.
Definition array.hpp:479
null_deleter< value_type[]> view_deleter
The deleter type used for views.
Definition array.hpp:181
void clear() noexcept
Deallocates all data used by the array.
Definition array.hpp:604
array(std::shared_ptr< const Executor > exec, const array &other)
Creates a copy of another array on a different executor.
Definition array.hpp:312
static detail::const_array_view< ValueType > const_view(std::shared_ptr< const Executor > exec, size_type size, const value_type *data)
Creates a constant (immutable) array from existing memory.
Definition array.hpp:384
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:682
bool is_owning()
Tells whether this array owns its data or not.
Definition array.hpp:723
executor_deleter< value_type[]> default_deleter
The default deleter type used by array.
Definition array.hpp:176
array(std::shared_ptr< const Executor > exec) noexcept
Creates an empty array tied to the specified Executor.
Definition array.hpp:205
void fill(const value_type value)
Fill the array with the given value.
array(std::shared_ptr< const Executor > exec, size_type size, value_type *data, DeleterType deleter)
Creates an array from existing memory.
Definition array.hpp:247
array< ValueType > as_view()
Returns a non-owning view of the memory owned by this array.
Definition array.hpp:395
ValueType value_type
The type of elements stored in the array.
Definition array.hpp:171
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:664
array(std::shared_ptr< const Executor > exec, size_type size)
Creates an array on the specified Executor.
Definition array.hpp:218
static array view(std::shared_ptr< const Executor > exec, size_type size, value_type *data)
Creates an array from existing memory.
Definition array.hpp:365
array(std::shared_ptr< const Executor > exec, size_type size, value_type *data)
Creates an array from existing memory.
Definition array.hpp:262
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:404
array & operator=(const array &other)
Copies data from another array or view.
Definition array.hpp:426
array() noexcept
Creates an empty array not tied to any executor.
Definition array.hpp:196
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:700
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition array.hpp:656
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:298
array(const array &other)
Creates a copy of another array.
Definition array.hpp:326
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:278
array & operator=(const detail::const_array_view< ValueType > &other)
Copies data from a const_array_view.
Definition array.hpp:569
array(std::shared_ptr< const Executor > exec, array &&other)
Moves another array to a different executor.
Definition array.hpp:337
This is a deleter that uses an executor's free method to deallocate the data.
Definition executor.hpp:1217
This is a deleter that does not delete the object.
Definition utils_helper.hpp:465
The Ginkgo namespace.
Definition abstract_factory.hpp:20
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:806
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:787
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:89
@ array
The matrix should be written as dense matrix in column-major order.
Definition mtx_io.hpp:96