1 | // RUN: %libomptarget-compilexx-and-run-generic |
2 | // RUN: %libomptarget-compileoptxx-and-run-generic |
3 | |
4 | // FIXME: This is a bug in host offload, this should run fine. |
5 | // UNSUPPORTED: aarch64-unknown-linux-gnu |
6 | // UNSUPPORTED: aarch64-unknown-linux-gnu-LTO |
7 | // UNSUPPORTED: x86_64-pc-linux-gnu |
8 | // UNSUPPORTED: x86_64-pc-linux-gnu-LTO |
9 | |
10 | // This test validates that the OpenMP target reductions to find a maximum work |
11 | // as indended for a few common data types. |
12 | |
13 | #include <algorithm> |
14 | #include <cassert> |
15 | #include <limits> |
16 | #include <vector> |
17 | |
18 | template <class Tp> void test_max_idx_reduction() { |
19 | const Tp length = 1000; |
20 | const Tp nmaximas = 8; |
21 | std::vector<float> a(length, 3.0f); |
22 | const Tp step = length / nmaximas; |
23 | for (Tp i = 0; i < nmaximas; i++) { |
24 | a[i * step] += 1.0f; |
25 | } |
26 | for (Tp i = nmaximas; i > 0; i--) { |
27 | Tp idx = 0; |
28 | float *b = a.data(); |
29 | #pragma omp target teams distribute parallel for reduction(max : idx) \ |
30 | map(always, to : b[0 : length]) |
31 | for (Tp j = 0; j < length - 1; j++) { |
32 | if (b[j] > b[j + 1]) { |
33 | idx = std::max(idx, j); |
34 | } |
35 | } |
36 | assert(idx == (i - 1) * step && |
37 | "#pragma omp target teams distribute parallel for " |
38 | "reduction(max:<identifier list>) does not work as intended." ); |
39 | a[idx] -= 1.0f; |
40 | } |
41 | } |
42 | |
43 | template <class Tp> void test_max_val_reduction() { |
44 | const int length = 1000; |
45 | const int half = length / 2; |
46 | std::vector<Tp> a(length, (Tp)3); |
47 | a[half] += (Tp)1; |
48 | Tp max_val = std::numeric_limits<Tp>::lowest(); |
49 | Tp *b = a.data(); |
50 | #pragma omp target teams distribute parallel for reduction(max : max_val) \ |
51 | map(always, to : b[0 : length]) |
52 | for (int i = 0; i < length; i++) { |
53 | max_val = std::max(max_val, b[i]); |
54 | } |
55 | assert(std::abs(((double)a[half + 1]) - ((double)max_val) + 1.0) < 1e-6 && |
56 | "#pragma omp target teams distribute parallel for " |
57 | "reduction(max:<identifier list>) does not work as intended." ); |
58 | } |
59 | |
60 | int main() { |
61 | // Reducing over indices |
62 | test_max_idx_reduction<int>(); |
63 | test_max_idx_reduction<unsigned int>(); |
64 | test_max_idx_reduction<long>(); |
65 | |
66 | // Reducing over values |
67 | test_max_val_reduction<int>(); |
68 | test_max_val_reduction<unsigned int>(); |
69 | test_max_val_reduction<long>(); |
70 | test_max_val_reduction<float>(); |
71 | test_max_val_reduction<double>(); |
72 | return 0; |
73 | } |
74 | |