OffsetView¶
Header File: Kokkos_OffsetView.hpp
Description¶
An OffsetView can be used when the indices of an array begin at something other than zero.
Its indices range from the begin index (inclusive) to the end index (exclusive) for each dimension.
Warning
The OffsetView is in the Experimental namespace.
Interface¶
-
template<class DataType, class ...Properties>
class OffsetView¶ An
OffsetViewis aViewwhose indices begin at arbitrary values per dimension. It derives fromViewand has the same template parameters.Constructors:
-
OffsetView()¶
The default constructor shares the properties of
View. Additionally, the begins are set to zero.
-
template<class RT, class ...RP>
OffsetView(const OffsetView<RT, RP...> &rhs)¶ Copy constructor with a compatible
OffsetView.See also
-
template<class RT, class ...RP>
OffsetView(const View<RT, RP...> &aview, const begins_type &begins)¶ Constructs an
OffsetViewreferencing the data ofaviewwith indices of dimensionrin the range[begin(r),begin(r) + extent(r)).- Parameters:
aview – the
Viewto reference.begins – the first index of each dimension.
-
template<class Begins, class Ends>
explicit OffsetView(const std::string &name, const Begins &begins, const Ends &ends)¶ Constructs an
OffsetViewwith indices of dimensionrin the range[begin(r),end(r)).- Template Parameters:
Begins –
std::initializer_list<int64_t>,Kokkos::Array,std::arrayorstd::spanwith a static extent.Ends –
std::initializer_list<int64_t>,Kokkos::Array,std::arrayorstd::spanwith a static extent.
- Parameters:
name – same as for
View.begins – the first index of each dimension (inclusive).
ends – the end index of each dimension (exclusive).
Requirements:
-
template<class Begins, class Ends>
explicit OffsetView(const ALLOC_PROP &prop, const Begins &begins, const Ends &ends)¶ Allocating constructor with allocation properties (created by a call to
view_alloc()).- Template Parameters:
Begins –
std::initializer_list<int64_t>,Kokkos::Array,std::arrayorstd::spanwith a static extent.Ends –
std::initializer_list<int64_t>,Kokkos::Array,std::arrayorstd::spanwith a static extent.
- Parameters:
prop – same as for
View.begins – the first index of each dimension (inclusive).
ends – the end index of each dimension (exclusive).
-
template<class Begins, class Ends>
OffsetView(const pointer_type &ptr, const Begins &begins, const Ends &ends)¶ Unmanaged data wrapping constructor. Indices of dimension
rare in the range[begin(r),end(r)).- Template Parameters:
Begins –
std::initializer_list<int64_t>,Kokkos::Array,std::arrayorstd::spanwith a static extent.Ends –
std::initializer_list<int64_t>,Kokkos::Array,std::arrayorstd::spanwith a static extent.
- Parameters:
ptr – pointer to a user provided memory allocation.
begins – the first index of each dimension (inclusive).
ends – the end index of each dimension (exclusive).
Requirements:
Public Member Functions:
-
template<class ...OtherIndexTypes>
constexpr reference_type operator()(OtherIndexTypes... indices) const¶ - Template Parameters:
OtherIndexTypes – types convertible to
index_type.- Parameters:
indices – the indices of the element to get a reference to, starting at
begin()of each dimension.- Returns:
a reference to the element at the given indices.
-
template<class iType>
int64_t begin(const iType dim) const¶ - Template Parameters:
iType – an integral type.
- Parameters:
dim – the dimension to get the first index of.
- Returns:
the first index of dimension
dim.
-
view_type view() const¶
- Returns:
a
Viewwith the properties of the correspondingOffsetView.
-
OffsetView()¶
Non-Member Functions¶
-
template<class LT, class ...LP, class RT, class ...RP>
bool operator==(const OffsetView<LT, LP...> &lhs, const OffsetView<RT, RP...> &rhs)¶ - Returns:
trueiflhs.view() == rhs.view()andlhs.begins() == rhs.begins().
-
template<class LT, class ...LP, class RT, class ...RP>
bool operator!=(const OffsetView<LT, LP...> &lhs, const OffsetView<RT, RP...> &rhs)¶
-
template<class D, class ...P, class ...Args>
auto subview(const OffsetView<D, P...> &src, Args... args)¶ Creates an
OffsetViewreferencing a subset of the data ofsrc.- Parameters:
src – the
OffsetViewto take the subview of.args – one slice per dimension of
src, given in the indices ofsrc. Accepted are an integral index,Kokkos::ALL(), or aKokkos::pair<T, T>orstd::pair<T, T>with an integralT.
- Returns:
an
OffsetView. A dimension sliced withKokkos::ALL()keeps its begin index, the indices of all other dimensions start at0.
See also
Examples¶
#include <iostream>
#include <Kokkos_Core.hpp>
#include <Kokkos_OffsetView.hpp>
int main(int argc, char* argv[]) {
Kokkos::initialize(argc, argv);
{
std::cout << std::boolalpha;
using Kokkos::Experimental::OffsetView;
OffsetView<int64_t*> ov("ov", {-2}, {3});
Kokkos::View<int64_t*> v = ov.view();
Kokkos::parallel_for(
"WriteIndices", Kokkos::RangePolicy(ov.begin(0), ov.end(0)),
KOKKOS_LAMBDA(const int64_t i) { ov(i) = i; });
auto ov_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), ov);
auto v_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), v);
std::cout << "ov_h(-1) == v_h(1): " << (ov_h(-1) == v_h(1)) << '\n';
std::cout << "ov_h(-1): " << ov_h(-1) << '\n';
std::cout << "v_h(1): " << v_h(1) << '\n';
std::cout << '\n';
// The begin index of a sub range is set to zero
auto sub = Kokkos::subview(ov, Kokkos::pair<int64_t, int64_t>{-1, 2});
auto sub_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sub);
std::cout << "sub_h(0) == ov_h(-1): " << (sub_h(0) == ov_h(-1)) << '\n';
std::cout << "sub_h(0): " << sub_h(0) << '\n';
std::cout << "ov_h(-1): " << ov_h(-1) << '\n';
std::cout << '\n';
// The begin index of a full range is kept
auto sub_all = Kokkos::subview(ov, Kokkos::ALL());
auto sub_all_h =
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sub_all);
std::cout << "sub_all_h(-2) == ov_h(-2): " << (sub_all_h(-2) == ov_h(-2))
<< '\n';
std::cout << "sub_all_h(-2) = " << sub_all_h(-2) << '\n';
std::cout << "ov_h(-2) = " << ov_h(-2) << '\n';
}
Kokkos::finalize();
}