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