Skip to content

Commit

Permalink
add array getter/setter
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed Dec 9, 2023
1 parent 7820183 commit 16e0e9f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/ginkgo/core/base/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 16e0e9f

Please sign in to comment.