teams-3.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* { dg-do run } */
  2. #include <stdlib.h>
  3. #define EPS 0.0001
  4. #define N 1024*1024
  5. void init (float B[], float C[], int n)
  6. {
  7. int i;
  8. for (i = 0; i < n; i++)
  9. {
  10. B[i] = 0.1 * i;
  11. C[i] = 0.01 * i * i;
  12. }
  13. }
  14. float dotprod_ref (float B[], float C[], int n)
  15. {
  16. int i;
  17. float sum = 0.0;
  18. for (i = 0; i < n; i++)
  19. sum += B[i] * C[i];
  20. return sum;
  21. }
  22. float dotprod (float B[], float C[], int n)
  23. {
  24. int i;
  25. float sum = 0;
  26. #pragma omp target teams map(to: B[0:n], C[0:n]) \
  27. map(tofrom: sum) reduction(+:sum)
  28. #pragma omp distribute parallel for reduction(+:sum)
  29. for (i = 0; i < n; i++)
  30. sum += B[i] * C[i];
  31. return sum;
  32. }
  33. void check (float a, float b)
  34. {
  35. float err = (b == 0.0) ? a : (a - b) / b;
  36. if (((err > 0) ? err : -err) > EPS)
  37. abort ();
  38. }
  39. int main ()
  40. {
  41. float *v1 = (float *) malloc (N * sizeof (float));
  42. float *v2 = (float *) malloc (N * sizeof (float));
  43. float p1, p2;
  44. init (v1, v2, N);
  45. p1 = dotprod_ref (v1, v2, N);
  46. p2 = dotprod (v1, v2, N);
  47. check (p1, p2);
  48. free (v1);
  49. free (v2);
  50. return 0;
  51. }