Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4b5e5a3
add ZeroTo RangeSegment
artv3 Mar 25, 2026
124338d
add python like behavior for RAJA::Range
artv3 Mar 31, 2026
801bd00
pr comments
artv3 Mar 31, 2026
6dbb367
Update include/RAJA/index/RangeSegment.hpp
artv3 Mar 31, 2026
17b5e57
include stride in common type find
artv3 Mar 31, 2026
ce732f4
Merge branch 'artv3/zeroTo' of https://github.com/LLNL/RAJA into artv…
artv3 Mar 31, 2026
3d0195a
PR comments
artv3 Mar 31, 2026
ddae21a
use _v instead of ::value
artv3 Mar 31, 2026
e243737
build fix
artv3 Mar 31, 2026
23a04d6
clean up pass
artv3 Mar 31, 2026
6a8995a
just use decay_t
artv3 Mar 31, 2026
66be94f
simplify
artv3 Mar 31, 2026
0c4666e
remove redundant decay
artv3 Mar 31, 2026
d2886c0
add strongly typed begin and end test
artv3 Mar 31, 2026
ab3c185
add mixed index support
artv3 Apr 1, 2026
ad5f132
add sphinx docs
artv3 Apr 2, 2026
1fc8ac3
Merge branch 'develop' into artv3/zeroTo
artv3 Apr 28, 2026
c7589b4
fix merge conflict
artv3 Jun 19, 2026
744cbc5
clean up pass, throw error for mixed types
artv3 Jun 19, 2026
c30299d
clean up pass
artv3 Jun 19, 2026
557dc27
Merge branch 'develop' into artv3/zeroTo
artv3 Jun 30, 2026
b7b14db
PR comments, add testing and checks
artv3 Jun 30, 2026
2f9edca
Merge branch 'artv3/zeroTo' of github.com:llnl/RAJA into artv3/zeroTo
artv3 Jun 30, 2026
0b1e0d2
Merge branch 'develop' into artv3/zeroTo
artv3 Jul 5, 2026
03b4af1
Merge branch 'develop' into artv3/zeroTo
artv3 Jul 6, 2026
5fd1ee6
Merge branch 'develop' into artv3/zeroTo
artv3 Jul 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ raja_add_executable(
NAME resource-forall
SOURCES resource-forall.cpp)

raja_add_executable(
NAME raja-ranges
SOURCES raja-ranges.cpp)

raja_add_executable(
NAME dynamic-forall
SOURCES dynamic-forall.cpp)
Expand Down
86 changes: 86 additions & 0 deletions examples/raja-ranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) Lawrence Livermore National Security, LLC and other
// RAJA Project Developers. See top-level LICENSE and COPYRIGHT
// files for dates and other details. No copyright assignment is required
// to contribute to RAJA.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

#include <iostream>

#include "RAJA/RAJA.hpp"

/*
* RAJA ranges example
*
* Demonstrates Python-like range helpers backed by RAJA segments:
* 1. RAJA::Range(N) for [0, N)
* 2. RAJA::Range<T>(N) for a TypedRangeSegment<T> over [0, N)
* 3. RAJA::Range(begin, end) for [begin, end)
* 4. RAJA::Range(begin, end, step) for a strided half-open interval
*/

RAJA_INDEX_VALUE(CellIndex, "CellIndex");

int main(int RAJA_UNUSED_ARG(argc), char** RAJA_UNUSED_ARG(argv))
{
constexpr RAJA::Index_type N = 8;

int values[N] = {};
int typed_values[N] = {};
int subrange_values[N] = {};
int odd_values[N] = {};

// Equivalent to Python range(N): [0, N)
RAJA::forall<RAJA::seq_exec>(RAJA::Range(N), [&](RAJA::Index_type i) {
Comment thread
artv3 marked this conversation as resolved.
Outdated
values[i] = static_cast<int>(i * i);
});

// Equivalent to Python range(N), but preserving a strong index type.
RAJA::forall<RAJA::seq_exec>(RAJA::Range<CellIndex>(N), [&](CellIndex i) {
typed_values[*i] = static_cast<int>(*i + 10);
});

// Equivalent to Python range(2, 6): [2, 6)
RAJA::forall<RAJA::seq_exec>(RAJA::Range(2, 6), [&](int i) {
subrange_values[i] = i;
});

// Equivalent to Python range(1, N, 2): odd indices in [1, N)
RAJA::forall<RAJA::seq_exec>(RAJA::Range(1, N, 2), [&](int i) {
odd_values[i] = i;
});

std::cout << "Range(N):";
for (auto i : RAJA::Range(N)) {
std::cout << ' ' << values[i];
}
std::cout << '\n';

std::cout << "Range<CellIndex>(N):";
for (auto i : RAJA::Range<CellIndex>(N)) {
std::cout << ' ' << typed_values[*i];
}
std::cout << '\n';

std::cout << "Range(2, 6):";
for (auto i : RAJA::Range(2, 6)) {
std::cout << ' ' << subrange_values[i];
}
std::cout << '\n';

std::cout << "Range(1, N, 2):";
for (auto i : RAJA::Range(1, N, 2)) {
std::cout << ' ' << odd_values[i];
}
std::cout << '\n';

std::cout << "Range(N - 1, -1, -2):";
for (auto i : RAJA::Range(N - 1, -1, -2)) {
std::cout << ' ' << i;
}
std::cout << '\n';

return 0;
}
77 changes: 77 additions & 0 deletions include/RAJA/index/RangeSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,33 @@ using RangeSegment = TypedRangeSegment<Index_type>;
//! Alias for TypedRangeStrideSegment<Index_type>
using RangeStrideSegment = TypedRangeStrideSegment<Index_type>;

/*!
* \brief Function to make a RangeSegment for the interval [0, end)
*
* \return a newly constructed RangeSegment over the half-open interval
* starting at zero and ending at @end.
*/
RAJA_HOST_DEVICE RAJA_INLINE constexpr RangeSegment Range(
Comment thread
MrBurmark marked this conversation as resolved.
Outdated
Index_type end) noexcept
{
return {0, end};
}

/*!
* \brief Function to make a TypedRangeSegment for the interval [0, end)
*
* \return a newly constructed TypedRangeSegment over the half-open interval
* starting at zero and ending at @end.
*/
template<typename StorageT>
RAJA_HOST_DEVICE RAJA_INLINE constexpr TypedRangeSegment<StorageT> Range(
strip_index_type_t<StorageT> end) noexcept
{
static_assert(!std::is_floating_point<strip_index_type_t<StorageT>>::value,
"Range requires a non-floating point index type.");
return {strip_index_type_t<StorageT> {0}, end};
}

namespace detail
{

Expand All @@ -549,6 +576,56 @@ using common_type_t = typename common_type<Ts...>::type;

} // namespace detail

/*!
* \brief Function to make a TypedRangeSegment for the interval [begin, end)
*
* \return a newly constructed TypedRangeSegment where the
* value_type is equivilent to the common type of
* @begin and @end. If there is no common type, then
* a compiler error will be produced.
*/
template<typename BeginT,
typename EndT,
typename Common = detail::common_type_t<BeginT, EndT>>
RAJA_HOST_DEVICE RAJA_INLINE constexpr TypedRangeSegment<Common> Range(
BeginT&& begin,
EndT&& end) noexcept
{
return {begin, end};
}
Comment thread
MrBurmark marked this conversation as resolved.
Outdated

/*!
* \brief Function to make a TypedRangeStrideSegment for the interval
* [begin, end) with given stride
*
* \return a newly constructed TypedRangeStrideSegment where the
* value_type is equivilent to the common type of
* @begin and @end. If stride is zero, execution aborts or throws.
*/
template<typename BeginT,
typename EndT,
typename StrideT,
typename Common = detail::common_type_t<BeginT, EndT>,
typename DiffT = make_signed_t<strip_index_type_t<Common>>>
RAJA_HOST_DEVICE RAJA_INLINE TypedRangeStrideSegment<Common> Range(
BeginT&& begin,
EndT&& end,
StrideT&& stride)
{
static_assert(std::is_signed<strip_index_type_t<StrideT>>::value,
"Range requires a signed stride type.");
static_assert(!std::is_floating_point<strip_index_type_t<StrideT>>::value,
Comment thread
artv3 marked this conversation as resolved.
Outdated
"Range requires a non-floating point stride type.");

DiffT const typed_stride = static_cast<DiffT>(stride);
if (typed_stride == DiffT {0})
{
RAJA_ABORT_OR_THROW("RAJA::Range requires a non-zero stride.");
}

return {begin, end, typed_stride};
}

/*!
* \brief Function to make a TypedRangeSegment for the interval [begin, end)
*
Expand Down
46 changes: 46 additions & 0 deletions test/unit/index/test-rangesegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "RAJA_unit-test-types.hpp"

RAJA_INDEX_VALUE(RangeStrongIndex, "RangeStrongIndex");

template<typename T>
class RangeSegmentUnitTest : public ::testing::Test {};

Expand Down Expand Up @@ -166,3 +168,47 @@ TYPED_TEST(RangeSegmentUnitTest, Equality)

ASSERT_NE(r1, r3);
}

TEST(RangeSegmentUnitTest, RangeEnd)
{
auto r = RAJA::Range(RAJA::Index_type(17));

ASSERT_EQ(RAJA::RangeSegment(RAJA::Index_type(0), RAJA::Index_type(17)), r);
ASSERT_EQ(RAJA::Index_type(0), *r.begin());
ASSERT_EQ(RAJA::Index_type(17), r.size());
}

TEST(RangeSegmentUnitTest, TypedRangeEnd)
{
auto r = RAJA::Range<RangeStrongIndex>(17);

ASSERT_EQ((RAJA::TypedRangeSegment<RangeStrongIndex>(0, 17)), r);
ASSERT_EQ(RangeStrongIndex(0), *r.begin());
ASSERT_EQ(RAJA::Index_type(17), r.size());
}

TEST(RangeSegmentUnitTest, RangeBeginEnd)
{
auto r = RAJA::Range(3, 17);

ASSERT_EQ((RAJA::TypedRangeSegment<int>(3, 17)), r);
ASSERT_EQ(14, r.size());
}

TEST(RangeSegmentUnitTest, RangeBeginEndStridePositive)
{
auto r = RAJA::Range(2, 11, 3);

ASSERT_EQ((RAJA::TypedRangeStrideSegment<int>(2, 11, 3)), r);
ASSERT_EQ(4, r.size());
ASSERT_EQ(2, *r.begin());
}

TEST(RangeSegmentUnitTest, RangeBeginEndStrideNegative)
{
auto r = RAJA::Range(10, -1, -2);

ASSERT_EQ((RAJA::TypedRangeStrideSegment<int>(10, -1, -2)), r);
ASSERT_EQ(6, r.size());
ASSERT_EQ(10, *r.begin());
}
Loading