[Openmp-commits] [openmp] 8c73be9 - [OpenMP] Limit number of dispatch buffers

via Openmp-commits openmp-commits at lists.llvm.org
Mon Feb 22 11:14:49 PST 2021


Author: Peyton, Jonathan L
Date: 2021-02-22T13:14:28-06:00
New Revision: 8c73be9d86758beae1cda30f9eba2e92023d6c60

URL: https://github.com/llvm/llvm-project/commit/8c73be9d86758beae1cda30f9eba2e92023d6c60
DIFF: https://github.com/llvm/llvm-project/commit/8c73be9d86758beae1cda30f9eba2e92023d6c60.diff

LOG: [OpenMP] Limit number of dispatch buffers

This patch limits the number of dispatch buffers (used for
loop worksharing construct) to between 1 and 4096.

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

Added: 
    openmp/runtime/test/env/kmp_dispatch_buf_range.c

Modified: 
    openmp/runtime/src/kmp.h
    openmp/runtime/src/kmp_csupport.cpp
    openmp/runtime/src/kmp_ftn_entry.h
    openmp/runtime/src/kmp_settings.cpp

Removed: 
    


################################################################################
diff  --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h
index fe554105fe11..d5b314c91468 100644
--- a/openmp/runtime/src/kmp.h
+++ b/openmp/runtime/src/kmp.h
@@ -1124,7 +1124,10 @@ extern kmp_uint64 __kmp_now_nsec();
 #define KMP_MAX_CHUNK (INT_MAX - 1)
 #define KMP_DEFAULT_CHUNK 1
 
+#define KMP_MIN_DISP_NUM_BUFF 1
 #define KMP_DFLT_DISP_NUM_BUFF 7
+#define KMP_MAX_DISP_NUM_BUFF 4096
+
 #define KMP_MAX_ORDERED 8
 
 #define KMP_MAX_FIELDS 32

diff  --git a/openmp/runtime/src/kmp_csupport.cpp b/openmp/runtime/src/kmp_csupport.cpp
index 2addb3d87d39..c5682fb6fcef 100644
--- a/openmp/runtime/src/kmp_csupport.cpp
+++ b/openmp/runtime/src/kmp_csupport.cpp
@@ -1988,8 +1988,10 @@ void kmpc_set_defaults(char const *str) {
 void kmpc_set_disp_num_buffers(int arg) {
   // ignore after initialization because some teams have already
   // allocated dispatch buffers
-  if (__kmp_init_serial == 0 && arg > 0)
+  if (__kmp_init_serial == FALSE && arg >= KMP_MIN_DISP_NUM_BUFF &&
+      arg <= KMP_MAX_DISP_NUM_BUFF) {
     __kmp_dispatch_num_buffers = arg;
+  }
 }
 
 int kmpc_set_affinity_mask_proc(int proc, void **mask) {

diff  --git a/openmp/runtime/src/kmp_ftn_entry.h b/openmp/runtime/src/kmp_ftn_entry.h
index 5c5f470c6878..9bd31d31dec3 100644
--- a/openmp/runtime/src/kmp_ftn_entry.h
+++ b/openmp/runtime/src/kmp_ftn_entry.h
@@ -202,8 +202,11 @@ void FTN_STDCALL FTN_SET_DISP_NUM_BUFFERS(int KMP_DEREF arg) {
 #else
   // ignore after initialization because some teams have already
   // allocated dispatch buffers
-  if (__kmp_init_serial == 0 && (KMP_DEREF arg) > 0)
-    __kmp_dispatch_num_buffers = KMP_DEREF arg;
+  int num_buffers = KMP_DEREF arg;
+  if (__kmp_init_serial == FALSE && num_buffers >= KMP_MIN_DISP_NUM_BUFF &&
+      num_buffers <= KMP_MAX_DISP_NUM_BUFF) {
+    __kmp_dispatch_num_buffers = num_buffers;
+  }
 #endif
 }
 

diff  --git a/openmp/runtime/src/kmp_settings.cpp b/openmp/runtime/src/kmp_settings.cpp
index 48551db9dfbf..fa63e55eac3b 100644
--- a/openmp/runtime/src/kmp_settings.cpp
+++ b/openmp/runtime/src/kmp_settings.cpp
@@ -1389,7 +1389,8 @@ static void __kmp_stg_parse_disp_buffers(char const *name, char const *value,
     KMP_WARNING(EnvSerialWarn, name);
     return;
   } // read value before serial initialization only
-  __kmp_stg_parse_int(name, value, 1, KMP_MAX_NTH, &__kmp_dispatch_num_buffers);
+  __kmp_stg_parse_int(name, value, KMP_MIN_DISP_NUM_BUFF, KMP_MAX_DISP_NUM_BUFF,
+                      &__kmp_dispatch_num_buffers);
 } // __kmp_stg_parse_disp_buffers
 
 static void __kmp_stg_print_disp_buffers(kmp_str_buf_t *buffer,

diff  --git a/openmp/runtime/test/env/kmp_dispatch_buf_range.c b/openmp/runtime/test/env/kmp_dispatch_buf_range.c
new file mode 100644
index 000000000000..ac0c48f9199f
--- /dev/null
+++ b/openmp/runtime/test/env/kmp_dispatch_buf_range.c
@@ -0,0 +1,18 @@
+// RUN: %libomp-compile
+// RUN: env KMP_DISP_NUM_BUFFERS=0 %libomp-run 2>&1 | FileCheck --check-prefix=SMALL %s
+// RUN: env KMP_DISP_NUM_BUFFERS=4097 %libomp-run 2>&1 | FileCheck --check-prefix=LARGE %s
+// SMALL: OMP: Warning
+// SMALL-SAME: KMP_DISP_NUM_BUFFERS
+// SMALL-SAME: too small
+// LARGE: OMP: Warning
+// LARGE-SAME: KMP_DISP_NUM_BUFFERS
+// LARGE-SAME: too large
+#include <stdio.h>
+#include <stdlib.h>
+
+int main() {
+  int i;
+  #pragma omp parallel for
+  for (i = 0; i < 1000; i++) {}
+  return EXIT_SUCCESS;
+}


        


More information about the Openmp-commits mailing list