reduction-8.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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])
  29. {
  30. struct C a[9] = {};
  31. short b[5] = {};
  32. int i;
  33. #pragma omp parallel for reduction(+:x[0:2][:][0:2], z[:4]) \
  34. reduction(*:y[:3]) reduction(|:a[:4]) \
  35. reduction(&:w[0:1][:2]) reduction(maxb:b)
  36. for (i = 0; i < 128; i++)
  37. {
  38. x[i / 64][i % 3][(i / 4) & 1].t += i;
  39. if ((i & 15) == 1)
  40. y[0].t *= 3;
  41. if ((i & 31) == 2)
  42. y[1].t *= 7;
  43. if ((i & 63) == 3)
  44. y[2].t *= 17;
  45. z[i / 32].t += (i & 3);
  46. if (i < 4)
  47. z[i].t += i;
  48. a[i / 32].t |= 1ULL << (i & 30);
  49. w[0][i & 1].t &= ~(1L << (i / 17 * 3));
  50. if ((i % 79) > b[0])
  51. b[0] = i % 79;
  52. if ((i % 13) > b[1])
  53. b[1] = i % 13;
  54. if ((i % 23) > b[2])
  55. b[2] = i % 23;
  56. if ((i % 85) > b[3])
  57. b[3] = i % 85;
  58. if ((i % 192) > b[4])
  59. b[4] = i % 192;
  60. }
  61. for (i = 0; i < 9; i++)
  62. if (a[i].t != (i < 4 ? 0x55555555ULL : 0))
  63. __builtin_abort ();
  64. if (b[0] != 78 || b[1] != 12 || b[2] != 22 || b[3] != 84 || b[4] != 127)
  65. __builtin_abort ();
  66. }
  67. int
  68. main ()
  69. {
  70. struct A a[4][3][2] = {};
  71. static int a2[4][3][2] = {{{ 0, 0 }, { 0, 0 }, { 0, 0 }},
  72. {{ 312, 381 }, { 295, 356 }, { 337, 335 }},
  73. {{ 1041, 975 }, { 1016, 1085 }, { 935, 1060 }},
  74. {{ 0, 0 }, { 0, 0 }, { 0, 0 }}};
  75. struct A y[5] = { { 0 }, { 1 }, { 1 }, { 1 }, { 0 } };
  76. int y2[5] = { 0, 6561, 2401, 289, 0 };
  77. char z2[10] = { 48, 49, 50, 51, 0, 0, 0, 0, 0, 0 };
  78. struct D w[1][2] = { { { ~0L }, { ~0L } } };
  79. foo (&a[1], y + 1, w);
  80. int i, j, k;
  81. for (i = 0; i < 4; i++)
  82. for (j = 0; j < 3; j++)
  83. for (k = 0; k < 2; k++)
  84. if (a[i][j][k].t != a2[i][j][k])
  85. __builtin_abort ();
  86. for (i = 0; i < 5; i++)
  87. if (y[i].t != y2[i])
  88. __builtin_abort ();
  89. for (i = 0; i < 10; i++)
  90. if (z[i].t != z2[i])
  91. __builtin_abort ();
  92. if (w[0][0].t != ~0x249249L || w[0][1].t != ~0x249249L)
  93. __builtin_abort ();
  94. return 0;
  95. }