<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54216>54216</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            OpenMP Offload: Race condition when mapping produce incorect values
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          TApplencourt
      </td>
    </tr>
</table>

<pre>
    Hi,

The code below gives incorrect results:

```
#include <iostream>
#include <cstdlib>
#include <algorithm>
bool almost_equal(float x, float gold, float rel_tol=1e-09, float abs_tol=0.0) {
  return std::abs(x-gold) <= std::max(rel_tol * std::max(std::abs(x), std::abs(gold)), abs_tol);
}
void test_parallel_for__target() {
  const int N0 { 32768 };
  const float expected_value { N0 };
  float counter_N0{};
  #pragma omp parallel for
  for (int i0 = 0 ; i0 < N0 ; i0++ )
  {
    #pragma omp target map(tofrom: counter_N0)
    {
      #pragma omp atomic update
      counter_N0 = counter_N0 + 1. ;
    }
  }
  if (!almost_equal(counter_N0, expected_value, 0.01)) {
    std::cerr << "Expected: " << expected_value << " Got: " << counter_N0 << std::endl;
    std::exit(112);
  }
}
int main()
{
    test_parallel_for__target();
}
```

By the OpenMP spec, the code is race free. I don't know if I can reproduce here the email exchange that happened between Michael Kruse and Bronis de Supinski in the OpenMP Mail list (Title of the email thread `[Omp-lang] Race conditions with target mapping tasks`), but the gist is:
- In OpenMP `map` are considered to be performed atomicaly
- Hence, no race condition in the code, just an undefined amount of data copy

Hope this help,
Don't hesitate if you have other question

PS: Because this code is a race condition, sometime it work, just tests it until it fail :) 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyFVtuOozgQ_RryUkpETNLpPOQhl5nt1qqnRzv9HjlgwNMGM7bpy9_vsQkJYXc0EiK2q3zqdqrISWefmwcZsX0UH6J4271fSkGpzgSdhNLvVMg3YUnWqTZGp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