File: kmp_num_teams.c

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (95 lines) | stat: -rw-r--r-- 2,492 bytes parent folder | download | duplicates (16)
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
// RUN: %libomp-compile-and-run
// UNSUPPORTED: gcc
// Linking fails for icc 18/19
// UNSUPPORTED: icc-18, icc-19

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define NT 8

#ifdef __cplusplus
extern "C" {
#endif
typedef int kmp_int32;
typedef struct ident {
  kmp_int32 reserved_1;
  kmp_int32 flags;
  kmp_int32 reserved_2;
  kmp_int32 reserved_3;
  char const *psource;
} ident_t;
extern int __kmpc_global_thread_num(ident_t *);
extern void __kmpc_push_num_teams_51(ident_t *, kmp_int32, kmp_int32, kmp_int32,
                                     kmp_int32);
#ifdef __cplusplus
}
#endif

void check_num_teams(int num_teams_lb, int num_teams_ub, int thread_limit) {
  int nteams, nthreads;
  int a = 0;

  int gtid = __kmpc_global_thread_num(NULL);
  __kmpc_push_num_teams_51(NULL, gtid, num_teams_lb, num_teams_ub,
                           thread_limit);

#pragma omp teams default(shared)
  {
    int priv_nteams;
    int team_num = omp_get_team_num();
    if (team_num == 0)
      nteams = omp_get_num_teams();
    priv_nteams = omp_get_num_teams();
#pragma omp parallel
    {
      int priv_nthreads;
      int thread_num = omp_get_thread_num();
      int teams_ub, teams_lb, thr_limit;
      if (team_num == 0 && thread_num == 0)
        nthreads = omp_get_num_threads();
      priv_nthreads = omp_get_num_threads();

      teams_ub = (num_teams_ub ? num_teams_ub : priv_nteams);
      teams_lb = (num_teams_lb ? num_teams_lb : teams_ub);
      thr_limit = (thread_limit ? thread_limit : priv_nthreads);

      if (priv_nteams < teams_lb || priv_nteams > teams_ub) {
        fprintf(stderr, "error: invalid number of teams=%d\n", priv_nteams);
        exit(1);
      }
      if (priv_nthreads > thr_limit) {
        fprintf(stderr, "error: invalid number of threads=%d\n", priv_nthreads);
        exit(1);
      }
#pragma omp atomic
      a++;
    }
  }
  if (a != nteams * nthreads) {
    fprintf(stderr, "error: a (%d) != nteams * nthreads (%d)\n", a,
            nteams * nthreads);
    exit(1);
  } else {
    printf("#teams %d, #threads %d: Hello!\n", nteams, nthreads);
  }
}

int main(int argc, char *argv[]) {
  omp_set_num_threads(NT);

  check_num_teams(1, 8, 2);
  check_num_teams(2, 2, 2);
  check_num_teams(2, 2, 0);
  check_num_teams(8, 16, 2);
  check_num_teams(9, 16, 0);
  check_num_teams(9, 16, 2);
  check_num_teams(2, 3, 0);
  check_num_teams(0, 0, 2);
  check_num_teams(0, 4, 0);
  check_num_teams(0, 2, 2);

  printf("Test Passed\n");
  return 0;
}