Skip to content

OMP003 collapse-imperfect-nest ​

error enabled by default

collapse(n) on OpenMP loops that are not tightly nested.

Why ​

collapse(n) merges the iteration spaces of n nested loops into one. The specification requires the loops to be tightly nested: no statement between the loop headers or between the end dos. Compilers reject some violations and mishandle others (the extra statements executed once per collapsed iteration, or not at all). Move the statements inside the innermost loop, or collapse fewer levels.

Reported ​

fortran
!$omp parallel do collapse(2)
do j = 1, m
  c = coef(j)          ! between the loop headers
  do i = 1, n
    a(i, j) = c * b(i, j)
  end do
end do

Preferred ​

fortran
!$omp parallel do collapse(2)

    do j = 1, m
      do i = 1, n
        a(i, j) = coef(j) * b(i, j)
      end do
    end do

Suppressing ​

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

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

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

References ​

Sections of the shipped knowledge packs behind this rule:

  • openmp:ch06-base-language#anti-patterns (fobis kb show openmp:ch06-base-language#anti-patterns)