-
Notifications
You must be signed in to change notification settings - Fork 116
[oneDPL] Indirectly Device Accessible Iterator Customization Point and Public Trait #620
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
Merged
Merged
Changes from all commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
d90682b
Initial draft
danhoeflinger 24b879d
grammar, clarity
danhoeflinger ff940e8
formatting
danhoeflinger 0c0804b
code indentation
danhoeflinger 8cff3e4
testing indentation formatting
danhoeflinger a36a006
indentation of comments in code
danhoeflinger cb102cb
removing unnecessary const ref
danhoeflinger bc32ce2
more indentation changes
danhoeflinger e7ffb99
language improvements
danhoeflinger 913d555
remove some repetition
danhoeflinger 29e209f
fix underline
danhoeflinger 180a500
improve example, shorten var names
danhoeflinger a852f0f
improve comment clarity
danhoeflinger 41b9828
Adding link for SYCL
danhoeflinger 96b1b45
Device accessible content instead of passed directly
danhoeflinger dd71fdf
more information about device policy iterator compatibility
danhoeflinger 21b8d0f
language improvement
danhoeflinger 7c76593
readjusting content
danhoeflinger 250aed2
minor improvements
danhoeflinger 696d925
Improve language
danhoeflinger b250e77
accept suggestion
danhoeflinger 8928a06
removing legacy passed direcly description
danhoeflinger 1fff43d
Adjusting structure, and some language improvements
danhoeflinger 59c147a
remove "always"
danhoeflinger 0fe3101
Address feedback, take suggestions
danhoeflinger 1d44b35
adding template to the text
danhoeflinger a918a45
remove usm std::vector::iterators mention
danhoeflinger 25edb0b
adjust code formatting
danhoeflinger b4aeaf0
Signed-off-by: Dan Hoeflinger <dan.hoeflinger@intel.com>
danhoeflinger 4967f43
are -> is
danhoeflinger 583fd52
language adjustment for base characteristic;
danhoeflinger b8a6018
rename to "indirectly device accessible iterators"
danhoeflinger 311f2cb
formatting
danhoeflinger 8676878
Adding a section for other iterators
danhoeflinger ef67c3d
remove unnecessary implementation details
danhoeflinger 0f64be0
trait<buffer wrapper> = true
danhoeflinger 780f501
restricting permutation_iterator SourceIterator to indirectly device …
danhoeflinger a57c7d7
formatting
danhoeflinger 6114c2c
language improvements
danhoeflinger 536f2fa
rewrite to focus on requirements over implementatation
danhoeflinger 632ac23
grammar clarity
danhoeflinger b5f1d8c
remove description of implementation detail
danhoeflinger 3fa2a02
formatting
danhoeflinger 1c71426
Apply suggestions from code review
danhoeflinger 3b94837
Apply suggestions from code review
danhoeflinger 699562b
reduced examples
danhoeflinger 1369be7
removing direct mention of std::vector iterators
danhoeflinger 7e02e6a
formatting
danhoeflinger 7d07c92
better grouping requirements for permutation iterator
danhoeflinger 8e32c96
extra space removal
danhoeflinger 5cee0b6
grammar
danhoeflinger 3970f44
mention of std::vector::iterator
danhoeflinger d00d9cd
minor improvement
danhoeflinger 98a0cf4
removing comments on device copyable where we have no control / knowl…
danhoeflinger 9499cdd
Adjust "Other iterators" section
danhoeflinger d239878
clarifying USM pointer assumption for device polices only
danhoeflinger 4080952
saying less about C++ standard iterators
danhoeflinger 00c6c7e
Update source/elements/oneDPL/source/parallel_api/iterators.rst
danhoeflinger 524f8f0
Apply suggestions from code review
danhoeflinger 7616251
removing excess italics
danhoeflinger 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 |
|---|---|---|
|
|
@@ -3,9 +3,52 @@ | |
| .. | ||
| .. SPDX-License-Identifier: CC-BY-4.0 | ||
|
|
||
| .. _iterators: | ||
|
|
||
| Iterators | ||
| --------- | ||
|
|
||
| Requirements For Iterator Use With Device Policies | ||
akukanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
| Iterators and iterator-like types may or may not refer to content accessible within a `SYCL`_ kernel on a device. | ||
| The term *indirectly device accessible* refers to a type that represents content accessible on a device. | ||
| An *indirectly device accessible iterator* is such a type that can also be dereferenced within a SYCL kernel. | ||
|
|
||
| An example of indirectly device accessible iterators include SYCL USM shared pointers which can inherently be used in | ||
| a SYCL kernel. An example of an iterator type that is not indirectly device accessible is a random access iterator | ||
| referring to host allocated data because dereferencing it within a SYCL kernel would result in undefined behavior. | ||
|
|
||
| :doc:`Buffer position objects <buffer_wrappers>` returned by ``oneapi::dpl::begin`` and ``oneapi::dpl::end`` are not | ||
| iterators. However, they are indirectly device accessible because they represent data accessible on the device. | ||
|
|
||
| When passed to oneDPL algorithms with a ``device_policy``, indirectly device accessible iterator types that are also | ||
| random access iterators and satisfy *SYCL device-copyable* must not cause unnecessary data movement beyond what is | ||
| required by the algorithm's semantics and what would be required to use the type directly within a SYCL kernel. | ||
| Indirectly device accessible buffer position objects must not cause unnecessary data movement beyond what is | ||
| required by the algorithm's semantics and what would be required by using an accessor to the buffer within a SYCL | ||
| kernel. | ||
|
|
||
| Indirect Device Accessibility Type Trait | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| The following class template and variable template are defined in ``<oneapi/dpl/iterator>`` inside the namespace | ||
| ``oneapi::dpl``: | ||
|
|
||
| .. code:: cpp | ||
|
|
||
| template <typename T> | ||
| struct is_indirectly_device_accessible{ /* see below */ }; | ||
|
|
||
| template <typename T> | ||
| inline constexpr bool is_indirectly_device_accessible_v = is_indirectly_device_accessible<T>::value; | ||
|
|
||
| ``template <typename T> oneapi::dpl::is_indirectly_device_accessible`` is a type which has the base characteristic | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to use "class template" or just "template" instead of type, because
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| of ``std::true_type`` if ``T`` is indirectly device accessible. Otherwise, it has the base characteristic of | ||
| ``std::false_type``. | ||
|
|
||
| oneDPL Iterators | ||
| ++++++++++++++++ | ||
|
|
||
| The oneDPL iterators are defined in the ``<oneapi/dpl/iterator>`` header, | ||
| in ``namespace oneapi::dpl``. | ||
|
|
||
|
|
@@ -64,6 +107,8 @@ counter; dereference operations cannot be used to modify the counter. The arithm | |
| operators of ``counting_iterator`` behave as if applied to the values of Integral type | ||
| representing the counters of the iterator instances passed to the operators. | ||
|
|
||
| ``counting_iterator`` is SYCL device-copyable, and is an indirectly device accessible iterator. | ||
|
|
||
| .. code:: cpp | ||
|
|
||
| class discard_iterator | ||
|
|
@@ -104,6 +149,8 @@ lvalue that may be assigned an arbitrary value. The assignment has no effect on | |
| of ``discard_iterator`` behave as if applied to integer counter values maintained by the | ||
| iterator instances to determine their position relative to each other. | ||
|
|
||
| ``discard_iterator`` is SYCL device-copyable, and is an indirectly device accessible iterator. | ||
|
|
||
| .. code:: cpp | ||
|
|
||
| template <typename SourceIterator, typename IndexMap> | ||
|
|
@@ -150,8 +197,14 @@ defined by the source iterator provided, and whose iteration order over the dere | |
| is defined by either another iterator or a functor that maps the ``permutation_iterator`` index | ||
| to the index of the source iterator. The arithmetic and comparison operators of | ||
| ``permutation_iterator`` behave as if applied to integer counter values maintained by the | ||
| iterator instances to determine their position in the index map. ``SourceIterator`` must satisfy | ||
| ``AdaptingIteratorSource``. | ||
| iterator instances to determine their position in the index map. | ||
|
|
||
| ``permutation_iterator`` is SYCL device-copyable if both the ``SourceIterator`` and the ``IndexMap`` | ||
| are SYCL device-copyable. ``permutation_iterator`` is indirectly device accessible if both the | ||
| ``SourceIterator`` and the ``IndexMap`` are indirectly device accessible. | ||
|
|
||
| ``SourceIterator`` must satisfy ``AdaptingIteratorSource``. When using ``permutation_iterator`` in combination with an | ||
| algorithm with a ``device_policy``, ``SourceIterator`` must be indirectly device accessible. | ||
|
|
||
| The type ``IndexMap`` must be one of the following: | ||
|
|
||
|
|
@@ -163,7 +216,6 @@ The type ``IndexMap`` must be one of the following: | |
| * A functor with a signature equivalent to ``T operator()(const T&) const`` where ``T`` is a | ||
| ``std::iterator_traits<SourceIterator>::difference_type`` | ||
|
|
||
|
|
||
| ``permutation_iterator::operator*`` uses the counter value of the instance on which | ||
| it is invoked to index into the index map. The corresponding value in the map is then used | ||
| to index into the value set defined by the source iterator. The resulting lvalue is returned | ||
|
|
@@ -234,6 +286,9 @@ arithmetic and comparison operators of ``transform_iterator`` behave as if appli | |
| source iterator itself. The template type ``Iterator`` must satisfy | ||
| ``AdaptingIteratorSource``. | ||
|
|
||
| ``transform_iterator`` is SYCL device-copyable if the source iterator is SYCL device-copyable, and | ||
| is indirectly device accessible if the source iterator is indirectly device accessible. | ||
|
|
||
| .. code:: cpp | ||
|
|
||
| template <typename UnaryFunc, typename Iterator> | ||
|
|
@@ -293,6 +348,9 @@ source iterators over which the ``zip_iterator`` is defined. The arithmetic oper | |
| operation were applied to each of these iterators. The types ``T`` within the template pack | ||
| ``Iterators...`` must satisfy ``AdaptingIteratorSource``. | ||
|
|
||
| ``zip_iterator`` is SYCL device-copyable if all the source iterators are SYCL device-copyable, and is indirectly | ||
| device accessible if all the source iterators are indirectly device accessible. | ||
|
|
||
| .. code:: cpp | ||
|
|
||
| template <typename... Iterators> | ||
|
|
@@ -301,3 +359,85 @@ operation were applied to each of these iterators. The types ``T`` within the te | |
|
|
||
| ``make_zip_iterator`` constructs and returns an instance of ``zip_iterator`` | ||
| using the set of source iterators provided. | ||
|
|
||
| Other Iterators | ||
| +++++++++++++++ | ||
| Pointers are assumed to be USM shared or device memory pointers when used in combination with an algorithm with a | ||
| ``device_policy`` and are indirectly device accessible. Pointers are trivially copyable and therefore SYCL | ||
| device-copyable. | ||
|
|
||
| It is implementation defined whether other iterators are indirectly device accessible, including iterator types from | ||
| the `C++ Standard`_. | ||
|
|
||
| .. _iterators-device-accessible: | ||
|
|
||
| Customization For User Defined Iterators | ||
akukanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ++++++++++++++++++++++++++++++++++++++++ | ||
| oneDPL provides a mechanism to indicate whether custom iterators are indirectly device accessible. | ||
|
|
||
| Applications may define a free function ``is_onedpl_indirectly_device_accessible(T)``, which accepts an argument of | ||
| type ``T`` and returns a type with the base characteristic of ``std::true_type`` if ``T`` is indirectly device | ||
| accessible. Otherwise, it returns a type with the base characteristic of ``std::false_type``. The function must be | ||
| discoverable by argument-dependent lookup (ADL). It may be provided as a forward declaration only, without defining a | ||
| body. | ||
|
|
||
| The return type of ``is_onedpl_indirectly_device_accessible`` is examined at compile time to determine if ``T`` is | ||
| indirectly device accessible. The function overload to use must be selected with argument-dependent lookup. | ||
| [*Note*: Therefore, according to the rules in the C++ Standard, a derived type for which there is no | ||
| function overload will match its most specific base type for which an overload exists. -- *end note*] | ||
|
|
||
| Once ``is_onedpl_indirectly_device_accessible(T)`` is defined, the public trait | ||
| ``template<typename T> oneapi::dpl::is_indirectly_device_accessible[_v]`` will return the appropriate value. This public | ||
| trait can also be used to define the return type of ``is_onedpl_indirectly_device_accessible(T)`` by applying it to any | ||
| source iterator component types. Refer to the example below. | ||
|
|
||
| Example | ||
| ^^^^^^^ | ||
|
|
||
| The following example shows how to define a customization for ``is_indirectly_device_accessible`` trait for a simple | ||
| user defined iterator. It also shows a more complicated example where the customization is defined as a hidden friend | ||
| of the iterator class. | ||
|
|
||
| .. code:: cpp | ||
|
|
||
| namespace usr | ||
| { | ||
| struct accessible_it | ||
| { | ||
| /* user definition of an indirectly device accessible iterator */ | ||
| }; | ||
|
|
||
| std::true_type | ||
| is_onedpl_indirectly_device_accessible(accessible_it); | ||
|
|
||
| struct inaccessible_it | ||
| { | ||
| /* user definition of an iterator which is not indirectly device accessible */ | ||
| }; | ||
|
|
||
| // The following could be omitted, as returning std::false_type matches the default behavior. | ||
| std::false_type | ||
| is_onedpl_indirectly_device_accessible(inaccessible_it); | ||
| } | ||
|
|
||
| static_assert(oneapi::dpl::is_indirectly_device_accessible<usr::accessible_it> == true); | ||
| static_assert(oneapi::dpl::is_indirectly_device_accessible<usr::inaccessible_it> == false); | ||
|
|
||
| // Example with base iterators and ADL overload as a hidden friend | ||
| template <typename It1, typename It2> | ||
| struct it_pair | ||
| { | ||
| It1 first; | ||
| It2 second; | ||
| friend auto is_onedpl_indirectly_device_accessible(it_pair) -> | ||
| std::conjunction<oneapi::dpl::is_indirectly_device_accessible<It1>, | ||
| oneapi::dpl::is_indirectly_device_accessible<It2>>; | ||
| }; | ||
|
|
||
| static_assert(oneapi::dpl::is_indirectly_device_accessible< | ||
| it_pair<usr::accessible_it, usr::accessible_it>> == true); | ||
| static_assert(oneapi::dpl::is_indirectly_device_accessible< | ||
| it_pair<usr::accessible_it, usr::inaccessible_it>> == false); | ||
|
|
||
| .. _`C++ Standard`: https://isocpp.org/std/the-standard | ||
| .. _`SYCL`: https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html | ||
Oops, something went wrong.
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.