Skip to content

Commit e7e0f2a

Browse files
committed
c++ 26 ranges
1 parent 84ff3c1 commit e7e0f2a

4 files changed

Lines changed: 89 additions & 1 deletion

File tree

include/boost/multi/adaptors/thrust/test/for_each.cu

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#include <numeric>
1616
#endif
1717
#endif
18-
// #include <boost/timer/timer.hpp>
18+
19+
#include <ranges>
1920

2021
#include <chrono>
2122

@@ -127,6 +128,50 @@ auto main()
127128
});
128129
}
129130
}
131+
{
132+
multi::array<int, 2> arr({5, 7}, 5);
133+
thrust::for_each(arr.elements().begin(), arr.elements().end(),
134+
[](auto& element) { ++element; }
135+
);
136+
137+
BOOST_TEST( arr[3][2]== 6 );
138+
}
139+
{
140+
multi::array<int, 2> arr({5, 7}, 5);
141+
thrust::for_each(arr.elements().begin(), arr.elements().end(),
142+
[](auto& element) { ++element; }
143+
);
144+
145+
BOOST_TEST( arr[3][2]== 6 );
146+
}
147+
{
148+
multi::array<int, 2> arr1({5, 7}, 5);
149+
multi::array<int, 2> arr2({5, 7}, 6);
150+
151+
thrust::for_each(
152+
thrust::make_zip_iterator(arr1.elements().begin(), arr2.elements().begin()),
153+
thrust::make_zip_iterator(arr1.elements().end(), arr2.elements().end()),
154+
[](auto&& pair) { using std::get; thrust::swap(get<0>(pair), get<1>(pair)); }
155+
);
156+
157+
BOOST_TEST( arr1[3][2]== 6 );
158+
BOOST_TEST( arr2[3][2]== 5 );
159+
}
160+
#if defined(__cpp_lib_ranges_zip) and (__cpp_lib_ranges_zip >= 202110L)
161+
{
162+
multi::array<int, 2> arr1({5, 7}, 5);
163+
multi::array<int, 2> arr2({5, 7}, 6);
164+
165+
std::ranges::for_each(
166+
std::views::zip(arr1.elements(), arr2.elements()).begin(),
167+
std::views::zip(arr1.elements(), arr2.elements()).end(),
168+
[](auto&& pair) { using std::get; thrust::swap(get<0>(pair), get<1>(pair)); }
169+
);
170+
171+
BOOST_TEST( arr1[3][2]== 6 );
172+
BOOST_TEST( arr2[3][2]== 5 );
173+
}
174+
#endif
130175

131176
return boost::report_errors();
132177
}

include/boost/multi/array_ref.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4319,6 +4319,9 @@ template<typename Element, ::boost::multi::dimensionality_type D, class... Rest>
43194319

43204320
template<typename Element, ::boost::multi::dimensionality_type D, class... Rest>
43214321
[[maybe_unused]] constexpr bool enable_borrowed_range<::boost::multi::const_subarray<Element, D, Rest...>> = true; // NOLINT(misc-definitions-in-headers)
4322+
4323+
template<typename Element, ::boost::multi::dimensionality_type D, class... Rest>
4324+
[[maybe_unused]] constexpr bool enable_borrowed_range<::boost::multi::array_ref<Element, D, Rest...>> = true; // NOLINT(misc-definitions-in-headers)
43224325
} // end namespace std::ranges
43234326
#endif
43244327

pre-push

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ fi
7373
#([ -d .build.clang++-16 ] || CXX=clang++-16 cmake -S . -B .build.clang++-16 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_STANDARD=20 && cmake --build .build.clang++-16 $CMT && (ctest --test-dir .build.clang++-16 $CTR || ctest --test-dir .build.clang++-16 --rerun-failed $CTR) ) || exit 666
7474
#([ -d .build.clang++.c++23 ] || CXX=clang++ cmake -S . -B .build.clang++.c++23 -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=23 && cmake --build .build.clang++.c++23 $CMT && (ctest --test-dir .build.clang++.c++23 $CTR || ctest --test-dir .build.clang++.c++23 --rerun-failed $CTR) ) || exit 666
7575
([ -d .build.g++.c++23 ] || CXX=g++ cmake -S . -B .build.g++.c++23 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_STANDARD=23 && cmake --build .build.g++.c++23 $CMT && (ctest --test-dir .build.g++.c++23 $CTR || ctest --test-dir .build.g++.c++23 --rerun-failed $CTR) ) || exit 666
76+
([ -d .build.g++.c++26 ] || CXX=g++ cmake -S . -B .build.g++.c++26 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_STANDARD=26 && cmake --build .build.g++.c++26 $CMT && (ctest --test-dir .build.g++.c++26 $CTR || ctest --test-dir .build.g++.c++26 --rerun-failed $CTR) ) || exit 666
7677
# -Wconsumed enforcement: only compiles partitioned.cpp.x — clang 18/19/20 consumed-analyzer crashes on ranges-heavy TUs (e.g. broadcast_softmax.cpp)
7778
#([ -d .build.clang++.consumed ] || CXX=clang++-20 cmake -S . -B .build.clang++.consumed -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_STANDARD=20 -DDISABLE_MPI=1 -DCMAKE_CXX_FLAGS="-Wconsumed -Werror=consumed") && cmake --build .build.clang++.consumed --target partitioned.cpp.x || exit 666
7879
#([ -d .build.g++-rel ] || CXX=g++ cmake -S . -B .build.g++-rel -DCMAKE_BUILD_TYPE=Release -DENABLE_TBLIS=ON -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=TRUE -DCMAKE_CXX_FLAGS="-Wodr -Wlto-type-mismatch" -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=gold -Wl,--detect-odr-violations -Wodr -Wlto-type-mismatch" && cmake --build .build.g++-rel $CMT && (ctest --test-dir .build.g++-rel $CTR || ctest --test-dir .build.g++-rel --rerun-failed $CTR) ) || exit 666

test/for_each.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include <iostream> // for basic_ostream, operator<<
2020
#include <string> // for char_traits, operator<<
2121

22+
#if (__cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
23+
#include <ranges>
24+
#endif
25+
2226
namespace multi = boost::multi;
2327

2428
namespace {
@@ -136,6 +140,41 @@ auto main() -> int { // NOLINT(bugprone-exception-escape)
136140
BOOST_TEST( std::abs(cpu[1][2][3] - 6.0) < 1e-10 );
137141
}
138142
}
143+
{
144+
multi::array<int, 2> arr({5, 7}, 5);
145+
std::for_each(arr.elements().begin(), arr.elements().end(), [](auto& element) { ++element; });
146+
147+
BOOST_TEST( arr[3][2]== 6 );
148+
}
149+
#if defined(__cpp_lib_ranges_zip) and (__cpp_lib_ranges_zip >= 202110L)
150+
{
151+
multi::array<int, 2> arr1({5, 7}, 5);
152+
multi::array<int, 2> arr2({5, 7}, 6);
153+
154+
auto e1 = arr1.elements(); // bind to lvalues: zip needs viewable_range; array_ref temporaries are not views
155+
auto e2 = arr2.elements();
156+
157+
std::ranges::for_each(
158+
std::views::zip(e1, e2),
159+
[](auto&& pair) { using std::get; thrust::swap(get<0>(pair), get<1>(pair)); }
160+
);
161+
162+
BOOST_TEST( arr1[3][2]== 6 );
163+
BOOST_TEST( arr2[3][2]== 5 );
164+
}
165+
{
166+
multi::array<int, 2> arr1({5, 7}, 5);
167+
multi::array<int, 2> arr2({5, 7}, 6);
168+
169+
std::ranges::for_each(
170+
std::views::zip(arr1.elements(), arr2.elements()),
171+
[](auto&& pair) { using std::get; thrust::swap(get<0>(pair), get<1>(pair)); }
172+
);
173+
174+
BOOST_TEST( arr1[3][2]== 6 );
175+
BOOST_TEST( arr2[3][2]== 5 );
176+
}
177+
#endif
139178

140179
return boost::report_errors();
141180
}

0 commit comments

Comments
 (0)