File: omp_task_red_taskloop.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 (71 lines) | stat: -rw-r--r-- 1,658 bytes parent folder | download | duplicates (19)
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
// RUN: %libomp-compile-and-run

// Parsing error until gcc8:
// UNSUPPORTED: gcc-4, gcc-5, gcc-6, gcc-7, gcc-8

// Parsing error until clang11:
// UNSUPPORTED: clang-10, clang-9, clang-8, clang-7

// No icc compiler support yet
// XFAIL: icc

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

int r;

int work(int k, int l)
{
  return k + l + 1;
}
void bar(int i) {
  #pragma omp taskgroup task_reduction(+:r)
 { int th_gen = omp_get_thread_num();
  #pragma omp task in_reduction(+:r) firstprivate(i, th_gen)
  {
    r += work(i, 0);
printf("executing task (%d, 0), th %d (gen by th %d)\n", i, omp_get_thread_num(), th_gen);
  }
  #pragma omp task in_reduction(+:r) firstprivate(i, th_gen)
  {
    r += work(i, 1);
printf("executing task (%d, 1), th %d (gen by th %d)\n", i, omp_get_thread_num(), th_gen);
  }
 }
}
int foo() {
  int i;
  int th_gen = omp_get_thread_num();
  #pragma omp taskgroup task_reduction(+:r)
  {
    bar(0);
  }
printf("th %d passed bar0\n", th_gen);
  #pragma omp taskloop reduction(+:r) firstprivate(th_gen)
  for (i = 1; i < 4; ++i) {
    bar(i);
printf("th %d (gen by th %d) passed bar%d in taskloop\n", omp_get_thread_num(), th_gen, i);
  #pragma omp task in_reduction(+:r)
    r += i;
  }
  return 0;
}
// res = ((1+2)+(2+3)+(3+4)+(4+5)+1+2+3) = 30
#define res 30
int main()
{
  r = 0;
  #pragma omp parallel num_threads(2)
  { // barrier ensures threads have started before tasks creation
    #pragma omp barrier
    // single ensures no race condition between taskgroup reductions
    #pragma omp single nowait
      foo();
  }
  if (r == res) {
    return 0;
  } else {
    printf("error r = %d (!= %d)\n", r, res);
    return 1;
  }
}