1 // RUN: %libomp-compile-and-run
2 // UNSUPPORTED: gcc
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <omp.h>
7 
8 #define NT 8
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 typedef int kmp_int32;
14 typedef struct ident {
15   kmp_int32 reserved_1;
16   kmp_int32 flags;
17   kmp_int32 reserved_2;
18   kmp_int32 reserved_3;
19   char const *psource;
20 } ident_t;
21 extern int __kmpc_global_thread_num(ident_t *);
22 extern void __kmpc_push_num_teams_51(ident_t *, kmp_int32, kmp_int32, kmp_int32,
23                                      kmp_int32);
24 #ifdef __cplusplus
25 }
26 #endif
27 
check_num_teams(int num_teams_lb,int num_teams_ub,int thread_limit)28 void check_num_teams(int num_teams_lb, int num_teams_ub, int thread_limit) {
29   int nteams, nthreads;
30   int a = 0;
31 
32   int gtid = __kmpc_global_thread_num(NULL);
33   __kmpc_push_num_teams_51(NULL, gtid, num_teams_lb, num_teams_ub,
34                            thread_limit);
35 
36 #pragma omp teams default(shared)
37   {
38     int priv_nteams;
39     int team_num = omp_get_team_num();
40     if (team_num == 0)
41       nteams = omp_get_num_teams();
42     priv_nteams = omp_get_num_teams();
43 #pragma omp parallel
44     {
45       int priv_nthreads;
46       int thread_num = omp_get_thread_num();
47       int teams_ub, teams_lb, thr_limit;
48       if (team_num == 0 && thread_num == 0)
49         nthreads = omp_get_num_threads();
50       priv_nthreads = omp_get_num_threads();
51 
52       teams_ub = (num_teams_ub ? num_teams_ub : priv_nteams);
53       teams_lb = (num_teams_lb ? num_teams_lb : teams_ub);
54       thr_limit = (thread_limit ? thread_limit : priv_nthreads);
55 
56       if (priv_nteams < teams_lb || priv_nteams > teams_ub) {
57         fprintf(stderr, "error: invalid number of teams=%d\n", priv_nteams);
58         exit(1);
59       }
60       if (priv_nthreads > thr_limit) {
61         fprintf(stderr, "error: invalid number of threads=%d\n", priv_nthreads);
62         exit(1);
63       }
64 #pragma omp atomic
65       a++;
66     }
67   }
68   if (a != nteams * nthreads) {
69     fprintf(stderr, "error: a (%d) != nteams * nthreads (%d)\n", a,
70             nteams * nthreads);
71     exit(1);
72   } else {
73     printf("#teams %d, #threads %d: Hello!\n", nteams, nthreads);
74   }
75 }
76 
main(int argc,char * argv[])77 int main(int argc, char *argv[]) {
78   omp_set_num_threads(NT);
79 
80   check_num_teams(1, 8, 2);
81   check_num_teams(2, 2, 2);
82   check_num_teams(2, 2, 0);
83   check_num_teams(8, 16, 2);
84   check_num_teams(9, 16, 0);
85   check_num_teams(9, 16, 2);
86   check_num_teams(2, 3, 0);
87   check_num_teams(0, 0, 2);
88   check_num_teams(0, 4, 0);
89   check_num_teams(0, 2, 2);
90 
91   printf("Test Passed\n");
92   return 0;
93 }
94