文字字符串的Fortran宏函数?

时间:2015-07-20 18:21:03

标签: macros fortran

我需要对各种数据类型名称进行一系列子例程调用。例如:

print*, 'Now giving information about Matrix1'
call mysub(Matrix1, size(Matrix1,1), size(Matrix1,2))
print*, 'About to do function on Matrix1'
call dofunction(Matrix1)
print*, 'Matrix1 is a nice matrix! Huzzah!'

print*, 'Now giving information about Matrix2'
call mysub(Matrix2, size(Matrix2,1), size(Matrix2,2))
print*, 'About to do function on Matrix2'
call dofunction(Matrix2)
print*, 'Matrix2 is a nice matrix! Huzzah!'

print*, 'Now giving information about OtherMat'
call mysub(OtherMat, size(OtherMat,1), size(OtherMat,2))
print*, 'About to do function on OtherMat'
call dofunction(OtherMat)
print*, 'OtherMat is a nice matrix! Huzzah!'

我想定义一个宏#define,以便我可以包含这一系列的调用。例如,定义宏mymacro,我希望能够简单地调用

mymacro(Matrix1)
mymacro(Matrix2)
mymacro(OtherMat)

完成同样的事情。这可能吗?

3 个答案:

答案 0 :(得分:3)

由于它不支持documentation,我需要一段时间才能使其适用于gfortran

我们的想法是使用;将多个命令放入一行。当然,这需要禁用通常的行限制。使用gfortran,这可以通过-ffree-line-length-0来实现。

然后,gfortran的预处理器使用"x"而不是#s(正如其他编译器所做的那样)。 结果如下:它正在处理ifortgfortran

#ifdef __GFORTRAN__

#define mymacro(x) print *, "Now giving information about ", "x" ; \
                   call mysub( x, size(x,1), size(x,2) ) ; \
                   print *, "About to do function on ", "x"; \
                   call dofunction(x) ; \
                   print *, "x"," is a nice matrix! Huzzah!"

#else

#define mymacro(x) print *, "Now giving information about ", #x ; \
                   call mysub( x, size(x,1), size(x,2) ) ; \
                   print *, "About to do function on ", #x; \
                   call dofunction(x) ; \
                   print *, #x," is a nice matrix! Huzzah!"

#endif

program main

implicit none
integer :: i(2,2)

i = 123

mymacro(i)

contains

subroutine mysub(ii, N, M)
  integer,intent(in) :: ii(:,:), N, M
  print *,'in mysub:', ii(1,1)
end subroutine

subroutine dofunction(ii)
  integer,intent(in) :: ii(:,:)
  print *,'in dofunction:', ii(1,1)
end subroutine
end program main

答案 1 :(得分:2)

如何编写这样的子程序:

body

对于你来说,获得矩阵的名称并不是很接近但不够接近。

subroutine mymacro(matrix, matrixName)
    ! declare matrix, precision should be declare somewhere as a constant
    real(precision), dimension(:,:) :: matrix
    character(*), intent(in) :: matrixname

    print*, 'Now giving information about '//matrixname
    call mysub(matrix, size(matrix,1), size(matrix,2))
    print*, 'About to do function on '//matrixname
    call dofunction(matrix)
    print*, matrixname//' is a nice matrix! Huzzah!'
end subroutine mymacro

变为

mymacro(matrix)

没有什么不同,你不觉得吗?

答案 2 :(得分:0)

宏本身不属于Fortran。它们由大多数(如果不是全部)编译器通过重用C预处理器来实现。我从这里学到了: https://gcc.gnu.org/onlinedocs/cpp/Macros.html

这可能会起到作用:(未经过测试!)

#define MYSUB(m) write(*,*) "Now giving information about ", #m;  \
                 call mysub(m, size(m,1), size(m,2) ); \
                 write(*,*) "About to do function on ",#m; \
                 call dofunction(m) ; \
                 write(*,*) #m , "is a nice matrix! Huzzah!" ;

请注意,您必须使用适当的编译器标志才能让编译器运行预处理器。