Skip to content

Commit 457b822

Browse files
committed
Update due to namespace change; apply review suggestion
1 parent 62f409e commit 457b822

File tree

2 files changed

+40
-62
lines changed

2 files changed

+40
-62
lines changed

examples/for_each_extents/for_each_extents.cpp

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <experimental/mdspan>
1+
#include <mdspan/mdspan.hpp>
22
#include <cassert>
33
#include <iostream>
44
#include <type_traits>
@@ -26,42 +26,22 @@
2626
#include <ranges>
2727
namespace ranges_views = std::views;
2828

29-
namespace stdex = std::experimental;
30-
3129
auto print_args = [] <class ... Args> (Args&&... args) {
3230
((std::cout << std::forward<Args>(args) << '\n'), ...);
3331
};
3432

35-
template<size_t ... Lefts, size_t ... Rights>
36-
auto concat_index_sequence(std::index_sequence<Lefts...>,
37-
std::index_sequence<Rights...>)
38-
{
39-
return std::index_sequence<Lefts..., Rights...>{};
40-
}
41-
42-
auto reverse_index_sequence(std::index_sequence<> x)
43-
{
44-
return x;
45-
}
33+
template <std::size_t... Is>
34+
auto reverse(std::index_sequence<Is...>) ->
35+
std::index_sequence<sizeof...(Is) - 1 - Is...>;
4636

47-
template<size_t First, size_t ... Rest>
48-
auto reverse_index_sequence(std::index_sequence<First, Rest...>)
49-
{
50-
return concat_index_sequence(
51-
reverse_index_sequence(std::index_sequence<Rest...>{}),
52-
std::index_sequence<First>{});
53-
}
54-
55-
template<size_t N>
56-
auto make_reverse_index_sequence()
57-
{
58-
return reverse_index_sequence(std::make_index_sequence<N>());
59-
}
37+
template <std::size_t N>
38+
using reverse_index_sequence_t =
39+
decltype(reverse(std::make_index_sequence<N>()));
6040

6141
template<class Callable, class IndexType,
6242
std::size_t ... Extents, std::size_t ... RankIndices>
6343
void for_each_in_extents_impl(Callable&& f,
64-
stdex::extents<IndexType, Extents...> e,
44+
Kokkos::extents<IndexType, Extents...> e,
6545
std::index_sequence<RankIndices...> rank_sequence)
6646
{
6747
// In the layout_left case, caller passes in N-1, N-2, ..., 1, 0.
@@ -82,17 +62,17 @@ void for_each_in_extents_impl(Callable&& f,
8262

8363
template<class Callable, class IndexType, std::size_t ... Extents, class Layout>
8464
void for_each_in_extents(Callable&& f,
85-
stdex::extents<IndexType, Extents...> e,
65+
Kokkos::extents<IndexType, Extents...> e,
8666
Layout)
8767
{
8868
using layout_type = std::remove_cvref_t<Layout>;
89-
if constexpr (std::is_same_v<layout_type, stdex::layout_left>) {
69+
if constexpr (std::is_same_v<layout_type, Kokkos::layout_left>) {
9070
for_each_in_extents_impl(std::forward<Callable>(f), e,
91-
make_reverse_index_sequence<e.rank()>());
71+
reverse_index_sequence_t<e.rank()>{});
9272
}
9373
else { // layout_right or any other layout
9474
for_each_in_extents_impl(std::forward<Callable>(f), e,
95-
std::make_index_sequence<e.rank()>());
75+
reverse_index_sequence_t<e.rank()>{});
9676
}
9777
}
9878

@@ -101,14 +81,14 @@ void for_each_in_extents(Callable&& f,
10181
int main() {
10282

10383
#if defined(MDSPAN_EXAMPLE_CAN_USE_STD_RANGES)
104-
stdex::extents<int, 2, 3> e;
84+
Kokkos::extents<int, 2, 3> e;
10585
auto printer = [] (int i, int j) {
10686
std::cout << "(" << i << "," << j << ")\n";
10787
};
10888
std::cout << "layout_right:\n";
109-
for_each_in_extents(printer, e, stdex::layout_right{});
89+
for_each_in_extents(printer, e, Kokkos::layout_right{});
11090
std::cout << "\nlayout_left:\n";
111-
for_each_in_extents(printer, e, stdex::layout_left{});
91+
for_each_in_extents(printer, e, Kokkos::layout_left{});
11292
#endif // defined(MDSPAN_EXAMPLE_CAN_USE_STD_RANGES)
11393

11494
return 0;

examples/for_each_extents/for_each_extents_no_ranges.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#include <experimental/mdspan>
1+
#include <mdspan/mdspan.hpp>
22
#include <array>
33
#include <iostream>
44
#include <tuple>
55
#include <type_traits>
66

7-
namespace stdex = std::experimental;
8-
97
// There's no separate feature test macro for the C++20 feature
108
// of lambdas with named template parameters (P0428R2).
119
#if __cplusplus >= 202002L
@@ -80,11 +78,11 @@ auto print_pack = []<class ... InputTypes>(InputTypes&& ... input) {
8078
// This example shows that you can do
8179
// index arithmetic on an index sequence.
8280
template<class IndexType, std::size_t ... Extents>
83-
auto right_extents( stdex::extents<IndexType, Extents...> e )
81+
auto right_extents( Kokkos::extents<IndexType, Extents...> e )
8482
{
8583
static_assert(sizeof...(Extents) != 0);
8684
return [&]<std::size_t ... Indices>( std::index_sequence<Indices...> ) {
87-
return stdex::extents<IndexType, e.static_extent(Indices + 1)...>{
85+
return Kokkos::extents<IndexType, e.static_extent(Indices + 1)...>{
8886
e.extent(Indices + 1)...
8987
};
9088
}( std::make_index_sequence<sizeof...(Extents) - 1>() );
@@ -101,10 +99,10 @@ auto right_extents( stdex::extents<IndexType, Extents...> e )
10199
// This needs to be a lambda or function object,
102100
// not a templated function.
103101
auto split_extents_at_leftmost =
104-
[]<class IndexType, std::size_t... Extents>(stdex::extents<IndexType, Extents...> e)
102+
[]<class IndexType, std::size_t... Extents>(Kokkos::extents<IndexType, Extents...> e)
105103
{
106104
static_assert(sizeof...(Extents) != 0);
107-
stdex::extents<IndexType, e.static_extent(0)> left_ext(
105+
Kokkos::extents<IndexType, e.static_extent(0)> left_ext(
108106
e.extent(0));
109107
return std::tuple{left_ext, right_extents(e)};
110108
};
@@ -116,22 +114,22 @@ auto split_extents_at_leftmost =
116114
// Returns a new extents object representing
117115
// all but the rightmost extent of e.
118116
template<class IndexType, std::size_t ... Extents>
119-
auto left_extents( stdex::extents<IndexType, Extents...> e )
117+
auto left_extents( Kokkos::extents<IndexType, Extents...> e )
120118
{
121119
static_assert(sizeof...(Extents) != 0);
122120
return [&]<std::size_t ... Indices>( std::index_sequence<Indices...> ) {
123-
return stdex::extents<IndexType, e.static_extent(Indices)...>{
121+
return Kokkos::extents<IndexType, e.static_extent(Indices)...>{
124122
e.extent(Indices)...
125123
};
126124
}( std::make_index_sequence<sizeof...(Extents) - 1>() );
127125
}
128126

129127
// This needs to be a lambda or function object, not a templated function.
130128
auto split_extents_at_rightmost =
131-
[]<class IndexType, std::size_t ... Extents>(stdex::extents<IndexType, Extents...> e)
129+
[]<class IndexType, std::size_t ... Extents>(Kokkos::extents<IndexType, Extents...> e)
132130
{
133131
static_assert(sizeof...(Extents) != 0);
134-
stdex::extents<IndexType, e.static_extent(e.rank() - 1)> right_ext(
132+
Kokkos::extents<IndexType, e.static_extent(e.rank() - 1)> right_ext(
135133
e.extent(e.rank() - 1));
136134
return std::tuple{left_extents(e), right_ext};
137135
};
@@ -149,10 +147,10 @@ auto split_extents_at_rightmost =
149147
// optimization information -- e.g., whether we want
150148
// to apply "#pragma omp simd" to a particular extent.
151149
template<class Callable, class IndexType, std::size_t Extent>
152-
void for_each_one_extent(Callable&& callable, stdex::extents<IndexType, Extent> ext)
150+
void for_each_one_extent(Callable&& callable, Kokkos::extents<IndexType, Extent> ext)
153151
{
154152
// If it's a run-time extent, do a run-time loop.
155-
if constexpr(ext.static_extent(0) == stdex::dynamic_extent) {
153+
if constexpr(ext.static_extent(0) == Kokkos::dynamic_extent) {
156154
for(IndexType index = 0; index < ext.extent(0); ++index) {
157155
std::forward<Callable>(callable)(index);
158156
}
@@ -176,7 +174,7 @@ void for_each_one_extent(Callable&& callable, stdex::extents<IndexType, Extent>
176174
template<class Callable, class IndexType, std::size_t ... Extents>
177175
void for_each_in_extents_row_major(
178176
Callable&& callable,
179-
stdex::extents<IndexType, Extents...> ext)
177+
Kokkos::extents<IndexType, Extents...> ext)
180178
{
181179
if constexpr(ext.rank() == 0) {
182180
return;
@@ -203,12 +201,12 @@ void for_each_in_extents_row_major(
203201
// The implementation differs in only two places from the row-major version.
204202
// This suggests a way to generalize.
205203
//
206-
// Overloading on stdex::extents<IndexType, LeftExtents..., RightExtent>
204+
// Overloading on extents<IndexType, LeftExtents..., RightExtent>
207205
// works fine for the row major case, but not for the column major case.
208206
template<class Callable, class IndexType, std::size_t ... Extents>
209207
void for_each_in_extents_col_major(
210208
Callable&& callable,
211-
stdex::extents<IndexType, Extents...> ext)
209+
Kokkos::extents<IndexType, Extents...> ext)
212210
{
213211
if constexpr(ext.rank() == 0) {
214212
return;
@@ -242,7 +240,7 @@ void for_each_in_extents_col_major(
242240
template<class Callable, class IndexType, std::size_t ... Extents,
243241
class ExtentsReorderer, class ExtentsSplitter, class IndicesReorderer>
244242
void for_each_in_extents_impl(Callable&& callable,
245-
stdex::extents<IndexType, Extents...> ext,
243+
Kokkos::extents<IndexType, Extents...> ext,
246244
ExtentsReorderer reorder_extents,
247245
ExtentsSplitter split_extents,
248246
IndicesReorderer reorder_indices)
@@ -280,18 +278,18 @@ void for_each_in_extents_impl(Callable&& callable,
280278
}
281279

282280
auto extents_identity = []<class IndexType, std::size_t ... Extents>(
283-
stdex::extents<IndexType, Extents...> ext)
281+
Kokkos::extents<IndexType, Extents...> ext)
284282
{
285283
return ext;
286284
};
287285

288286
auto extents_reverse = []<class IndexType, std::size_t ... Extents>(
289-
stdex::extents<IndexType, Extents...> ext)
287+
Kokkos::extents<IndexType, Extents...> ext)
290288
{
291289
constexpr std::size_t N = ext.rank();
292290

293291
return [&]<std::size_t ... Indices>( std::index_sequence<Indices...> ) {
294-
return stdex::extents<
292+
return Kokkos::extents<
295293
IndexType,
296294
ext.static_extent(N - 1 - Indices)...
297295
>{ ext.extent(N - 1 - Indices)... };
@@ -325,8 +323,8 @@ auto indices_reverse = [](auto... args) {
325323
// Row-major iteration
326324
template<class Callable, class IndexType, std::size_t ... Extents>
327325
void for_each_in_extents(Callable&& callable,
328-
stdex::extents<IndexType, Extents...> ext,
329-
stdex::layout_right)
326+
Kokkos::extents<IndexType, Extents...> ext,
327+
Kokkos::layout_right)
330328
{
331329
for_each_in_extents_impl(std::forward<Callable>(callable), ext,
332330
extents_identity, split_extents_at_leftmost, indices_identity);
@@ -335,8 +333,8 @@ void for_each_in_extents(Callable&& callable,
335333
// Column-major iteration
336334
template<class Callable, class IndexType, std::size_t ... Extents>
337335
void for_each_in_extents(Callable&& callable,
338-
stdex::extents<IndexType, Extents...> ext,
339-
stdex::layout_left)
336+
Kokkos::extents<IndexType, Extents...> ext,
337+
Kokkos::layout_left)
340338
{
341339
for_each_in_extents_impl(std::forward<Callable>(callable), ext,
342340
extents_reverse, split_extents_at_rightmost, indices_reverse);
@@ -349,7 +347,7 @@ int main() {
349347
#if ! defined(__clang__) && defined(MDSPAN_EXAMPLE_CAN_USE_LAMBDA_TEMPLATE_PARAM_LIST)
350348
// The functions work for any combination
351349
// of compile-time or run-time extents.
352-
stdex::extents<int, 3, stdex::dynamic_extent, 5> e{4};
350+
Kokkos::extents<int, 3, Kokkos::dynamic_extent, 5> e{4};
353351

354352
std::cout << "\nRow major:\n";
355353
for_each_in_extents_row_major(print_pack, e);
@@ -358,10 +356,10 @@ int main() {
358356
for_each_in_extents_col_major(print_pack, e);
359357

360358
std::cout << "\nfor_each_in_extents: row major:\n";
361-
for_each_in_extents(print_pack, e, stdex::layout_right{});
359+
for_each_in_extents(print_pack, e, Kokkos::layout_right{});
362360

363361
std::cout << "\nfor_each_in_extents: column major:\n";
364-
for_each_in_extents(print_pack, e, stdex::layout_left{});
362+
for_each_in_extents(print_pack, e, Kokkos::layout_left{});
365363
#endif // defined(MDSPAN_EXAMPLE_CAN_USE_LAMBDA_TEMPLATE_PARAM_LIST)
366364

367365
return 0;

0 commit comments

Comments
 (0)