aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c++/task-reduction-16.C
blob: 5835edcbd5b35a54d3ae8830dc0f8625b7549b77 (plain)
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
extern "C" void abort ();

struct S { S (); S (long long int, int); ~S (); static int cnt1, cnt2, cnt3; long long int s; int t; };

int S::cnt1;
int S::cnt2;
int S::cnt3;

S::S ()
{
  #pragma omp atomic
  cnt1++;
}

S::S (long long int x, int y) : s (x), t (y)
{
  #pragma omp atomic update
  ++cnt2;
}

S::~S ()
{
  #pragma omp atomic
  cnt3 = cnt3 + 1;
  if (t < 3 || t > 9 || (t & 1) == 0)
    abort ();
}

void
bar (S *p, S *o)
{
  p->s = 1;
  if (o->t != 5)
    abort ();
  p->t = 9;
}

static inline void
baz (S *o, S *i)
{
  if (o->t != 5 || i->t != 9)
    abort ();
  o->s *= i->s;
}

#pragma omp declare reduction (+: S : omp_out.s += omp_in.s) initializer (omp_priv (0, 3))
#pragma omp declare reduction (*: S : baz (&omp_out, &omp_in)) initializer (bar (&omp_priv, &omp_orig))

S as = { 0LL, 7 };
S &a = as;
S bs (1LL, 5);
S &b = bs;

void
foo (S &c, S &d)
{
  int i;
  for (i = 0; i < 2; i++)
    #pragma omp task in_reduction (+: c) in_reduction (*: b, d) in_reduction (+: a)
    {
      a.s += 7;
      b.s *= 2;
      c.s += 9;
      d.s *= 3;
      if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9)
	  || (c.t != 7 && c.t != 3) || (d.t != 5 && d.t != 9))
	abort ();
    }
}

void
test ()
{
  S cs = { 0LL, 7 };
  S &c = cs;
  S ds (1LL, 5);
  #pragma omp parallel if (0)
  {
    S &d = ds;
    #pragma omp parallel shared (a, b, c, d)
    {
      #pragma omp for schedule (static, 1) reduction (task, +: a, c) reduction (task, *: b, d)
      for (int i = 0; i < 4; i++)
	#pragma omp task in_reduction (*: b, d) in_reduction (+: a, c)
	{
	  int j;
	  a.s += 7;
	  b.s *= 2;
	  for (j = 0; j < 2; j++)
	    #pragma omp task in_reduction (+: a) in_reduction (*: b) \
			     in_reduction (+: c) in_reduction (*: d)
	    {
	      a.s += 7;
	      b.s *= 2;
	      c.s += 9;
	      d.s *= 3;
	      foo (c, d);
	      if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9)
		  || (c.t != 7 && c.t != 3) || (d.t != 5 && d.t != 9))
		abort ();
	    }
	  c.s += 9;
	  d.s *= 3;
	  if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9)
	      || (c.t != 7 && c.t != 3) || (d.t != 5 && d.t != 9))
	    abort ();
	}
#define THREEP7 (3LL * 3LL * 3LL * 3LL * 3LL * 3LL * 3LL)
      if (d.s != (THREEP7 * THREEP7 * THREEP7 * THREEP7) || d.t != 5)
	abort ();
      if (a.s != 28 * 7 || a.t != 7 || b.s != (1L << 28) || b.t != 5
	  || c.s != 28 * 9 || c.t != 7)
	abort ();
    }
  }
  if (a.s != 28 * 7 || a.t != 7 || b.s != (1L << 28) || b.t != 5
      || c.s != 28 * 9 || c.t != 7)
    abort ();
  if (ds.s != (THREEP7 * THREEP7 * THREEP7 * THREEP7) || ds.t != 5)
    abort ();
}

int
main ()
{
  int c1 = S::cnt1, c2 = S::cnt2, c3 = S::cnt3;
  test ();
  if (S::cnt1 + S::cnt2 - c1 - c2 != S::cnt3 - c3)
    abort ();
}