71 lines · cpp
1// RUN: %libomptarget-compilexx-and-run-generic2// RUN: %libomptarget-compileoptxx-and-run-generic3 4// FIXME: This is a bug in host offload, this should run fine.5// REQUIRES: gpu6 7// This test validates that the OpenMP target reductions to find a minimum work8// as intended for a few common data types.9 10#include <algorithm>11#include <cassert>12#include <limits>13#include <vector>14 15template <class Tp> void test_min_idx_reduction() {16 const Tp length = 1000;17 const Tp nminimas = 8;18 std::vector<float> a(length, 3.0f);19 const Tp step = length / nminimas;20 for (Tp i = 0; i < nminimas; i++) {21 a[i * step] -= 1.0f;22 }23 for (Tp i = 0; i < nminimas; i++) {24 Tp idx = a.size();25 float *b = a.data();26#pragma omp target teams distribute parallel for reduction(min : 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::min(idx, j);31 }32 }33 assert(idx == i * step &&34 "#pragma omp target teams distribute parallel for "35 "reduction(min:<identifier list>) does not work as intended.");36 a[idx] += 1.0f;37 }38}39 40template <class Tp> void test_min_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 min_val = std::numeric_limits<Tp>::max();46 Tp *b = a.data();47#pragma omp target teams distribute parallel for reduction(min : min_val) \48 map(always, to : b[0 : length])49 for (int i = 0; i < length; i++) {50 min_val = std::min(min_val, b[i]);51 }52 assert(std::abs(((double)a[half + 1]) - ((double)min_val) - 1.0) < 1e-6 &&53 "#pragma omp target teams distribute parallel for "54 "reduction(min:<identifier list>) does not work as intended.");55}56 57int main() {58 // Reducing over indices59 test_min_idx_reduction<int>();60 test_min_idx_reduction<unsigned int>();61 test_min_idx_reduction<long>();62 63 // Reducing over values64 test_min_val_reduction<int>();65 test_min_val_reduction<unsigned int>();66 test_min_val_reduction<long>();67 test_min_val_reduction<float>();68 test_min_val_reduction<double>();69 return 0;70}71