[Openmp-commits] [openmp] [libomp] Set `KMP_USE_YIELD=0` when `OMP_WAIT_POLICY=active` if it's not specified explicitly (PR #199628)

Ryotaro Kasuga via Openmp-commits openmp-commits at lists.llvm.org
Tue May 26 01:36:56 PDT 2026


https://github.com/kasuga-fj created https://github.com/llvm/llvm-project/pull/199628

This patch sets `KMP_USE_YIELD=0` when `OMP_WAIT_POLICY=active` and `KMP_USE_YIELD` is not explicitly set by the user. This is the same as the value of `KMP_BLOCKTIME` is set to infinite when `KMP_BLOCKTIME` is not explicitly set and  `OMP_WAIT_POLICY=active`.

### Background

`KMP_USE_YIELD` is an environment variable for libomp to control the behavior of threads when they are idle. We can specify 0, 1, or 2 as the value of `KMP_USE_YIELD`. As I read the code, the behavior is as follows:

- `KMP_USE_YIELD=0`: Threads will not yield the CPU when they are idle.
- `KMP_USE_YIELD=1`: Threads will yield the CPU when they are idle.
- `KMP_USE_YIELD=2`: Threads will yield the CPU when they are idle if we are oversubscribed (i.e., more threads than CPU cores).

The default value of `KMP_USE_YIELD` is 1, which means that threads will yield the CPU when they are idle.

On the other hand, there is an environment variable `OMP_WAIT_POLICY` that controls the waiting behavior of threads in OpenMP. The OpenMP 6.0 Specification (https://www.openmp.org/wp-content/uploads/OpenMP-API-Specification-6-0.pdf) states as follows:

> The active value specifies that waiting native threads should mostly be active, consuming processor cycles, while waiting. A compliant implementation may, for example, make waiting native threads spin.

Therefore, I think it is more consistent to set `KMP_USE_YIELD=0` (or 2) when `OMP_WAIT_POLICY=active`.

### Performance

To confirm the effect of this patch, I ran SPEC CPU 2017 with several configurations, including with and without this patch. As a result, I observed that the performance of some benchmarks improved significantly with this patch. Also I didn't see any performance degradation. Here are the details of the performance improvements:

- Machine: Neoverse V2 (Grace, binding 64 threads to 64 cores)
- Configuration: `OMP_WAIT_POLICY=active OMP_NUM_THREADS=64`

| Benchmark | Performance Improvement |
|-|-|
| 621.wrf_s | 2.6x |
| 628.pop2_s | 5.5x |

---

In conclusion, I think it's reasonable to set `KMP_USE_YIELD=0` (or 2) by default when `OMP_WAIT_POLICY=active` is specified, when the user likely expects the threads to be active while waiting.

Resolve #181784.

>From ca696a051bb83a9bb342128809fe89d8124c428b Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Thu, 7 May 2026 05:01:35 +0000
Subject: [PATCH] [libomp] Set `KMP_USE_YIELD=0` when `OMP_WAIT_POLICY=active`

---
 openmp/runtime/src/kmp_settings.cpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/openmp/runtime/src/kmp_settings.cpp b/openmp/runtime/src/kmp_settings.cpp
index 66ef6f8097dce..2f846fcc760af 100644
--- a/openmp/runtime/src/kmp_settings.cpp
+++ b/openmp/runtime/src/kmp_settings.cpp
@@ -807,6 +807,7 @@ static void __kmp_stg_print_inherit_fp_control(kmp_str_buf_t *buffer,
 
 // Used for OMP_WAIT_POLICY
 static char const *blocktime_str = NULL;
+static char const *use_yield_str = NULL;
 
 // -----------------------------------------------------------------------------
 // KMP_LIBRARY, OMP_WAIT_POLICY
@@ -829,6 +830,10 @@ static void __kmp_stg_parse_wait_policy(char const *name, char const *value,
         // KMP_BLOCKTIME not specified, so set default to "infinite".
         __kmp_dflt_blocktime = KMP_MAX_BLOCKTIME;
       }
+      if (use_yield_str == NULL) {
+        // KMP_USE_YIELD not specified, so set default to 0.
+        __kmp_use_yield = 0;
+      }
     } else if (__kmp_str_match("PASSIVE", 1, value)) {
       __kmp_library = library_throughput;
       __kmp_wpolicy_passive = true; /* allow sleep while active tasking */
@@ -6147,8 +6152,9 @@ void __kmp_env_initialize(char const *string) {
     }
   }
 
-  // We need to know if blocktime was set when processing OMP_WAIT_POLICY
+  // We need to know if blocktime and use_yield were set when processing OMP_WAIT_POLICY
   blocktime_str = __kmp_env_blk_var(&block, "KMP_BLOCKTIME");
+  use_yield_str = __kmp_env_blk_var(&block, "KMP_USE_YIELD");
 
   // Special case. If we parse environment, not a string, process KMP_WARNINGS
   // first.



More information about the Openmp-commits mailing list