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()) {
651 std::vector<value_type> result(this->get_size());
653 this->get_size(), result.data());
663 void fill(
const value_type value);
677 GKO_DEPRECATED(
"use get_size() instead")
678 size_type get_num_elems() const noexcept {
return get_size(); }
720 array tmp(std::move(exec));
722 exec_ = std::move(tmp.exec_);
723 data_ = std::move(tmp.data_);
745 template <
typename OtherValueType>
749 std::unique_ptr<value_type[], std::function<void(value_type[])>>;
753 std::shared_ptr<const Executor> exec_;
757 template <
typename ValueType>
758 using Array GKO_DEPRECATED(
"please use array") = array<ValueType>;
771 template <
typename ValueType>
772 ValueType reduce_add(
const array<ValueType>& input_arr,
773 const ValueType init_val = 0);
785 template <
typename ValueType>
786 void reduce_add(
const array<ValueType>& input_arr, array<ValueType>& result);
800 template <
typename ValueType>
819 template <
typename ValueType>
821 std::shared_ptr<const Executor> exec,
size_type size,
const ValueType* data)
830 template <
typename T>
831 struct temporary_clone_helper<array<T>> {
832 static std::unique_ptr<array<T>> create(
833 std::shared_ptr<const Executor> exec, array<T>* ptr,
bool copy_data)
836 return std::make_unique<array<T>>(std::move(exec), *ptr);
838 return std::make_unique<array<T>>(std::move(exec), ptr->get_size());
843 template <
typename T>
844 struct temporary_clone_helper<const
array<T>> {
845 static std::unique_ptr<const array<T>> create(
846 std::shared_ptr<const Executor> exec,
const array<T>* ptr,
bool)
848 return std::make_unique<const array<T>>(std::move(exec), *ptr);
854 template <
typename T>
855 class copy_back_deleter<
array<T>>
856 :
public copy_back_deleter_from_assignment<array<T>> {
858 using copy_back_deleter_from_assignment<
859 array<T>>::copy_back_deleter_from_assignment;
875 template <
typename ValueType>
876 array<ValueType> array_const_cast(const_array_view<ValueType> view)
878 return array<ValueType>::view(
879 view.get_executor(), view.get_size(),
880 const_cast<ValueType*>(view.get_const_data()));
884 template <
typename ValueType>
885 array<ValueType> const_array_view<ValueType>::copy_to_array()
const
887 array<ValueType> result(this->get_executor(), this->get_size());
888 result.get_executor()->copy_from(this->get_executor(), this->get_size(),
889 this->get_const_data(), result.get_data());
898 #endif // GKO_PUBLIC_CORE_BASE_ARRAY_HPP_