xref: /llvm-project/openmp/runtime/test/teams/teams.c (revision ea34d95e0ad664fa879bb1d8b71f32928b1d6c0f)
1 // RUN: %libomp-compile-and-run
2 // UNSUPPORTED: gcc-4, gcc-5, gcc-6, gcc-7, gcc-8
3 // UNSUPPORTED: icc, clang
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <omp.h>
8 
9 #define NUM_TEAMS 2
10 #define NUM_THREADS_PER_TEAM 3
11 
main(int argc,char ** argv)12 int main(int argc, char** argv) {
13   #pragma omp teams num_teams(NUM_TEAMS)
14   {
15     int i;
16     int members[NUM_THREADS_PER_TEAM];
17     // Only an upper bound is guaranteed for number of teams
18     int nteams = omp_get_num_teams();
19     if (nteams > NUM_TEAMS) {
20       fprintf(stderr, "error: too many teams: %d\n", nteams);
21       exit(1);
22     }
23     for (i = 0; i < NUM_THREADS_PER_TEAM; ++i)
24       members[i] = -1;
25     #pragma omp parallel num_threads(NUM_THREADS_PER_TEAM) private(i)
26     {
27       int tid = omp_get_thread_num();
28       int team_id = omp_get_team_num();
29       int nthreads = omp_get_num_threads();
30       if (nthreads != NUM_THREADS_PER_TEAM) {
31         fprintf(stderr, "error: detected number of threads (%d) is not %d\n",
32                 nthreads, NUM_THREADS_PER_TEAM);
33         exit(1);
34       }
35       if (tid < 0 || tid >= nthreads) {
36         fprintf(stderr, "error: thread id is out of range: %d\n", tid);
37         exit(1);
38       }
39       if (team_id < 0 || team_id > omp_get_num_teams()) {
40         fprintf(stderr, "error: team id is out of range: %d\n", team_id);
41         exit(1);
42       }
43       members[omp_get_thread_num()] = 1;
44       #pragma omp barrier
45       #pragma omp single
46       {
47         for (i = 0; i < NUM_THREADS_PER_TEAM; ++i) {
48           if (members[i] != 1) {
49             fprintf(stderr, "error: worker %d not flagged\n", i);
50             exit(1);
51           }
52         }
53       }
54     }
55   }
56   return 0;
57 }
58