teams-4.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 map(to: B[0:n], C[0:n]) map(tofrom:sum)
  27. #pragma omp teams num_teams(8) thread_limit(16) reduction(+:sum)
  28. #pragma omp distribute parallel for reduction(+:sum) \
  29. dist_schedule(static, 1024) \
  30. schedule(static, 64)
  31. for (i = 0; i < n; i++)
  32. sum += B[i] * C[i];
  33. return sum;
  34. }
  35. void check (float a, float b)
  36. {
  37. float err = (b == 0.0) ? a : (a - b) / b;
  38. if (((err > 0) ? err : -err) > EPS)
  39. abort ();
  40. }
  41. int main ()
  42. {
  43. float *v1 = (float *) malloc (N * sizeof (float));
  44. float *v2 = (float *) malloc (N * sizeof (float));
  45. float p1, p2;
  46. init (v1, v2, N);
  47. p1 = dotprod_ref (v1, v2, N);
  48. p2 = dotprod (v1, v2, N);
  49. check (p1, p2);
  50. free (v1);
  51. free (v2);
  52. return 0;
  53. }