task-reduction-2.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifdef __cplusplus
  2. extern "C"
  3. #endif
  4. void abort (void);
  5. struct S { long int s, t; };
  6. void
  7. bar (struct S *p, struct S *o)
  8. {
  9. p->s = 1;
  10. if (o->t != 5)
  11. abort ();
  12. p->t = 9;
  13. }
  14. static inline void
  15. baz (struct S *o, struct S *i)
  16. {
  17. if (o->t != 5 || i->t != 9)
  18. abort ();
  19. o->s *= i->s;
  20. }
  21. #pragma omp declare reduction (+: struct S : omp_out.s += omp_in.s) initializer (omp_priv = { 0, 3 })
  22. #pragma omp declare reduction (*: struct S : baz (&omp_out, &omp_in)) initializer (bar (&omp_priv, &omp_orig))
  23. struct S a = { 0, 7 };
  24. struct S b = { 1, 5 };
  25. void
  26. foo (void)
  27. {
  28. int i;
  29. for (i = 0; i < 2; i++)
  30. #pragma omp task in_reduction (*: b) in_reduction (+: a)
  31. {
  32. a.s += 7;
  33. b.s *= 2;
  34. if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9))
  35. abort ();
  36. }
  37. }
  38. int
  39. main ()
  40. {
  41. struct S c = { 0, 7 };
  42. #pragma omp parallel
  43. #pragma omp single
  44. {
  45. struct S d = { 1, 5 };
  46. #pragma omp taskgroup task_reduction (+: a, c) task_reduction (*: b, d)
  47. {
  48. int i;
  49. for (i = 0; i < 4; i++)
  50. #pragma omp task in_reduction (*: b, d) in_reduction (+: a, c)
  51. {
  52. int j;
  53. a.s += 7;
  54. b.s *= 2;
  55. for (j = 0; j < 2; j++)
  56. #pragma omp task in_reduction (+: a) in_reduction (*: b) \
  57. in_reduction (+: c) in_reduction (*: d)
  58. {
  59. a.s += 7;
  60. b.s *= 2;
  61. c.s += 9;
  62. d.s *= 3;
  63. foo ();
  64. if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9)
  65. || (c.t != 7 && c.t != 3) || (d.t != 5 && d.t != 9))
  66. abort ();
  67. }
  68. c.s += 9;
  69. d.s *= 3;
  70. if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9)
  71. || (c.t != 7 && c.t != 3) || (d.t != 5 && d.t != 9))
  72. abort ();
  73. }
  74. }
  75. #define THREEP4 (3L * 3L * 3L * 3L)
  76. if (d.s != (THREEP4 * THREEP4 * THREEP4) || d.t != 5)
  77. abort ();
  78. }
  79. if (a.s != 28 * 7 || a.t != 7 || b.s != (1L << 28) || b.t != 5
  80. || c.s != 12 * 9 || c.t != 7)
  81. abort ();
  82. return 0;
  83. }