Undefined references to cublas functions using ifort (cuBLAS Fortran Bindings)

谁说胖子不能爱 提交于 2020-02-05 07:29:05

问题


I have a sample cuBLAS Fortran binding routine provided from a previous question here. I'm running Ubuntu 13.10, IFORT 14.0.1, and Cuda 5.5. The code is below:

cublas.f

      program cublas_fortran_example
      implicit none
      integer i, j

c     helper functions
      integer cublas_init
      integer cublas_shutdown
      integer cublas_alloc
      integer cublas_free
      integer cublas_set_vector
      integer cublas_get_vector
c     selected blas functions
      double precision cublas_ddot
      external cublas_daxpy
      external cublas_dscal
      external cublas_dcopy
      double precision cublas_dnrm2
c     cublas variables
      integer cublas_status
      real*8 x(30), y(30)
      double precision alpha, beta
      double precision nrm
      integer*8 d_x, d_y, d_alpha, d_beta, d_nrm
      integer*8 dsize1, dlength1, dlength2
      double precision dresult



      write(*,*) "testing cublas fortran example"

c     initialize cublas library
c     CUBLAS_STATUS_SUCCESS=0
      cublas_status = cublas_init()
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS Library initialization failed"
         write(*,*) "cublas_status=",cublas_status
         stop
      endif
c     initialize data
      do j=1,30
        x(j) = 1.0
        y(j) = 2.0
      enddo
      dsize1 = 8
      dlength1 = 30
      dlength2 = 1
      alpha = 2.0
      beta = 3.0
c     allocate device storage
      cublas_status = cublas_alloc(dlength1, dsize1, d_x)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS device malloc failed"
         stop
      endif
      cublas_status = cublas_alloc(dlength1, dsize1, d_y)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS device malloc failed"
         stop
      endif
      cublas_status = cublas_alloc(dlength2, dsize1, d_alpha)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS device malloc failed"
         stop
      endif
      cublas_status = cublas_alloc(dlength2, dsize1, d_beta)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS device malloc failed"
         stop
      endif
      cublas_status = cublas_alloc(dlength2, dsize1, d_nrm)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS device malloc failed"
         stop
      endif

c     copy data from host to device

      cublas_status = cublas_set_vector(dlength1, dsize1, x, dlength2,
     >     d_x, dlength2)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS copy to device failed"
         write(*,*) "cublas_status=",cublas_status
         stop
      endif
      cublas_status = cublas_set_vector(dlength1, dsize1, y, dlength2,
     >     d_y, dlength2)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS copy to device failed"
         write(*,*) "cublas_status=",cublas_status
         stop
      endif

      dresult = cublas_ddot(dlength1, d_x, dlength2, d_y, dlength2)
      write(*,*) "dot product result=",dresult

      dresult = cublas_dnrm2(dlength1, d_x, dlength2)
      write(*,*) "nrm2 of x result=",dresult

      dresult = cublas_dnrm2(dlength1, d_y, dlength2)
      write(*,*) "nrm2 of y result=",dresult

      call cublas_daxpy(dlength1, alpha, d_x, dlength2, d_y, dlength2)
      cublas_status = cublas_get_vector(dlength1, dsize1, d_y, dlength2,
     >     y, dlength2)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS copy to host failed"
         write(*,*) "cublas_status=",cublas_status
         stop
      endif
      write(*,*) "daxpy y(1)  =", y(1)
      write(*,*) "daxpy y(30) =", y(30)


      call cublas_dscal(dlength1, beta, d_x, dlength2)
      cublas_status = cublas_get_vector(dlength1, dsize1, d_x, dlength2,
     >     x, dlength2)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS copy to host failed"
         write(*,*) "cublas_status=",cublas_status
         stop
      endif
      write(*,*) "dscal x(1)  =", x(1)
      write(*,*) "dscal x(30) =", x(30)


      call cublas_dcopy(dlength1, d_x, dlength2, d_y, dlength2)
      cublas_status = cublas_get_vector(dlength1, dsize1, d_y, dlength2,
     >     y, dlength2)
      if (cublas_status /= 0) then
         write(*,*) "CUBLAS copy to host failed"
         write(*,*) "cublas_status=",cublas_status
         stop
      endif
      write(*,*) "dcopy y(1)  =", y(1)
      write(*,*) "dcopy y(30) =", y(30)

c     deallocate GPU memory and exit
      cublas_status = cublas_free(d_x)
      cublas_status = cublas_free(d_y)
      cublas_status = cublas_free(d_alpha)
      cublas_status = cublas_free(d_beta)
      cublas_status = cublas_free(d_nrm)
      cublas_status = cublas_shutdown()
      stop
      end

When I compile using gfortran, I get the correct output as show below:

compile and run

$gfortran -c -o cublasf.o cublasf.f
$nvcc -c -DCUBLAS_GFORTRAN -I/usr/local/cuda/include -I/usr/local/cuda/src -o fortran.o /usr/local/cuda/src/fortran.c
$gfortran -o cublasf cublasf.o fortran.o -L/usr/local/cuda/lib64 -lcublas
$ ./cublasf 
 testing cublas fortran example
 dot product result=   60.000000000000000     
 nrm2 of x result=   5.4772255750516612     
 nrm2 of y result=   10.954451150103322     
 daxpy y(1)  =   4.0000000000000000     
 daxpy y(30) =   4.0000000000000000     
 dscal x(1)  =   3.0000000000000000     
 dscal x(30) =   3.0000000000000000     
 dcopy y(1)  =   3.0000000000000000     
 dcopy y(30) =   3.0000000000000000     

However, I need to use the Intel Fortran compiler with the cuBLAS library, but when I switch from gfortran to ifort, I get a variety of undefined references, as show below:

$ifort -c -o cublasf.o cublasf.f
$nvcc -c -DCUBLAS_INTEL_FORTRAN -I/usr/local/cuda/include -I/usr/local/cuda/src -o fortran.o /usr/local/cuda/src/fortran.c
$ifort -o cublasf cublasf.o fortran.o -L/usr/local/cuda/lib64 -lcublas
cublasf.o: In function `MAIN__':
cublasf.f:(.text+0x8a): undefined reference to `cublas_init_'
cublasf.f:(.text+0x1f5): undefined reference to `cublas_alloc_'
cublasf.f:(.text+0x21c): undefined reference to `cublas_alloc_'
cublasf.f:(.text+0x243): undefined reference to `cublas_alloc_'
cublasf.f:(.text+0x26a): undefined reference to `cublas_alloc_'
cublasf.f:(.text+0x291): undefined reference to `cublas_alloc_'
cublasf.f:(.text+0x2c8): undefined reference to `cublas_set_vector_'
cublasf.f:(.text+0x3d1): undefined reference to `cublas_set_vector_'
cublasf.f:(.text+0x4d5): undefined reference to `cublas_ddot_'
cublasf.f:(.text+0x56f): undefined reference to `cublas_dnrm2_'
cublasf.f:(.text+0x609): undefined reference to `cublas_dnrm2_'
cublasf.f:(.text+0x6b6): undefined reference to `cublas_daxpy_'
cublasf.f:(.text+0x6e6): undefined reference to `cublas_get_vector_'
cublasf.f:(.text+0x8c5): undefined reference to `cublas_dscal_'
cublasf.f:(.text+0x8f5): undefined reference to `cublas_get_vector_'
cublasf.f:(.text+0xad7): undefined reference to `cublas_dcopy_'
cublasf.f:(.text+0xb07): undefined reference to `cublas_get_vector_'
cublasf.f:(.text+0xcce): undefined reference to `cublas_free_'
cublasf.f:(.text+0xcdd): undefined reference to `cublas_free_'
cublasf.f:(.text+0xcec): undefined reference to `cublas_free_'
cublasf.f:(.text+0xcfb): undefined reference to `cublas_free_'
cublasf.f:(.text+0xd0a): undefined reference to `cublas_free_'
cublasf.f:(.text+0xd11): undefined reference to `cublas_shutdown_'

I've seen some other posts regarding this same issue but with no real solution/explanation. If anyone could explain why these references are undefined when using ifort and not when using gfortran, along with any solutions to fixing the issue, I would greatly appreciate it. Thank you so much in advance!


回答1:


Intel Fortran documentation states, that

On Linux and OS X systems, the compiler appends an underscore character to external user-defined names.

You need to disable it by

ifort -assume nounderscore ...

Alse consider names case

This is the default on Linux* and OS X* systems. The compiler ignores case differences in identifiers and converts external names to lowercase.

So you would maybe need also

-names uppercase


来源:https://stackoverflow.com/questions/22641241/undefined-references-to-cublas-functions-using-ifort-cublas-fortran-bindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!