taskloop-reduction-1.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. extern
  2. #ifdef __cplusplus
  3. "C"
  4. #endif
  5. void abort (void);
  6. struct S { unsigned long long int s, t; };
  7. void
  8. rbar (struct S *p, struct S *o)
  9. {
  10. p->s = 1;
  11. if (o->t != 5)
  12. abort ();
  13. p->t = 9;
  14. }
  15. static inline void
  16. rbaz (struct S *o, struct S *i)
  17. {
  18. if (o->t != 5 || i->t != 9)
  19. abort ();
  20. o->s *= i->s;
  21. }
  22. #pragma omp declare reduction (+: struct S : omp_out.s += omp_in.s) \
  23. initializer (omp_priv = { 0, 3 })
  24. #pragma omp declare reduction (*: struct S : rbaz (&omp_out, &omp_in)) \
  25. initializer (rbar (&omp_priv, &omp_orig))
  26. struct S g = { 0, 7 };
  27. struct S h = { 1, 5 };
  28. int
  29. foo (int *a, int *b)
  30. {
  31. int x = 0;
  32. #pragma omp taskloop reduction (+:x) in_reduction (+:b[0])
  33. for (int i = 0; i < 64; i++)
  34. {
  35. x += a[i];
  36. *b += a[i] * 2;
  37. }
  38. return x;
  39. }
  40. unsigned long long int
  41. bar (int *a, unsigned long long int *b)
  42. {
  43. unsigned long long int x = 1;
  44. #pragma omp taskloop reduction (*:x) in_reduction (*:b[0])
  45. for (int i = 0; i < 64; i++)
  46. {
  47. #pragma omp task in_reduction (*:x)
  48. x *= a[i];
  49. #pragma omp task in_reduction (*:b[0])
  50. *b *= (3 - a[i]);
  51. }
  52. return x;
  53. }
  54. void
  55. baz (int i, int *a, int *c)
  56. {
  57. #pragma omp task in_reduction (*:h) in_reduction (+:g)
  58. {
  59. g.s += 7 * a[i];
  60. h.s *= (3 - c[i]);
  61. if ((g.t != 7 && g.t != 3) || (h.t != 5 && h.t != 9))
  62. abort ();
  63. }
  64. }
  65. int
  66. main ()
  67. {
  68. int i, j, a[64], b = 0, c[64];
  69. unsigned long long int d = 1, e;
  70. struct S m = { 0, 7 };
  71. for (i = 0; i < 64; i++)
  72. {
  73. a[i] = 2 * i;
  74. c[i] = 1 + ((i % 3) != 1);
  75. }
  76. #pragma omp parallel
  77. #pragma omp master
  78. {
  79. struct S n = { 1, 5 };
  80. #pragma omp taskgroup task_reduction (+:b)
  81. j = foo (a, &b);
  82. #pragma omp taskgroup task_reduction (*:d)
  83. e = bar (c, &d);
  84. #pragma omp taskloop reduction (+: g, m) reduction (*: h, n)
  85. for (i = 0; i < 64; ++i)
  86. {
  87. g.s += 3 * a[i];
  88. h.s *= (3 - c[i]);
  89. m.s += 4 * a[i];
  90. n.s *= c[i];
  91. if ((g.t != 7 && g.t != 3) || (h.t != 5 && h.t != 9)
  92. || (m.t != 7 && m.t != 3) || (n.t != 5 && n.t != 9))
  93. abort ();
  94. baz (i, a, c);
  95. }
  96. if (n.s != (1ULL << 43) || n.t != 5)
  97. abort ();
  98. }
  99. if (j != 63 * 64 || b != 63 * 64 * 2)
  100. abort ();
  101. if (e != (1ULL << 43) || d != (1ULL << 21))
  102. abort ();
  103. if (g.s != 63 * 64 * 10 || g.t != 7)
  104. abort ();
  105. if (h.s != (1ULL << 42) || h.t != 5)
  106. abort ();
  107. if (m.s != 63 * 64 * 4 || m.t != 7)
  108. abort ();
  109. return 0;
  110. }