5 #ifndef GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
6 #define GKO_PUBLIC_CORE_BASE_ARRAY_HPP_
12 #include <type_traits>
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>
25 template <
typename ValueType>
38 template <
typename SourceType,
typename TargetType>
39 void convert_data(std::shared_ptr<const Executor> exec,
size_type size,
40 const SourceType* src, TargetType* dst);
52 template <
typename ValueType>
53 class const_array_view {
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; }
132 array<ValueType> copy_to_array()
const;
135 std::shared_ptr<const Executor> exec_;
137 const ValueType* data_;
141 template <
typename ValueType>
142 using ConstArrayView GKO_DEPRECATED(
"please use const_array_view") =
143 const_array_view<ValueType>;
146 template <
typename ValueType>
147 array<ValueType> array_const_cast(const_array_view<ValueType> view);
165 template <
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)
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) {
432 exec_ = other.get_executor();
433 data_ = data_manager{
nullptr, other.data_.get_deleter()};
435 if (other.get_executor() ==
nullptr) {
443 GKO_ENSURE_COMPATIBLE_BOUNDS(other.get_size(), this->
get_size());
445 exec_->copy_from(other.get_executor(), other.get_size(),
446 other.get_const_data(), this->
get_data());
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) {
527 this->exec_ = other.get_executor();
530 if (other.get_executor() ==
nullptr) {
535 if (this->is_owning()) {
536 this->resize_and_reset(other.get_size());
538 GKO_ENSURE_COMPATIBLE_BOUNDS(other.get_size(), this->get_size());
541 const OtherValueType* source = other.get_const_data();
543 if (this->exec_ != other.get_executor()) {
545 source = tmp.get_const_data();
547 detail::convert_data(this->exec_, other.get_size(), source,
571 if (this->exec_ ==
nullptr) {
572 this->exec_ = other.get_executor();
575 if (other.get_executor() ==
nullptr) {
580 if (this->is_owning()) {
581 this->resize_and_reset(other.get_size());
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)");
631 if (!this->is_owning()) {
633 "Non owning gko::array cannot be resized.");
636 if (size > 0 && this->is_owning()) {
649 void fill(
const value_type value);
663 GKO_DEPRECATED(
"use get_size() instead")
664 size_type get_num_elems() const noexcept {
return get_size(); }
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_;
743 template <
typename ValueType>
744 using Array GKO_DEPRECATED(
"please use array") = array<ValueType>;
757 template <
typename ValueType>
758 ValueType reduce_add(
const array<ValueType>& input_arr,
759 const ValueType init_val = 0);
771 template <
typename ValueType>
772 void reduce_add(
const array<ValueType>& input_arr, array<ValueType>& result);
786 template <
typename ValueType>
805 template <
typename ValueType>
807 std::shared_ptr<const Executor> exec,
size_type size,
const ValueType* data)
816 template <
typename T>
817 struct 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());
829 template <
typename T>
830 struct 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);
840 template <
typename T>
841 class 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;
861 template <
typename ValueType>
862 array<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()));
870 template <
typename ValueType>
871 array<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());
884 #endif // GKO_PUBLIC_CORE_BASE_ARRAY_HPP_