1// RUN: %libomp-compile && env OMP_THREAD_LIMIT=4 %libomp-run 4
2// RUN: %libomp-compile && env OMP_THREAD_LIMIT=7 %libomp-run 7
3//
4// OMP_THREAD_LIMIT=N should imply that no more than N threads are active in
5// a contention group
6#include <stdio.h>
7#include <string.h>
8#include <limits.h>
9#include "omp_testsuite.h"
10
11int failed = 0;
12
13void usage() {
14 fprintf(stderr, format: "usage: omp_thread_limit <n>\n");
15}
16
17void verify(const char* file_name, int line_number, int team_size) {
18 int num_threads = omp_get_num_threads();
19 if (team_size != num_threads) {
20#pragma omp critical(A)
21 {
22 char label[256];
23 snprintf(s: label, maxlen: sizeof(label), format: "%s:%d", file_name, line_number);
24 failed = 1;
25 printf(format: "failed: %s: team_size(%d) != omp_get_num_threads(%d)\n",
26 label, team_size, num_threads);
27 }
28 }
29}
30
31int main(int argc, char** argv)
32{
33 int cl_thread_limit;
34
35 if (argc != 2) {
36 usage();
37 return 1;
38 }
39 cl_thread_limit = atoi(argv[1]);
40
41 omp_set_dynamic(0);
42 if (omp_get_thread_limit() != cl_thread_limit) {
43 fprintf(stderr, format: "omp_get_thread_limit failed with %d, should be%d\n",
44 omp_get_thread_limit(), cl_thread_limit);
45 return 1;
46 }
47 else if (omp_get_max_threads() > cl_thread_limit) {
48#if _OPENMP
49 int team_size = cl_thread_limit;
50#else
51 int team_size = 1;
52#endif
53 omp_set_num_threads(19);
54 verify(__FILE__, __LINE__, team_size: 1);
55#pragma omp parallel
56 {
57 verify(__FILE__, __LINE__, team_size);
58 verify(__FILE__, __LINE__, team_size);
59 }
60 verify(__FILE__, __LINE__, team_size: 1);
61
62 omp_set_nested(1);
63#pragma omp parallel num_threads(3)
64 {
65 verify(__FILE__, __LINE__, team_size: 3);
66#pragma omp master
67#pragma omp parallel num_threads(21)
68 {
69 verify(__FILE__, __LINE__, team_size: team_size-2);
70 verify(__FILE__, __LINE__, team_size: team_size-2);
71 }
72 }
73 verify(__FILE__, __LINE__, team_size: 1);
74
75 return failed;
76 } else {
77 fprintf(stderr, format: "This test is not applicable for max num_threads='%d'\n",
78 omp_get_max_threads());
79 return 0;
80 }
81
82}
83

source code of openmp/runtime/test/env/omp_thread_limit.c