Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cxx20standard #1768

Draft
wants to merge 18 commits into
base: distributed-ranges
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ jobs:
# device_type: HOST
- os: ubuntu-24.04
cxx_compiler: icpx
std: 23
std: 20
build_type: release
backend: dpcpp
device_type: CPU
Expand Down Expand Up @@ -202,7 +202,7 @@ jobs:
run: |
sudo apt-get install intel-oneapi-compiler-fpga -y
- name: Install libfmt and gtest needed by Distributed Ranges tests
if: (matrix.std == '23')
if: (matrix.std == '20')
run: |
sudo apt-get install libgtest-dev libfmt-dev -y
- name: Install OpenMP dependencies
Expand All @@ -224,10 +224,10 @@ jobs:
ctest_flags="-R sycl_iterator_.*\.pass"
echo "::warning::dpcpp backend is set. Compile and run only sycl_iterator tests"

if [[ "${{ matrix.std }}" == "23" ]]; then
if [[ "${{ matrix.std }}" == "20" ]]; then
make_targets="$make_targets sp-all-tests"
ctest_flags="$ctest_flags|sp_tests"
echo "::warning::23 standard is set. Compile and run distributed ranges tests"
echo "::warning::20 standard is set. Compile and run distributed ranges tests"
fi

else
Expand Down
2 changes: 1 addition & 1 deletion include/oneapi/dpl/distributed_ranges
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "oneapi/dpl/internal/common_config.h"
#include "oneapi/dpl/pstl/onedpl_config.h"

#if __cplusplus > 202002L
#if __cplusplus > 202001L
#include "oneapi/dpl/internal/distributed_ranges_impl/concepts.hpp"
#include "oneapi/dpl/internal/distributed_ranges_impl/sp.hpp"
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ namespace oneapi::dpl::experimental::dr
namespace __detail
{

template <stdrng::viewable_range R>
auto
make_enumerate(R&& r)
{
using W = std::size_t;
return __ranges::make_zip_view(stdrng::views::iota(W{0}, W{stdrng::size(r)}), std::forward<R>(r));
}

// Take all elements up to and including segment `segment_id` at index
// `local_id`
template <typename R>
Expand All @@ -49,7 +57,7 @@ take_segments(R&& segments, std::size_t last_seg, std::size_t local_id)
}
};

return stdrng::views::enumerate(segments) | stdrng::views::take(last_seg + 1) |
return make_enumerate(segments) | stdrng::views::take(last_seg + 1) |
stdrng::views::transform(std::move(take_partial));
}

Expand Down Expand Up @@ -96,7 +104,7 @@ drop_segments(R&& segments, std::size_t first_seg, std::size_t local_id)
}
};

return stdrng::views::enumerate(segments) | stdrng::views::drop(first_seg) |
return make_enumerate(segments) | stdrng::views::drop(first_seg) |
stdrng::views::transform(std::move(drop_partial));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define _ONEDPL_DR_DETAIL_VIEW_DETECTORS_HPP

#include <type_traits>
#include "oneapi/dpl/pstl/utils_ranges.h" // zip_view

namespace oneapi::dpl::experimental::dr
{
Expand Down Expand Up @@ -101,6 +102,11 @@ struct is_zip_view : std::false_type
{
};

template <typename... T>
struct is_zip_view<__ranges::zip_view<T...>> : std::true_type
{
};

#if (defined _cpp_lib_ranges_zip)
template <typename... Views>
struct is_zip_view<stdrng::zip_view<Views...>> : std::true_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ sort(R&& r, Compare comp = Compare())

T* medians = sycl::malloc_device<T>(n_segments * n_splitters, sp::devices()[0], sp::context());

for (auto&& [segment_id_, segment] : stdrng::views::enumerate(segments))
for (auto&& [segment_id_, segment] : dr::__detail::make_enumerate(segments))
Copy link
Contributor

@MikeDvorskiy MikeDvorskiy Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that we are using a functional approach for the sake of a functional approach...
I would suggest a little bit re-writing this loop is following way (w/o make_enumerate):

for (auto segment_id = 0, n =  stdrng::size(segment); segment_id < n; ++segment_id)
{
         auto segment = segments[segment_id ];
.......

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MikeDvorskiy , thanks for looking into this. Looks acceptable, although it is more lines of code.

Are other places with enumarate also easily changeable? Do you think it is more clean than having make_enumerate' or does it fixes some problems which make_enumerate` has (if it has)?

Enumerate is quite common programming pattern making code shorter and more readable.

I don't object of changing all places where there is enumerate to some code like this if it is needed to add C++20 support. Please feel free to clone my change to your branch and continue adding any changes you think may be good for C++20 support.

{
auto const segment_id = static_cast<std::size_t>(segment_id_);
auto&& q = __detail::queue(ranges::rank(segment));
Expand Down Expand Up @@ -172,7 +172,7 @@ sort(R&& r, Compare comp = Compare())
// segments". Simultaneously compute the offsets `push_positions` where each
// segments' corresponding elements will be pushed.

for (auto&& [segment_id, segment] : stdrng::views::enumerate(segments))
for (auto&& [segment_id, segment] : dr::__detail::make_enumerate(segments))
{
auto&& q = __detail::queue(ranges::rank(segment));
auto&& local_policy = __detail::dpl_policy(ranges::rank(segment));
Expand Down Expand Up @@ -208,7 +208,7 @@ sort(R&& r, Compare comp = Compare())
// Allocate new "sorted segments"
std::vector<T*> sorted_segments;

for (auto&& [segment_id, segment] : stdrng::views::enumerate(segments))
for (auto&& [segment_id, segment] : dr::__detail::make_enumerate(segments))
{
auto&& q = __detail::queue(ranges::rank(segment));

Expand All @@ -217,7 +217,7 @@ sort(R&& r, Compare comp = Compare())
}

// Copy corresponding elements to each "sorted segment"
for (auto&& [segment_id_, segment] : stdrng::views::enumerate(segments))
for (auto&& [segment_id_, segment] : dr::__detail::make_enumerate(segments))
{
auto&& local_segment = ranges::__detail::local_or_identity(segment);
const auto segment_id = static_cast<std::size_t>(segment_id_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define _ONEDPL_DR_SP_ZIP_VIEW_HPP

#include "oneapi/dpl/iterator"
#include "oneapi/dpl/pstl/utils_ranges.h"

#include "../../detail/iterator_adaptor.hpp"
#include "../../detail/owning_view.hpp"
Expand Down Expand Up @@ -282,7 +283,7 @@ class zip_view : public stdrng::view_interface<zip_view<Rs...>>
auto
local_impl_(std::index_sequence<Ints...>) const noexcept
{
return stdrng::views::zip(ranges::__detail::local_or_identity(std::get<Ints>(views_))...);
return __ranges::make_zip_view(ranges::__detail::local_or_identity(std::get<Ints>(views_))...);
}

template <std::size_t I, typename R>
Expand Down
4 changes: 2 additions & 2 deletions test/distributed_ranges/common/distributed_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TYPED_TEST(DistributedVectorAllTypes, Stream) {
TYPED_TEST(DistributedVectorAllTypes, Equality) {
Ops1<TypeParam> ops(10);
iota(ops.dist_vec, 100);
stdrng::iota(ops.vec, 100);
std::iota(stdrng::begin(ops.vec), stdrng::end(ops.vec), 100);
EXPECT_TRUE(ops.dist_vec == ops.vec);
EXPECT_EQ(ops.vec, ops.dist_vec);
}
Expand All @@ -83,7 +83,7 @@ TEST(DistributedVector, ConstructorBasic) {
iota(dist_vec, 100);

std::vector<int> local_vec(10);
stdrng::iota(local_vec, 100);
std::iota(stdrng::begin(local_vec), stdrng::end(local_vec), 100);

EXPECT_EQ(local_vec, dist_vec);
}
Expand Down
3 changes: 2 additions & 1 deletion test/distributed_ranges/common/for_each.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//===----------------------------------------------------------------------===//

#include "xp_tests.hpp"
#include "oneapi/dpl/pstl/utils_ranges.h" // zip_view

// Fixture
template <typename T> class ForEach : public testing::Test {
Expand Down Expand Up @@ -109,7 +110,7 @@ TYPED_TEST(ForEach, DISABLED_RangeUnalignedZip) {
auto copy = [](auto v) { std::get<0>(v) = std::get<1>(v); };
auto dist =
xp::views::zip(xp::views::drop(ops.dist_vec0, 1), ops.dist_vec1);
auto local = stdrng::views::zip(stdrng::views::drop(ops.vec0, 1), ops.vec1);
auto local = __ranges::make_zip(stdrng::views::drop(ops.vec0, 1), ops.vec1);

xp::for_each(dist, copy);
stdrng::for_each(local, copy);
Expand Down
2 changes: 1 addition & 1 deletion test/distributed_ranges/common/sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ TEST(Sort, Wave) { test_sort2s({1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1}); }

TEST(Sort, LongSorted) {
LV v(100000);
stdrng::iota(v, 1);
std::iota(std::begin(v), std::end(v), 1);
test_sort2s(v);

stdrng::reverse(v);
Expand Down
3 changes: 2 additions & 1 deletion test/distributed_ranges/common/zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//===----------------------------------------------------------------------===//

#include "xp_tests.hpp"
#include "oneapi/dpl/pstl/utils_ranges.h"

template <typename... Rs> auto test_zip(Rs &&...rs) {
return xp::views::zip(std::forward<Rs>(rs)...);
Expand All @@ -29,7 +30,7 @@ TYPED_TEST_SUITE(Zip, AllTypes);
TYPED_TEST(Zip, Dist1) {
Ops1<TypeParam> ops(10);

auto local = stdrng::views::zip(ops.vec);
auto local = oneapi::dpl::__ranges::make_zip_view(ops.vec);
auto dist = test_zip(ops.dist_vec);
static_assert(compliant_view<decltype(dist)>);
EXPECT_TRUE(check_view(local, dist));
Expand Down
12 changes: 6 additions & 6 deletions test/distributed_ranges/include/common_tests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ inline std::ostream &operator<<(std::ostream &os, const AOS_Struct &st) {
template <typename T> struct Ops1 {
Ops1(std::size_t n) : dist_vec(n), vec(n) {
iota(dist_vec, 100);
stdrng::iota(vec, 100);
std::iota(stdrng::begin(vec), stdrng::end(vec), 100);
}

T dist_vec;
Expand All @@ -68,8 +68,8 @@ template <typename T> struct Ops2 {
Ops2(std::size_t n) : dist_vec0(n), dist_vec1(n), vec0(n), vec1(n) {
iota(dist_vec0, 100);
iota(dist_vec1, 200);
stdrng::iota(vec0, 100);
stdrng::iota(vec1, 200);
std::iota(stdrng::begin(vec0), stdrng::end(vec0), 100);
std::iota(stdrng::begin(vec1), stdrng::end(vec1), 200);
}

T dist_vec0, dist_vec1;
Expand All @@ -82,9 +82,9 @@ template <typename T> struct Ops3 {
iota(dist_vec0, 100);
iota(dist_vec1, 200);
iota(dist_vec2, 300);
stdrng::iota(vec0, 100);
stdrng::iota(vec1, 200);
stdrng::iota(vec2, 300);
std::iota(stdrng::begin(vec0), stdrng::end(vec0), 100);
std::iota(stdrng::begin(vec1), stdrng::end(vec1), 200);
std::iota(stdrng::begin(vec2), stdrng::end(vec2), 300);
}

T dist_vec0, dist_vec1, dist_vec2;
Expand Down
4 changes: 2 additions & 2 deletions test/distributed_ranges/sp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ else ()
message(STATUS "suitable for distributed ranges cmake version ${CMAKE_VERSION} found")
endif()

if (CMAKE_CXX_STANDARD LESS 23)
message(STATUS "disabled distributed ranges tests, C++23 or higher standard is required")
if (CMAKE_CXX_STANDARD LESS 20)
message(STATUS "disabled distributed ranges tests, C++20 or higher standard is required")
return()
else()
message(STATUS "suitable for distributed ranges C++ standard ${CMAKE_CXX_STANDARD}")
Expand Down
Loading