2016-11-11 61 views
0

this thread後,我想將一個單/雙精度實數「AA」轉換爲一個整數「II」來計算分佈式變量的校驗和。Fortran和MPI_Reduce如何處理整數溢出?

以下評論,我已經使用內在的'轉移',並完全改寫這篇文章。下面是一個小Fortran模塊,可用於計算分佈式數組的校驗和,這取決於庫2DECOMP&FFT。該模塊似乎在我的工作站上工作(gfortran 4.9.2,openmpi 1.6.5,4個處理器)。任何意見/評論,可能會提高代碼的可移植性將不勝感激。關於可移植性的主要問題是:Fortran和MPI_reduce是否按照標準以相同的方式處理整數溢出?

module checksum 

    use MPI 
    use decomp_2d, only : mytype, nrank, & 
    xsize, ysize, zsize, & 
    transpose_x_to_y, transpose_y_to_z, & 
    transpose_z_to_y, transpose_y_to_x 

    implicit none 

    private ! Make everything private unless declared public 

    real(mytype), parameter :: xx=1. 

    integer, parameter, public :: chksum_size = size(transfer(xx,(/0,0/))) 

    integer, dimension(chksum_size) :: chkr1, chkr2, chkr3 

    logical, save :: chksum_is_working 

    ! Temporary work variables/arrays 
    integer :: code 
    integer, dimension(chksum_size) :: tmprchk 

    public :: init_chksum, chksum, equal_chksum 

    contains 

    ! 
    ! Function to compute the checksum of a real 3D array var 
    ! 
    function chksum(var,nx,ny,nz) 
     integer, intent(in) :: nx, ny, nz 
     real(mytype), dimension(nx,ny,nz), intent(in) :: var 
     integer, dimension(chksum_size) :: chksum 

     tmprchk = sum(transfer(var,(/0,0/))) 
     call MPI_ALLREDUCE(tmprchk,chksum,chksum_size,MPI_INTEGER,MPI_SUM,MPI_COMM_WORLD,code) 

    end function chksum 

    ! 
    ! Subroutine to make sure input arrays have the same checksum 
    ! First/second/third array are in X/Y/Z pencil 
    ! If switch is provided, reference array is var3. 
    ! Otherwise, reference array is var1 
    ! 
    subroutine equal_chksum(var1, var2, var3, switch) 
     real(mytype), dimension(xsize(1),xsize(2),xsize(3)), intent(inout) :: var1 
     real(mytype), dimension(ysize(1),ysize(2),ysize(3)), intent(inout) :: var2 
     real(mytype), dimension(zsize(1),zsize(2),zsize(3)), intent(inout) :: var3 
     logical, optional, intent(in) :: switch 

     if (chksum_is_working) then ! compute checksums 
     chkr1 = chksum(var1,xsize(1),xsize(2),xsize(3)) 
     chkr2 = chksum(var2,ysize(1),ysize(2),ysize(3)) 
     chkr3 = chksum(var3,zsize(1),zsize(2),zsize(3)) 
     else ! generate checksums 
     chkr1 = 1 
     chkr2 = 2 
     chkr3 = 3 
     endif 

     if (present(switch)) then 
     if (any(chkr3.ne.chkr2)) call transpose_z_to_y(var3,var2) 
     if (any(chkr3.ne.chkr1)) call transpose_y_to_x(var2,var1) 
     else 
     if (any(chkr1.ne.chkr2)) call transpose_x_to_y(var1,var2) 
     if (any(chkr1.ne.chkr3)) call transpose_y_to_z(var2,var3) 
     endif 

    end subroutine equal_chksum 

    ! 
    ! Subroutine used to check we have a working checksum 
    ! 
    subroutine init_chksum(var1,var2,var3) 
     real(mytype), dimension(xsize(1),xsize(2),xsize(3)), intent(out) :: var1 
     real(mytype), dimension(ysize(1),ysize(2),ysize(3)), intent(out) :: var2 
     real(mytype), dimension(zsize(1),zsize(2),zsize(3)), intent(out) :: var3 

     ! Same random data inside all arrays 
     call random_number(var1) 
     call transpose_x_to_y(var1,var2) 
     call transpose_y_to_z(var2,var3) 

     ! Compute checksums 
     chkr1 = chksum(var1,xsize(1),xsize(2),xsize(3)) 
     chkr2 = chksum(var2,ysize(1),ysize(2),ysize(3)) 
     chkr3 = chksum(var3,zsize(1),zsize(2),zsize(3)) 

     ! Check checksums 
     if (any(chkr1.ne.chkr2).or.any(chkr1.ne.chkr3)) then 
     chksum_is_working = .false. 
     if (nrank.eq.0) print *,'Checksums based on integer overflow do not work' 
     else 
     chksum_is_working = .true. 
     endif 

    end subroutine init_chksum 

end module checksum 
+1

你是什麼意思的「演員」?你想使用相同的位模式而不是數值(例如'transfer')? – francescalus

+0

@francescalus謝謝,我不知道轉讓,這正是我需要的。它是標準/便攜式嗎? – user1824346

+1

你應該明白* cast *這個詞是不明確的,你只需解釋你對一些代碼做了什麼。例如在C++中有reinterpret_cast,dynamic_cast,static_cast和更多http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used我們無法從水晶球上看出你的意思。 –

回答

1

按標準做Fortran和MPI_reduce處理整數溢出以同樣的方式?

Fortran標準和MPI 3.0標準都沒有提到整數溢出,所以你受到實現者的擺佈。

但是,我看到您只使用默認種類的整數,對於中間結果使用較大的整數類型,您可以實現自己的溢出檢測。

1

整數溢出未由Fortran標準定義。在C中有符號整數溢出是未定義的行爲

如果您在gfortran中啓用未定義的行爲santizations,您的程序將停止並顯示錯誤消息。 (當我使用第三方隨機數發生器時發生在我身上)

您可以使用較大的整數執行操作並裁剪結果或調用使用無符號整數的C函數。整數溢出僅爲有符號整數定義好。