Skip to content

Commit 65cf4a2

Browse files
alex-katranovkboyarinovaepanchi
authored
[oneTBB] Add task_scheduler_handle (#358)
* Add task_scheduler_handle Signed-off-by: Alexei Katranov <[email protected]> * Update source/elements/oneTBB/source/task_scheduler/scheduling_controls/task_scheduler_handle_cls.rst Co-authored-by: kboyarinov <[email protected]> * Update task_scheduler_handle_cls.rst Fix formatting * Apply suggestions from code review Co-authored-by: Alexandra <[email protected]> * Update source/elements/oneTBB/source/index.rst Co-authored-by: Alexandra <[email protected]> * Apply review remarks Signed-off-by: Alexei Katranov <[email protected]> * Update source/elements/oneTBB/source/index.rst * Apply suggestions from code review Rework otherwise text * Change copyright year - task_arena_attach_tag.rst * Change copyright - index.rst * Add deprecated nested index page * Fix copyright in nested index * Fix namespace indentation Co-authored-by: kboyarinov <[email protected]> Co-authored-by: Alexandra <[email protected]>
1 parent 0d60525 commit 65cf4a2

File tree

8 files changed

+329
-56
lines changed

8 files changed

+329
-56
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
.. SPDX-FileCopyrightText: 2021 Intel Corporation
2+
..
3+
.. SPDX-License-Identifier: CC-BY-4.0
4+
5+
==================
6+
task_arena::attach
7+
==================
8+
**[deprecated.task_arena_attach_tag]**
9+
10+
.. caution::
11+
12+
Deprecated in oneTBB Specification 1.1.
13+
14+
A set of methods for constructing a ``task_arena`` with ``attach``.
15+
16+
.. code:: cpp
17+
18+
// Defined in header <oneapi/tbb/task_arena.h>
19+
20+
namespace oneapi {
21+
namespace tbb {
22+
23+
class task_arena {
24+
public:
25+
// ...
26+
struct attach {};
27+
28+
explicit task_arena(task_arena::attach);
29+
void initialize(task_arena::attach);
30+
// ...
31+
};
32+
33+
} // namespace tbb
34+
} // namespace oneapi
35+
36+
37+
Member types and constants
38+
--------------------------
39+
40+
.. cpp:struct:: attach
41+
42+
A tag for constructing a ``task_arena`` with ``attach``.
43+
44+
Member functions
45+
----------------
46+
47+
.. cpp:function:: explicit task_arena(task_arena::attach)
48+
49+
Creates an instance of ``task_arena`` that is connected to the internal task arena representation currently used by the calling thread.
50+
If no such arena exists yet, creates a ``task_arena`` with default parameters.
51+
52+
.. note::
53+
54+
Unlike other ``task_arena`` constructors, this one automatically initializes
55+
the new ``task_arena`` when connecting to an already existing arena.
56+
57+
58+
.. cpp:function:: void initialize(task_arena::attach)
59+
60+
If an internal task arena representation currently used by the calling thread, the method ignores arena
61+
parameters and connects ``task_arena`` to that internal task arena representation.
62+
The method has no effect when called for an already initialized ``task_arena``.
63+
64+
See also:
65+
66+
* :doc:`attach <../task_scheduler/attach_tag_type>`

source/elements/oneTBB/source/index.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. SPDX-FileCopyrightText: 2019-2020 Intel Corporation
1+
.. SPDX-FileCopyrightText: 2019-2021 Intel Corporation
22
..
33
.. SPDX-License-Identifier: CC-BY-4.0
44
@@ -35,3 +35,9 @@ oneAPI Threading Building Blocks Specification
3535
mutual_exclusion.rst
3636
timing.rst
3737
info_namespace.rst
38+
39+
.. toctree::
40+
:maxdepth: 2
41+
:caption: oneTBB Deprecated Interfaces:
42+
43+
deprecated/task_arena_attach_tag.rst
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. SPDX-FileCopyrightText: 2021 Intel Corporation
2+
..
3+
.. SPDX-License-Identifier: CC-BY-4.0
4+
5+
=============================
6+
oneTBB Deprecated Interfaces
7+
=============================
8+
9+
.. toctree::
10+
:maxdepth: 2
11+
12+
deprecated/task_arena_attach_tag.rst

source/elements/oneTBB/source/nested-index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. SPDX-FileCopyrightText: 2019-2020 Intel Corporation
1+
.. SPDX-FileCopyrightText: 2019-2021 Intel Corporation
22
..
33
.. SPDX-License-Identifier: CC-BY-4.0
44
@@ -14,3 +14,4 @@ oneTBB
1414
nested-gen-info
1515
nested-interfaces
1616
nested-aux-interfaces
17+
nested-depr-interfaces

source/elements/oneTBB/source/task_scheduler.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. SPDX-FileCopyrightText: 2019-2020 Intel Corporation
1+
.. SPDX-FileCopyrightText: 2019-2021 Intel Corporation
22
..
33
.. SPDX-License-Identifier: CC-BY-4.0
44
@@ -37,6 +37,7 @@ Scheduling controls
3737
task_scheduler/scheduling_controls/task_group_context_cls.rst
3838
task_scheduler/scheduling_controls/global_control_cls.rst
3939
task_scheduler/scheduling_controls/resumable_tasks.rst
40+
task_scheduler/scheduling_controls/task_scheduler_handle_cls.rst
4041

4142
Task Group
4243
----------
@@ -56,3 +57,12 @@ Task Arena
5657
task_scheduler/task_arena/task_arena_cls.rst
5758
task_scheduler/task_arena/this_task_arena_ns.rst
5859
task_scheduler/task_arena/task_scheduler_observer_cls.rst
60+
61+
Helper types
62+
------------
63+
64+
.. toctree::
65+
:titlesonly:
66+
67+
task_scheduler/attach_tag_type.rst
68+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. SPDX-FileCopyrightText: 2021 Intel Corporation
2+
..
3+
.. SPDX-License-Identifier: CC-BY-4.0
4+
5+
===============
6+
attach tag type
7+
===============
8+
**[scheduler.attach]**
9+
10+
An ``attach`` tag type is specifically used with ``task_arena`` and
11+
``task_scheduler_handle`` interfaces. It is guaranteed to be constructible by default.
12+
13+
.. code:: cpp
14+
15+
namespace oneapi {
16+
namespace tbb {
17+
using attach = /* unspecified */
18+
}
19+
}
20+
21+
See also:
22+
23+
* :doc:`task_arena <task_arena/task_arena_cls>`
24+
* :doc:`task_scheduler_handle <scheduling_controls/task_scheduler_handle_cls>`
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
.. SPDX-FileCopyrightText: 2021 Intel Corporation
2+
..
3+
.. SPDX-License-Identifier: CC-BY-4.0
4+
5+
=====================
6+
task_scheduler_handle
7+
=====================
8+
**[scheduler.task_scheduler_handle]**
9+
10+
The ``oneapi::tbb::task_scheduler_handle`` class and the ``oneapi::tbb::finalize`` function allow user to wait for completion of worker threads.
11+
12+
When the ``oneapi::tbb::finalize`` function is called with an ``oneapi::tbb::task_scheduler_handle`` instance, it blocks the calling
13+
thread until the completion of all worker threads that were implicitly created by the library.
14+
15+
16+
.. code:: cpp
17+
18+
// Defined in header <oneapi/tbb/global_control.h>
19+
20+
namespace oneapi {
21+
namespace tbb {
22+
23+
class task_scheduler_handle {
24+
public:
25+
task_scheduler_handle() = default;
26+
task_scheduler_handle(oneapi::tbb::attach);
27+
~task_scheduler_handle();
28+
29+
task_scheduler_handle(const task_scheduler_handle& other) = delete;
30+
task_scheduler_handle(task_scheduler_handle&& other) noexcept;
31+
task_scheduler_handle& operator=(const task_scheduler_handle& other) = delete;
32+
task_scheduler_handle& operator=(task_scheduler_handle&& other) noexcept;
33+
34+
explicit operator bool() const noexcept;
35+
36+
void release();
37+
};
38+
39+
void finalize(task_scheduler_handle& handle);
40+
bool finalize(task_scheduler_handle& handle, const std::nothrow_t&) noexcept;
41+
42+
} // namespace tbb
43+
} // namespace oneapi
44+
45+
Member Functions
46+
----------------
47+
48+
.. cpp:function:: task_scheduler_handle()
49+
50+
**Effects**: Creates an empty instance of the ``task_scheduler_handle`` class that does not contain any references to the task scheduler.
51+
52+
-------------------------------------------------------
53+
54+
.. cpp:function:: task_scheduler_handle(oneapi::tbb::attach)
55+
56+
**Effects**: Creates an instance of the ``task_scheduler_handle`` class that holds a reference to the task scheduler preventing
57+
its premature destruction.
58+
59+
-------------------------------------------------------
60+
61+
.. cpp:function:: ~task_scheduler_handle()
62+
63+
**Effects**: Destroys an instance of the ``task_scheduler_handle`` class.
64+
If not empty, releases a reference to the task scheduler and deactivates an instance of the ``task_scheduler_handle`` class.
65+
66+
-------------------------------------------------------
67+
68+
.. cpp:function:: task_scheduler_handle(task_scheduler_handle&& other) noexcept
69+
70+
**Effects**: Creates an instance of the ``task_scheduler_handle`` class that references the task scheduler referenced by ``other``. In turn, ``other`` releases its reference to the task scheduler.
71+
72+
-------------------------------------------------------
73+
74+
.. cpp:function:: task_scheduler_handle& operator=(task_scheduler_handle&& other) noexcept
75+
76+
**Effects**: If not empty, releases a reference to the task scheduler referenced by ``this``. Adds a reference to the task scheduler referenced by ``other``.
77+
In turn, ``other`` releases its reference to the task scheduler.
78+
**Returns**: A reference to ``*this``.
79+
80+
-------------------------------------------------------
81+
82+
.. cpp:function:: explicit operator bool() const noexcept
83+
84+
**Returns**: ``true`` if ``this`` is not empty and refers to some task scheduler; ``false`` otherwise.
85+
86+
-------------------------------------------------------
87+
88+
.. cpp:function:: void release()
89+
90+
**Effects**: If not empty, releases a reference to the task scheduler and deactivates an instance of the ``task_scheduler_handle``
91+
class; otherwise, does nothing. Non-blocking method.
92+
93+
Non-member Functions
94+
--------------------
95+
96+
.. cpp:function:: void finalize(task_scheduler_handle& handle)
97+
98+
**Effects**: If ``handle`` is not empty, blocks the program execution until all worker threads have been completed; otherwise, does nothing.
99+
Throws the ``oneapi::tbb::unsafe_wait`` exception if it is not safe to wait for the completion of the worker threads.
100+
101+
The following conditions should be met for finalization to succeed:
102+
103+
- No active, not yet terminated, instances of ``task_arena`` class exist in the whole program.
104+
- ``task_scheduler_handle::release`` is called for each other active instance of ``task_scheduler_handle`` class, possibly by different application threads.
105+
106+
Under these conditions, it is guaranteed that at least one ``finalize`` call succeeds,
107+
at which point all worker threads have been completed.
108+
If calls are performed simultaneously, more than one call might succeed.
109+
110+
.. note::
111+
112+
If user knows how many active ``task_scheduler_handle`` instances exist in the program,
113+
it is necessary to ``release`` all but the last one, then call ``finalize`` for
114+
the last instance.
115+
116+
.. caution::
117+
118+
The method always fails if called within a task, a parallel algorithm, or a flow graph node.
119+
120+
-------------------------------------------------------
121+
122+
.. cpp:function:: bool finalize(task_scheduler_handle& handle, const std::nothrow_t&) noexcept
123+
124+
**Effects**: If ``handle`` is not empty, blocks the program execution until all worker threads have been completed; otherwise, does nothing.
125+
The behavior is the same as finalize(handle); however, ``false`` is returned instead of exception or ``true`` if no exception.
126+
127+
Examples
128+
--------
129+
130+
.. code:: cpp
131+
132+
#include <oneapi/tbb/global_control.h>
133+
#include <oneapi/tbb/parallel_for.h>
134+
135+
#include <iostream>
136+
137+
int main() {
138+
oneapi::tbb::task_scheduler_handle handle;
139+
140+
handle = oneapi::tbb::task_scheduler_handle{oneapi::tbb::attach{}};
141+
142+
// Do some parallel work here, e.g.
143+
oneapi::tbb::parallel_for(0, 10000, [](int){});
144+
try {
145+
oneapi::tbb::finalize(handle);
146+
// oneTBB worker threads are terminated at this point.
147+
} catch (const oneapi::tbb::unsafe_wait&) {
148+
std::cerr << "Failed to terminate the worker threads." << std::endl;
149+
}
150+
return 0;
151+
}
152+
153+
See also:
154+
155+
* :doc:`attach <../attach_tag_type>`

0 commit comments

Comments
 (0)