5#ifndef GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
6#define GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
16#include <ginkgo/core/base/exception.hpp>
17#include <ginkgo/core/base/exception_helpers.hpp>
18#include <ginkgo/core/base/executor.hpp>
19#include <ginkgo/core/base/types.hpp>
20#include <ginkgo/core/base/utils.hpp>
26template <
typename ValueType>
39template <
typename SourceType,
typename TargetType>
40void convert_data(std::shared_ptr<const Executor> exec,
size_type size,
41 const SourceType* src, TargetType* dst);
53template <
typename ValueType>
54class const_array_view {
59 using value_type = ValueType;
68 const_array_view(std::shared_ptr<const Executor> exec,
size_type size,
69 const ValueType* data)
70 : exec_{std::move(exec)}, size_{size}, data_{data}
77 const_array_view& operator=(
const const_array_view&) =
delete;
78 const_array_view& operator=(const_array_view&&) =
delete;
79 const_array_view(
const const_array_view&) =
delete;
84 const_array_view(const_array_view&& other)
85 : const_array_view{other.exec_, other.size_, other.data_}
88 other.data_ =
nullptr;
96 size_type get_size() const noexcept {
return size_; }
103 GKO_DEPRECATED(
"use get_size() instead")
104 size_type get_num_elems() const noexcept {
return get_size(); }
111 const value_type* get_const_data() const noexcept {
return data_; }
118 std::shared_ptr<const Executor> get_executor() const noexcept
126 bool is_owning() const noexcept {
return false; }
136 std::shared_ptr<const Executor> exec_;
138 const ValueType* data_;
142template <
typename ValueType>
143using ConstArrayView GKO_DEPRECATED(
"please use const_array_view") =
144 const_array_view<ValueType>;
147template <
typename ValueType>
166template <
typename ValueType>
206 explicit array(std::shared_ptr<const Executor> exec) noexcept
209 exec_(std::move(exec))
222 exec_(std::move(exec))
247 template <
typename DeleterType>
250 : size_{size}, data_(data, deleter), exec_{exec}
278 template <
typename RandomAccessIterator>
279 array(std::shared_ptr<const Executor> exec, RandomAccessIterator begin,
280 RandomAccessIterator end)
283 array tmp(exec->get_master(), std::distance(begin, end));
284 std::copy(begin, end, tmp.data_.get());
285 *
this = std::move(tmp);
298 template <
typename T>
299 array(std::shared_ptr<const Executor> exec,
300 std::initializer_list<T> init_list)
301 :
array(exec, begin(init_list), end(init_list))
313 array(std::shared_ptr<const Executor> exec,
const array& other)
340 *
this = std::move(other);
386 std::shared_ptr<const Executor> exec,
size_type size,
389 return {exec, size, data};
429 if (&other ==
this) {
432 if (exec_ ==
nullptr) {
433 exec_ = other.get_executor();
434 data_ = data_manager{
nullptr, other.data_.get_deleter()};
436 if (other.get_executor() ==
nullptr) {
444 GKO_ENSURE_COMPATIBLE_BOUNDS(other.get_size(), this->get_size());
446 exec_->copy_from(other.get_executor(), other.get_size(),
447 other.get_const_data(), this->get_data());
482 if (&other ==
this) {
485 if (exec_ ==
nullptr) {
486 exec_ = other.get_executor();
489 if (other.get_executor() ==
nullptr) {
493 if (exec_ == other.get_executor()) {
495 data_ = std::exchange(
496 other.data_, data_manager{nullptr, default_deleter{exec_}});
497 size_ = std::exchange(other.size_, 0);
523 template <
typename OtherValueType>
524 std::enable_if_t<!std::is_same<ValueType, OtherValueType>::value,
array>&
527 if (this->exec_ ==
nullptr) {
528 this->exec_ = other.get_executor();
531 if (other.get_executor() ==
nullptr) {
536 if (this->is_owning()) {
537 this->resize_and_reset(other.get_size());
539 GKO_ENSURE_COMPATIBLE_BOUNDS(other.get_size(), this->get_size());
542 const OtherValueType* source = other.get_const_data();
544 if (this->exec_ != other.get_executor()) {
546 source = tmp.get_const_data();
548 detail::convert_data(this->exec_, other.get_size(), source,
572 if (this->exec_ ==
nullptr) {
573 this->exec_ = other.get_executor();
576 if (other.get_executor() ==
nullptr) {
581 if (this->is_owning()) {
582 this->resize_and_reset(other.get_size());
584 GKO_ENSURE_COMPATIBLE_BOUNDS(other.get_size(), this->get_size());
586 array tmp{this->exec_};
587 const ValueType* source = other.get_const_data();
589 if (this->exec_ != other.get_executor()) {
590 tmp = other.copy_to_array();
591 source = tmp.get_const_data();
593 exec_->copy_from(other.get_executor(), other.get_size(), source,
608 data_.reset(
nullptr);
628 if (exec_ ==
nullptr) {
630 "gko::Executor (nullptr)");
632 if (!this->is_owning()) {
634 "Non owning gko::array cannot be resized.");
637 if (size > 0 && this->is_owning()) {
664 GKO_DEPRECATED(
"use get_size() instead")
665 size_type get_num_elems() const noexcept {
return get_size(); }
707 array tmp(std::move(exec));
709 exec_ = std::move(tmp.exec_);
710 data_ = std::move(tmp.data_);
732 template <
typename OtherValueType>
736 std::unique_ptr<value_type[], std::function<void(value_type[])>>;
740 std::shared_ptr<const Executor> exec_;
744template <
typename ValueType>
745using Array GKO_DEPRECATED(
"please use array") = array<ValueType>;
758template <
typename ValueType>
760 const ValueType init_val = 0);
772template <
typename ValueType>
787template <
typename ValueType>
806template <
typename ValueType>
808 std::shared_ptr<const Executor> exec,
size_type size,
const ValueType* data)
818struct temporary_clone_helper<array<T>> {
819 static std::unique_ptr<array<T>> create(
820 std::shared_ptr<const Executor> exec, array<T>* ptr,
bool copy_data)
823 return std::make_unique<array<T>>(std::move(exec), *ptr);
825 return std::make_unique<array<T>>(std::move(exec), ptr->get_size());
831struct temporary_clone_helper<const
array<T>> {
832 static std::unique_ptr<const array<T>> create(
833 std::shared_ptr<const Executor> exec,
const array<T>* ptr,
bool)
835 return std::make_unique<const array<T>>(std::move(exec), *ptr);
842class copy_back_deleter<
array<T>>
843 :
public copy_back_deleter_from_assignment<array<T>> {
845 using copy_back_deleter_from_assignment<
846 array<T>>::copy_back_deleter_from_assignment;
862template <
typename ValueType>
863array<ValueType> array_const_cast(const_array_view<ValueType> view)
865 return array<ValueType>::view(
866 view.get_executor(), view.get_size(),
867 const_cast<ValueType*
>(view.get_const_data()));
871template <
typename ValueType>
872array<ValueType> const_array_view<ValueType>::copy_to_array()
const
874 array<ValueType> result(this->get_executor(), this->get_size());
875 result.get_executor()->copy_from(this->get_executor(), this->get_size(),
876 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:128
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition logger.hpp:25
void resize_and_reset(size_type size)
Resizes the array so it is able to hold the specified number of elements.
Definition array.hpp:623
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:525
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:674
array(array &&other)
Moves another array.
Definition array.hpp:351
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition array.hpp:690
array & operator=(array &&other)
Moves data from another array or view.
Definition array.hpp:480
void clear() noexcept
Deallocates all data used by the array.
Definition array.hpp:605
array(std::shared_ptr< const Executor > exec, const array &other)
Creates a copy of another array on a different executor.
Definition array.hpp:313
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:385
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:683
bool is_owning()
Tells whether this array owns its data or not.
Definition array.hpp:724
array(std::shared_ptr< const Executor > exec) noexcept
Creates an empty array tied to the specified Executor.
Definition array.hpp:206
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:248
array< ValueType > as_view()
Returns a non-owning view of the memory owned by this array.
Definition array.hpp:396
ValueType value_type
The type of elements stored in the array.
Definition array.hpp:172
array(std::shared_ptr< const Executor > exec, size_type size)
Creates an array on the specified Executor.
Definition array.hpp:219
static array view(std::shared_ptr< const Executor > exec, size_type size, value_type *data)
Creates an array from existing memory.
Definition array.hpp:366
array(std::shared_ptr< const Executor > exec, size_type size, value_type *data)
Creates an array from existing memory.
Definition array.hpp:263
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:405
array & operator=(const array &other)
Copies data from another array or view.
Definition array.hpp:427
array() noexcept
Creates an empty array not tied to any executor.
Definition array.hpp:197
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:701
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition array.hpp:657
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:299
array(const array &other)
Creates a copy of another array.
Definition array.hpp:327
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:279
array & operator=(const detail::const_array_view< ValueType > &other)
Copies data from a const_array_view.
Definition array.hpp:570
array(std::shared_ptr< const Executor > exec, array &&other)
Moves another array to a different executor.
Definition array.hpp:338
This is a deleter that uses an executor's free method to deallocate the data.
Definition executor.hpp:1171
This is a deleter that does not delete the object.
Definition utils_helper.hpp:467
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:807
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:788
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:86
@ array
The matrix should be written as dense matrix in column-major order.