33 #ifndef GKO_CORE_BASE_ARRAY_H_ 34 #define GKO_CORE_BASE_ARRAY_H_ 41 #include <ginkgo/core/base/exception.hpp> 42 #include <ginkgo/core/base/executor.hpp> 43 #include <ginkgo/core/base/types.hpp> 44 #include <ginkgo/core/base/utils.hpp> 62 template <
typename ValueType>
104 Array(std::shared_ptr<const Executor> exec) noexcept
107 exec_(std::move(exec))
118 : num_elems_(num_elems),
120 exec_(std::move(exec))
123 data_.reset(exec_->alloc<
value_type>(num_elems));
145 template <
typename DeleterType>
148 : num_elems_{num_elems}, data_(data, deleter), exec_{exec}
176 template <
typename RandomAccessIterator>
177 Array(std::shared_ptr<const Executor> exec, RandomAccessIterator begin,
178 RandomAccessIterator end)
181 Array tmp(exec->get_master(), end - begin);
183 for (
auto it = begin; it != end; ++it, ++i) {
186 *
this = std::move(tmp);
199 template <
typename T>
200 Array(std::shared_ptr<const Executor> exec,
201 std::initializer_list<T> init_list)
202 :
Array(exec, begin(init_list), end(init_list))
214 Array(std::shared_ptr<const Executor> exec,
const Array &other)
241 *
this = std::move(other);
287 if (&other ==
this) {
290 if (exec_ ==
nullptr) {
292 data_ = data_manager{
nullptr, other.data_.get_deleter()};
299 exec_->copy_from(other.
get_executor().get(), num_elems_,
319 if (&other ==
this) {
322 if (exec_ ==
nullptr) {
323 exec_ = other.get_executor();
324 data_ = data_manager{
nullptr, other.data_.get_deleter()};
326 if (other.get_executor() ==
nullptr) {
330 if (exec_ == other.get_executor() &&
331 data_.get_deleter().target_type() !=
typeid(
view_deleter)) {
334 swap(data_, other.data_);
335 swap(num_elems_, other.num_elems_);
353 data_.reset(
nullptr);
368 if (num_elems == num_elems_) {
371 if (exec_ ==
nullptr) {
373 "gko::Executor (nullptr)");
375 num_elems_ = num_elems;
377 data_.reset(exec_->alloc<
value_type>(num_elems));
379 data_.reset(
nullptr);
430 Array tmp(std::move(exec));
432 exec_ = std::move(tmp.exec_);
433 data_ = std::move(tmp.data_);
438 std::unique_ptr<value_type[], std::function<void(value_type[])>>;
442 std::shared_ptr<const Executor> exec_;
449 #endif // GKO_CORE_BASE_ARRAY_H_ size_type get_num_elems() const noexcept
Returns the number of elements in the Array.
Definition: array.hpp:388
Array(std::shared_ptr< const Executor > exec, const Array &other)
Creates a copy of another array on a different executor.
Definition: array.hpp:214
null_deleter< value_type[]> view_deleter
The deleter type used for views.
Definition: array.hpp:78
Array & operator=(const Array &other)
Copies data from another array.
Definition: array.hpp:285
Array(std::shared_ptr< const Executor > exec, Array &&other)
Moves another array to a different executor.
Definition: array.hpp:239
This is a deleter that uses an executor's free method to deallocate the data.
Definition: executor.hpp:628
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
Array & operator=(Array &&other)
Moves data from another array.
Definition: array.hpp:317
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition: array.hpp:413
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:406
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
index_type value_type
The type of elements stored in the array.
Definition: array.hpp:68
Array(std::shared_ptr< const Executor > exec, size_type num_elems, value_type *data)
Creates an Array from existing memory.
Definition: array.hpp:161
Array() noexcept
Creates an empty Array not tied to any executor.
Definition: array.hpp:93
Array(Array &&other)
Moves another array.
Definition: array.hpp:252
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:177
Array(std::shared_ptr< const Executor > exec, size_type num_elems)
Creates an Array on the specified Executor.
Definition: array.hpp:117
This is a deleter that does not delete the object.
Definition: utils.hpp:325
An Array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the Arr...
Definition: array.hpp:63
void resize_and_reset(size_type num_elems)
Resizes the array so it is able to hold the specified number of elements.
Definition: array.hpp:366
Array(std::shared_ptr< const Executor > exec) noexcept
Creates an empty Array tied to the specified Executor.
Definition: array.hpp:104
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:200
Array(const Array &other)
Creates a copy of another array.
Definition: array.hpp:228
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the Array. ...
Definition: array.hpp:397
Array(std::shared_ptr< const Executor > exec, size_type num_elems, value_type *data, DeleterType deleter)
Creates an Array from existing memory.
Definition: array.hpp:146
void clear() noexcept
Deallocates all data used by the Array.
Definition: array.hpp:350
NotSupported is thrown in case it is not possible to perform the requested operation on the given obj...
Definition: exception.hpp:153
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:424
static Array view(std::shared_ptr< const Executor > exec, size_type num_elems, value_type *data)
Creates an Array from existing memory.
Definition: array.hpp:266