【问题标题】:Undefined references to cublas functions using ifort (cuBLAS Fortran Bindings)使用 ifort 对 cublas 函数的未定义引用(cuBLAS Fortran 绑定)
【发布时间】:2014-05-03 16:32:17
【问题描述】:

我有一个从上一个问题here 中提供的示例 cuBLAS Fortran 绑定例程。我正在运行 Ubuntu 13.10、IFORT 14.0.1 和 Cuda 5.5。代码如下:

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

当我使用 gfortran 编译时,我得到正确的输出,如下所示:

编译运行

$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     

但是,我需要将 Intel Fortran 编译器与 cuBLAS 库一起使用,但是当我从 gfortran 切换到 ifort 时,会得到各种未定义的引用,如下所示:

$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_'

我看过其他一些关于这个问题的帖子,但没有真正的解决方案/解释。如果有人能解释为什么这些引用在使用 ifort 而不是在使用 gfortran 时未定义,以及解决该问题的任何解决方案,我将不胜感激。提前非常感谢您!

【问题讨论】:

  • 需要注意的重要一点是在 ifort 发出的代码中符号末尾附加的 _。可能有一种方法可以控制 ifort 中的符号修改。我不知道那是什么。否则你可能需要使用 ISO_C_BINDING 机制
  • Linux ifort 默认使用单个下划线 (-assume underscore -- 请阅读手册页了解详细信息。如何将库放在首位?如 ifort -o cublasf -L/usr/local/cuda/lib64 -lcublas cublasf.o fortran.o

标签: cuda fortran intel-fortran cublas


【解决方案1】:

Intel Fortran documentation states, that

在 Linux 和 OS X 系统上,编译器会附加一个下划线 字符转换为外部用户定义的名称。

你需要禁用它

ifort -assume nounderscore ...

还要考虑names 的情况

这是 Linux* 和 OS X* 系统的默认设置。编译器会忽略标识符的大小写差异,并将外部名称转换为小写。

所以你可能还需要

-names uppercase

【讨论】:

  • 我尝试使用 ifort -assme nounderscore ... 并得到与上述相同的错误,但函数调用末尾没有下划线。如果有任何帮助,我正在尝试访问/usr/local/cuda/src/fortran_common.h 中的函数包装器。它似乎包含我在#elif defined(CUBLAS_INTEL_FORTRAN) 标记下需要的函数包装器,但是它似乎正在通过我得到的输出访问#if defined(CUBLAS_GFORTRAN) || defined(CUBLAS_G95) 下的包装器。谢谢
  • 什么给你 nm -D /usr/local/cuda/lib64/libcublas.so | grep "cublas_init" ?
  • 我运行它时什么也没得到
  • 是在 /usr/local/cuda/src/fortran.c 中定义的 cublas_init 吗?
  • 是的。 int CUBLAS_INIT (void) { return (int)cublasInit (); }
猜你喜欢
  • 2014-04-11
  • 2014-02-05
  • 2013-09-20
  • 2015-05-08
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
相关资源
最近更新 更多