在Fortran中使用MPI从文件中读取数据

时间:2017-10-06 20:42:05

标签: fortran mpi mpi-io

我想将一些.dat文件中的数据读取到Fortran代码进行后处理。作为测试用例,我只是使用一个处理器用于MPI并尝试将单个数据文件读取到我的代码中。数据文件的内容如下:

qout0050.dat : 1 1 1

然而,应该存储该数据文件内容的矩阵(在这种情况下为Vn)显示全部0值。从数据文件读取并存储到矩阵的代码的相关部分如下:

subroutine postproc()

use precision_mod
use mpicomms_mod
implicit none


integer(kind=MPI_Offset_kind)   :: i, j , igrid, k, l, disp, iproc, info, lwork
integer                         :: rst, numvar, ifile, number, num, step, ntot
integer                         :: Nx_max, Ny_max, Nz_max
integer                         :: Nxp, Nzp, Nyp, Ngrid
integer                         :: Ifirst, Ilast, Jfirst, Jlast, Kfirst, Klast
character*(64)                  :: fname, buffer, ffname
integer                         :: tmp, N1, N2, N3, tmpp
real(WP), allocatable           :: qout(:,:,:,:), phi_xyz(:,:,:,:,:)
real(WP), allocatable           :: Vn(:,:), Vntmp(:,:), TAU(:,:)
real(WP), allocatable           :: Rprime(:,:), Rtmp(:,:), Rend(:,:), Q(:,:), tmpL(:), tmpG(:)
real(WP), allocatable           :: s(:), vt(:,:), u(:,:), utmp(:,:)
real(WP), allocatable           :: tmp1(:,:), tmp2(:,:), tmp3(:,:), phi(:,:)
integer, dimension(2)           :: view
integer                         :: view1
integer, dimension(3)           :: lsizes, gsizes, start
real(WP)                        :: tmpr
real(WP), allocatable           :: work(:), mu(:), eigY(:,:), wr(:), wi(:), beta(:)
integer(kind=MPI_Offset_kind)   :: SP_MOK, Nx_MOK, Ny_MOK, Nz_MOK, WP_MOK



open(unit=110,file='postparameters.dat',form="formatted")
read (110,*) Nx_max
read (110,*) Ny_max
read (110,*) Nz_max
read (110,*) numvar
read (110,*) step
close(110)


! Define the size of grid on each processor
if (mod(Nx_max,px).ne.0) then
   write(*,*) 'Error in preproc: Nx_max is not devisable by px'
   call MPI_ABORT(MPI_COMM_WORLD,0,ierr)
end if

Nxp = Nx_max/px

if (mod(Ny_max,py).ne.0) then
   write(*,*) 'Error in preproc: Nx_max is not devisable by px'
   call MPI_ABORT(MPI_COMM_WORLD,0,ierr)
end if

Nyp = Ny_max/py

if (mod(Nz_max,pz).ne.0) then
   write(*,*) 'Error in preproc: Nx_max is not devisable by px'
   call MPI_ABORT(MPI_COMM_WORLD,0,ierr)
end if

Nzp = Nz_max/pz

Ifirst = irank*Nxp + 1
Ilast  = Ifirst + Nxp - 1

Jfirst = jrank*Nyp + 1
Jlast  = Jfirst + Nyp - 1

Kfirst = krank*Nzp + 1
Klast  = Kfirst + Nzp - 1




! Setting the view for phi
gsizes(1)  = Nx_max 
gsizes(2)  = Ny_max
gsizes(3)  = Nz_max
lsizes(1)  = Nxp
lsizes(2)  = Nyp
lsizes(3)  = Nzp
start(1)   = Ifirst - 1 
start(2)   = Jfirst - 1 
start(3)   = Kfirst - 1 

call MPI_TYPE_CREATE_SUBARRAY(3,gsizes,lsizes,start,& 
    MPI_ORDER_FORTRAN,MPI_REAL_SP,view,ierr)

call MPI_TYPE_COMMIT(view,ierr)

call MPI_TYPE_CREATE_SUBARRAY(3,gsizes,lsizes,start,&
   MPI_ORDER_FORTRAN,MPI_REAL_WP,view1,ierr)

call MPI_TYPE_COMMIT(view1,ierr)


WP_MOK     = int(8,               MPI_Offset_kind)
Nx_MOK     = int(Nx_max,          MPI_Offset_kind) 
Ny_MOK     = int(Ny_max,          MPI_Offset_kind)
Nz_MOK     = int(Nz_max,          MPI_Offset_kind)

! Reading the qout file
ffname   = 'qout'

allocate(qout(Nxp,Nyp,Nzp,numvar))
allocate(Vn(Nxp*Nyp*Nzp*numvar,step))

do rst = 1,step
   if (myrank == 0) print*, 'Step = ',  50 + rst -1

   write(buffer,"(i4.4)") 50 + rst -1
   fname = trim(ffname)//trim(buffer)
   fname = trim('ufs')//":"// trim(fname)
   fname = trim(adjustl(fname))//'.dat'

   call MPI_FILE_OPEN(MPI_COMM_WORLD,fname,MPI_MODE_RDONLY,MPI_INFO_NULL,ifile,ierr)

   call MPI_FILE_READ(ifile,Ngrid,1,MPI_INTEGER,status,ierr)
   if (1 /= Ngrid)   then
      if (myrank == 0 )   write(*,*) Ngrid
   endif

   call MPI_FILE_READ(ifile,tmp,1,MPI_INTEGER,status,ierr)
   if (tmp /= Nx_max) write(*,*) tmp

   call MPI_FILE_READ(ifile,tmp,1,MPI_INTEGER,status,ierr)
   if (tmp /= Ny_max) write(*,*) tmp

   call MPI_FILE_READ(ifile,tmp,1,MPI_INTEGER,status,ierr)
   if (tmp /= Nz_max) write(*,*) tmp

   call MPI_FILE_READ(ifile,tmpr,1,MPI_REAL_WP,status,ierr)
   call MPI_FILE_READ(ifile,tmpr,1,MPI_REAL_WP,status,ierr)
   call MPI_FILE_READ(ifile,tmpr,1,MPI_REAL_WP,status,ierr)
   call MPI_FILE_READ(ifile,tmpr,1,MPI_REAL_WP,status,ierr)

   do l=1,numvar
      disp = 4*4 + 4*WP_MOK + Nx_MOK*Ny_MOK*Nz_MOK*WP_MOK*(l-1)
      call MPI_FILE_SET_VIEW(ifile,disp,MPI_REAL_WP,view1,"native",MPI_INFO_NULL,ierr)
      call MPI_FILE_READ_ALL(ifile,qout(1:Nxp,1:Nyp,1:Nzp,l),Nxp*Nzp*Nyp, MPI_REAL_WP,status,ierr)
   end do
   call MPI_FILE_CLOSE(ifile,ierr)

 !-----------------------------------------------
 ! Bluiding the snapshot matrix Vn --------------
 !-----------------------------------------------

   do i=1,numvar
      do k=1,Nzp
         do j=1,Nyp
            Vn((1 + Nxp*(j-1) + Nxp*Nyp*(k-1) + Nxp*Nyp*Nzp*(i-1)):(Nxp*j + Nxp*Nyp*(k-1) + Nxp*Nyp*Nzp*(i-1)),rst) = qout(1:Nxp,j,k,i)
         end do
      end do
   end do

end do
call MPI_BARRIER(MPI_COMM_WORLD,ierr)     

deallocate(qout)

0 个答案:

没有答案