|
| 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