You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用gfortran编译cuBLAS示例时遇expected right parenthesis错误求助

解决Fortran 77调用cuBLAS时IDX2F宏编译错误的问题

问题背景

我尝试编译NVIDIA cuBLAS文档中的Fortran 77示例程序,通过NVIDIA提供的Fortran绑定从Fortran应用中调用cuBLAS函数,使用C风格宏IDX2F实现CUDA设备指针的索引运算。

完整程序代码

! Example B.2. Same Application Using Non-thunking cuBLAS Calls
!-------------------------------------------------------------#define IDX2F (i,j,ld) ((((j)-1)*(ld))+((i)-1))
      subroutine modify ( devPtrM, ldm, n, p, q, alpha, beta )
      implicit none
      integer sizeof_real
      parameter (sizeof_real=4)
      integer ldm, n, p, q
#if ARCH_64
      integer*8 devPtrM
#else
      integer*4 devPtrM
#endif
      real*4 alpha, beta
      call cublas_sscal ( n-p+1, alpha,
     1                   devPtrM+IDX2F(p, q, ldm)*sizeof_real,
     2                   ldm)
      call cublas_sscal(ldm-p+1, beta,
     1                 devPtrM+IDX2F(p, q, ldm)*sizeof_real,
     2                 1)
      return
      end
      program matrixmod
      implicit none
      integer M,N,sizeof_real
#if ARCH_64
      integer*8 devPtrA
#else
      integer*4 devPtrA
#endif
      parameter(M=6,N=5,sizeof_real=4)
      real*4 a(M,N)
      integer i,j,stat
      external cublas_init, cublas_set_matrix, cublas_get_matrix
      external cublas_shutdown, cublas_alloc
      integer cublas_alloc, cublas_set_matrix, cublas_get_matrix
      do j=1,N
          do i=1,M
              a(i,j)=(i-1)*M+j
          enddo
      enddo
      call cublas_init
      stat= cublas_alloc(M*N, sizeof_real, devPtrA)
      if (stat.NE.0) then
          write(*,*) "device memory allocation failed"
          call cublas_shutdown
          stop
      endif
      stat = cublas_set_matrix(M,N,sizeof_real,a,M,devPtrA,M)
      if (stat.NE.0) then
          call cublas_free( devPtrA )
          write(*,*) "data download failed"
          call cublas_shutdown
          stop
      endif

编译错误信息

使用gfortran 8.5.0,添加-cpp选项编译时,出现以下错误:

cuBLAStest1NonThunking.f:16:33:
     1                   devPtrM+IDX2F(p, q, ldm)*sizeof_real,
                                  1
Error: Expected a right parenthesis at (1)

以及

cuBLAStest1NonThunking.f:19:33:
     1                   devPtrM+IDX2F(p, q, ldm)*sizeof_real,
                                  1
Error: Expected a right parenthesis at (1)

错误原因

gfortran的预处理器对C风格宏的定义语法敏感:

  1. 原宏定义#define IDX2F (i,j,ld)中,宏名IDX2F和参数列表(i,j,ld)之间有空格,预处理器会将IDX2F识别为无参数宏,后面的(i,j,ld)被当作普通文本,导致宏展开时语法结构混乱。
  2. 宏定义和前一行注释连在一起,可能导致预处理器解析错误。

解决方案

1. 修正IDX2F宏定义

将宏定义中的空格去掉,并让宏定义单独占一行:

! Example B.2. Same Application Using Non-thunking cuBLAS Calls
!-------------------------------------------------------------
#define IDX2F(i,j,ld) ((((j)-1)*(ld))+((i)-1))

2. 补充未声明的函数

程序中调用了cublas_free但未声明,需要在matrixmod程序的external声明中添加:

external cublas_init, cublas_set_matrix, cublas_get_matrix
external cublas_shutdown, cublas_alloc, cublas_free

3. 完整编译命令

如果是64位系统,编译时需要定义ARCH_64宏,并链接cuBLAS库:

gfortran -cpp -DARCH_64 cuBLAStest1NonThunking.f -lcublas -o cublas_test

内容的提问来源于stack exchange,提问作者Brassard1984

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 22:13:18