reduction-10.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. struct A { int t; };
  2. struct B { char t; };
  3. struct C { unsigned long long t; };
  4. struct D { long t; };
  5. void
  6. add (struct B *x, struct B *y)
  7. {
  8. x->t += y->t;
  9. }
  10. void
  11. zero (struct B *x)
  12. {
  13. x->t = 0;
  14. }
  15. void
  16. orit (struct C *x, struct C *y)
  17. {
  18. y->t |= x->t;
  19. }
  20. #pragma omp declare reduction(+:struct A:omp_out.t += omp_in.t)
  21. #pragma omp declare reduction(+:struct B:add (&omp_out, &omp_in)) initializer(zero (&omp_priv))
  22. #pragma omp declare reduction(*:struct A:omp_out.t *= omp_in.t) initializer(omp_priv = { 1 })
  23. #pragma omp declare reduction(|:struct C:orit (&omp_in, &omp_out))
  24. #pragma omp declare reduction(&:struct D:omp_out.t = omp_out.t & omp_in.t) initializer(omp_priv = { ~0L })
  25. #pragma omp declare reduction(maxb:short:omp_out = omp_in > omp_out ? omp_in : omp_out) initializer(omp_priv = -6)
  26. struct B z[10];
  27. __attribute__((noinline, noclone)) void
  28. foo (struct A (*x)[3][2], struct A *y, struct D w[1][2], int p1, long p2, long p3, int p4,
  29. int p5, long p6, short p7)
  30. {
  31. struct C a[p7 + 4];
  32. short b[p7];
  33. int i;
  34. for (i = 0; i < p7 + 4; i++)
  35. {
  36. if (i < p7)
  37. b[i] = -6;
  38. a[i].t = 0;
  39. }
  40. #pragma omp parallel for reduction(+:x[0:p1 + 1][:p2], z[:p3]) \
  41. reduction(*:y[:p4]) reduction(|:a[:p5]) \
  42. reduction(&:w[0:p6 - 1][:p6]) reduction(maxb:b)
  43. for (i = 0; i < 128; i++)
  44. {
  45. x[i / 64][i % 3][(i / 4) & 1].t += i;
  46. if ((i & 15) == 1)
  47. y[0].t *= 3;
  48. if ((i & 31) == 2)
  49. y[1].t *= 7;
  50. if ((i & 63) == 3)
  51. y[2].t *= 17;
  52. z[i / 32].t += (i & 3);
  53. if (i < 4)
  54. z[i].t += i;
  55. a[i / 32].t |= 1ULL << (i & 30);
  56. w[0][i & 1].t &= ~(1L << (i / 17 * 3));
  57. if ((i % 79) > b[0])
  58. b[0] = i % 79;
  59. if ((i % 13) > b[1])
  60. b[1] = i % 13;
  61. if ((i % 23) > b[2])
  62. b[2] = i % 23;
  63. if ((i % 85) > b[3])
  64. b[3] = i % 85;
  65. if ((i % 192) > b[4])
  66. b[4] = i % 192;
  67. }
  68. for (i = 0; i < 9; i++)
  69. if (a[i].t != (i < 4 ? 0x55555555ULL : 0))
  70. __builtin_abort ();
  71. if (b[0] != 78 || b[1] != 12 || b[2] != 22 || b[3] != 84 || b[4] != 127)
  72. __builtin_abort ();
  73. }
  74. int
  75. main ()
  76. {
  77. struct A a[4][3][2] = {};
  78. static int a2[4][3][2] = {{{ 0, 0 }, { 0, 0 }, { 0, 0 }},
  79. {{ 312, 381 }, { 295, 356 }, { 337, 335 }},
  80. {{ 1041, 975 }, { 1016, 1085 }, { 935, 1060 }},
  81. {{ 0, 0 }, { 0, 0 }, { 0, 0 }}};
  82. struct A y[5] = { { 0 }, { 1 }, { 1 }, { 1 }, { 0 } };
  83. int y2[5] = { 0, 6561, 2401, 289, 0 };
  84. char z2[10] = { 48, 49, 50, 51, 0, 0, 0, 0, 0, 0 };
  85. struct D w[1][2] = { { { ~0L }, { ~0L } } };
  86. foo (&a[1], y + 1, w, 1, 3L, 4L, 3, 4, 2L, 5);
  87. int i, j, k;
  88. for (i = 0; i < 4; i++)
  89. for (j = 0; j < 3; j++)
  90. for (k = 0; k < 2; k++)
  91. if (a[i][j][k].t != a2[i][j][k])
  92. __builtin_abort ();
  93. for (i = 0; i < 5; i++)
  94. if (y[i].t != y2[i])
  95. __builtin_abort ();
  96. for (i = 0; i < 10; i++)
  97. if (z[i].t != z2[i])
  98. __builtin_abort ();
  99. if (w[0][0].t != ~0x249249L || w[0][1].t != ~0x249249L)
  100. __builtin_abort ();
  101. return 0;
  102. }