This repository was archived by the owner on Sep 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
added count to mp algorithms #812
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
25243a9
added count to mhp algorithms
c950ea3
Merge branch 'main' of https://github.com/oneapi-src/distributed-rang…
7eec868
minor fix
6090fdc
minor fixes
167702d
code review fixes
755c896
more code review fixes
e98de3b
removed redundant conditional
f31b80c
fixes according to pre-commit checks
b847d04
Merge branch 'main' of https://github.com/oneapi-src/distributed-rang…
06cc78b
Merge branch 'main' of https://github.com/oneapi-src/distributed-rang…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// SPDX-FileCopyrightText: Intel Corporation | ||
// | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#pragma once | ||
|
||
namespace dr::mp::__detail { | ||
|
||
inline auto add_counts(rng::forward_range auto &&r) { | ||
rng::range_difference_t<decltype(r)> zero{}; | ||
|
||
return std::accumulate(rng::begin(r), rng::end(r), zero); | ||
} | ||
|
||
inline auto count_if_local(rng::forward_range auto &&r, auto &&pred) { | ||
if (mp::use_sycl()) { | ||
dr::drlog.debug(" with DPL\n"); | ||
#ifdef SYCL_LANGUAGE_VERSION | ||
return std::count_if(mp::dpl_policy(), | ||
dr::__detail::direct_iterator(rng::begin(r)), | ||
dr::__detail::direct_iterator(rng::end(r)), pred); | ||
#else | ||
assert(false); | ||
#endif | ||
} else { | ||
dr::drlog.debug(" with CPU\n"); | ||
return std::count_if(std::execution::par_unseq, | ||
dr::__detail::direct_iterator(rng::begin(r)), | ||
dr::__detail::direct_iterator(rng::end(r)), pred); | ||
} | ||
} | ||
|
||
template <dr::distributed_range DR> | ||
auto count_if(std::size_t root, bool root_provided, DR &&dr, auto &&pred) { | ||
using count_type = rng::range_difference_t<decltype(dr)>; | ||
auto comm = mp::default_comm(); | ||
|
||
if (rng::empty(dr)) { | ||
return count_type{}; | ||
} | ||
|
||
dr::drlog.debug("Parallel count\n"); | ||
|
||
// Count within the local segments | ||
auto count = [=](auto &&r) { | ||
assert(rng::size(r) > 0); | ||
return count_if_local(r, pred); | ||
}; | ||
auto locals = rng::views::transform(local_segments(dr), count); | ||
auto local = add_counts(locals); | ||
|
||
std::vector<count_type> all(comm.size()); | ||
if (root_provided) { | ||
// Everyone gathers to root, only root adds up the counts | ||
comm.gather(local, std::span{all}, root); | ||
if (root == comm.rank()) { | ||
return add_counts(all); | ||
} else { | ||
return count_type{}; | ||
} | ||
} else { | ||
// Everyone gathers and everyone adds up the counts | ||
comm.all_gather(local, all); | ||
return add_counts(all); | ||
} | ||
} | ||
|
||
} // namespace dr::mp::__detail | ||
|
||
namespace dr::mp { | ||
|
||
class count_fn_ { | ||
public: | ||
template <typename T, dr::distributed_range DR> | ||
auto operator()(std::size_t root, DR &&dr, const T &value) const { | ||
auto pred = [=](auto &&v) { return v == value; }; | ||
return __detail::count_if(root, true, dr, pred); | ||
} | ||
|
||
template <typename T, dr::distributed_range DR> | ||
auto operator()(DR &&dr, const T &value) const { | ||
auto pred = [=](auto &&v) { return v == value; }; | ||
return __detail::count_if(0, false, dr, pred); | ||
} | ||
|
||
template <typename T, dr::distributed_iterator DI> | ||
auto operator()(std::size_t root, DI first, DI last, const T &value) const { | ||
auto pred = [=](auto &&v) { return v == value; }; | ||
return __detail::count_if(root, true, rng::subrange(first, last), pred); | ||
} | ||
|
||
template <typename T, dr::distributed_iterator DI> | ||
auto operator()(DI first, DI last, const T &value) const { | ||
auto pred = [=](auto &&v) { return v == value; }; | ||
return __detail::count_if(0, false, rng::subrange(first, last), pred); | ||
} | ||
}; | ||
|
||
inline constexpr count_fn_ count; | ||
|
||
class count_if_fn_ { | ||
public: | ||
template <dr::distributed_range DR> | ||
auto operator()(std::size_t root, DR &&dr, auto &&pred) const { | ||
return __detail::count_if(root, true, dr, pred); | ||
} | ||
|
||
template <dr::distributed_range DR> | ||
auto operator()(DR &&dr, auto &&pred) const { | ||
return __detail::count_if(0, false, dr, pred); | ||
} | ||
|
||
template <dr::distributed_iterator DI> | ||
auto operator()(std::size_t root, DI first, DI last, auto &&pred) const { | ||
return __detail::count_if(root, true, rng::subrange(first, last), pred); | ||
} | ||
|
||
template <dr::distributed_iterator DI> | ||
auto operator()(DI first, DI last, auto &&pred) const { | ||
return __detail::count_if(0, false, rng::subrange(first, last), pred); | ||
} | ||
}; | ||
|
||
inline constexpr count_if_fn_ count_if; | ||
|
||
}; // namespace dr::mp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-FileCopyrightText: Intel Corporation | ||
// | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include "xp-tests.hpp" | ||
|
||
// Fixture | ||
template <typename T> class Count : public testing::Test { | ||
protected: | ||
}; | ||
|
||
TYPED_TEST_SUITE(Count, AllTypes); | ||
|
||
TYPED_TEST(Count, EmptyIf) { | ||
std::vector<int> vec; | ||
|
||
Ops1<TypeParam> ops(0); | ||
|
||
auto pred = [=](auto &&v) { return true; }; | ||
|
||
EXPECT_EQ(xp::count_if(ops.dist_vec, pred), 0); | ||
EXPECT_EQ(std::count_if(ops.vec.begin(), ops.vec.end(), pred), | ||
xp::count_if(ops.dist_vec, pred)); | ||
} | ||
|
||
TYPED_TEST(Count, BasicFirstElem) { | ||
std::vector<int> vec{1, 2, 3, 1, 1, 3, 4, 1, 5, 6, 7}; | ||
|
||
Ops1<TypeParam> ops(vec.size()); | ||
ops.vec = vec; | ||
xp::copy(ops.vec, ops.dist_vec.begin()); | ||
|
||
auto value = *ops.vec.begin(); | ||
|
||
EXPECT_EQ(xp::count(ops.dist_vec, value), 4); | ||
EXPECT_EQ(std::count(ops.vec.begin(), ops.vec.end(), value), | ||
xp::count(ops.dist_vec, value)); | ||
} | ||
|
||
TYPED_TEST(Count, BasicFirstElemIf) { | ||
std::vector<int> vec{1, 2, 3, 1, 1, 3, 4, 1, 5, 6, 7}; | ||
|
||
Ops1<TypeParam> ops(vec.size()); | ||
ops.vec = vec; | ||
xp::copy(ops.vec, ops.dist_vec.begin()); | ||
|
||
auto value = *vec.begin(); | ||
auto pred = [=](auto &&v) { return v == value; }; | ||
|
||
EXPECT_EQ(xp::count_if(ops.dist_vec, pred), 4); | ||
EXPECT_EQ(std::count_if(ops.vec.begin(), ops.vec.end(), pred), | ||
xp::count_if(ops.dist_vec, pred)); | ||
} | ||
|
||
TYPED_TEST(Count, FirstElemsIf) { | ||
std::vector<int> vec(20); | ||
std::iota(vec.begin(), vec.end(), 0); | ||
|
||
Ops1<TypeParam> ops(vec.size()); | ||
ops.vec = vec; | ||
xp::copy(ops.vec, ops.dist_vec.begin()); | ||
|
||
auto pred = [=](auto &&v) { return v < 5; }; | ||
|
||
EXPECT_EQ(xp::count_if(ops.dist_vec, pred), 5); | ||
EXPECT_EQ(std::count_if(ops.vec.begin(), ops.vec.end(), pred), | ||
lslusarczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
xp::count_if(ops.dist_vec, pred)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.