File: issue-69733.c

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (147 lines) | stat: -rw-r--r-- 3,198 bytes parent folder | download | duplicates (7)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// RUN: %libomp-compile-and-run

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

int a;

void inc_a() {
#pragma omp atomic
  a++;
}

void root_team_detached() {
  a = 0;
  omp_event_handle_t ev;
#pragma omp task detach(ev)
  inc_a();
  omp_fulfill_event(ev);
  if (a != 1) {
    fprintf(stderr, "error: root_team_detached(): a != 1\n");
    exit(EXIT_FAILURE);
  }
}

void root_team_hidden_helpers() {
  a = 0;
#pragma omp target nowait
  inc_a();

#pragma omp taskwait

  if (a != 1) {
    fprintf(stderr, "error: root_team_hidden_helpers(): a != 1\n");
    exit(EXIT_FAILURE);
  }
}

void parallel_detached(int nth1) {
  a = 0;
  omp_event_handle_t *evs =
      (omp_event_handle_t *)malloc(sizeof(omp_event_handle_t) * nth1);
#pragma omp parallel num_threads(nth1)
  {
    int tid = omp_get_thread_num();
    omp_event_handle_t e = evs[tid];
#pragma omp task detach(e)
    inc_a();
    omp_fulfill_event(e);
  }
  free(evs);
  if (a != nth1) {
    fprintf(stderr, "error: parallel_detached(): a (%d) != %d\n", a, nth1);
    exit(EXIT_FAILURE);
  }
}

void parallel_hidden_helpers(int nth1) {
  a = 0;
#pragma omp parallel num_threads(nth1)
  {
#pragma omp target nowait
    inc_a();
  }
  if (a != nth1) {
    fprintf(stderr, "error: parallel_hidden_helpers(): a (%d) != %d\n", a,
            nth1);
    exit(EXIT_FAILURE);
  }
}

void nested_parallel_detached(int nth1, int nth2) {
  a = 0;
  omp_event_handle_t **evs =
      (omp_event_handle_t **)malloc(sizeof(omp_event_handle_t *) * nth1);
#pragma omp parallel num_threads(nth1)
  {
    int tid = omp_get_thread_num();
    evs[tid] = (omp_event_handle_t *)malloc(sizeof(omp_event_handle_t) * nth2);
#pragma omp parallel num_threads(nth2) shared(tid)
    {
      int tid2 = omp_get_thread_num();
      omp_event_handle_t e = evs[tid][tid2];
#pragma omp task detach(e)
      inc_a();
      omp_fulfill_event(e);
    }
    free(evs[tid]);
  }
  free(evs);
  if (a != nth1 * nth2) {
    fprintf(stderr, "error: nested_parallel_detached(): a (%d) != %d * %d\n", a,
            nth1, nth2);
    exit(EXIT_FAILURE);
  }
}

void nested_parallel_hidden_helpers(int nth1, int nth2) {
  a = 0;
#pragma omp parallel num_threads(nth1)
  {
#pragma omp parallel num_threads(nth2)
    {
#pragma omp target nowait
      inc_a();
    }
  }
  if (a != nth1 * nth2) {
    fprintf(stderr,
            "error: nested_parallel_hidden_helpers(): a (%d) != %d * %d\n", a,
            nth1, nth2);
    exit(EXIT_FAILURE);
  }
}

int main() {
  int i, nth1, nth2;

  omp_set_max_active_levels(2);
  omp_set_dynamic(0);

  for (i = 0; i < 10; ++i)
    root_team_detached();

  for (i = 0; i < 10; ++i)
    root_team_hidden_helpers();

  for (i = 0; i < 10; ++i)
    for (nth1 = 1; nth1 <= 4; ++nth1)
      parallel_detached(nth1);

  for (i = 0; i < 10; ++i)
    for (nth1 = 1; nth1 <= 4; ++nth1)
      parallel_hidden_helpers(nth1);

  for (i = 0; i < 10; ++i)
    for (nth1 = 1; nth1 <= 4; ++nth1)
      for (nth2 = 1; nth2 <= 4; ++nth2)
        nested_parallel_detached(nth1, nth2);

  for (i = 0; i < 10; ++i)
    for (nth1 = 1; nth1 <= 4; ++nth1)
      for (nth2 = 1; nth2 <= 4; ++nth2)
        nested_parallel_hidden_helpers(nth1, nth2);

  return 0;
}