declare_target-4.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* { dg-do run } */
  2. /* { dg-additional-options "-DTESTITERS=20" { target { ! run_expensive_tests } } } */
  3. #include <stdlib.h>
  4. #define EPS 0.00001
  5. #define N 1000
  6. #ifndef TESTITERS
  7. #define TESTITERS N
  8. #endif
  9. #pragma omp declare target
  10. float Q[N][N];
  11. float Pfun (const int i, const int k)
  12. {
  13. return Q[i][k] * Q[k][i];
  14. }
  15. #pragma omp end declare target
  16. void init ()
  17. {
  18. int i, j;
  19. for (i = 0; i < N; i++)
  20. for (j = 0; j < N; j++)
  21. Q[i][j] = 0.001 * i * j;
  22. }
  23. float accum_ref (int k)
  24. {
  25. int i;
  26. float tmp = 0.0;
  27. for (i = 0; i < N; i++)
  28. tmp += Pfun (i, k);
  29. return tmp;
  30. }
  31. float accum (int k)
  32. {
  33. int i;
  34. float tmp = 0.0;
  35. #pragma omp target map(tofrom:tmp)
  36. #pragma omp parallel for reduction(+:tmp)
  37. for (i = 0; i < N; i++)
  38. tmp += Pfun (i, k);
  39. return tmp;
  40. }
  41. void check (float a, float b)
  42. {
  43. float err = (b == 0.0) ? a : (a - b) / b;
  44. if (((err > 0) ? err : -err) > EPS)
  45. abort ();
  46. }
  47. int main ()
  48. {
  49. int i;
  50. init ();
  51. #pragma omp target update to(Q)
  52. for (i = 0; i < TESTITERS; i++)
  53. check (accum (i), accum_ref (i));
  54. return 0;
  55. }