Skip to content

Commit a23806e

Browse files
Create a runtime option to disable task throttling.
Patch by viroulep (Philippe Virouleau) Differential Revision: https://reviews.llvm.org/D63196 llvm-svn: 364934
1 parent 50be348 commit a23806e

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

openmp/runtime/src/kmp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,7 @@ typedef enum kmp_tasking_mode {
21212121
extern kmp_tasking_mode_t
21222122
__kmp_tasking_mode; /* determines how/when to execute tasks */
21232123
extern int __kmp_task_stealing_constraint;
2124+
extern int __kmp_enable_task_throttling;
21242125
#if OMP_40_ENABLED
21252126
extern kmp_int32 __kmp_default_device; // Set via OMP_DEFAULT_DEVICE if
21262127
// specified, defaults to 0 otherwise

openmp/runtime/src/kmp_global.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ omp_memspace_handle_t const omp_low_lat_mem_space =
341341
KMP_BUILD_ASSERT(sizeof(kmp_tasking_flags_t) == 4);
342342

343343
int __kmp_task_stealing_constraint = 1; /* Constrain task stealing by default */
344+
int __kmp_enable_task_throttling = 1;
344345

345346
#ifdef DEBUG_SUSPEND
346347
int __kmp_suspend_count = 0;

openmp/runtime/src/kmp_settings.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4682,6 +4682,20 @@ static void __kmp_stg_print_forkjoin_frames_mode(kmp_str_buf_t *buffer,
46824682
} // __kmp_stg_print_forkjoin_frames
46834683
#endif /* USE_ITT_BUILD */
46844684

4685+
// -----------------------------------------------------------------------------
4686+
// KMP_ENABLE_TASK_THROTTLING
4687+
4688+
static void __kmp_stg_parse_task_throttling(char const *name,
4689+
char const *value, void *data) {
4690+
__kmp_stg_parse_bool(name, value, &__kmp_enable_task_throttling);
4691+
} // __kmp_stg_parse_task_throttling
4692+
4693+
4694+
static void __kmp_stg_print_task_throttling(kmp_str_buf_t *buffer,
4695+
char const *name, void *data) {
4696+
__kmp_stg_print_bool(buffer, name, __kmp_enable_task_throttling);
4697+
} // __kmp_stg_print_task_throttling
4698+
46854699
// -----------------------------------------------------------------------------
46864700
// OMP_DISPLAY_ENV
46874701

@@ -5003,6 +5017,8 @@ static kmp_setting_t __kmp_stg_table[] = {
50035017
{"KMP_FORKJOIN_FRAMES_MODE", __kmp_stg_parse_forkjoin_frames_mode,
50045018
__kmp_stg_print_forkjoin_frames_mode, NULL, 0, 0},
50055019
#endif
5020+
{"KMP_ENABLE_TASK_THROTTLING", __kmp_stg_parse_task_throttling,
5021+
__kmp_stg_print_task_throttling, NULL, 0, 0},
50065022

50075023
#if OMP_40_ENABLED
50085024
{"OMP_DISPLAY_ENV", __kmp_stg_parse_omp_display_env,

openmp/runtime/src/kmp_tasking.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
374374
// Check if deque is full
375375
if (TCR_4(thread_data->td.td_deque_ntasks) >=
376376
TASK_DEQUE_SIZE(thread_data->td)) {
377-
if (__kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
377+
if (__kmp_enable_task_throttling &&
378+
__kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
378379
thread->th.th_current_task)) {
379380
KA_TRACE(20, ("__kmp_push_task: T#%d deque is full; returning "
380381
"TASK_NOT_PUSHED for task %p\n",
@@ -394,7 +395,8 @@ static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) {
394395
// Need to recheck as we can get a proxy task from thread outside of OpenMP
395396
if (TCR_4(thread_data->td.td_deque_ntasks) >=
396397
TASK_DEQUE_SIZE(thread_data->td)) {
397-
if (__kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
398+
if (__kmp_enable_task_throttling &&
399+
__kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata,
398400
thread->th.th_current_task)) {
399401
__kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock);
400402
KA_TRACE(20, ("__kmp_push_task: T#%d deque is full on 2nd check; "
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %libomp-compile && env KMP_ENABLE_TASK_THROTTLING=0 %libomp-run
2+
// RUN: %libomp-compile && env KMP_ENABLE_TASK_THROTTLING=1 %libomp-run
3+
4+
#include<omp.h>
5+
#include<stdlib.h>
6+
#include<string.h>
7+
8+
/**
9+
* Test the task throttling behavior of the runtime.
10+
* Unless OMP_NUM_THREADS is 1, the master thread pushes tasks to its own tasks
11+
* queue until either of the following happens:
12+
* - the task queue is full, and it starts serializing tasks
13+
* - all tasks have been pushed, and it can begin execution
14+
* The idea is to create a huge number of tasks which execution are blocked
15+
* until the master thread comes to execute tasks (they need to be blocking,
16+
* otherwise the second thread will start emptying the queue).
17+
* At this point we can check the number of enqueued tasks: iff all tasks have
18+
* been enqueued, then there was no task throttling.
19+
* Otherwise there has been some sort of task throttling.
20+
* If what we detect doesn't match the value of the environment variable, the
21+
* test is failed.
22+
*/
23+
24+
25+
#define NUM_TASKS 2000
26+
27+
28+
int main()
29+
{
30+
int i;
31+
int block = 1;
32+
int tid;
33+
int throttling = strcmp(getenv("KMP_ENABLE_TASK_THROTTLING"), "1") == 0;
34+
int enqueued = 0;
35+
int failed = -1;
36+
37+
#pragma omp parallel num_threads(2)
38+
#pragma omp master
39+
{
40+
for (i = 0; i < NUM_TASKS; i++) {
41+
enqueued++;
42+
#pragma omp task
43+
{
44+
tid = omp_get_thread_num();
45+
if (tid == 0) {
46+
// As soon as the master thread starts executing task we should unlock
47+
// all tasks, and detect the test failure if it has not been done yet.
48+
if (failed < 0)
49+
failed = throttling ? enqueued == NUM_TASKS : enqueued < NUM_TASKS;
50+
block = 0;
51+
}
52+
while (block)
53+
;
54+
}
55+
}
56+
block = 0;
57+
}
58+
59+
return failed;
60+
}

0 commit comments

Comments
 (0)