Bump version to 19.1.0-rc3
[llvm-project.git] / offload / test / offloading / bug49021.cpp
blob37da8ce5d861bd044367c8c25a816b18d740360c
1 // RUN: %libomptarget-compilexx-generic -O3 && %libomptarget-run-generic
2 // RUN: %libomptarget-compilexx-generic -O3 -ffast-math && %libomptarget-run-generic
3 // RUN: %libomptarget-compileoptxx-generic -O3 && %libomptarget-run-generic
4 // RUN: %libomptarget-compileoptxx-generic -O3 -ffast-math && %libomptarget-run-generic
6 #include <iostream>
8 template <typename T> int test_map() {
9 std::cout << "map(T)" << std::endl;
10 T a(0.2), a_check;
11 #pragma omp target map(from : a_check)
12 { a_check = a; }
14 if (a_check != a) {
15 std::cout << " wrong results";
16 return 1;
19 return 0;
22 template <typename T> int test_reduction() {
23 std::cout << "flat parallelism" << std::endl;
24 T sum(0), sum_host(0);
25 const int size = 100;
26 T array[size];
27 for (int i = 0; i < size; i++) {
28 array[i] = i;
29 sum_host += array[i];
32 #pragma omp target teams distribute parallel for map(to : array[ : size]) \
33 reduction(+ : sum)
34 for (int i = 0; i < size; i++)
35 sum += array[i];
37 if (sum != sum_host)
38 std::cout << " wrong results " << sum << " host " << sum_host << std::endl;
40 std::cout << "hierarchical parallelism" << std::endl;
41 const int nblock(10), block_size(10);
42 T block_sum[nblock];
43 #pragma omp target teams distribute map(to : array[ : size]) \
44 map(from : block_sum[ : nblock])
45 for (int ib = 0; ib < nblock; ib++) {
46 T partial_sum = 0;
47 const int istart = ib * block_size;
48 const int iend = (ib + 1) * block_size;
49 #pragma omp parallel for reduction(+ : partial_sum)
50 for (int i = istart; i < iend; i++)
51 partial_sum += array[i];
52 block_sum[ib] = partial_sum;
55 sum = 0;
56 for (int ib = 0; ib < nblock; ib++) {
57 sum += block_sum[ib];
60 if (sum != sum_host) {
61 std::cout << " wrong results " << sum << " host " << sum_host << std::endl;
62 return 1;
65 return 0;
68 template <typename T> int test_POD() {
69 int ret = 0;
70 ret |= test_map<T>();
71 ret |= test_reduction<T>();
72 return ret;
75 int main() {
76 int ret = 0;
77 std::cout << "Testing float" << std::endl;
78 ret |= test_POD<float>();
79 std::cout << "Testing double" << std::endl;
80 ret |= test_POD<double>();
81 return ret;