标签: gfortran

在 gfortran 中预处理 !DEC$ 指令

我有一个最初针对英特尔编译器的大型 Fortran 代码库。我现在正准备用 gfortran 进行编译。不幸的是,代码中充斥着 Intel 风格的预处理指令,例如:

!DEC$ IF DEFINED (MYDIRECTIVE)
   REAL, DIMENSION(:,:,:), ALLOCATABLE :: my_real_var
!DEC$ ENDIF
Run Code Online (Sandbox Code Playgroud)

从我通过谷歌搜索和 gfortran 文档可以看出,除了 C 风格的预处理之外,没有内部 gfortran 支持,例如:

#if defined MYDIRECTIVE
   REAL, DIMENSION(:,:,:), ALLOCATABLE :: my_real_var
#endif
Run Code Online (Sandbox Code Playgroud)

有没有其他人遇到过这个问题并提出了一个优雅的解决方案?显然,我可以编写一个 shell 脚本,在将代码传递给 gfortran 进行编译之前调用外部预处理器,但这对我来说似乎不是一个很好的解决方案。

有什么想法吗?谢谢大师!

preprocessor fortran gfortran intel-fortran

5
推荐指数
1
解决办法
3747
查看次数

在 Fortran 中捕获整数异常

有没有办法捕获整数异常,gfortran或者ifort像捕获浮点异常一样?

考虑这个简单的程序来计算阶乘:

program factorial
  use, intrinsic :: iso_fortran_env 

  implicit none
  integer(8)          :: fac
  real(REAL64)        :: facR
  integer,parameter   :: maxOrder = 30
  integer             :: i

  fac = 1 ; facR = 1.e0_REAL64
  do i=2,maxOrder
    fac=fac*i ; facR=facR*real(i,REAL64)
    write(*,*) i, fac, facR
  enddo ! i

end program
Run Code Online (Sandbox Code Playgroud)

在某些时候会出现溢出 -integer(8)如此处所示,它将发生在 21 左右。但是如果没有使用浮点数作为参考的计算,我无法确定......

fortran integer-overflow gfortran intel-fortran

5
推荐指数
1
解决办法
1387
查看次数

编译Trilinos源代码时,汇编程序收到垃圾错误消息

我正在从源代码编译Trilinos并得到一个错误,它抱怨垃圾部分.我的内核版本3.13.0和我的g ++版本是4.8.2.我在x86_64机器上运行.错误消息如下所示:

scalar_field.s:24649: Error: junk `@104.5037' after expression
Run Code Online (Sandbox Code Playgroud)

命令运行是

cd /home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/ForTrilinos/example/ADT_3D_Burgers_6th_Pade && /usr/bin/gfortran   -ffree-line-length-none -g -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD -I/home/shuang/trilinos-12.0.1-Source/packages/ForTrilinos/src/epetra -I/home/shuang/trilinos-12.0.1-Source/packages/ForTrilinos/src/teuchos -I/home/shuang/trilinos-12.0.1-Source/packages/ForTrilinos/src/amesos -I/home/shuang/trilinos-12.0.1-Source/packages/ForTrilinos/src/aztecoo -I/home/shuang/trilinos-12.0.1-Source/packages/ForTrilinos/src/galeri -I/home/shuang/trilinos-12.0.1-Source/packages/ForTrilinos/src/ifpack -I/home/shuang/trilinos-12.0.1-Source/packages/ForTrilinos/src/pliris -I/home/shuang/trilinos-12.0.1-Source -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/ForTrilinos/src -I/home/shuang/trilinos-12.0.1-Source/packages/ForTrilinos/src -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/ForTrilinos/src/mod -I/home/shuang/trilinos-12.0.1-Source/packages/CTrilinos/src/epetra -I/home/shuang/trilinos-12.0.1-Source/packages/CTrilinos/src/teuchos -I/home/shuang/trilinos-12.0.1-Source/packages/CTrilinos/src/amesos -I/home/shuang/trilinos-12.0.1-Source/packages/CTrilinos/src/aztecoo -I/home/shuang/trilinos-12.0.1-Source/packages/CTrilinos/src/galeri -I/home/shuang/trilinos-12.0.1-Source/packages/CTrilinos/src/ifpack -I/home/shuang/trilinos-12.0.1-Source/packages/CTrilinos/src/pliris -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/CTrilinos/src -I/home/shuang/trilinos-12.0.1-Source/packages/CTrilinos/src -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/ifpack/src -I/home/shuang/trilinos-12.0.1-Source/packages/ifpack/src -I/home/shuang/trilinos-12.0.1-Source/packages/ifpack/../amesos/src/SuiteSparse/AMD/Include -I/home/shuang/trilinos-12.0.1-Source/packages/ifpack/../amesos/src/SuiteSparse/UFconfig -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/amesos/src -I/home/shuang/trilinos-12.0.1-Source/packages/amesos/src -I/home/shuang/trilinos-12.0.1-Source/packages/amesos/src/SuiteSparse/AMD/Include -I/home/shuang/trilinos-12.0.1-Source/packages/amesos/src/SuiteSparse/COLAMD/Include -I/home/shuang/trilinos-12.0.1-Source/packages/amesos/src/SuiteSparse/CHOLMOD/Include -I/home/shuang/trilinos-12.0.1-Source/packages/amesos/src/SuiteSparse/UFconfig -I/home/shuang/trilinos-12.0.1-Source/packages/amesos/src/SuiteSparse/KLU/Include -I/home/shuang/trilinos-12.0.1-Source/packages/amesos/src/SuiteSparse/BTF/Include -I/home/shuang/trilinos-12.0.1-Source/packages/amesos/src/SuiteSparse/CAMD/Include -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/epetraext/src -I/home/shuang/trilinos-12.0.1-Source/packages/epetraext/src -I/home/shuang/trilinos-12.0.1-Source/packages/epetraext/src/transform -I/home/shuang/trilinos-12.0.1-Source/packages/epetraext/src/inout -I/home/shuang/trilinos-12.0.1-Source/packages/epetraext/src/coloring -I/home/shuang/trilinos-12.0.1-Source/packages/epetraext/src/model_evaluator -I/home/shuang/trilinos-12.0.1-Source/packages/epetraext/src/block -I/home/shuang/trilinos-12.0.1-Source/packages/epetraext/src/restrict -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/triutils/src -I/home/shuang/trilinos-12.0.1-Source/packages/triutils/src -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/epetra/src -I/home/shuang/trilinos-12.0.1-Source/packages/epetra/src -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/teuchos/kokkoscomm/src -I/home/shuang/trilinos-12.0.1-Source/packages/teuchos/kokkoscomm/src -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/teuchos/kokkoscompat/src -I/home/shuang/trilinos-12.0.1-Source/packages/teuchos/kokkoscompat/src -I/home/shuang/trilinos-12.0.1-Source/packages/teuchos/parameterlist/src -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/teuchos/core/src -I/home/shuang/trilinos-12.0.1-Source/packages/teuchos/core/src -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/kokkos/core/src -I/home/shuang/trilinos-12.0.1-Source/packages/kokkos/core/src -I/usr/include -I/home/shuang/trilinos-12.0.1-Source/packages/teuchos/comm/src -I/home/shuang/trilinos-12.0.1-Source/packages/teuchos/remainder/src -I/home/shuang/trilinos-12.0.1-Source/packages/teuchos/numerics/src -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/aztecoo/src -I/home/shuang/trilinos-12.0.1-Source/packages/aztecoo/src -I/home/shuang/trilinos-12.0.1-Source/packages/galeri/src-xpetra -I/home/shuang/trilinos-12.0.1-Source/MY_BUILD/packages/galeri/src-xpetra/../src -I/home/shuang/trilinos-12.0.1-Source/packages/galeri/src-xpetra/../src/Utils -I/home/shuang/trilinos-12.0.1-Source/packages/galeri/src-xpetra/../src/Headers …
Run Code Online (Sandbox Code Playgroud)

assembly gcc gnu gfortran trilinos

5
推荐指数
0
解决办法
408
查看次数

将 gfortran 链接到 LAPACK 和 BLAS 时出错

我已经从 Ubuntu 中的 Synaptic 包管理器安装了 LAPACK 和 BLAS。

whereis libblas
libblas: /usr/lib/libblas.so /usr/lib/libblas.a /usr/lib/libblas

whereis liblapack
liblapack: /usr/lib/liblapack.a /usr/lib/liblapack.so
Run Code Online (Sandbox Code Playgroud)

当我尝试编译randomsys1 示例时gfortran我收到以下错误消息。

gfortran randomsys1.f90 -L/usr/lib/lapack -llapack -L/usr/lib/libblas -lblas
/tmp/cclwtifh.o: In function `MAIN__':
randomsys1.f90:(.text+0x12): undefined reference to `init_random_seed_'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

或者

gfortran randomsys1.f90 -llapack -lblas
/tmp/ccB1isEC.o: In function `MAIN__':
randomsys1.f90:(.text+0x12): undefined reference to `init_random_seed_'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

根据我的理解,这是gfortranlapackand链接的推荐方式blas(请参考gfortran LAPACK “undefined reference” error …

fortran gfortran blas lapack

5
推荐指数
1
解决办法
8454
查看次数

无法运行 fortran 调试器。视觉工作室代码

我正在尝试在 Visual Studio 代码(ubuntu 18.04)上调试 fortran 文件。

我安装了以下扩展

在此处输入图片说明

我的launch.json文件如下

"version": "0.0.1",
"configurations": [
    {
        "name": "Fortran Launch (GDB)",
        "type": "cppdbg",
        "request": "launch",
        "targetArchitecture": "x86",
        "program": "${workspaceRoot}/./a.out",
        "miDebuggerPath": "gdb",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceRoot}",
        "externalConsole": true,
        "preLaunchTask": "gfortran"
    }
]
Run Code Online (Sandbox Code Playgroud)

因为我使用的是 linux,所以我不需要给出 gfortran 的路径。我还尝试通过更改.exe为 linux 扩展来稍微更改 launch.json 。我已经在问题中更新了它。但是调试器仍然没有运行并在控制台中给出以下错误

and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other …
Run Code Online (Sandbox Code Playgroud)

debugging fortran gdb gfortran visual-studio-code

5
推荐指数
1
解决办法
6422
查看次数

数组的分段错误,但仅限于派生类型的组件

非常简单的设置,在linux上使用gfortran 4.8.5(红帽):

  • 如果我的实数数组(在派生类型中)的大小> 2,000,000,我会得到一个段错误.这似乎是一个标准的堆栈/堆问题,因为我检查时我的堆栈大小是8mb ulimit.

  • 这是没有问题,如果数组NOT派生类型内

  • 请注意,正如@francescalus猜测的那样,删除初始值可以= 0.0消除这个问题

编辑添加:请注意,我发布了一个后续问题与派生类型的组件相关的分段错误,它代表了一个更现实的用例,并进一步缩小了发生这种情况的条件.

program main

    call sub1     ! seg fault  if col size >   2,100,000
    call sub2     ! works fine at col size = 100,000,000  

end program main

subroutine sub1

    type table
        real :: col(2100000) = 0.0     ! works if "= 0.0" removed
    end type table

    type(table) :: table1
    table1%col = 1.0

end subroutine sub1

subroutine sub2
    real :: col(100000000) = 0.0
    col = 1.0
end …
Run Code Online (Sandbox Code Playgroud)

fortran gfortran

5
推荐指数
1
解决办法
180
查看次数

Fortran PURE功能

在我看来,Fortran中所谓的纯函数对于那些使用函数式编程的人来说并不算纯粹.所以这是我的问题.假设我有以下代码:

MODULE basics
  IMPLICIT NONE
  INTEGER, PARAMETER      :: dp = kind(1.0d0)
  REAL(dp), PARAMETER     :: PI=3.1415926535897932_dp
  REAL(dp), PARAMETER     :: earthEquatorialRadius=6378.137_dp
END MODULE basics

MODULE myFunctions
  USE basics
  IMPLICIT NONE

  PURE REAL(dp) FUNCTION sphericalArc(angleInRadians) 
    REAL(dp),INTENT(IN)  :: angleInRadians 

    sphericalArc= 2.0*PI*earthEquatorialRadius*angleInRadians
  END FUNCTION sphericalArc
END MODULE myFunctions
Run Code Online (Sandbox Code Playgroud)

函数sphericalArc没有副作用,所以它在这个意义上是纯粹的,但它使用全局常量.确实可以在函数内部定义参数PI和earthEquatorialRadius,但这是不可取的,因为我想在其他函数和子例程中使用它们.在每个函数或过程中定义dp类型会更加繁琐.

因此,从Fortran的角度来看,一个函数使用在函数之外定义的全局参数仍然被认为是纯粹的并且可以从do并发循环中调用吗?

fortran gfortran intel-fortran

5
推荐指数
2
解决办法
280
查看次数

dyld:库未加载:/usr/local/gfortran/lib/libgfortran.3.dylib 原因:找不到图像

我尝试了很多解决方法(brew 升级/更新、brew 医生、重新安装 gcc 和 gfortran、导出库路径等)来解决这个问题,但没有一个成功。当我尝试执行 Fortran 可执行文件时,我收到相同的错误消息:

dyld:库未加载:/usr/local/gfortran/lib/libgfortran.3.dylib 引用自:/Users/... 原因:找不到图像

我已经阅读了许多不同的建议,是否有一组步骤可以解决这个问题?

谢谢你。

更多信息:MacOS Mojave 10.14.5

哪个 gcc 返回 /usr/bin/gcc

其中 gfortran 返回 /usr/local/bin/gfortran

unix gcc gfortran macos-mojave

5
推荐指数
1
解决办法
8069
查看次数

使用零宽度格式化描述符,例如 F0.6 或 G0.8

Fortran 格式说明符的标准文档解释了如何使用两个参数(通常称为w和 )控制大多数格式d。例如,格式说明符Fw.d表示将打印出一个数字,其中包含w字符宽度(包括小数点)和d小数点右侧的数字。G 格式类似,只是d被解释为要打印的有效位数。

到目前为止,我找到的所有文档都使用wis > 0 的示例,但我最近在这里了解到,您可以在格式说明符中使用零作为宽度参数,这对于 CSV 输出之类的内容非常有用。似乎w=0被解释为“可变宽度”,并且您可以根据需要获得尽可能多或尽可能小的空间,以满足参数d(它为 F 格式提供小数位,为 G 格式提供有效数字)。例如,以下是具有不同值的 F 格式(“b”表示空白字符):

! tested with gfortran 5.5 on linux

print '(F8.3)', 7.777   ! prints bbb7.777
print '(F4.3)', 7.777   ! prints ****
print '(F0.3)', 7.777   ! prints 7.777
Run Code Online (Sandbox Code Playgroud)

这一切似乎都有道理。G 对我来说有点令人困惑,因为它G8.3没有任何空格,我不知道为什么G4.3会失败,但无论如何,它是这样的:

print '(G8.3)', 7.777   ! prints 7.78
print '(G4.3)', 7.777   ! prints ****
print …
Run Code Online (Sandbox Code Playgroud)

fortran gfortran

5
推荐指数
1
解决办法
1821
查看次数

从扩展类型的实例打印父类型的成员

场景(见下面的代码):考虑一些 Fortran 模块my_module,它包含两个派生类型的定义:test(parent) 和sub_test(extends test, subclass)。除了两个构件ab父类的,sub_test定义了一个新成员c。最终,该模块my_module将用于某些程序中my_program

module my_module

implicit none

private
public :: test, sub_test

type :: test
  integer :: a = 1
  integer :: b = 2
end type test

type, extends(test) :: sub_test
  integer :: c = 3
contains
  procedure, public :: increment
end type sub_test

contains

subroutine increment (this)
class(sub_test) :: this
this % a = this …
Run Code Online (Sandbox Code Playgroud)

linux debugging fortran gdb gfortran

5
推荐指数
0
解决办法
120
查看次数