xref: /llvm-project/offload/test/mapping/map_back_race.cpp (revision 330d8983d25d08580fc1642fea48b2473f47a9da)
1 // RUN: %libomptarget-compilexx-and-run-generic
2 
3 // Taken from https://github.com/llvm/llvm-project/issues/54216
4 
5 #include <algorithm>
6 #include <cstdlib>
7 #include <iostream>
8 
almost_equal(float x,float gold,float rel_tol=1e-09,float abs_tol=0.0)9 bool almost_equal(float x, float gold, float rel_tol = 1e-09,
10                   float abs_tol = 0.0) {
11   return std::abs(x - gold) <=
12          std::max(rel_tol * std::max(std::abs(x), std::abs(gold)), abs_tol);
13 }
test_parallel_for__target()14 void test_parallel_for__target() {
15   const int N0{32768};
16   const float expected_value{N0};
17   float counter_N0{};
18 #pragma omp parallel for
19   for (int i0 = 0; i0 < N0; i0++) {
20 #pragma omp target map(tofrom : counter_N0)
21     {
22 #pragma omp atomic update
23       counter_N0 = counter_N0 + 1.;
24     }
25   }
26   if (!almost_equal(counter_N0, expected_value, 0.01)) {
27     std::cerr << "Expected: " << expected_value << " Got: " << counter_N0
28               << std::endl;
29     std::exit(112);
30   }
31 }
main()32 int main() { test_parallel_for__target(); }
33