summaryrefslogtreecommitdiffstats
path: root/Tests/FindOpenACC/CXXTest/main.cxx
blob: 7369045c0b7847f61cdffc0ca35c142e3aec03c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include <vector>

void vecaddgpu(float* r, float* a, float* b, std::size_t n)
{
#pragma acc kernels loop present(r, a, b)
  for (std::size_t i = 0; i < n; ++i)
    r[i] = a[i] + b[i];
}

int main(int, char* [])
{
  const std::size_t n = 100000; /* vector length */
  std::vector<float> a(n);      /* input vector 1 */
  std::vector<float> b(n);      /* input vector 2 */
  std::vector<float> r(n);      /* output vector */
  std::vector<float> e(n);      /* expected output values */

  for (std::size_t i = 0; i < n; ++i) {
    a[i] = static_cast<float>(i + 1);
    b[i] = static_cast<float>(1000 * i);
  }

  /* compute on the GPU */
  auto a_ptr = a.data();
  auto b_ptr = b.data();
  auto r_ptr = r.data();
#pragma acc data copyin(a_ptr [0:n], b_ptr [0:n]) copyout(r_ptr [0:n])
  {
    vecaddgpu(r_ptr, a_ptr, b_ptr, n);
  }
  /* compute on the host to compare */
  for (std::size_t i = 0; i < n; ++i)
    e[i] = a[i] + b[i];
  /* compare results */
  int errs = 0;
  for (std::size_t i = 0; i < n; ++i) {
    if (r[i] != e[i]) {
      ++errs;
    }
  }
  return errs;
}