Skip to content

Commit ed1d7a7

Browse files
authored
[Documentation] Use task_arena interoperability functionality with task_group (#1866)
Signed-off-by: Isaev, Ilya <[email protected]>
1 parent 6c1ea12 commit ed1d7a7

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

doc/main/tbb_userguide/Guiding_Task_Scheduler_Execution.rst

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,10 @@ a different NUMA node. To reduce this overhead, the work may be divided among se
4242
instances, whose execution preference is set to different NUMA nodes. To set execution preference,
4343
assign a NUMA node identifier to the ``task_arena::constraints::numa_id`` field.
4444

45-
::
46-
47-
std::vector<tbb::numa_node_id> numa_indexes = tbb::info::numa_nodes();
48-
std::vector<tbb::task_arena> arenas(numa_indexes.size());
49-
std::vector<tbb::task_group> task_groups(numa_indexes.size());
50-
51-
for(unsigned j = 0; j < numa_indexes.size(); j++) {
52-
arenas[j].initialize(tbb::task_arena::constraints(numa_indexes[j]));
53-
arenas[j].execute([&task_groups, &j](){ 
54-
task_groups[j].run([](){/*some parallel stuff*/});
55-
});
56-
}
57-
58-
for(unsigned j = 0; j < numa_indexes.size(); j++) {
59-
arenas[j].execute([&task_groups, &j](){ task_groups[j].wait(); });
60-
}
45+
.. literalinclude:: ./examples/guiding_task_scheduler_execution.cpp
46+
:language: c++
47+
:start-after: /*begin_set_numa_node_example*/
48+
:end-before: /*end_set_numa_node_example*/
6149

6250
Set Core Type
6351
*************
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright (c) 2025 Intel Corporation
3+
Copyright (c) 2025 UXL Foundation Contributors
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
#include <vector>
19+
#include <oneapi/tbb/info.h>
20+
#include <oneapi/tbb/task_arena.h>
21+
#include <oneapi/tbb/task_group.h>
22+
23+
void set_numa_node_example() {
24+
/*begin_set_numa_node_example*/
25+
std::vector<tbb::numa_node_id> numa_nodes = tbb::info::numa_nodes();
26+
std::vector<tbb::task_arena> arenas(numa_nodes.size());
27+
std::vector<tbb::task_group> task_groups(numa_nodes.size());
28+
29+
// Since the library creates one less worker thread than the number of total cores,
30+
// all but one of the arenas are created without reserving a slot for an external thread,
31+
// allowing them to be fully populated.
32+
for(unsigned j = 1; j < numa_nodes.size(); j++) {
33+
arenas[j].initialize(tbb::task_arena::constraints(numa_nodes[j]), /*reserved_slots*/ 0);
34+
arenas[j].enqueue([](){/*some parallel work*/}, task_groups[j]);
35+
}
36+
37+
// The main thread executes in the arena with the reserved slot.
38+
arenas[0].initialize(tbb::task_arena::constraints(numa_nodes[0]));
39+
arenas[0].execute([&task_groups](){
40+
task_groups[0].run([](){/*some parallel work*/});
41+
});
42+
43+
for(unsigned j = 0; j < numa_nodes.size(); j++) {
44+
arenas[j].wait_for(task_groups[j]);
45+
}
46+
/*end_set_numa_node_example*/
47+
}
48+
49+
int main() {
50+
set_numa_node_example();
51+
return 0;
52+
}

0 commit comments

Comments
 (0)