claw-project / claw-compiler

CLAW Compiler for Performance Portability
https://claw-project.github.io
BSD 2-Clause "Simplified" License
40 stars 15 forks source link

"column caching" directive alters results of computation #596

Open FrostyMike opened 3 years ago

FrostyMike commented 3 years ago

Description of the issue in few words.

Original code

!
! This file is released under terms of BSD license
! See LICENSE file for more information
!
! Simple program to test the kcache directive
!

PROGRAM claw_test
  INTEGER :: istart = 1
  INTEGER :: iend = 10
  INTEGER :: jstart = 1
  INTEGER :: jend = 20
  CALL kcache(istart,iend,jstart,jend)
END PROGRAM claw_test

SUBROUTINE kcache(istart,iend,jstart,jend)
  INTEGER, INTENT(IN) :: istart, iend, jstart, jend
  INTEGER :: i,j
  REAL(KIND=8), DIMENSION(istart:iend,jstart:jend) :: array6, array7, array8, &
                                                      array9, array10
  array6(:, :) = 0.0;
  array7(:, :) = 0.0;
  array8(:, :) = 0.0;
  array9(:, :) = 0.0;
  array10(:, :) = 0.0;

  DO i = istart, iend
    array6(i,1) = 1.0
    array7(i,1) = 2.0
    array8(i,1) = 3.0
    array9(i,1) = 4.0
    array10(i,1) = 4.0
  END DO

  DO i = istart, iend
    DO j = jstart+1, jend
      !$claw kcache data(array6, array7, array8, array9) offset(0,-1)
      array6(i,j) = array6(i,j) * 2.0
      array7(i,j) = array7(i,j) * 2.0 + array6(i,j-1)
      array8(i,j) = array8(i,j) * 2.0 + array6(i,j-1) + array7(i,j-1)
      array9(i,j) = array9(i,j) * 2.0 + array6(i,j-1) + array8(i,j-1)
      array10(i,j) = array9(i,j-1) + 1.0
    END DO
  END DO
  PRINT*, SUM(array6)
  PRINT*, SUM(array7)
  PRINT*, SUM(array8)
  PRINT*, SUM(array9)
END SUBROUTINE kcache

Transformation code

PROGRAM claw_test
  INTEGER :: istart = 1
  INTEGER :: iend = 10
  INTEGER :: jstart = 1
  INTEGER :: jend = 20
  CALL kcache(istart,iend,jstart,jend)
END PROGRAM claw_test

SUBROUTINE kcache ( istart , iend , jstart , jend )

 INTEGER , INTENT(IN) :: istart
 INTEGER , INTENT(IN) :: iend
 INTEGER , INTENT(IN) :: jstart
 INTEGER , INTENT(IN) :: jend
 INTEGER :: i
 INTEGER :: j
 REAL ( KIND= 8 ) :: array6 ( istart : iend , jstart : jend )
 REAL ( KIND= 8 ) :: array7 ( istart : iend , jstart : jend )
 REAL ( KIND= 8 ) :: array8 ( istart : iend , jstart : jend )
 REAL ( KIND= 8 ) :: array9 ( istart : iend , jstart : jend )
 REAL ( KIND= 8 ) :: array10 ( istart : iend , jstart : jend )
 REAL ( KIND= 8 ) :: array6_k_m1
 REAL ( KIND= 8 ) :: array7_k_m1
 REAL ( KIND= 8 ) :: array8_k_m1
 REAL ( KIND= 8 ) :: array9_k_m1

 array6 ( : , : ) = 0.0
 array7 ( : , : ) = 0.0
 array8 ( : , : ) = 0.0
 array9 ( : , : ) = 0.0
 array10 ( : , : ) = 0.0
 DO i = istart , iend , 1
  array6 ( i , 1 ) = 1.0
  array7 ( i , 1 ) = 2.0
  array8 ( i , 1 ) = 3.0
  array9 ( i , 1 ) = 4.0
  array10 ( i , 1 ) = 4.0
 END DO
 DO i = istart , iend , 1
  DO j = jstart + 1 , jend , 1
   array6_k_m1 = array6 ( i , j ) * 2.0
   array6 ( i , j ) = array6_k_m1
   array7_k_m1 = array7 ( i , j ) * 2.0 + array6_k_m1
   array7 ( i , j ) = array7_k_m1
   array8_k_m1 = array8 ( i , j ) * 2.0 + array6_k_m1 + array7_k_m1
   array8 ( i , j ) = array8_k_m1
   array9_k_m1 = array9 ( i , j ) * 2.0 + array6_k_m1 + array8_k_m1
   array9 ( i , j ) = array9_k_m1
   array10 ( i , j ) = array9_k_m1 + 1.0
  END DO
 END DO
 PRINT * , sum ( array6 )
 PRINT * , sum ( array7 )
 PRINT * , sum ( array8 )
 PRINT * , sum ( array9 )
END SUBROUTINE kcache

Original output

   10.000000000000000     
   30.000000000000000     
   70.000000000000000     
   120.00000000000000

Transformed output

   10.000000000000000     
   20.000000000000000     
   30.000000000000000     
   40.000000000000000
clementval commented 3 years ago

The test is obviously wrong. I guess it's a good idea to enable the output diff on this test and probably the directive is not placed correctly. The cached value should probably be updated later in the loop.

The directive !$claw kcache data(array6, array7, array8, array9) offset(0,-1) init should probably be placed after array9(i,j) = array9(i,j) * 2.0 + array6(i,j-1) + array8(i,j-1).