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 minimum work
11// as indended for a few common data types.
12
13#include <algorithm>
14#include <cassert>
15#include <limits>
16#include <vector>
17
18template <class Tp> void test_min_idx_reduction() {
19 const Tp length = 1000;
20 const Tp nminimas = 8;
21 std::vector<float> a(length, 3.0f);
22 const Tp step = length / nminimas;
23 for (Tp i = 0; i < nminimas; i++) {
24 a[i * step] -= 1.0f;
25 }
26 for (Tp i = 0; i < nminimas; i++) {
27 Tp idx = a.size();
28 float *b = a.data();
29#pragma omp target teams distribute parallel for reduction(min : 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::min(idx, j);
34 }
35 }
36 assert(idx == i * step &&
37 "#pragma omp target teams distribute parallel for "
38 "reduction(min:<identifier list>) does not work as intended.");
39 a[idx] += 1.0f;
40 }
41}
42
43template <class Tp> void test_min_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 min_val = std::numeric_limits<Tp>::max();
49 Tp *b = a.data();
50#pragma omp target teams distribute parallel for reduction(min : min_val) \
51 map(always, to : b[0 : length])
52 for (int i = 0; i < length; i++) {
53 min_val = std::min(min_val, b[i]);
54 }
55 assert(std::abs(((double)a[half + 1]) - ((double)min_val) - 1.0) < 1e-6 &&
56 "#pragma omp target teams distribute parallel for "
57 "reduction(min:<identifier list>) does not work as intended.");
58}
59
60int main() {
61 // Reducing over indices
62 test_min_idx_reduction<int>();
63 test_min_idx_reduction<unsigned int>();
64 test_min_idx_reduction<long>();
65
66 // Reducing over values
67 test_min_val_reduction<int>();
68 test_min_val_reduction<unsigned int>();
69 test_min_val_reduction<long>();
70 test_min_val_reduction<float>();
71 test_min_val_reduction<double>();
72 return 0;
73}
74

source code of offload/test/offloading/parallel_target_teams_reduction_min.cpp