diff --git a/include/ginkgo/core/base/array.hpp b/include/ginkgo/core/base/array.hpp index 467c0bfc0e1..c1efa14d899 100644 --- a/include/ginkgo/core/base/array.hpp +++ b/include/ginkgo/core/base/array.hpp @@ -110,6 +110,44 @@ class const_array_view { */ const value_type* get_const_data() const noexcept { return data_; } + /** + * Returns a single value from the array. + * This involves a bounds check, polymorphic calls and potentially a + * device-to-host copy, so it is not suitable for accessing many elements + * with high performance. + * + * @param index the array element index. + * @return the value at index. + */ + value_type get_value(size_type index) const + { + if (index < 0 || index >= this->get_num_elems()) { + throw OutOfBoundsError{__FILE__, __LINE__, index, + this->get_num_elems()}; + } + return this->get_executor()->copy_val_to_host(this->get_const_data() + + index); + } + + /** + * Sets a single entry in the array to a new value. + * This involves a bounds check, polymorphic calls and potentially a + * host-to-device copy, so it is not suitable for accessing many elements + * with high performance. + * + * @param index the array element index. + * @param value the new value. + */ + void set_value(size_type index, value_type value) const + { + if (index < 0 || index >= this->get_num_elems()) { + throw OutOfBoundsError{__FILE__, __LINE__, index, + this->get_num_elems()}; + } + this->get_executor()->copy_from(this->get_executor()->get_master(), 1, + &value, this->get_data() + index); + } + /** * Returns the Executor associated with the array view. *