如何在Fortran中使用多GPU cuFFT库?

时间:2019-06-19 01:26:57

标签: cuda fortran fortran-iso-c-binding cufft

我从固有的cufft模块中找不到任何多GPU功能,因此我编写了一个C-Fortran接口。我正在使用PGI编译器v19.4和MVAPICH2。我使用以下命令来编译和运行代码,但是它给出了错误代码1:CUFFT_INVALID_PLAN。请帮忙。

mpif90 -Mcuda -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lcufft testxt.cuf -o testxt
mpirun -np 4 ./testxt
program testxt

   use cudafor
   use cufft
   use mpi 
   use iso_c_binding
   implicit none
   interface
      function cufftXtSetGPUs &
         (plan, nGPUs, whichGPUs) bind(C,name='cufftXtSetGPUs')
         use iso_c_binding
         integer(c_int)        :: plan
         integer(c_int), value :: nGPUs
         integer(c_int)        :: whichGPUs(*)
         integer(c_int), value :: cufftXtSetGPUs
      end function
   end interface
   integer :: localRank, rank, nProcs, ierr, i, N
   integer(c_int) :: plan
   integer, allocatable :: whichGPUs(:)
   character(len=10) :: rankStr

   ! MPI initialization
   call GET_ENVIRONMENT_VARIABLE ('MV2_COMM_WORLD_LOCAL_RANK',rankStr)
   read (rankStr,'(i10)') localRank
   call CHECK_STAT ( cudaSetDevice (localRank) )
   call MPI_init(ierr)
   call MPI_comm_rank(MPI_COMM_WORLD, rank, ierr)
   call MPI_comm_size(MPI_COMM_WORLD, nProcs, ierr)

   ! Device ID
   allocate(whichGPUs(nProcs))
   do i=1,nProcs
      whichGPUs(i)=i
   enddo

   ! Setup cufft
   call CHECK_STAT ( cufftCreate (plan) )
   call CHECK_STAT ( cufftXtSetGPUs (plan,nProcs,whichGPUs) )
   call CHECK_STAT ( cufftPlan1D (plan,N,CUFFT_Z2Z,1) )

   deallocate(whichGPUs)
   call MPI_finalize(ierr)

end program testxt

subroutine CHECK_STAT (istat)
   use cudafor
   implicit none
   integer, intent(in) :: istat
   if (istat /= cudaSuccess) then
      write(0,'(A,I2,A)',advance='no') " Error code ",istat,": "
      write(*,*) cudaGetErrorString(istat)
      stop
   endif
end subroutine CHECK_STAT

0 个答案:

没有答案