-
Notifications
You must be signed in to change notification settings - Fork 108
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
[oneDPL] Add parallel range algorithms #569
Open
akukanov
wants to merge
18
commits into
uxlfoundation:main
Choose a base branch
from
akukanov:parallel-range-algorithms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
24ed052
General description of oneDPL parallel range algorithms
akukanov 1b5c99e
Add whole sequence operations
akukanov 3147053
Add missing namespaces
akukanov 520e688
Format to fit the code block width
akukanov 8e67056
Add element search operations
akukanov 62a8d07
Add missing namespaces
akukanov 61c15e4
Add search and equal
akukanov 949162d
Add sorting and merge algorithms
akukanov 5ac78d1
Add mutating operations
akukanov 67e55a6
Separate function names from parameters
akukanov be00893
Merge updates from the upstream
akukanov c70213e
Change the header file to match the standard
akukanov 7e00eeb
Add a note describing a usual implementation
akukanov 3e3a4fb
Merge latest specification updates
akukanov 8e5706f
Add a note about parallel range algorithms
akukanov a37c240
Address the review feedback
akukanov e1f3667
Merge branch 'main' into parallel-range-algorithms
akukanov 19d7532
Add the SPDX tag for contributors' copyright
akukanov 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 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
321 changes: 321 additions & 0 deletions
321
source/elements/oneDPL/source/parallel_api/parallel_range_api.rst
This file contains 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,321 @@ | ||
.. SPDX-FileCopyrightText: Contributors to the oneAPI Specification project. | ||
.. | ||
.. SPDX-License-Identifier: CC-BY-4.0 | ||
|
||
Parallel Ranges API | ||
------------------- | ||
|
||
oneDPL provides variations of algorithms that work with ranges defined in the `C++ Standard`_, 6th edition (C++20). | ||
These algorithms execute according to a oneDPL execution policy supplied as the first argument, similarly to other | ||
oneDPL algorithms. | ||
|
||
The oneDPL parallel range algorithms rely on the functionality of C++20 and are not available in the code | ||
compiled for earlier editions of the standard. | ||
|
||
The parallel range algorithms reside in ``namespace oneapi::dpl::ranges``. Same as the range algorithm functions | ||
defined by the `C++ standard` in ``namespace std::ranges``, they cannot be found by argument-dependent name lookup. | ||
|
||
The following differences to the standard C++ range algorithms apply: | ||
|
||
- The execution policy parameter is added. | ||
- Output data sequences are defined as ranges, not iterators. | ||
- Both input and output ranges must support random access. | ||
- For a given algorithm, at least one of the input ranges as well as the output range must be bounded. | ||
- ``for_each`` does not return its function object. | ||
danhoeflinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Whole Sequence Operations | ||
+++++++++++++++++++++++++ | ||
|
||
.. code:: cpp | ||
|
||
// Defined in <oneapi/dpl/ranges> | ||
akukanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace oneapi::dpl::ranges { | ||
|
||
// all_of | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_unary_predicate< std::projected<std::ranges::iterator_t<R>, Proj> > Pred> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
bool all_of (ExecutionPolicy&& pol, R&& r, Pred pred, Proj proj = {}); | ||
|
||
// any_of | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_unary_predicate< std::projected<std::ranges::iterator_t<R>, Proj> > Pred> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
bool any_of (ExecutionPolicy&& pol, R&& r, Pred pred, Proj proj = {}); | ||
|
||
// none_of | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_unary_predicate< std::projected<std::ranges::iterator_t<R>, Proj> > Pred> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
bool none_of (ExecutionPolicy&& pol, R&& r, Pred pred, Proj proj = {}); | ||
|
||
// for_each | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirectly_unary_invocable< std::projected<std::ranges::iterator_t<R>, Proj> > Fn> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
std::ranges::borrowed_iterator_t<R> | ||
for_each (ExecutionPolicy&& pol, R&& r, Fn f, Proj proj = {}); | ||
|
||
// count | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
typename T = std::projected_value_t<std::ranges::iterator_t<R>, Proj>> | ||
danhoeflinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> && | ||
std::indirect_binary_predicate< std::ranges::equal_to, | ||
std::projected<std::ranges::iterator_t<R>, Proj>, | ||
const T* > | ||
std::ranges::range_difference_t<R> | ||
count (ExecutionPolicy&& pol, R&& r, const T& value, Proj proj = {}); | ||
|
||
// count_if | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_unary_predicate< std::projected<std::ranges::iterator_t<R>, Proj> > Pred> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
std::ranges::range_difference_t<R> | ||
count_if (ExecutionPolicy&& pol, R&& r, Pred pred, Proj proj = {}); | ||
|
||
} | ||
|
||
Element Search Operations | ||
+++++++++++++++++++++++++ | ||
|
||
.. code:: cpp | ||
|
||
// Defined in <oneapi/dpl/ranges> | ||
|
||
namespace oneapi::dpl::ranges { | ||
|
||
// find | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
typename T = std::projected_value_t<std::ranges::iterator_t<R>, Proj>> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> && | ||
std::indirect_binary_predicate< std::ranges::equal_to, | ||
std::projected<std::ranges::iterator_t<R>, Proj>, | ||
const T* > | ||
std::ranges::borrowed_iterator_t<R> | ||
find (ExecutionPolicy&& pol, R&& r, const T& value, Proj proj = {}); | ||
|
||
// find_if | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_unary_predicate< std::projected<std::ranges::iterator_t<R>, Proj> > Pred> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
std::ranges::borrowed_iterator_t<R> | ||
find_if (ExecutionPolicy&& pol, R&& r, Pred pred, Proj proj = {}); | ||
|
||
// find_if_not | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_unary_predicate< std::projected<std::ranges::iterator_t<R>, Proj> > Pred> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
std::ranges::borrowed_iterator_t<R> | ||
find_if_not (ExecutionPolicy&& pol, R&& r, Pred pred, Proj proj = {}); | ||
|
||
// adjacent_find | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_binary_predicate< std::projected<std::ranges::iterator_t<R>, Proj>, | ||
std::projected<std::ranges::iterator_t<R>, Proj> > | ||
Pred = std::ranges::equal_to> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
std::ranges::borrowed_iterator_t<R> | ||
adjacent_find (ExecutionPolicy&& pol, R&& r, Pred pred = {}, Proj proj = {}); | ||
|
||
// min_element | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_strict_weak_order< std::projected<std::ranges::iterator_t<R>, Proj> > | ||
Comp = std::ranges::less> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
std::ranges::borrowed_iterator_t<R> | ||
min_element (ExecutionPolicy&& pol, R&& r, Comp comp = {}, Proj proj = {}); | ||
|
||
// max_element | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_strict_weak_order< std::projected<std::ranges::iterator_t<R>, Proj> > | ||
Comp = std::ranges::less> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
std::ranges::borrowed_iterator_t<R> | ||
max_element (ExecutionPolicy&& pol, R&& r, Comp comp = {}, Proj proj = {}); | ||
|
||
} | ||
|
||
Sequence Search and Comparison | ||
++++++++++++++++++++++++++++++ | ||
|
||
.. code:: cpp | ||
|
||
// Defined in <oneapi/dpl/ranges> | ||
|
||
namespace oneapi::dpl::ranges { | ||
|
||
// equal | ||
template<typename ExecutionPolicy, std::ranges::random_access_range R1, | ||
std::ranges::random_access_range R2, typename Pred = std::ranges::equal_to, | ||
typename Proj1 = std::identity, typename Proj2 = std::identity> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
(std::ranges::sized_range<R1> || std::ranges::sized_range<R2>) && | ||
std::indirectly_comparable< std::ranges::iterator_t<R1>, std::ranges::iterator_t<R2>, | ||
Pred, Proj1, Proj2 > | ||
bool equal (ExecutionPolicy&& pol, R1&& r1, R2&& r2, Pred pred = {}, | ||
Proj1 proj1 = {}, Proj2 proj2 = {}); | ||
|
||
// search | ||
template<typename ExecutionPolicy, std::ranges::random_access_range R1, | ||
std::ranges::random_access_range R2, typename Pred = std::ranges::equal_to, | ||
typename Proj1 = std::identity, typename Proj2 = std::identity> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R1> && std::ranges::sized_range<R2> && | ||
std::indirectly_comparable< std::ranges::iterator_t<R1>, std::ranges::iterator_t<R2>, | ||
Pred, Proj1, Proj2 > | ||
std::ranges::borrowed_subrange_t<R1> | ||
search (ExecutionPolicy&& pol, R1&& r1, R2&& r2, Pred pred = {}, | ||
Proj1 proj1 = {}, Proj2 proj2 = {}); | ||
|
||
// search_n | ||
danhoeflinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
template<typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Pred = std::ranges::equal_to, typename Proj = std::identity, | ||
typename T = std::projected_value_t<std::ranges::iterator_t<R>, Proj>> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> && | ||
std::indirectly_comparable< std::ranges::iterator_t<R>, const T*, Pred, Proj > | ||
std::ranges::borrowed_subrange_t<R> | ||
search_n (ExecutionPolicy&& pol, R&& r, std::ranges::range_difference_t<R> count, | ||
const T& value, Pred pred = {}, Proj proj = {}); | ||
|
||
} | ||
|
||
Sorting and Merge | ||
+++++++++++++++++ | ||
|
||
.. code:: cpp | ||
|
||
// Defined in <oneapi/dpl/ranges> | ||
|
||
namespace oneapi::dpl::ranges { | ||
|
||
// sort | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Comp = std::ranges::less, typename Proj = std::identity> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> && std::sortable<std::ranges::iterator_t<R>, Comp, Proj> | ||
std::ranges::borrowed_iterator_t<R> | ||
sort (ExecutionPolicy&& pol, R&& r, Comp comp = {}, Proj proj = {}); | ||
|
||
// stable_sort | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Comp = std::ranges::less, typename Proj = std::identity> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> && std::sortable<std::ranges::iterator_t<R>, Comp, Proj> | ||
std::ranges::borrowed_iterator_t<R> | ||
stable_sort (ExecutionPolicy&& pol, R&& r, Comp comp = {}, Proj proj = {}); | ||
|
||
// is_sorted | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
typename Proj = std::identity, | ||
std::indirect_strict_weak_order< std::projected<std::ranges::iterator_t<R>, Proj> > | ||
Comp = std::ranges::less> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> | ||
bool is_sorted (ExecutionPolicy&& pol, R&& r, Comp comp = {}, Proj proj = {}); | ||
|
||
// merge | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R1, | ||
std::ranges::random_access_range R2, std::ranges::random_access_range OutR, | ||
typename Comp = std::ranges::less, typename Proj1 = std::identity, | ||
typename Proj2 = std::identity> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R1> && std::ranges::sized_range<R2> && | ||
std::ranges::sized_range<OutR> && | ||
std::mergeable<std::ranges::iterator_t<R1>, std::ranges::iterator_t<R2>, | ||
std::ranges::iterator_t<OutR>, Comp, Proj1, Proj2> | ||
std::ranges::merge_result<std::ranges::borrowed_iterator_t<R1>, | ||
std::ranges::borrowed_iterator_t<R2>, | ||
std::ranges::borrowed_iterator_t<OutR>> | ||
merge (R1&& r1, R2&& r2, OutR&& result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); | ||
|
||
} | ||
|
||
Mutating Operations | ||
+++++++++++++++++++ | ||
|
||
.. code:: cpp | ||
|
||
// Defined in <oneapi/dpl/ranges> | ||
|
||
namespace oneapi::dpl::ranges { | ||
|
||
// copy | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
std::ranges::random_access_range OutR> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> && std::ranges::sized_range<OutR> && | ||
std::indirectly_copyable<std::ranges::iterator_t<R>, std::ranges::iterator_t<OutR>> | ||
std::ranges::copy_result<std::ranges::borrowed_iterator_t<R>, | ||
std::ranges::borrowed_iterator_t<OutR>> | ||
copy (ExecutionPolicy&& pol, R&& r, OutR&& result); | ||
|
||
// copy_if | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
std::ranges::random_access_range OutR, typename Proj = std::identity, | ||
std::indirect_unary_predicate< std::projected<std::ranges::iterator_t<R>, Proj> > Pred> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> && std::ranges::sized_range<OutR> && | ||
std::indirectly_copyable<std::ranges::iterator_t<R>, std::ranges::iterator_t<OutR>> | ||
std::ranges::copy_if_result<std::ranges::borrowed_iterator_t<R>, | ||
std::ranges::borrowed_iterator_t<OutR>> | ||
copy_if (ExecutionPolicy&& pol, R&& r, OutR&& result, Pred pred, Proj proj = {}); | ||
|
||
// transform (unary) | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R, | ||
std::ranges::random_access_range OutR, std::copy_constructible Fn, | ||
typename Proj = std::identity> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
std::ranges::sized_range<R> && std::ranges::sized_range<OutR> && | ||
std::indirectly_writable< std::ranges::iterator_t<OutR>, | ||
std::indirect_result_t<Fn&, std::projected<std::ranges::iterator_t<R>, Proj>> > | ||
std::ranges::unary_transform_result<std::ranges::borrowed_iterator_t<R>, | ||
std::ranges::borrowed_iterator_t<OutR>> | ||
transform (ExecutionPolicy&& pol, R&& r, OutR&& result, Fn unary_op, Proj proj = {}); | ||
|
||
// transform (binary) | ||
template <typename ExecutionPolicy, std::ranges::random_access_range R1, | ||
std::ranges::random_access_range R2, std::ranges::random_access_range OutR, | ||
std::copy_constructible Fn, typename Proj1 = std::identity, | ||
typename Proj2 = std::identity> | ||
requires oneapi::dpl::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> && | ||
(std::ranges::sized_range<R1> || std::ranges::sized_range<R2>) && | ||
std::ranges::sized_range<OutR> && | ||
std::indirectly_writable< std::ranges::iterator_t<OutR>, | ||
std::indirect_result_t<Fn&, std::projected<std::ranges::iterator_t<R1>, Proj1>, | ||
std::projected<std::ranges::iterator_t<R2>, Proj2>> > | ||
std::ranges::binary_transform_result<std::ranges::borrowed_iterator_t<R1>, | ||
std::ranges::borrowed_iterator_t<R2>, | ||
std::ranges::borrowed_iterator_t<OutR>> | ||
transform (ExecutionPolicy&& pol, R1&& r1, R2&& r2, OutR&& result, Fn binary_op, | ||
Proj1 proj1 = {}, Proj2 proj2 = {}); | ||
|
||
} | ||
|
||
.. _`C++ Standard`: https://isocpp.org/std/the-standard | ||
.. _`SYCL`: https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want just require (on the specification level) that all of them are global callable objects (independently of where C++ standard goes)? With that requirement we give extra benefits for the users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which specifically benefits would that give? Not theoretical but real ones, something that you can imagine being useful in practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passing algorithms as arguments to a function. This is the only practical difference I can see. It's guaranteed to work. With having "special function that..." it's not guaranteed to work. That's it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's a possibility. But I do not know why one would use it in practice, given that calling signatures of those algorithms are quite different and there seems no obvious reason why this would be useful.
In simple words, I see practical value of this being (nearly) absent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a note about function object based implementations.