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 OffsetView is a View whose indices begin at arbitrary values per dimension. It derives from View and 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

Assignment Rules

template<class RT, class ...RP>
OffsetView(const View<RT, RP...> &aview, const begins_type &begins)

Constructs an OffsetView referencing the data of aview with indices of dimension r in the range [begin(r),begin(r) + extent(r)).

Parameters:
  • aview – the View to 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 OffsetView with indices of dimension r in the range [begin(r),end(r)).

Template Parameters:
  • Begins – std::initializer_list<int64_t>, Kokkos::Array, std::array or std::span with a static extent.

  • Ends – std::initializer_list<int64_t>, Kokkos::Array, std::array or std::span with 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:

  • begins.size() == rank() and ends.size() == rank(), which is checked at runtime for std::initializer_list.

  • begins[r] <= ends[r] for all 0 <= r < rank(), which is checked at runtime.

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::array or std::span with a static extent.

  • Ends – std::initializer_list<int64_t>, Kokkos::Array, std::array or std::span with 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 r are in the range [begin(r),end(r)).

Template Parameters:
  • Begins – std::initializer_list<int64_t>, Kokkos::Array, std::array or std::span with a static extent.

  • Ends – std::initializer_list<int64_t>, Kokkos::Array, std::array or std::span with 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:

  • begins.size() == rank() and ends.size() == rank(), which is checked at runtime for std::initializer_list.

  • begins[r] <= ends[r] for all 0 <= r < rank(), which is checked at runtime.

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.

template<class iType>
int64_t end(const iType dim) const
Template Parameters:

iType – an integral type.

Parameters:

dim – the dimension to get the end index of.

Returns:

begin(dim) + extent(dim), i.e., one more than the last index of dimension dim.

view_type view() const
Returns:

a View with the properties of the corresponding 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:

true if lhs.view() == rhs.view() and lhs.begins() == rhs.begins().

template<class LT, class ...LP, class RT, class ...RP>
bool operator!=(const OffsetView<LT, LP...> &lhs, const OffsetView<RT, RP...> &rhs)
Returns:

!(lhs == rhs)

template<class D, class ...P, class ...Args>
auto subview(const OffsetView<D, P...> &src, Args... args)

Creates an OffsetView referencing a subset of the data of src.

Parameters:
  • src – the OffsetView to take the subview of.

  • args – one slice per dimension of src, given in the indices of src. Accepted are an integral index, Kokkos::ALL(), or a Kokkos::pair<T, T> or std::pair<T, T> with an integral T.

Returns:

an OffsetView. A dimension sliced with Kokkos::ALL() keeps its begin index, the indices of all other dimensions start at 0.

See also

subview

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();
}