Skip to content

OMP001 shared-scalar-written-in-parallel-region ​

warning enabled by default

A shared scalar is assigned inside an OpenMP parallel region: a data race.

Why ​

Inside a parallel region every variable of the enclosing procedure is shared unless a clause says otherwise, so a scalar temporary assigned in the loop body is written by all threads at once: each thread may read another thread's value. The loop variables of the region's DO loops are private automatically (Fortran rule), nothing else is. List the variable in private (or use default(none) to make the compiler ask for every variable), or protect the write with critical/atomic when the sharing is intended. Offload regions are no exception: in target teams distribute parallel do, an unmapped scalar is firstprivate to the target region, and that single copy is shared by all the threads of its teams. Compilers often privatise such scalars as an optimisation, which hides the race until a compiler or a flag changes. Writing the same value from every thread (n = omp_get_num_threads()) is a race too: use single.

Reported ​

fortran
!$omp parallel do
do i = 1, n
  tmp = a(i) * 2      ! tmp is shared: race
  b(i) = tmp + 1
end do

Preferred ​

fortran
!$omp parallel do private(tmp)
do i = 1, n
  tmp = a(i) * 2
  b(i) = tmp + 1
end do

Suppressing ​

For one occurrence, with the reason on the same line (or alone on the line before the statement):

fortran
! fobis: ignore[OMP001] <why this occurrence is fine>

For the whole project, ignore = OMP001 in the fobos [lint] section.

References ​

Sections of the shipped knowledge packs behind this rule:

  • openmp:ch07-data-environment#key-concepts (fobis kb show openmp:ch07-data-environment#key-concepts)
  • openmp:overview#two-attribute-systems-ch-7-never-conflate (fobis kb show openmp:overview#two-attribute-systems-ch-7-never-conflate)