File: omp_parallel_num_threads_strict.c

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (103 lines) | stat: -rw-r--r-- 3,754 bytes parent folder | download | duplicates (7)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// RUN: %libomp-compile && env OMP_NUM_THREADS=2,2,2,2,2 OMP_THREAD_LIMIT=16 \
// RUN: %libomp-run
#include <stdio.h>
#include "omp_testsuite.h"

// When compiler supports num_threads clause list format and strict modifier,
// remove the following and use num_threads clause directly
#if defined(__cplusplus)
extern "C" {
#endif

int __kmpc_global_thread_num(void *loc);
void __kmpc_push_num_threads_list(void *loc, int gtid, unsigned length,
                                  int *list);
void __kmpc_push_num_threads_strict(void *loc, int gtid, int nth, int sev,
                                    const char *msg);
void __kmpc_push_num_threads_list_strict(void *loc, int gtid, unsigned length,
                                         int *list, int sev, const char *msg);

#if defined(__cplusplus)
}
#endif

int test_omp_parallel_num_threads_strict() {
  int num_failed = 0;

// Test regular runtime warning about exceeding thread limit.
// Tolerate whatever value was given.
#pragma omp parallel reduction(+ : num_failed) num_threads(22)
#pragma omp single
  num_failed = num_failed + !(omp_get_num_threads() <= 22);

  // Test with 4 threads and strict -- no problem, no warning.
  __kmpc_push_num_threads_strict(NULL, __kmpc_global_thread_num(NULL), 4, 1,
                                 "This warning shouldn't happen.");
#pragma omp parallel reduction(+ : num_failed) // num_threads(strict:4)
#pragma omp single
  num_failed = num_failed + !(omp_get_num_threads() == 4);

  // Exceed limit, specify user warning message. Tolerate whatever was given.
  __kmpc_push_num_threads_strict(NULL, __kmpc_global_thread_num(NULL), 20, 1,
                                 "User-supplied warning for strict.");
#pragma omp parallel reduction(+ : num_failed)
  // num_threads(strict:20) severity(warning)
  // message("User-supplied warning for strict.")
#pragma omp single
  num_failed = num_failed + !(omp_get_num_threads() <= 20);

  // Exceed limit, no user message, use runtime default message for strict.
  // Tolerate whatever value was given.
  __kmpc_push_num_threads_strict(NULL, __kmpc_global_thread_num(NULL), 21, 1,
                                 NULL);
#pragma omp parallel reduction(+ : num_failed) // num_threads(strict:21)
#pragma omp single
  num_failed = num_failed + !(omp_get_num_threads() <= 21);

  // Exceed limit at top level. Should see user warning message.
  int threads3[2] = {24, 2};
  __kmpc_push_num_threads_list_strict(NULL, __kmpc_global_thread_num(NULL), 2,
                                      threads3, 1,
                                      "User-supplied warning on strict list.");
#pragma omp parallel reduction(+ : num_failed)
  // num_threads(strict:24,2)  severity(warning)
  // message("User-supplied warning on strict. list") // 1st level
  {
#pragma omp single
    num_failed = num_failed + !(omp_get_num_threads() <= 24);
#pragma omp parallel reduction(+ : num_failed) // 2nd level
    {
#pragma omp single
      num_failed = num_failed + !(omp_get_num_threads() <= 2);
    }
  }

  // No strict limit at top level. Regular runtime limiting applies.
  __kmpc_push_num_threads_list(NULL, __kmpc_global_thread_num(NULL), 2,
                               threads3);
#pragma omp parallel reduction(+ : num_failed)
  // num_threads(24,2) // 1st level
  {
#pragma omp single
    num_failed = num_failed + !(omp_get_num_threads() <= 24);
#pragma omp parallel reduction(+ : num_failed) // 2nd level
    {
#pragma omp single
      num_failed = num_failed + !(omp_get_num_threads() <= 2);
    }
  }

  return (!num_failed);
}

int main() {
  int i;
  int num_failed = 0;

  for (i = 0; i < REPETITIONS; i++) {
    if (!test_omp_parallel_num_threads_strict()) {
      num_failed++;
    }
  }
  return num_failed;
}