Debian Codegraph @

third-party/tbb/src/tbbbind/tbb_bind.cpp

raw
1 /*
2 Copyright (c) 2019-2024 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #include <vector>
18 #include <mutex>
19
20 #include "../tbb/assert_impl.h" // Out-of-line TBB assertion handling routines are instantiated here.
21 #include "oneapi/tbb/detail/_assert.h"
22 #include "oneapi/tbb/detail/_config.h"
23
24 #if _MSC_VER && !__INTEL_COMPILER && !__clang__
25 #pragma warning( push )
26 #pragma warning( disable : 4100 )
27 #elif _MSC_VER && __clang__
28 #pragma GCC diagnostic push
29 #pragma GCC diagnostic ignored "-Wunused-parameter"
30 #endif
31 #include <hwloc.h>
32 #if _MSC_VER && !__INTEL_COMPILER && !__clang__
33 #pragma warning( pop )
34 #elif _MSC_VER && __clang__
35 #pragma GCC diagnostic pop
36 #endif
37
38 #define __TBBBIND_HWLOC_HYBRID_CPUS_INTERFACES_PRESENT (HWLOC_API_VERSION >= 0x20400)
39 #define __TBBBIND_HWLOC_TOPOLOGY_FLAG_RESTRICT_TO_CPUBINDING_PRESENT (HWLOC_API_VERSION >= 0x20500)
40 #define __TBBBIND_HWLOC_WINDOWS_API_AVAILABLE (_WIN32 && HWLOC_API_VERSION >= 0x20500)
41 #if __TBBBIND_HWLOC_WINDOWS_API_AVAILABLE
42 #include <hwloc/windows.h>
43 #endif
44
45 // Most of hwloc calls returns negative exit code on error.
46 // This macro tracks error codes that are returned from the hwloc interfaces.
47 #define assertion_hwloc_wrapper(command, ...) \
48 __TBB_ASSERT_EX( (command(__VA_ARGS__)) >= 0, "Error occurred during call to hwloc API.");
49
50 namespace tbb {
51 namespace detail {
52 namespace r1 {
53
54 //------------------------------------------------------------------------
55 // Information about the machine's hardware TBB is happen to work on
56 //------------------------------------------------------------------------
57 class system_topology {
58 friend class binding_handler;
59
60 // Common topology members
61 hwloc_topology_t topology{nullptr};
62 hwloc_cpuset_t process_cpu_affinity_mask{nullptr};
63 hwloc_nodeset_t process_node_affinity_mask{nullptr};
64 std::size_t number_of_processors_groups{1};
65 std::vector<hwloc_cpuset_t> processor_groups_affinity_masks_list{};
66
67 // NUMA API related topology members
68 std::vector<hwloc_cpuset_t> numa_affinity_masks_list{};
69 std::vector<int> numa_indexes_list{};
70 int numa_nodes_count{0};
71
72 // Hybrid CPUs API related topology members
73 std::vector<hwloc_cpuset_t> core_types_affinity_masks_list{};
74 std::vector<int> core_types_indexes_list{};
75
76 enum init_stages { uninitialized,
77 started,
78 topology_allocated,
79 topology_loaded,
80 topology_parsed } initialization_state;
81
82 // Binding threads that locate in another Windows Processor groups
83 // is allowed only if machine topology contains several Windows Processors groups
84 // and process affinity mask wasn't limited manually (affinity mask cannot violate
85 // processors group boundaries).
86 bool intergroup_binding_allowed(std::size_t groups_num) { return groups_num > 1; }
87
88 private:
89 void topology_initialization(std::size_t groups_num) {
90 initialization_state = started;
91
92 // Parse topology
93 if ( hwloc_topology_init( &topology ) == 0 ) {
94 initialization_state = topology_allocated;
95 #if __TBBBIND_HWLOC_TOPOLOGY_FLAG_RESTRICT_TO_CPUBINDING_PRESENT
96 unsigned long flags = 0;
97 if (groups_num > 1) {
98 // HWLOC x86 backend might interfere with process affinity mask on
99 // Windows systems with multiple processor groups.
100 flags = HWLOC_TOPOLOGY_FLAG_DONT_CHANGE_BINDING;
101 } else {
102 flags = HWLOC_TOPOLOGY_FLAG_IS_THISSYSTEM | HWLOC_TOPOLOGY_FLAG_RESTRICT_TO_CPUBINDING;
103 }
104 if (hwloc_topology_set_flags(topology, flags) != 0) {
105 return;
106 }
107 #endif
108 if ( hwloc_topology_load( topology ) == 0 ) {
109 initialization_state = topology_loaded;
110 }
111 }
112 if ( initialization_state != topology_loaded )
113 return;
114
115 #if __TBB_CPUBIND_PRESENT
116 // Getting process affinity mask
117 if ( intergroup_binding_allowed(groups_num) ) {
118 process_cpu_affinity_mask = hwloc_bitmap_dup(hwloc_topology_get_complete_cpuset (topology));
119 process_node_affinity_mask = hwloc_bitmap_dup(hwloc_topology_get_complete_nodeset(topology));
120 } else {
121 process_cpu_affinity_mask = hwloc_bitmap_alloc();
122 process_node_affinity_mask = hwloc_bitmap_alloc();
123
124 assertion_hwloc_wrapper(hwloc_get_cpubind, topology, process_cpu_affinity_mask, 0);
125 hwloc_cpuset_to_nodeset(topology, process_cpu_affinity_mask, process_node_affinity_mask);
126 }
127 #else
128 process_cpu_affinity_mask = hwloc_bitmap_dup(hwloc_topology_get_complete_cpuset (topology));
129 process_node_affinity_mask = hwloc_bitmap_dup(hwloc_topology_get_complete_nodeset(topology));
130 #endif
131
132 number_of_processors_groups = groups_num;
133 }
134
135 void numa_topology_parsing() {
136 // Fill parameters with stubs if topology parsing is broken.
137 if ( initialization_state != topology_loaded ) {
138 numa_nodes_count = 1;
139 numa_indexes_list.push_back(-1);
140 return;
141 }
142
143 // If system contains no NUMA nodes, HWLOC 1.11 returns an infinitely filled bitmap.
144 // hwloc_bitmap_weight() returns negative value for such bitmaps, so we use this check
145 // to change way of topology initialization.
146 numa_nodes_count = hwloc_bitmap_weight(process_node_affinity_mask);
147 if (numa_nodes_count <= 0) {
148 // numa_nodes_count may be empty if the process affinity mask is empty too (invalid case)
149 // or if some internal HWLOC error occurred.
150 // So we place -1 as index in this case.
151 numa_indexes_list.push_back(numa_nodes_count == 0 ? -1 : 0);
152 numa_nodes_count = 1;
153
154 numa_affinity_masks_list.push_back(hwloc_bitmap_dup(process_cpu_affinity_mask));
155 } else {
156 // Get NUMA logical indexes list
157 unsigned counter = 0;
158 int i = 0;
159 int max_numa_index = -1;
160 numa_indexes_list.resize(numa_nodes_count);
161 hwloc_obj_t node_buffer;
162 hwloc_bitmap_foreach_begin(i, process_node_affinity_mask) {
163 node_buffer = hwloc_get_numanode_obj_by_os_index(topology, i);
164 numa_indexes_list[counter] = static_cast<int>(node_buffer->logical_index);
165
166 if ( numa_indexes_list[counter] > max_numa_index ) {
167 max_numa_index = numa_indexes_list[counter];
168 }
169
170 counter++;
171 } hwloc_bitmap_foreach_end();
172 __TBB_ASSERT(max_numa_index >= 0, "Maximal NUMA index must not be negative");
173
174 // Fill concurrency and affinity masks lists
175 numa_affinity_masks_list.resize(max_numa_index + 1);
176 int index = 0;
177 hwloc_bitmap_foreach_begin(i, process_node_affinity_mask) {
178 node_buffer = hwloc_get_numanode_obj_by_os_index(topology, i);
179 index = static_cast<int>(node_buffer->logical_index);
180
181 hwloc_cpuset_t& current_mask = numa_affinity_masks_list[index];
182 current_mask = hwloc_bitmap_dup(node_buffer->cpuset);
183
184 hwloc_bitmap_and(current_mask, current_mask, process_cpu_affinity_mask);
185 __TBB_ASSERT(!hwloc_bitmap_iszero(current_mask), "hwloc detected unavailable NUMA node");
186 } hwloc_bitmap_foreach_end();
187 }
188 }
189
190 void core_types_topology_parsing() {
191 // Fill parameters with stubs if topology parsing is broken.
192 if ( initialization_state != topology_loaded ) {
193 core_types_indexes_list.push_back(-1);
194 return;
195 }
196 #if __TBBBIND_HWLOC_HYBRID_CPUS_INTERFACES_PRESENT
197 __TBB_ASSERT(hwloc_get_api_version() >= 0x20400, "Hybrid CPUs support interfaces required HWLOC >= 2.4");
198 // Parsing the hybrid CPU topology
199 int core_types_number = hwloc_cpukinds_get_nr(topology, 0);
200 bool core_types_parsing_broken = core_types_number <= 0;
201 if (!core_types_parsing_broken) {
202 core_types_affinity_masks_list.resize(core_types_number);
203 int efficiency{-1};
204
205 for (int core_type = 0; core_type < core_types_number; ++core_type) {
206 hwloc_cpuset_t& current_mask = core_types_affinity_masks_list[core_type];
207 current_mask = hwloc_bitmap_alloc();
208
209 if (!hwloc_cpukinds_get_info(topology, core_type, current_mask, &efficiency, nullptr, nullptr, 0)
210 && efficiency >= 0
211 ) {
212 hwloc_bitmap_and(current_mask, current_mask, process_cpu_affinity_mask);
213
214 if (hwloc_bitmap_weight(current_mask) > 0) {
215 core_types_indexes_list.push_back(core_type);
216 }
217 __TBB_ASSERT(hwloc_bitmap_weight(current_mask) >= 0, "Infinivitely filled core type mask");
218 } else {
219 core_types_parsing_broken = true;
220 break;
221 }
222 }
223 }
224 #else /*!__TBBBIND_HWLOC_HYBRID_CPUS_INTERFACES_PRESENT*/
225 bool core_types_parsing_broken{true};
226 #endif /*__TBBBIND_HWLOC_HYBRID_CPUS_INTERFACES_PRESENT*/
227
228 if (core_types_parsing_broken) {
229 for (auto& core_type_mask : core_types_affinity_masks_list) {
230 hwloc_bitmap_free(core_type_mask);
231 }
232 core_types_affinity_masks_list.resize(1);
233 core_types_indexes_list.resize(1);
234
235 core_types_affinity_masks_list[0] = hwloc_bitmap_dup(process_cpu_affinity_mask);
236 core_types_indexes_list[0] = -1;
237 }
238 }
239
240 #if __TBBBIND_HWLOC_WINDOWS_API_AVAILABLE
241 void processor_groups_topology_parsing() {
242 __TBB_ASSERT(number_of_processors_groups > 1, nullptr);
243 processor_groups_affinity_masks_list.resize(number_of_processors_groups);
244 for (unsigned group = 0; group < number_of_processors_groups; ++group) {
245 processor_groups_affinity_masks_list[group] = hwloc_bitmap_alloc();
246 assertion_hwloc_wrapper(hwloc_windows_get_processor_group_cpuset, topology, group,
247 processor_groups_affinity_masks_list[group], /*flags*/0);
248 }
249
250 #if TBB_USE_ASSERT
251 affinity_mask tmp = hwloc_bitmap_alloc();
252 for (auto proc_group_mask : processor_groups_affinity_masks_list) {
253 __TBB_ASSERT(!hwloc_bitmap_intersects(tmp, proc_group_mask), "Masks of processor groups intersect.");
254 hwloc_bitmap_or(tmp, tmp, proc_group_mask);
255 }
256 hwloc_bitmap_free(tmp);
257 #endif
258 }
259 #endif
260
261 void enforce_hwloc_2_5_runtime_linkage() {
262 // Without the call of this function HWLOC 2.4 can be successfully loaded during the tbbbind_2_5 loading.
263 // It is possible since tbbbind_2_5 don't use any new entry points that were introduced in HWLOC 2.5
264 // But tbbbind_2_5 compiles with HWLOC 2.5 header, therefore such situation requires binary forward compatibility
265 // which are not guaranteed by the HWLOC library. To enforce linkage tbbbind_2_5 only with HWLOC >= 2.5 version
266 // this function calls the interface that is available in the HWLOC 2.5 only.
267 #if HWLOC_API_VERSION >= 0x20500
268 auto some_core = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_CORE, nullptr);
269 hwloc_get_obj_with_same_locality(topology, some_core, HWLOC_OBJ_CORE, nullptr, nullptr, 0);
270 #endif
271 }
272
273
274 void initialize( std::size_t groups_num ) {
275 if ( initialization_state != uninitialized )
276 return;
277
278 topology_initialization(groups_num);
279 numa_topology_parsing();
280 core_types_topology_parsing();
281 #if __TBBBIND_HWLOC_WINDOWS_API_AVAILABLE
282 if (intergroup_binding_allowed(groups_num)) {
283 processor_groups_topology_parsing();
284 }
285 #endif
286
287 enforce_hwloc_2_5_runtime_linkage();
288
289 if (initialization_state == topology_loaded)
290 initialization_state = topology_parsed;
291 }
292
293 static system_topology* instance_ptr;
294 public:
295 typedef hwloc_cpuset_t affinity_mask;
296 typedef hwloc_const_cpuset_t const_affinity_mask;
297
298 bool is_topology_parsed() { return initialization_state == topology_parsed; }
299
300 static void construct( std::size_t groups_num ) {
301 if (instance_ptr == nullptr) {
302 instance_ptr = new system_topology();
303 instance_ptr->initialize(groups_num);
304 }
305 }
306
307 static system_topology& instance() {
308 __TBB_ASSERT(instance_ptr != nullptr, "Getting instance of non-constructed topology");
309 return *instance_ptr;
310 }
311
312 static void destroy() {
313 __TBB_ASSERT(instance_ptr != nullptr, "Destroying non-constructed topology");
314 delete instance_ptr;
315 }
316
317 ~system_topology() {
318 if ( is_topology_parsed() ) {
319 for (auto& numa_node_mask : numa_affinity_masks_list) {
320 hwloc_bitmap_free(numa_node_mask);
321 }
322
323 for (auto& core_type_mask : core_types_affinity_masks_list) {
324 hwloc_bitmap_free(core_type_mask);
325 }
326
327 for (auto& processor_group : processor_groups_affinity_masks_list) {
328 hwloc_bitmap_free(processor_group);
329 }
330
331 hwloc_bitmap_free(process_node_affinity_mask);
332 hwloc_bitmap_free(process_cpu_affinity_mask);
333 }
334
335 if ( initialization_state >= topology_allocated ) {
336 hwloc_topology_destroy(topology);
337 }
338
339 initialization_state = uninitialized;
340 }
341
342 void fill_topology_information(
343 int& _numa_nodes_count, int*& _numa_indexes_list,
344 int& _core_types_count, int*& _core_types_indexes_list
345 ) {
346 __TBB_ASSERT(is_topology_parsed(), "Trying to get access to uninitialized system_topology");
347 _numa_nodes_count = numa_nodes_count;
348 _numa_indexes_list = numa_indexes_list.data();
349
350 _core_types_count = (int)core_types_indexes_list.size();
351 _core_types_indexes_list = core_types_indexes_list.data();
352 }
353
354 void fill_constraints_affinity_mask(affinity_mask input_mask, int numa_node_index, int core_type_index, int max_threads_per_core) {
355 __TBB_ASSERT(is_topology_parsed(), "Trying to get access to uninitialized system_topology");
356 __TBB_ASSERT(numa_node_index < (int)numa_affinity_masks_list.size(), "Wrong NUMA node id");
357 __TBB_ASSERT(core_type_index < (int)core_types_affinity_masks_list.size(), "Wrong core type id");
358 __TBB_ASSERT(max_threads_per_core == -1 || max_threads_per_core > 0, "Wrong max_threads_per_core");
359
360 hwloc_cpuset_t constraints_mask = hwloc_bitmap_alloc();
361 hwloc_cpuset_t core_mask = hwloc_bitmap_alloc();
362
363 hwloc_bitmap_copy(constraints_mask, process_cpu_affinity_mask);
364 if (numa_node_index >= 0) {
365 hwloc_bitmap_and(constraints_mask, constraints_mask, numa_affinity_masks_list[numa_node_index]);
366 }
367 if (core_type_index >= 0) {
368 hwloc_bitmap_and(constraints_mask, constraints_mask, core_types_affinity_masks_list[core_type_index]);
369 }
370 if (max_threads_per_core > 0) {
371 // clear input mask
372 hwloc_bitmap_zero(input_mask);
373
374 hwloc_obj_t current_core = nullptr;
375 while ((current_core = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_CORE, current_core)) != nullptr) {
376 hwloc_bitmap_and(core_mask, constraints_mask, current_core->cpuset);
377
378 // fit the core mask to required bits number
379 int current_threads_per_core = 0;
380 for (int id = hwloc_bitmap_first(core_mask); id != -1; id = hwloc_bitmap_next(core_mask, id)) {
381 if (++current_threads_per_core > max_threads_per_core) {
382 hwloc_bitmap_clr(core_mask, id);
383 }
384 }
385
386 hwloc_bitmap_or(input_mask, input_mask, core_mask);
387 }
388 } else {
389 hwloc_bitmap_copy(input_mask, constraints_mask);
390 }
391
392 hwloc_bitmap_free(core_mask);
393 hwloc_bitmap_free(constraints_mask);
394 }
395
396 void fit_num_threads_per_core(affinity_mask result_mask, affinity_mask current_mask, affinity_mask constraints_mask) {
397 hwloc_bitmap_zero(result_mask);
398 hwloc_obj_t current_core = nullptr;
399 while ((current_core = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_CORE, current_core)) != nullptr) {
400 if (hwloc_bitmap_intersects(current_mask, current_core->cpuset)) {
401 hwloc_bitmap_or(result_mask, result_mask, current_core->cpuset);
402 }
403 }
404 hwloc_bitmap_and(result_mask, result_mask, constraints_mask);
405 }
406
407 /**
408 * Finds processor group for the passed slot number, which are from 0 to max concurrency - 1, by
409 * traversing masks of processor groups one by one, intersecting them with the constrained mask.
410 * Once total weight of processor groups united mask is greater than the slot number, the mask
411 * of the last traversed processor group is returned, denoting the mask to apply to the thread
412 * occupying given slot number.
413 */
414 void fit_to_processor_group(affinity_mask result_mask, affinity_mask constraints_mask, std::size_t slot_num) {
415 __TBB_ASSERT(number_of_processors_groups > 1, nullptr);
416 hwloc_bitmap_zero(result_mask);
417 int constraints_mask_weight = hwloc_bitmap_weight(constraints_mask);
418 // Map slot number to a number within constraints mask if
419 // max concurrency is greater than weight of the mask.
420 slot_num %= constraints_mask_weight;
421 std::size_t total_weight = 0;
422 for (auto& processor_group : processor_groups_affinity_masks_list) {
423 if (hwloc_bitmap_intersects(constraints_mask, processor_group)) {
424 hwloc_bitmap_and(result_mask, processor_group, constraints_mask);
425 total_weight += hwloc_bitmap_weight(result_mask);
426 if (slot_num < total_weight) {
427 return; // Corresponding processor group where to bind the thread is found
428 }
429 }
430 }
431 }
432
433 int get_default_concurrency(int numa_node_index, int core_type_index, int max_threads_per_core) {
434 __TBB_ASSERT(is_topology_parsed(), "Trying to get access to uninitialized system_topology");
435
436 hwloc_cpuset_t constraints_mask = hwloc_bitmap_alloc();
437 fill_constraints_affinity_mask(constraints_mask, numa_node_index, core_type_index, max_threads_per_core);
438
439 int default_concurrency = hwloc_bitmap_weight(constraints_mask);
440 hwloc_bitmap_free(constraints_mask);
441 return default_concurrency;
442 }
443
444 affinity_mask allocate_process_affinity_mask() {
445 __TBB_ASSERT(is_topology_parsed(), "Trying to get access to uninitialized system_topology");
446 return hwloc_bitmap_dup(process_cpu_affinity_mask);
447 }
448
449 void free_affinity_mask( affinity_mask mask_to_free ) {
450 hwloc_bitmap_free(mask_to_free); // If bitmap is nullptr, no operation is performed.
451 }
452
453 void store_current_affinity_mask( affinity_mask current_mask ) {
454 assertion_hwloc_wrapper(hwloc_get_cpubind, topology, current_mask, HWLOC_CPUBIND_THREAD);
455
456 hwloc_bitmap_and(current_mask, current_mask, process_cpu_affinity_mask);
457 __TBB_ASSERT(!hwloc_bitmap_iszero(current_mask),
458 "Current affinity mask must intersects with process affinity mask");
459 }
460
461 void set_affinity_mask( const_affinity_mask mask ) {
462 if (hwloc_bitmap_weight(mask) > 0) {
463 assertion_hwloc_wrapper(hwloc_set_cpubind, topology, mask, HWLOC_CPUBIND_THREAD);
464 }
465 }
466 };
467
468 system_topology* system_topology::instance_ptr{nullptr};
469
470 class binding_handler {
471 // Following vector saves thread affinity mask on scheduler entry to return it to this thread
472 // on scheduler exit.
473 typedef std::vector<system_topology::affinity_mask> affinity_masks_container;
474 affinity_masks_container affinity_backup;
475 system_topology::affinity_mask handler_affinity_mask;
476
477 #ifdef _WIN32
478 affinity_masks_container affinity_buffer;
479 int my_numa_node_id;
480 int my_core_type_id;
481 int my_max_threads_per_core;
482 #endif
483
484 public:
485 binding_handler( std::size_t size, int numa_node_id, int core_type_id, int max_threads_per_core )
486 : affinity_backup(size)
487 #ifdef _WIN32
488 , affinity_buffer(size)
489 , my_numa_node_id(numa_node_id)
490 , my_core_type_id(core_type_id)
491 , my_max_threads_per_core(max_threads_per_core)
492 #endif
493 {
494 for (std::size_t i = 0; i < size; ++i) {
495 affinity_backup[i] = system_topology::instance().allocate_process_affinity_mask();
496 #ifdef _WIN32
497 affinity_buffer[i] = system_topology::instance().allocate_process_affinity_mask();
498 #endif
499 }
500 handler_affinity_mask = system_topology::instance().allocate_process_affinity_mask();
501 system_topology::instance().fill_constraints_affinity_mask
502 (handler_affinity_mask, numa_node_id, core_type_id, max_threads_per_core);
503 }
504
505 ~binding_handler() {
506 for (std::size_t i = 0; i < affinity_backup.size(); ++i) {
507 system_topology::instance().free_affinity_mask(affinity_backup[i]);
508 #ifdef _WIN32
509 system_topology::instance().free_affinity_mask(affinity_buffer[i]);
510 #endif
511 }
512 system_topology::instance().free_affinity_mask(handler_affinity_mask);
513 }
514
515 void apply_affinity( unsigned slot_num ) {
516 auto& topology = system_topology::instance();
517 __TBB_ASSERT(slot_num < affinity_backup.size(),
518 "The slot number is greater than the number of slots in the arena");
519 __TBB_ASSERT(topology.is_topology_parsed(),
520 "Trying to get access to uninitialized system_topology");
521
522 topology.store_current_affinity_mask(affinity_backup[slot_num]);
523 system_topology::affinity_mask thread_affinity = handler_affinity_mask;
524 #ifdef _WIN32
525 // If we have a constraint based only on the max_threads_per_core setting, then the
526 // constraints affinity mask may cross the border between several processor groups
527 // on systems with more then 64 logical processors. That is why we need to use the special
528 // function, which regulates the number of threads in the current threads mask.
529 bool is_default_numa = my_numa_node_id == -1 || topology.numa_indexes_list.size() == 1;
530 bool is_default_core_type = my_core_type_id == -1 || topology.core_types_indexes_list.size() == 1;
531 if (topology.number_of_processors_groups > 1 && my_max_threads_per_core != -1 &&
532 is_default_numa && is_default_core_type
533 ) {
534 topology.fit_num_threads_per_core(affinity_buffer[slot_num], affinity_backup[slot_num], handler_affinity_mask);
535 thread_affinity = affinity_buffer[slot_num];
536 }
537 #if __TBBBIND_HWLOC_WINDOWS_API_AVAILABLE
538 else if (topology.number_of_processors_groups > 1) {
539 topology.fit_to_processor_group(affinity_buffer[slot_num], handler_affinity_mask, slot_num);
540 thread_affinity = affinity_buffer[slot_num];
541 }
542 #endif
543 #endif
544 topology.set_affinity_mask(thread_affinity);
545 }
546
547 void restore_previous_affinity_mask( unsigned slot_num ) {
548 auto& topology = system_topology::instance();
549 __TBB_ASSERT(topology.is_topology_parsed(),
550 "Trying to get access to uninitialized system_topology");
551 topology.set_affinity_mask(affinity_backup[slot_num]);
552 };
553
554 };
555
556 extern "C" { // exported to TBB interfaces
557
558 TBBBIND_EXPORT void __TBB_internal_initialize_system_topology(
559 std::size_t groups_num,
560 int& numa_nodes_count, int*& numa_indexes_list,
561 int& core_types_count, int*& core_types_indexes_list
562 ) {
563 system_topology::construct(groups_num);
564 system_topology::instance().fill_topology_information(
565 numa_nodes_count, numa_indexes_list,
566 core_types_count, core_types_indexes_list
567 );
568 }
569
570 TBBBIND_EXPORT binding_handler* __TBB_internal_allocate_binding_handler(int number_of_slots, int numa_id, int core_type_id, int max_threads_per_core) {
571 __TBB_ASSERT(number_of_slots > 0, "Trying to create numa handler for 0 threads.");
572 return new binding_handler(number_of_slots, numa_id, core_type_id, max_threads_per_core);
573 }
574
575 TBBBIND_EXPORT void __TBB_internal_deallocate_binding_handler(binding_handler* handler_ptr) {
576 __TBB_ASSERT(handler_ptr != nullptr, "Trying to deallocate nullptr pointer.");
577 delete handler_ptr;
578 }
579
580 TBBBIND_EXPORT void __TBB_internal_apply_affinity(binding_handler* handler_ptr, int slot_num) {
581 __TBB_ASSERT(handler_ptr != nullptr, "Trying to get access to uninitialized metadata.");
582 handler_ptr->apply_affinity(slot_num);
583 }
584
585 TBBBIND_EXPORT void __TBB_internal_restore_affinity(binding_handler* handler_ptr, int slot_num) {
586 __TBB_ASSERT(handler_ptr != nullptr, "Trying to get access to uninitialized metadata.");
587 handler_ptr->restore_previous_affinity_mask(slot_num);
588 }
589
590 TBBBIND_EXPORT int __TBB_internal_get_default_concurrency(int numa_id, int core_type_id, int max_threads_per_core) {
591 return system_topology::instance().get_default_concurrency(numa_id, core_type_id, max_threads_per_core);
592 }
593
594 TBBBIND_EXPORT void __TBB_internal_destroy_system_topology() {
595 return system_topology::destroy();
596 }
597
598 } // extern "C"
599
600 } // namespace r1
601 } // namespace detail
602 } // namespace tbb
603
604 #undef assertion_hwloc_wrapper