Skip to content

Commit 192af07

Browse files
akukanovrarutyun
andauthored
[oneDPL] Clarify the semantics of dpl::begin/end return type (#570)
Co-authored-by: Ruslan Arutyunyan <[email protected]>
1 parent 61ebe78 commit 192af07

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

source/elements/oneDPL/source/parallel_api/buffer_wrappers.rst

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,57 @@ Buffer Wrappers
4343
4444
``oneapi::dpl::begin`` and ``oneapi::dpl::end`` are helper functions
4545
for passing `SYCL`_ buffers to oneDPL algorithms.
46-
These functions accept a buffer and return an object
47-
of an unspecified type that satisfies the following requirements:
48-
49-
- it is ``CopyConstructible``, ``CopyAssignable``, and comparable
50-
with operators ``==`` and ``!=``;
51-
- the following expressions are valid: ``a + n``, ``a - n``,
52-
``a - b``, where ``a`` and ``b`` are objects of the type,
53-
and ``n`` is an integer value;
54-
- it provides the ``get_buffer()`` method that returns the buffer
55-
passed to the ``begin`` or ``end`` function.
46+
These functions accept a SYCL buffer and return a *buffer position object*,
47+
which type satisfies the following requirements but is otherwise unspecified:
48+
49+
- It is copy-constructible and copy-assignable.
50+
- It is comparable with ``operator==`` and ``operator!=``.
51+
- It provides the ``get_buffer()`` method that for a buffer position object returns the SYCL buffer,
52+
which the object was built over.
53+
- The expressions ``a + n`` and ``a - n``, where ``a`` is a buffer position object and ``n``
54+
is an integer value, are valid and evaluate to a buffer position object such that
55+
the corresponding position in the buffer follows (precedes) that of ``a`` by ``n``.
56+
If this new position is out of the buffer bounds, the behavior is undefined.
57+
- The expression ``a - b``, where ``a`` and ``b`` are buffer position objects,
58+
is valid and evaluates to an integer value ``n`` such that ``a == b + n``.
59+
60+
If these operators and expressions are applied to buffer position objects that are not built
61+
over the same SYCL buffer, the behavior is undefined.
5662

5763
When invoking an algorithm, the buffer passed to ``begin`` should be the same
5864
as the buffer passed to ``end``. Otherwise, the behavior is undefined.
5965

66+
An algorithm may return a buffer position object, which can then be compared and used
67+
in expressions with other objects built over the same buffer.
68+
69+
.. code:: cpp
70+
71+
namespace dpl = oneapi::dpl; // see "Namespaces"
72+
sycl::buffer buf {/*...*/};
73+
auto pos = dpl::find(dpl::execution::dpcpp_default, dpl::begin(buf), dpl::end(buf), value);
74+
int value_index = (pos == dpl::end(buf)) ? -1 : (pos - dpl::begin(buf));
75+
76+
.. note::
77+
Though buffer position objects substitute for iterators as arguments and return values
78+
of oneDPL algorithms, they cannot be used as iterators in other contexts, including dereference,
79+
as their type does not satisfy the C++ standard requirements for an iterator.
80+
6081
SYCL deduction tags (the ``TagT`` parameters) and ``sycl::property::no_init``
61-
allow to specify an access mode to be used by algorithms for accessing the buffer.
82+
allow to specify an access mode to be used by algorithms for accessing a SYCL buffer.
6283
The mode serves as a hint, and can be overridden depending on semantics of the algorithm.
6384
When invoking an algorithm, the same access mode arguments should be used
6485
for ``begin`` and ``end``. Otherwise, the behavior is undefined.
6586

6687
.. code:: cpp
67-
68-
using namespace oneapi;
69-
auto buf_begin = dpl::begin(buf, sycl::write_only);
70-
auto buf_end_1 = dpl::end(buf, sycl::write_only);
71-
auto buf_end_2 = dpl::end(buf, sycl::write_only, sycl::no_init);
72-
dpl::fill(dpl::execution::dpcpp_default, buf_begin, buf_end_1, 42); // allowed
73-
dpl::fill(dpl::execution::dpcpp_default, buf_begin, buf_end_2, 42); // not allowed
88+
89+
namespace dpl = oneapi::dpl;
90+
sycl::buffer buf {/*...*/};
91+
auto policy = dpl::execution::dpcpp_default;
92+
93+
auto buf_begin = dpl::begin(buf, sycl::write_only);
94+
auto buf_end_1 = dpl::end(buf, sycl::write_only); // arguments match begin()
95+
dpl::fill(policy, buf_begin, buf_end_1, 42); // OK
96+
auto buf_end_2 = dpl::end(buf, sycl::write_only, sycl::no_init); // arguments do not match
97+
dpl::fill(policy, buf_begin, buf_end_2, 42); // undefined behavior
7498
7599
.. _`SYCL`: https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html

0 commit comments

Comments
 (0)