2015-06-24 68 views
1

派生類型對象我已經定義了一個名爲「圖表」類型:發送和接收使用MPI

Type :: chart 
    Character(len=32) :: title 
    Character(len=32) :: xAxis 
    Character(len=32) :: yAxis 
    Real(kind=DoubleReal) :: xMin=1.1D99 
    Real(kind=DoubleReal) :: xMax=-1.1D99 
    Real(kind=DoubleReal) :: yMin=1.1D99 
    Real(kind=DoubleReal) :: yMax=-1.1D99 
    Logical :: cleanPyFile=.true. 
End Type 

我想知道如何從一個過程發送一個用戶定義的變量到另一個使用的Fortran與MPI庫。

+1

你可以發佈你試過的東西嗎? –

+0

嗨,謝謝你的回覆。我希望能夠編寫一個子例程來將0類型圖表的變量(例如)從0進程分發到所有其他進程。我計劃這樣做的方式是循環遍歷圖表變量的各個部分,並分別發送每個部分。我還沒有嘗試過其他任何東西。 – benpalmer

+0

嗨。我有興趣知道是否有其他原因循環遍歷定義類型的元素的方法,但是我也想知道如何使用MPI子例程在Fortran中發送和接收定義的數據類型。 – benpalmer

回答

1

謝謝你的回覆和建議閱讀。我查看了C++主題,並且還找到了子例程open-mpi.org以及如何使用它們computing.llnl.gov的信息。

我剛剛把一個子程序這就是我寫程序的一部分,這是一個有點周圍的邊緣粗糙,但它的工作原理:

Module typesM 

    ! Setup Modules 
     Use kinds 
    ! Force declaration of all variables 
     Implicit None 
    ! Include MPI header 
     Include 'mpif.h' 
    ! Declare global variables 
    ! Privacy of functions/subroutines/variables 
     Private 
    ! Public subroutines 
     Public :: SetUpMPITypes 

     Contains 

     Subroutine SetUpMPITypes() 
     Implicit None ! Force declaration of all variables 
    ! Private variables 
     Integer(kind=StandardInteger) :: error 
     Integer(kind=StandardInteger) :: iExtent, rExtent, dpExtent 
     Integer(kind=StandardInteger) :: elementTypes(0:1), blockCounts(0:1), blockOffset(0:1) 
     Integer(kind=StandardInteger) :: rssConfigID 
     Integer(kind=StandardInteger) :: mpiProcessCount, mpiProcessID 
     Integer(kind=StandardInteger) :: i, processTo, processFrom, tag 
     Integer, Dimension(MPI_STATUS_SIZE) :: status 
    ! Define my rssConfig type in Fortran 
     Type :: rssConfig 
      sequence 
      Real(kind=DoubleReal) :: total=0.0D0 
      Real(kind=DoubleReal) :: energy=0.0D0 
      Real(kind=DoubleReal) :: force=0.0D0 
      Real(kind=DoubleReal) :: stress=0.0D0 
      Integer(kind=StandardInteger) :: n 
     End Type rssConfig 
    ! Declare rssTest as rssConfig type 
     Type (rssConfig) :: rssTest 
    ! Extent of data types 
     Call MPI_TYPE_EXTENT(MPI_INTEGER, iExtent, error) 
     Call MPI_TYPE_EXTENT(MPI_REAL, rExtent, error) 
     Call MPI_TYPE_EXTENT(MPI_DOUBLE_PRECISION, dpExtent, error)  
    ! Block 1 
     blockCounts(0) = 4 
     blockOffset(0) = 0 
     elementTypes(0) = MPI_DOUBLE_PRECISION 
    ! Block 2 
     blockCounts(1) = 1 
     blockOffset(1) = 4 * dpExtent 
     elementTypes(1) = MPI_INTEGER 
    ! Make mpi structure 
     call MPI_TYPE_STRUCT(2, blockCounts, blockOffset, elementTypes, rssConfigID, error) 
     call MPI_TYPE_COMMIT(rssConfigID, error) 
    ! Get process ID and total mpi process count 
     Call MPI_Comm_size(MPI_COMM_WORLD ,mpiProcessCount,error) 
     Call MPI_Comm_rank(MPI_COMM_WORLD,mpiProcessID,error) 
    ! Set values on the root process  
     If(mpiProcessID.eq.0)Then 
      rssTest%total = 8.1D0 
     End If 
    ! wait and print out  
     call sleep(1) 
     print *,mpiProcessID,rssTest%total 
    ! Send from root to workers using rssConfig type 
     If(mpiProcessID.eq.0)Then 
      Do i=1,(mpiProcessCount-1) 
      processTo = i 
      tag = 114 + i 
      Call MPI_SEND(rssTest,1,rssConfigID,processTo,tag,MPI_COMM_WORLD,error) 
      End Do 
     End If 
    ! Recieve by worker processes from root process 
     If(mpiProcessID.gt.0)Then 
      processFrom = 0 
      tag = 114 + mpiProcessID 
      Call MPI_RECV(rssTest,1,rssConfigID,processFrom,tag,MPI_COMM_WORLD,status,error) 
     End If 
    ! wait and printout 
     call sleep(1) 
     print *,mpiProcessID,rssTest%total 
     End Subroutine SetUpMPITypes 

    End Module typesM 

謝謝你的建議。我將在未來嘗試使用MPI_TYPE_CREATE_STRUCT子例程,但是我還沒有完成它的工作。