OpenMP target (offloading) question

2021-03-31 Thread Harald Anlauf via Fortran
Dear experts,

sorry if this is a stupid question, but I was playing with offloading for
the nvptx-none target and found different behavior between e.g. gfortran-10
on OpenSuse and the Nvidia compiler (nvfortran) for the attached code.

With "nvfortran -mp=multicore offload-test.f90" the code prints:

2.002000.000
 s1:1001000.
 s2:1001000.

With "/usr/bin/gfortran-10 -fopenmp -foffload=nvptx-none offload-test.f90":

   2.   2000.0
 s1:   1001000.00
 s2:   0.

The core difference between the evaluations s1 and s2 is:

s1:

!$omp target data map(a,s)
!$omp target teams reduction(+:s) map(s)
do i = 1, n
   s = s + a(i)
end do
!$omp end target teams
!$omp end target data

s2:

!$omp target data map(a,s)
!$omp target teams reduction(+:s)
do i = 1, n
   s = s + a(i)
end do
!$omp end target teams
!$omp end target data

I was assuming that the map clause in the reduction should not be necessary,
but the result seems to tell me that either I am wrong (and gfortran is right),
or nvfortran is wrong.

With OpenACC this seems to be different; at least a simple example I tried
with the reduction within an !$acc data ... !$acc end data did not show
unexpected behavior.

Can anybody tell me that I am wrong (and point me to the right place in the
OpenMP standard), or should I open a PR?

Thanks
Harald
program p5
  implicit none
  integer   :: i, n = 1000
  real  :: s
  real, allocatable :: a(:)
  allocate (a(n))
  do i = 1, n
 a(i) = 2*i
  end do
  print *, a(1),a(n)
  call s1 ()
  print *, "s1:", s
  call s2 ()
  print *, "s2:", s
contains
  subroutine s1 ()
integer :: i
s = 0.
!$omp target data map(a,s)
!$omp target teams reduction(+:s) map(s)
do i = 1, n
   s = s + a(i)
end do
!$omp end target teams
!$omp end target data
  end subroutine s1
  !
  subroutine s2 ()
integer :: i
s = 0.
!$omp target data map(a,s)
!$omp target teams reduction(+:s)
do i = 1, n
   s = s + a(i)
end do
!$omp end target teams
!$omp end target data
  end subroutine s2
end program


[PATCH] PR fortran/99840 - [8/9/10/11 Regression] ICE in gfc_simplify_matmul, at fortran/simplify.c:4777

2021-03-31 Thread Harald Anlauf via Fortran
Dear all,

the simplification of the TRANSPOSE of a zero-sized array would lead to
an ICE if the result was used in a subsequent simplification of a MATMUL.
The reason was the lack of the proper initialization of the shape, which
is mpz_t.  Use mpz_init_set instead of mpz_set.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Since this is a regression, backport to all affected branches?

Thanks,
Harald


PR fortran/99840 - ICE in gfc_simplify_matmul, at fortran/simplify.c:4777

The simplification of the transposition of a constant array shall properly
initialize and set the shape of the result.

gcc/fortran/ChangeLog:

PR fortran/99840
* simplify.c (gfc_simplify_transpose): Properly initialize
resulting shape.

gcc/testsuite/ChangeLog:

PR fortran/99840
* gfortran.dg/transpose_5.f90: New test.

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 388aca7c38c..c27b47aa98f 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -8123,8 +8123,8 @@ gfc_simplify_transpose (gfc_expr *matrix)
 			   &matrix->where);
   result->rank = 2;
   result->shape = gfc_get_shape (result->rank);
-  mpz_set (result->shape[0], matrix->shape[1]);
-  mpz_set (result->shape[1], matrix->shape[0]);
+  mpz_init_set (result->shape[0], matrix->shape[1]);
+  mpz_init_set (result->shape[1], matrix->shape[0]);

   if (matrix->ts.type == BT_CHARACTER)
 result->ts.u.cl = matrix->ts.u.cl;
diff --git a/gcc/testsuite/gfortran.dg/transpose_5.f90 b/gcc/testsuite/gfortran.dg/transpose_5.f90
new file mode 100644
index 000..682b1c8552b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/transpose_5.f90
@@ -0,0 +1,8 @@
+! { dg-do compiler }
+! { dg-options "-O2" }
+! PR fortran/99840 - ICE in gfc_simplify_matmul, at fortran/simplify.c:4777
+program p
+  integer, parameter :: x(0,0) = 0
+  integer :: y(0,0)
+  y = matmul (x, transpose(x))
+end


Re: [PATCH] PR fortran/99840 - [8/9/10/11 Regression] ICE in gfc_simplify_matmul, at fortran/simplify.c:4777

2021-03-31 Thread Jerry DeLisle

Yes OK for trunk and affected branches.

Thanks,

Jerry

On 3/31/21 2:08 PM, Harald Anlauf via Fortran wrote:

Dear all,

the simplification of the TRANSPOSE of a zero-sized array would lead to
an ICE if the result was used in a subsequent simplification of a MATMUL.
The reason was the lack of the proper initialization of the shape, which
is mpz_t.  Use mpz_init_set instead of mpz_set.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Since this is a regression, backport to all affected branches?

Thanks,
Harald


PR fortran/99840 - ICE in gfc_simplify_matmul, at fortran/simplify.c:4777

The simplification of the transposition of a constant array shall properly
initialize and set the shape of the result.

gcc/fortran/ChangeLog:

PR fortran/99840
* simplify.c (gfc_simplify_transpose): Properly initialize
resulting shape.

gcc/testsuite/ChangeLog:

PR fortran/99840
* gfortran.dg/transpose_5.f90: New test.