Skip to content

Commit 9355d0d

Browse files
committedJan 15, 2019
[OpenMP] Fix for nested proc_bind affinity bug
Using proc_bind clause on a nested #pragma omp parallel region with KMP_AFFINITY set causes an assertion error. This assertion occurs because the place-partition-var is not properly initialized in the nested master threads. Trying to get an intuitive result with KMP_AFFINITY + proc_bind is difficult because of how the KMP_AFFINITY gtid-to-place mapping occurs. This patch creates an initial place list no matter what affinity mechanism is used. For KMP_AFFINITY, the place-partition-var is initialized to all the places. Differential Revision: https://reviews.llvm.org/D55795 llvm-svn: 351227
1 parent 45b511f commit 9355d0d

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed
 

‎openmp/runtime/src/kmp_affinity.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -4505,6 +4505,7 @@ static void __kmp_aux_affinity_initialize(void) {
45054505
KMP_WARNING(AffNoValidProcID);
45064506
}
45074507
__kmp_affinity_type = affinity_none;
4508+
__kmp_create_affinity_none_places();
45084509
return;
45094510
}
45104511
break;
@@ -4557,11 +4558,9 @@ static void __kmp_aux_affinity_initialize(void) {
45574558
KMP_WARNING(AffBalancedNotAvail, "KMP_AFFINITY");
45584559
}
45594560
__kmp_affinity_type = affinity_none;
4561+
__kmp_create_affinity_none_places();
45604562
return;
4561-
} else if (__kmp_affinity_uniform_topology()) {
4562-
break;
4563-
} else { // Non-uniform topology
4564-
4563+
} else if (!__kmp_affinity_uniform_topology()) {
45654564
// Save the depth for further usage
45664565
__kmp_aff_depth = depth;
45674566

@@ -4602,8 +4601,9 @@ static void __kmp_aux_affinity_initialize(void) {
46024601

46034602
procarr[core * maxprocpercore + inlastcore] = proc;
46044603
}
4605-
4606-
break;
4604+
}
4605+
if (__kmp_affinity_compact >= depth) {
4606+
__kmp_affinity_compact = depth - 1;
46074607
}
46084608

46094609
sortAddresses:
@@ -4781,6 +4781,11 @@ void __kmp_affinity_set_init_mask(int gtid, int isa_root) {
47814781
th->th.th_new_place = i;
47824782
th->th.th_first_place = 0;
47834783
th->th.th_last_place = __kmp_affinity_num_masks - 1;
4784+
} else if (KMP_AFFINITY_NON_PROC_BIND) {
4785+
// When using a Non-OMP_PROC_BIND affinity method,
4786+
// set all threads' place-partition-var to the entire place list
4787+
th->th.th_first_place = 0;
4788+
th->th.th_last_place = __kmp_affinity_num_masks - 1;
47844789
}
47854790

47864791
if (i == KMP_PLACE_ALL) {

‎openmp/runtime/src/kmp_ftn_entry.h

-7
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,6 @@ int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_PARTITION_NUM_PLACES)(void) {
858858
}
859859
if (!KMP_AFFINITY_CAPABLE())
860860
return 0;
861-
if (KMP_AFFINITY_NON_PROC_BIND) {
862-
return 1;
863-
}
864861
gtid = __kmp_entry_gtid();
865862
thread = __kmp_thread_from_gtid(gtid);
866863
first_place = thread->th.th_first_place;
@@ -889,10 +886,6 @@ void
889886
return;
890887
gtid = __kmp_entry_gtid();
891888
thread = __kmp_thread_from_gtid(gtid);
892-
if (KMP_AFFINITY_NON_PROC_BIND) {
893-
place_nums[0] = thread->th.th_current_place;
894-
return;
895-
}
896889
first_place = thread->th.th_first_place;
897890
last_place = thread->th.th_last_place;
898891
if (first_place < 0 || last_place < 0)
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %libomp-compile && env KMP_AFFINITY=compact %libomp-run
2+
// REQUIRES: openmp-4.0
3+
4+
#include <stdio.h>
5+
#include <stdint.h>
6+
#include <omp.h>
7+
#include "omp_testsuite.h"
8+
9+
int test_nested_affinity_bug() {
10+
int a = 0;
11+
omp_set_nested(1);
12+
#pragma omp parallel num_threads(2) shared(a)
13+
{
14+
#pragma omp parallel num_threads(2) shared(a) proc_bind(close)
15+
{
16+
#pragma omp atomic
17+
a++;
18+
}
19+
}
20+
return 1;
21+
}
22+
23+
int main() {
24+
int i;
25+
int num_failed = 0;
26+
27+
for (i = 0; i < REPETITIONS; i++) {
28+
if (!test_nested_affinity_bug()) {
29+
num_failed++;
30+
}
31+
}
32+
return num_failed;
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.