43 lines · cpp
1// RUN: %libomptarget-compilexx-and-run-generic2 3// REQUIRES: gpu4 5#include <cmath>6#include <cstdlib>7#include <iostream>8 9bool almost_equal(float x, float gold, float tol) {10 if (std::signbit(x) != std::signbit(gold))11 x = std::abs(gold) - std::abs(x);12 13 return std::abs(gold) * (1 - tol) <= std::abs(x) &&14 std::abs(x) <= std::abs(gold) * (1 + tol);15}16 17int main(int argc, char *argv[]) {18 constexpr const int N0{2};19 constexpr const int N1{182};20 constexpr const float expected_value{N0 * N1};21 float counter_N0{};22 23#pragma omp target data map(tofrom : counter_N0)24 {25#pragma omp taskloop shared(counter_N0)26 for (int i0 = 0; i0 < N0; i0++) {27#pragma omp target teams distribute parallel for map(tofrom : counter_N0) nowait28 for (int i1 = 0; i1 < N1; i1++) {29#pragma omp atomic update30 counter_N0 = counter_N0 + 1.;31 }32 }33 }34 35 if (!almost_equal(counter_N0, expected_value, 0.1)) {36 std::cerr << "Expected: " << expected_value << " Got: " << counter_N037 << '\n';38 return -1;39 }40 41 return 0;42}43