[flang-commits] [flang] [flang][OpenMP][RFC] Add support for COPYPRIVATE (PR #73128)

Leandro Lupori via flang-commits flang-commits at lists.llvm.org
Mon Nov 27 05:57:41 PST 2023


luporl wrote:

> I am a bit rusty with OpenMP, but how do you ensure that the sync variable is shared by all the threads when this is a single in a subprogram without a parallel?
> 
> I would expect the alloca for the buffer to be "private" to each threads that entered the subprogram from a call in some parallel region (so unusable to broadcast the value to all threads).
> 
> e.g: do all threads print `1` in the program below?
> 
> ```
> subroutine foo(i)
>  !$omp single
>  i = 1
>  !$omp end single copyprivate(i)
> end subroutine
> 
>   integer, save :: i = 0
>   !$omp threadprivate(i)
>   !$omp parallel
>   call foo(i)
>   !$omp critical
>   print *, i
>   !$omp end critical 
>   !$omp end parallel
> end
> ```
> 
This program doesn't compile:
```
test.f90:4:31: error: COPYPRIVATE variable 'i' is not PRIVATE or THREADPRIVATE in outer context
   !$omp end single copyprivate(i)
```

Making `i` threadprivate results in:
```
test.f90:2:22: error: A variable that appears in a THREADPRIVATE directive must be declared in the scope of a module or have the SAVE attribute, either explicitly or implicitly
   !$omp threadprivate(i)
```

And it is not possible to add the SAVE attribute to a dummy argument. But the following program shows the issue you are mentioning:
```
program main
  implicit none
  integer, save :: i = 0
  !$omp threadprivate(i)

  !$omp parallel
  call foo
  !$omp critical
  print *, i
  !$omp end critical
  !$omp end parallel

contains
  subroutine foo()
    !$omp single
    i = 1
    !$omp end single copyprivate(i)
  end subroutine
end program
```

Only one thread prints 1, all the others print 0. In this case the sync variable will indeed end up in a "private" thread stack, instead of the "shared" stack. So with this approach a global sync variable would be needed instead. Thanks for catching this.

https://github.com/llvm/llvm-project/pull/73128


More information about the flang-commits mailing list