File: omp_fill_taskqueue.c

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,388 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (60 lines) | stat: -rw-r--r-- 1,805 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// RUN: %libomp-compile && env KMP_ENABLE_TASK_THROTTLING=0 %libomp-run
// RUN: %libomp-compile && env KMP_ENABLE_TASK_THROTTLING=1 %libomp-run

#include<omp.h>
#include<stdlib.h>
#include<string.h>

/**
 * Test the task throttling behavior of the runtime.
 * Unless OMP_NUM_THREADS is 1, the master thread pushes tasks to its own tasks
 * queue until either of the following happens:
 *   - the task queue is full, and it starts serializing tasks
 *   - all tasks have been pushed, and it can begin execution
 * The idea is to create a huge number of tasks which execution are blocked
 * until the master thread comes to execute tasks (they need to be blocking,
 * otherwise the second thread will start emptying the queue).
 * At this point we can check the number of enqueued tasks: iff all tasks have
 * been enqueued, then there was no task throttling.
 * Otherwise there has been some sort of task throttling.
 * If what we detect doesn't match the value of the environment variable, the
 * test is failed.
 */


#define NUM_TASKS 2000


int main()
{
  int i;
  int block = 1;
  int tid;
  int throttling = strcmp(getenv("KMP_ENABLE_TASK_THROTTLING"), "1") == 0;
  int enqueued = 0;
  int failed = -1;

  #pragma omp parallel num_threads(2)
  #pragma omp master
  {
    for (i = 0; i < NUM_TASKS; i++) {
      enqueued++;
      #pragma omp task
      {
        tid = omp_get_thread_num();
        if (tid == 0) {
          // As soon as the master thread starts executing task we should unlock
          // all tasks, and detect the test failure if it has not been done yet.
          if (failed < 0)
            failed = throttling ? enqueued == NUM_TASKS : enqueued < NUM_TASKS;
          block = 0;
        }
        while (block)
          ;
      }
    }
    block = 0;
  }

  return failed;
}