5 #ifndef GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
6 #define GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
12 #include <type_traits>
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>
26 template <
typename ValueType>
39 template <
typename SourceType,
typename TargetType>
40 void convert_data(std::shared_ptr<const Executor> exec,
size_type size,
41 const SourceType* src, TargetType* dst);
53 template <
typename ValueType>
54 class const_array_view {
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; }
133 array<ValueType> copy_to_array()
const;
136 std::shared_ptr<const Executor> exec_;
138 const ValueType* data_;
142 template <
typename ValueType>
143 using ConstArrayView GKO_DEPRECATED(
"please use const_array_view") =
144 const_array_view<ValueType>;
147 template <
typename ValueType>
148 array<ValueType> array_const_cast(const_array_view<ValueType> view);
166 template <
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()) {
650 void fill(
const value_type value);
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_;
744 template <
typename ValueType>
745 using Array GKO_DEPRECATED(
"please use array") = array<ValueType>;
758 template <
typename ValueType>
759 ValueType reduce_add(
const array<ValueType>& input_arr,
760 const ValueType init_val = 0);
772 template <
typename ValueType>
773 void reduce_add(
const array<ValueType>& input_arr, array<ValueType>& result);
787 template <
typename ValueType>
806 template <
typename ValueType>
808 std::shared_ptr<const Executor> exec,
size_type size,
const ValueType* data)
817 template <
typename T>
818 struct 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());
830 template <
typename T>
831 struct 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);
841 template <
typename T>
842 class 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;
862 template <
typename ValueType>
863 array<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()));
871 template <
typename ValueType>
872 array<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());
885 #endif // GKO_PUBLIC_CORE_BASE_ARRAY_HPP_