[Openmp-commits] [PATCH] D119586: [OpenMP][libomp] Fix crash when LIBOMP_NUM_HIDDEN_HELPER_THREADS=1.

Andrey Churbanov via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Fri Feb 11 13:22:45 PST 2022


AndreyChurbanov created this revision.
AndreyChurbanov added reviewers: hbae, jlpeyton, tianshilei1992.
AndreyChurbanov added a project: OpenMP.
Herald added subscribers: guansong, yaxunl.
AndreyChurbanov requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: openmp-commits, sstefan1.

It looks logical to treat user request for number of helper threads as
the number of threads doing actual job, excluding main thread of hidden helper team
which does not participate in tasks execution.

The increment also fixes the division by 0 caused by % operation:
#define KMP_GTID_TO_SHADOW_GTID(gtid) ((gtid) % (__kmp_hidden_helper_threads_num - 1) + 2)
when __kmp_hidden_helper_threads_num == 1.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119586

Files:
  openmp/runtime/src/kmp_settings.cpp
  openmp/runtime/test/tasking/hidden_helper_task/single_helper_thread.c


Index: openmp/runtime/test/tasking/hidden_helper_task/single_helper_thread.c
===================================================================
--- /dev/null
+++ openmp/runtime/test/tasking/hidden_helper_task/single_helper_thread.c
@@ -0,0 +1,21 @@
+// RUN: %libomp-compile && env LIBOMP_NUM_HIDDEN_HELPER_THREADS=1 %libomp-run
+
+// The test checks that "devide-by-0" bug fixed in runtime.
+// The fix is to increment number of threads by 1 if positive,
+// so that operation
+//   (gtid) % (__kmp_hidden_helper_threads_num - 1)
+// does not cause crash.
+
+#include <stdio.h>
+#include <omp.h>
+
+int main(){
+#pragma omp target nowait
+   {
+      printf("----- in  target region\n");
+   }
+  printf("------ before taskwait\n");
+#pragma omp taskwait
+  printf("passed\n");
+  return 0;
+}
Index: openmp/runtime/src/kmp_settings.cpp
===================================================================
--- openmp/runtime/src/kmp_settings.cpp
+++ openmp/runtime/src/kmp_settings.cpp
@@ -1245,13 +1245,25 @@
   // task
   if (__kmp_hidden_helper_threads_num == 0) {
     __kmp_enable_hidden_helper = FALSE;
+  } else {
+    // Once the main thread of hidden helper team dooes not participate
+    // in tasks execution let's increment the number of threads by one
+    // so that requested number of threads do actual job.
+    __kmp_hidden_helper_threads_num++;
   }
 } // __kmp_stg_parse_num_hidden_helper_threads
 
 static void __kmp_stg_print_num_hidden_helper_threads(kmp_str_buf_t *buffer,
                                                       char const *name,
                                                       void *data) {
-  __kmp_stg_print_int(buffer, name, __kmp_hidden_helper_threads_num);
+  if (__kmp_hidden_helper_threads_num == 0) {
+    __kmp_stg_print_int(buffer, name, __kmp_hidden_helper_threads_num);
+  } else {
+    KMP_DEBUG_ASSERT(__kmp_hidden_helper_threads_num > 1);
+    // Let's exclude the main thread of hidden helper team and print
+    // number of worker threads those do actual job
+    __kmp_stg_print_int(buffer, name, __kmp_hidden_helper_threads_num - 1);
+  }
 } // __kmp_stg_print_num_hidden_helper_threads
 
 static void __kmp_stg_parse_use_hidden_helper(char const *name,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119586.408015.patch
Type: text/x-patch
Size: 2223 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20220211/6a63bef8/attachment.bin>


More information about the Openmp-commits mailing list