fortran cudamalloc和cublassetmatrix包装器问题

时间:2019-02-22 05:47:34

标签: cuda fortran malloc

我可能是一个问题,可能是因为未正确地从fortran中调用“ cudamalloc”或“ cublassetmatrix”:

module cuda
 interface 
   integer (C_INT) function cudaMallocHost(buffer, size)  bind(C,name="cudaMallocHost")
     use iso_c_binding
     implicit none
     type (C_PTR)  :: buffer
     integer (C_SIZE_T), value :: size
   end function cudaMallocHost
   integer (C_INT) function cudaFreeHost(buffer)  bind(C,name="cudaFreeHost")
     use iso_c_binding
     implicit none
     type (C_PTR), value :: buffer
   end function cudaFreeHost
   integer (C_INT) function cudaMalloc(buffer, size)  bind(C,name="cudaMalloc")
     use iso_c_binding
     implicit none
     type (C_PTR)  :: buffer
     integer (C_SIZE_T), value :: size
   end function cudaMalloc
   integer (C_INT) function cudaFree(buffer)  bind(C,name="cudaFree")
     use iso_c_binding
     implicit none
     type (C_PTR), value :: buffer
   end function cudaFree
   integer (C_INT) function cublassetmatrix(M,N,size,A_host,lda_h&
     &,A_dev,lda_d)  bind(C,name="cublasSetMatrix")
     use iso_c_binding
     implicit none
     type (C_PTR) :: A_dev
     type(c_ptr) :: A_host
     integer (C_SIZE_T), value :: size
     integer(C_Int) :: M,N,lda_h,lda_d
   end function cublassetmatrix
   Type(c_ptr) function cudaGetErrorString(err) bind(C,name="cudaGetErrorString")
     use iso_c_binding
     implicit none
     integer (C_SIZE_T), value :: err
   end function cudaGetErrorString
   integer (C_INT) function cublasCreate(handle)  bind(C,name="cublasCreate_v2")
     use iso_c_binding
     implicit none
     Type(C_Ptr) :: handle
   end function cublasCreate
 end interface
end module cuda
program test
  use iso_c_binding
  use cuda
  implicit none
  integer, parameter :: fp_kind = kind(0.0)
  type(C_PTR) :: cptr_A, cptr_A_D
  real (fp_kind), dimension(:,:), pointer :: A=>null()
  real :: time_start,time_end
  integer:: i,j, res, m1
  integer(c_int) :: x
  type(c_ptr) :: handle
  logical:: lsexit
  CHARACTER(len=50), POINTER :: errchar
  m1=500
  res=cublasCreate(handle)
  if(res/=0) Then
    write(*,*) "ERROR 1 ",res;
  end if
  res = cudaMallocHost ( cptr_A, m1*m1*sizeof(fp_kind) )
  if(res/=0) Then
    write(*,*) "ERROR 2 ",res;
  end if
  call c_f_pointer ( cptr_A, A, (/ m1, m1 /) )
  A=1._fp_kind
  res = cudaMalloc ( cptr_A_D, m1*m1*sizeof(fp_kind) )
  if(res/=0) Then
    write(*,*) "ERROR 3 ",res;
  end if
  res=cublasSetMatrix (m1,m1,sizeof(fp_kind),cptr_A,m1,cptr_A_D,m1)
  if(res/=0) Then
    write(*,*) "ERROR 4 ",res,sizeof(fp_kind)
    call c_f_pointer ( cudageterrorstring(int(res,kind=8)),&
      & errchar, [ len(errchar) ] )
    write(*,*) trim(adjustl(errchar))
  end if
end program test

make命令是:

tmp:
    ifort  -O3 -o tmp $(FFLAGS) tmp.f90 -L/opt/cuda/lib64 -lcublas -lcudart
clean:
    rm tmp cuda.mod 

尽管“ cudamallochost”期望“ void ** ptr”,但它似乎可以使用,因为fortran指针可用,并且与“ cudafree”一起放入循环时不会产生内存泄漏。

该代码在“ cublassetmatrix”功能处失败,错误代码为7(“资源过多”)或11(“无效参数”)。

有什么主意吗?

0 个答案:

没有答案