public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/109767] New: [OpenMP][5.2] Missing loop-variable privatization inside 'teams'
@ 2023-05-08  9:10 burnus at gcc dot gnu.org
  2023-05-08 14:27 ` [Bug middle-end/109767] " jakub at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: burnus at gcc dot gnu.org @ 2023-05-08  9:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109767

            Bug ID: 109767
           Summary: [OpenMP][5.2] Missing loop-variable privatization
                    inside 'teams'
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Keywords: openmp, wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
  Target Milestone: ---

Cf. https://github.com/SOLLVE/sollve_vv/issues/731

OpenMP 5.1 had:
* "Loop iteration variables inside 'parallel' or task generating constructs are
private in the innermost such construct that encloses the loop."

OpenMP 5.2 added 'teams':
* "Loop iteration variables inside 'parallel', 'teams', or task generating
constructs are private in the innermost such construct that encloses the loop."


In GCC, the 'j' is shared, causing race issues. Testcases:

==> teams-loop-var.c <==
#define N 10
int
main ()
{
  int x[N][N], y[N][N];
  int j;

  #pragma omp teams  // in GCC implied: shared(j)  <<< (!) >>>
                     // [but 'private(j) with gfortran]
    for (int i = 0; i < N; i++)
      for (j = 0; j < N; j++)
        // TR12: #pragma omp atomic write
        x[i][j] = 2*j;

  #pragma omp teams   // in GCC implied: shared(j)  <<< (!) >>>
                      // [but 'private(j) with gfortran]
    #pragma omp loop  // in GCC implied: omp parallel shared(j)  <<< (!) >>>
    for (int i = 0; i < N; i++)
      for (j = 0; j < N; j++)
        // TR12: #pragma omp atomic write
        y[i][j] = j + x[i][j];

  for (int i = 0; i < N; i++)
    for (j = 0; j < N; j++)
      if (y[i][j] != j + 2*j)
        __builtin_abort ();
  return 0;
}


==> teams-loop-var.f90 <==
implicit none
integer, parameter :: N = 10
integer :: x(N,N), y(N,N)
integer :: i, j

!$omp teams  ! in GCC implied: private(j)  [but shared(j) with gcc/g++]
  do i = 1, N
    do j = 1, N
      ! TR12: !$omp atomic write
      x(j,i) = 2*j
    end do
  end do
!$omp end teams

!$omp teams   ! in GCC implied: private(j)  [but shared(j) with gcc/g++]
  !$omp loop  ! gcc generated: omp parallel shared(j) <<< (!) >>>
  do i = 1, N
    do j = 1, N
      ! TR12: !$omp atomic write
      y(j,i) = j + x(j, i)
    end do
  end do
!$omp end teams

do i = 1, N
  do j = 1, N
    if (y(j,i) /= j + 2*j)  &
      stop 1
  end do
end do
end

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/109767] [OpenMP][5.2] Missing loop-variable privatization inside 'teams'
  2023-05-08  9:10 [Bug middle-end/109767] New: [OpenMP][5.2] Missing loop-variable privatization inside 'teams' burnus at gcc dot gnu.org
@ 2023-05-08 14:27 ` jakub at gcc dot gnu.org
  2023-05-08 14:41 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-08 14:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109767

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Note, it is a Fortran only rule.
So, shared(j) on teams-loop-var.c is fully correct.
And, looking at Fortran, I already see it being private (by fortran/openmp.cc
(gfc_resolve_do_iterator)).
Thus, I don't see anything that needs to be done here.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/109767] [OpenMP][5.2] Missing loop-variable privatization inside 'teams'
  2023-05-08  9:10 [Bug middle-end/109767] New: [OpenMP][5.2] Missing loop-variable privatization inside 'teams' burnus at gcc dot gnu.org
  2023-05-08 14:27 ` [Bug middle-end/109767] " jakub at gcc dot gnu.org
@ 2023-05-08 14:41 ` jakub at gcc dot gnu.org
  2023-05-08 15:20 ` burnus at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-08 14:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109767

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Even
subroutine foo
  integer :: i, j
  !$omp parallel
  do i = 1, 10
    do j = 1, 10
    end do
  end do
  !$omp end parallel
end subroutine
subroutine bar
  integer :: i, j
  !$omp teams
  do i = 1, 10
    do j = 1, 10
    end do
  end do
  !$omp end teams
end subroutine
subroutine baz
  integer :: i, j
  !$omp parallel
  !$omp loop bind(parallel)
  do i = 1, 10
    do j = 1, 10
    end do
  end do
  !$omp end parallel
end subroutine
subroutine qux
  integer :: i, j
  !$omp teams
  !$omp loop bind(teams)
  do i = 1, 10
    do j = 1, 10
    end do
  end do
  !$omp end teams
end subroutine
has
grep private pr109767.f90.005t.original 
  #pragma omp parallel private(i) private(j)
  #pragma omp teams private(i) private(j)
  #pragma omp parallel private(j)
  #pragma omp teams private(j)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/109767] [OpenMP][5.2] Missing loop-variable privatization inside 'teams'
  2023-05-08  9:10 [Bug middle-end/109767] New: [OpenMP][5.2] Missing loop-variable privatization inside 'teams' burnus at gcc dot gnu.org
  2023-05-08 14:27 ` [Bug middle-end/109767] " jakub at gcc dot gnu.org
  2023-05-08 14:41 ` jakub at gcc dot gnu.org
@ 2023-05-08 15:20 ` burnus at gcc dot gnu.org
  2023-05-08 15:29 ` jakub at gcc dot gnu.org
  2023-05-08 15:51 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: burnus at gcc dot gnu.org @ 2023-05-08 15:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109767

--- Comment #3 from Tobias Burnus <burnus at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #1)
> Note, it is a Fortran only rule.
> So, shared(j) on teams-loop-var.c is fully correct.
Ups. True.

> And, looking at Fortran, I already see it being private (by
> fortran/openmp.cc (gfc_resolve_do_iterator)).
> Thus, I don't see anything that needs to be done here.

The problem is that for:

!$omp teams
  !$omp loop

the code GCC generates is:

#pragma omp teams private(j) shared(i)
#pragma omp distribute bind(teams) order(concurrent) private(i.2)
lastprivate(i)
#pragma omp parallel shared(j) lastprivate(i)
#pragma omp for bind(teams) order(concurrent) private(i.1)
#pragma omp simd lastprivate(i) bind(teams) order(concurrent)

and there is a 'shared(j)' for parallel and no later 'private(j)' on either
'for' nor 'simd'. Thus, it is shared. - Which breaks the code.


The problem is that PRIVATE happens early during parsing (→ search for
gfc_resolve_do_iterator in openmp.cc) – but PARALLEL is only added in the
middle end (→ gimplify_omp_loop).

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/109767] [OpenMP][5.2] Missing loop-variable privatization inside 'teams'
  2023-05-08  9:10 [Bug middle-end/109767] New: [OpenMP][5.2] Missing loop-variable privatization inside 'teams' burnus at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-05-08 15:20 ` burnus at gcc dot gnu.org
@ 2023-05-08 15:29 ` jakub at gcc dot gnu.org
  2023-05-08 15:51 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-08 15:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109767

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Ah, ok, I see what do you mean.
The thing is that the implementation of loop bind(teams) as distribute parallel
do simd
is effectively just an implementation detail, and per the spec j is not private
in the
loop construct, but private on teams.
I think we need to discuss this in lang committee then (what behavior is for
int i, j;
#pragma omp teams private (j)
{
  #pragma omp loop bind(teams)
  for (i = 0; i < 16; ++i)
    for (j = 0; j < 16; ++j)
      ;
}
).
Obviously, if private (j) is on omp loop rather than omp teams, it has to work
as expected.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/109767] [OpenMP][5.2] Missing loop-variable privatization inside 'teams'
  2023-05-08  9:10 [Bug middle-end/109767] New: [OpenMP][5.2] Missing loop-variable privatization inside 'teams' burnus at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-05-08 15:29 ` jakub at gcc dot gnu.org
@ 2023-05-08 15:51 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-08 15:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109767

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Or even
#pragma omp teams
{
  int i, j;
  #pragma omp loop bind(teams)
  for (i = 0; i < 16; ++i)
    for (j = 0; j < 16; ++j)
      ;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-05-08 15:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-08  9:10 [Bug middle-end/109767] New: [OpenMP][5.2] Missing loop-variable privatization inside 'teams' burnus at gcc dot gnu.org
2023-05-08 14:27 ` [Bug middle-end/109767] " jakub at gcc dot gnu.org
2023-05-08 14:41 ` jakub at gcc dot gnu.org
2023-05-08 15:20 ` burnus at gcc dot gnu.org
2023-05-08 15:29 ` jakub at gcc dot gnu.org
2023-05-08 15:51 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).