[OpenMP] Fix affinity API for KMP_AFFINITY=none|compact|scatter

Currently, the affinity API reports garbage for the initial place list and any
thread's place lists when using KMP_AFFINITY=none|compact|scatter.
This patch does two things:

for KMP_AFFINITY=none, Creates a one entry table for the places, this way, the
initial place list is just a single place with all the proc ids in it. We also
set the initial place of any thread to 0 instead of KMP_PLACE_ALL so that the
thread reports that single place (place 0) instead of garbage (-1) when using
the affinity API.

When non-OMP_PROC_BIND affinity is used
(including KMP_AFFINITY=compact|scatter), a thread's place list is populated
correctly. We assume that each thread is assigned to a single place. This is
implemented in two of the affinity API functions

Differential Revision: https://reviews.llvm.org/D45527


git-svn-id: https://llvm.org/svn/llvm-project/openmp/trunk@330283 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/runtime/src/kmp.h b/runtime/src/kmp.h
index 27c2a53..94485a1 100644
--- a/runtime/src/kmp.h
+++ b/runtime/src/kmp.h
@@ -781,6 +781,11 @@
 #if KMP_AFFINITY_SUPPORTED
 #define KMP_PLACE_ALL (-1)
 #define KMP_PLACE_UNDEFINED (-2)
+// Is KMP_AFFINITY is being used instead of OMP_PROC_BIND/OMP_PLACES?
+#define KMP_AFFINITY_NON_PROC_BIND                                             \
+  ((__kmp_nested_proc_bind.bind_types[0] == proc_bind_false ||                 \
+    __kmp_nested_proc_bind.bind_types[0] == proc_bind_intel) &&                \
+   (__kmp_affinity_num_masks > 0 || __kmp_affinity_type == affinity_balanced))
 #endif /* KMP_AFFINITY_SUPPORTED */
 
 extern int __kmp_affinity_num_places;
diff --git a/runtime/src/kmp_affinity.cpp b/runtime/src/kmp_affinity.cpp
index 9cc504b..b7da8d4 100644
--- a/runtime/src/kmp_affinity.cpp
+++ b/runtime/src/kmp_affinity.cpp
@@ -3957,8 +3957,20 @@
   KMP_ASSERT(__kmp_affinity_type == affinity_none);                            \
   KMP_ASSERT(address2os == NULL);                                              \
   __kmp_apply_thread_places(NULL, 0);                                          \
+  __kmp_create_affinity_none_places();                                         \
   return;
 
+// Create a one element mask array (set of places) which only contains the
+// initial process's affinity mask
+static void __kmp_create_affinity_none_places() {
+  KMP_ASSERT(__kmp_affin_fullMask != NULL);
+  KMP_ASSERT(__kmp_affinity_type == affinity_none);
+  __kmp_affinity_num_masks = 1;
+  KMP_CPU_ALLOC_ARRAY(__kmp_affinity_masks, __kmp_affinity_num_masks);
+  kmp_affin_mask_t *dest = KMP_CPU_INDEX(__kmp_affinity_masks, 0);
+  KMP_CPU_COPY(dest, __kmp_affin_fullMask);
+}
+
 static int __kmp_affinity_cmp_Address_child_num(const void *a, const void *b) {
   const Address *aa = &(((const AddrUnsPair *)a)->first);
   const Address *bb = &(((const AddrUnsPair *)b)->first);
@@ -4295,6 +4307,7 @@
       KMP_WARNING(ErrorInitializeAffinity);
     }
     __kmp_affinity_type = affinity_none;
+    __kmp_create_affinity_none_places();
     KMP_AFFINITY_DISABLE();
     return;
   }
@@ -4578,7 +4591,7 @@
   int i;
 
 #if OMP_40_ENABLED
-  if (__kmp_nested_proc_bind.bind_types[0] == proc_bind_intel)
+  if (KMP_AFFINITY_NON_PROC_BIND)
 #endif
   {
     if ((__kmp_affinity_type == affinity_none) ||
@@ -4589,7 +4602,7 @@
       }
 #endif
       KMP_ASSERT(__kmp_affin_fullMask != NULL);
-      i = KMP_PLACE_ALL;
+      i = 0;
       mask = __kmp_affin_fullMask;
     } else {
       KMP_DEBUG_ASSERT(__kmp_affinity_num_masks > 0);
diff --git a/runtime/src/kmp_ftn_entry.h b/runtime/src/kmp_ftn_entry.h
index f98ee9d..0f09828 100644
--- a/runtime/src/kmp_ftn_entry.h
+++ b/runtime/src/kmp_ftn_entry.h
@@ -691,6 +691,9 @@
   }
   if (!KMP_AFFINITY_CAPABLE())
     return 0;
+  if (KMP_AFFINITY_NON_PROC_BIND) {
+    return 1;
+  }
   gtid = __kmp_entry_gtid();
   thread = __kmp_thread_from_gtid(gtid);
   first_place = thread->th.th_first_place;
@@ -718,6 +721,10 @@
     return;
   gtid = __kmp_entry_gtid();
   thread = __kmp_thread_from_gtid(gtid);
+  if (KMP_AFFINITY_NON_PROC_BIND) {
+    place_nums[0] = thread->th.th_current_place;
+    return;
+  }
   first_place = thread->th.th_first_place;
   last_place = thread->th.th_last_place;
   if (first_place < 0 || last_place < 0)
diff --git a/runtime/src/kmp_settings.cpp b/runtime/src/kmp_settings.cpp
index 6fb772b..d2502d0 100644
--- a/runtime/src/kmp_settings.cpp
+++ b/runtime/src/kmp_settings.cpp
@@ -5407,6 +5407,8 @@
     KMP_DEBUG_ASSERT(__kmp_affinity_type != affinity_default);
 #if OMP_40_ENABLED
     KMP_DEBUG_ASSERT(__kmp_nested_proc_bind.bind_types[0] != proc_bind_default);
+    K_DIAG(1, ("__kmp_nested_proc_bind.bind_types[0] == %d\n",
+               __kmp_nested_proc_bind.bind_types[0]));
 #endif
   }