@@ -174,17 +174,18 @@ int kmp_hw_thread_t::compare_compact(const void *a, const void *b) {
174174 const kmp_hw_thread_t *aa = (const kmp_hw_thread_t *)a;
175175 const kmp_hw_thread_t *bb = (const kmp_hw_thread_t *)b;
176176 int depth = __kmp_topology->get_depth ();
177- KMP_DEBUG_ASSERT (__kmp_affinity.compact >= 0 );
178- KMP_DEBUG_ASSERT (__kmp_affinity.compact <= depth);
179- for (i = 0 ; i < __kmp_affinity.compact ; i++) {
177+ int compact = __kmp_topology->compact ;
178+ KMP_DEBUG_ASSERT (compact >= 0 );
179+ KMP_DEBUG_ASSERT (compact <= depth);
180+ for (i = 0 ; i < compact; i++) {
180181 int j = depth - i - 1 ;
181182 if (aa->sub_ids [j] < bb->sub_ids [j])
182183 return -1 ;
183184 if (aa->sub_ids [j] > bb->sub_ids [j])
184185 return 1 ;
185186 }
186187 for (; i < depth; i++) {
187- int j = i - __kmp_affinity. compact ;
188+ int j = i - compact;
188189 if (aa->sub_ids [j] < bb->sub_ids [j])
189190 return -1 ;
190191 if (aa->sub_ids [j] > bb->sub_ids [j])
@@ -583,6 +584,7 @@ kmp_topology_t *kmp_topology_t::allocate(int nproc, int ndepth,
583584 retval->count = arr + 2 * (size_t )KMP_HW_LAST;
584585 retval->num_core_efficiencies = 0 ;
585586 retval->num_core_types = 0 ;
587+ retval->compact = 0 ;
586588 for (int i = 0 ; i < KMP_HW_MAX_NUM_CORE_TYPES; ++i)
587589 retval->core_types [i] = KMP_HW_CORE_TYPE_UNKNOWN;
588590 KMP_FOREACH_HW_TYPE (type) { retval->equivalent [type] = KMP_HW_UNKNOWN; }
@@ -4287,6 +4289,7 @@ static bool __kmp_aux_affinity_initialize_topology(kmp_affinity_t &affinity) {
42874289
42884290static void __kmp_aux_affinity_initialize (kmp_affinity_t &affinity) {
42894291 bool is_regular_affinity = (&affinity == &__kmp_affinity);
4292+ bool is_hidden_helper_affinity = (&affinity == &__kmp_hh_affinity);
42904293 const char *env_var = affinity.env_var ;
42914294
42924295 if (affinity.flags .initialized ) {
@@ -4335,7 +4338,8 @@ static void __kmp_aux_affinity_initialize(kmp_affinity_t &affinity) {
43354338
43364339 case affinity_explicit:
43374340 KMP_DEBUG_ASSERT (affinity.proclist != NULL );
4338- if (__kmp_nested_proc_bind.bind_types [0 ] == proc_bind_intel) {
4341+ if (is_hidden_helper_affinity ||
4342+ __kmp_nested_proc_bind.bind_types [0 ] == proc_bind_intel) {
43394343 __kmp_affinity_process_proclist (affinity);
43404344 } else {
43414345 __kmp_affinity_process_placelist (affinity);
@@ -4391,7 +4395,7 @@ static void __kmp_aux_affinity_initialize(kmp_affinity_t &affinity) {
43914395 goto sortTopology;
43924396
43934397 case affinity_balanced:
4394- if (depth <= 1 ) {
4398+ if (depth <= 1 || is_hidden_helper_affinity ) {
43954399 KMP_AFF_WARNING (affinity, AffBalancedNotAvail, env_var);
43964400 affinity.type = affinity_none;
43974401 __kmp_create_affinity_none_places (affinity);
@@ -4451,15 +4455,16 @@ static void __kmp_aux_affinity_initialize(kmp_affinity_t &affinity) {
44514455
44524456 if ((__kmp_nested_proc_bind.bind_types [0 ] != proc_bind_intel) &&
44534457 (__kmp_affinity_num_places > 0 ) &&
4454- ((unsigned )__kmp_affinity_num_places < affinity.num_masks )) {
4458+ ((unsigned )__kmp_affinity_num_places < affinity.num_masks ) &&
4459+ !is_hidden_helper_affinity) {
44554460 affinity.num_masks = __kmp_affinity_num_places;
44564461 }
44574462
44584463 KMP_CPU_ALLOC_ARRAY (affinity.masks , affinity.num_masks );
44594464
44604465 // Sort the topology table according to the current setting of
44614466 // affinity.compact, then fill out affinity.masks.
4462- __kmp_topology->sort_compact ();
4467+ __kmp_topology->sort_compact (affinity );
44634468 {
44644469 int i;
44654470 unsigned j;
@@ -4510,8 +4515,7 @@ void __kmp_affinity_initialize(kmp_affinity_t &affinity) {
45104515}
45114516
45124517void __kmp_affinity_uninitialize (void ) {
4513- {
4514- kmp_affinity_t *affinity = &__kmp_affinity;
4518+ for (kmp_affinity_t *affinity : __kmp_affinities) {
45154519 if (affinity->masks != NULL )
45164520 KMP_CPU_FREE_ARRAY (affinity->masks , affinity->num_masks );
45174521 if (affinity->os_id_masks != NULL )
@@ -4546,6 +4550,21 @@ void __kmp_affinity_uninitialize(void) {
45464550 KMPAffinity::destroy_api ();
45474551}
45484552
4553+ static void __kmp_select_mask_by_gtid (int gtid, const kmp_affinity_t *affinity,
4554+ int *place, kmp_affin_mask_t **mask) {
4555+ int mask_idx;
4556+ bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD (gtid);
4557+ if (is_hidden_helper)
4558+ // The first gtid is the regular primary thread, the second gtid is the main
4559+ // thread of hidden team which does not participate in task execution.
4560+ mask_idx = gtid - 2 ;
4561+ else
4562+ mask_idx = __kmp_adjust_gtid_for_hidden_helpers (gtid);
4563+ KMP_DEBUG_ASSERT (affinity->num_masks > 0 );
4564+ *place = (mask_idx + affinity->offset ) % affinity->num_masks ;
4565+ *mask = KMP_CPU_INDEX (affinity->masks , *place);
4566+ }
4567+
45494568void __kmp_affinity_set_init_mask (int gtid, int isa_root) {
45504569 if (!KMP_AFFINITY_CAPABLE ()) {
45514570 return ;
@@ -4565,13 +4584,20 @@ void __kmp_affinity_set_init_mask(int gtid, int isa_root) {
45654584 // same as the mask of the initialization thread.
45664585 kmp_affin_mask_t *mask;
45674586 int i;
4568- const kmp_affinity_t *affinity = &__kmp_affinity;
4569- const char *env_var = affinity->env_var ;
4587+ const kmp_affinity_t *affinity;
4588+ const char *env_var;
4589+ bool is_hidden_helper = KMP_HIDDEN_HELPER_THREAD (gtid);
45704590
4571- if (KMP_AFFINITY_NON_PROC_BIND) {
4591+ if (is_hidden_helper)
4592+ affinity = &__kmp_hh_affinity;
4593+ else
4594+ affinity = &__kmp_affinity;
4595+ env_var = affinity->env_var ;
4596+
4597+ if (KMP_AFFINITY_NON_PROC_BIND || is_hidden_helper) {
45724598 if ((affinity->type == affinity_none) ||
45734599 (affinity->type == affinity_balanced) ||
4574- KMP_HIDDEN_HELPER_THREAD (gtid)) {
4600+ KMP_HIDDEN_HELPER_MAIN_THREAD (gtid)) {
45754601#if KMP_GROUP_AFFINITY
45764602 if (__kmp_num_proc_groups > 1 ) {
45774603 return ;
@@ -4581,14 +4607,10 @@ void __kmp_affinity_set_init_mask(int gtid, int isa_root) {
45814607 i = 0 ;
45824608 mask = __kmp_affin_fullMask;
45834609 } else {
4584- int mask_idx = __kmp_adjust_gtid_for_hidden_helpers (gtid);
4585- KMP_DEBUG_ASSERT (affinity->num_masks > 0 );
4586- i = (mask_idx + affinity->offset ) % affinity->num_masks ;
4587- mask = KMP_CPU_INDEX (affinity->masks , i);
4610+ __kmp_select_mask_by_gtid (gtid, affinity, &i, &mask);
45884611 }
45894612 } else {
4590- if ((!isa_root) || KMP_HIDDEN_HELPER_THREAD (gtid) ||
4591- (__kmp_nested_proc_bind.bind_types [0 ] == proc_bind_false)) {
4613+ if (!isa_root || __kmp_nested_proc_bind.bind_types [0 ] == proc_bind_false) {
45924614#if KMP_GROUP_AFFINITY
45934615 if (__kmp_num_proc_groups > 1 ) {
45944616 return ;
@@ -4598,17 +4620,12 @@ void __kmp_affinity_set_init_mask(int gtid, int isa_root) {
45984620 i = KMP_PLACE_ALL;
45994621 mask = __kmp_affin_fullMask;
46004622 } else {
4601- // int i = some hash function or just a counter that doesn't
4602- // always start at 0. Use adjusted gtid for now.
4603- int mask_idx = __kmp_adjust_gtid_for_hidden_helpers (gtid);
4604- KMP_DEBUG_ASSERT (affinity->num_masks > 0 );
4605- i = (mask_idx + affinity->offset ) % affinity->num_masks ;
4606- mask = KMP_CPU_INDEX (affinity->masks , i);
4623+ __kmp_select_mask_by_gtid (gtid, affinity, &i, &mask);
46074624 }
46084625 }
46094626
46104627 th->th .th_current_place = i;
4611- if (isa_root || KMP_HIDDEN_HELPER_THREAD (gtid) ) {
4628+ if (isa_root && !is_hidden_helper ) {
46124629 th->th .th_new_place = i;
46134630 th->th .th_first_place = 0 ;
46144631 th->th .th_last_place = affinity->num_masks - 1 ;
@@ -4629,28 +4646,18 @@ void __kmp_affinity_set_init_mask(int gtid, int isa_root) {
46294646
46304647 KMP_CPU_COPY (th->th .th_affin_mask , mask);
46314648
4632- if (affinity->flags .verbose && !KMP_HIDDEN_HELPER_THREAD (gtid)
4633- /* to avoid duplicate printing (will be correctly printed on barrier) */
4634- && (affinity->type == affinity_none ||
4635- (i != KMP_PLACE_ALL && affinity->type != affinity_balanced))) {
4649+ /* to avoid duplicate printing (will be correctly printed on barrier) */
4650+ if (affinity->flags .verbose &&
4651+ (affinity->type == affinity_none ||
4652+ (i != KMP_PLACE_ALL && affinity->type != affinity_balanced)) &&
4653+ !KMP_HIDDEN_HELPER_MAIN_THREAD (gtid)) {
46364654 char buf[KMP_AFFIN_MASK_PRINT_LEN];
46374655 __kmp_affinity_print_mask (buf, KMP_AFFIN_MASK_PRINT_LEN,
46384656 th->th .th_affin_mask );
46394657 KMP_INFORM (BoundToOSProcSet, env_var, (kmp_int32)getpid (), __kmp_gettid (),
46404658 gtid, buf);
46414659 }
46424660
4643- #if KMP_DEBUG
4644- // Hidden helper thread affinity only printed for debug builds
4645- if (affinity->flags .verbose && KMP_HIDDEN_HELPER_THREAD (gtid)) {
4646- char buf[KMP_AFFIN_MASK_PRINT_LEN];
4647- __kmp_affinity_print_mask (buf, KMP_AFFIN_MASK_PRINT_LEN,
4648- th->th .th_affin_mask );
4649- KMP_INFORM (BoundToOSProcSet, " KMP_AFFINITY (hidden helper thread)" ,
4650- (kmp_int32)getpid (), __kmp_gettid (), gtid, buf);
4651- }
4652- #endif
4653-
46544661#if KMP_OS_WINDOWS
46554662 // On Windows* OS, the process affinity mask might have changed. If the user
46564663 // didn't request affinity and this call fails, just continue silently.
@@ -4663,7 +4670,8 @@ void __kmp_affinity_set_init_mask(int gtid, int isa_root) {
46634670}
46644671
46654672void __kmp_affinity_set_place (int gtid) {
4666- if (!KMP_AFFINITY_CAPABLE ()) {
4673+ // Hidden helper threads should not be affected by OMP_PLACES/OMP_PROC_BIND
4674+ if (!KMP_AFFINITY_CAPABLE () || KMP_HIDDEN_HELPER_THREAD (gtid)) {
46674675 return ;
46684676 }
46694677
0 commit comments