LLVM OpenMP 19.0.0git
kmp_num_teams.c
Go to the documentation of this file.
1// RUN: %libomp-compile-and-run
2// UNSUPPORTED: gcc
3// Linking fails for icc 18/19
4// UNSUPPORTED: icc-18, icc-19
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <omp.h>
9
10#define NT 8
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15typedef int kmp_int32;
16typedef struct ident {
21 char const *psource;
25 kmp_int32);
26#ifdef __cplusplus
27}
28#endif
29
30void check_num_teams(int num_teams_lb, int num_teams_ub, int thread_limit) {
31 int nteams, nthreads;
32 int a = 0;
33
34 int gtid = __kmpc_global_thread_num(NULL);
35 __kmpc_push_num_teams_51(NULL, gtid, num_teams_lb, num_teams_ub,
36 thread_limit);
37
38#pragma omp teams default(shared)
39 {
40 int priv_nteams;
41 int team_num = omp_get_team_num();
42 if (team_num == 0)
43 nteams = omp_get_num_teams();
44 priv_nteams = omp_get_num_teams();
45#pragma omp parallel
46 {
47 int priv_nthreads;
48 int thread_num = omp_get_thread_num();
49 int teams_ub, teams_lb, thr_limit;
50 if (team_num == 0 && thread_num == 0)
51 nthreads = omp_get_num_threads();
52 priv_nthreads = omp_get_num_threads();
53
54 teams_ub = (num_teams_ub ? num_teams_ub : priv_nteams);
55 teams_lb = (num_teams_lb ? num_teams_lb : teams_ub);
56 thr_limit = (thread_limit ? thread_limit : priv_nthreads);
57
58 if (priv_nteams < teams_lb || priv_nteams > teams_ub) {
59 fprintf(stderr, "error: invalid number of teams=%d\n", priv_nteams);
60 exit(1);
61 }
62 if (priv_nthreads > thr_limit) {
63 fprintf(stderr, "error: invalid number of threads=%d\n", priv_nthreads);
64 exit(1);
65 }
66#pragma omp atomic
67 a++;
68 }
69 }
70 if (a != nteams * nthreads) {
71 fprintf(stderr, "error: a (%d) != nteams * nthreads (%d)\n", a,
72 nteams * nthreads);
73 exit(1);
74 } else {
75 printf("#teams %d, #threads %d: Hello!\n", nteams, nthreads);
76 }
77}
78
79int main(int argc, char *argv[]) {
81
82 check_num_teams(1, 8, 2);
83 check_num_teams(2, 2, 2);
84 check_num_teams(2, 2, 0);
85 check_num_teams(8, 16, 2);
86 check_num_teams(9, 16, 0);
87 check_num_teams(9, 16, 2);
88 check_num_teams(2, 3, 0);
89 check_num_teams(0, 0, 2);
90 check_num_teams(0, 4, 0);
91 check_num_teams(0, 2, 2);
92
93 printf("Test Passed\n");
94 return 0;
95}
void __kmpc_push_num_teams_51(ident_t *, kmp_int32, kmp_int32, kmp_int32, kmp_int32)
int __kmpc_global_thread_num(ident_t *)
void check_num_teams(int num_teams_lb, int num_teams_ub, int thread_limit)
Definition: kmp_num_teams.c:30
#define NT
Definition: kmp_num_teams.c:10
int kmp_int32
Definition: kmp_num_teams.c:15
struct ident ident_t
#define omp_set_num_threads
Definition: kmp_stub.cpp:34
int a
int32_t kmp_int32
The ident structure that describes a source location.
Definition: kmp.h:247
char const * psource
String describing the source location.
Definition: kmp.h:257
kmp_int32 reserved_1
might be used in Fortran; see above
Definition: kmp.h:248
kmp_int32 reserved_2
not really used in Fortran any more; see above
Definition: kmp.h:251
kmp_int32 reserved_3
source[4] in Fortran, do not use for C++
Definition: kmp.h:256
kmp_int32 flags
also f.flags; KMP_IDENT_xxx flags; KMP_IDENT_KMPC identifies this union member
Definition: kmp.h:249
int omp_get_num_threads()
int main()
Definition: test-touch.c:21