您不是第一个遇到这个问题的人,我很高兴地说,标准中的这个缺陷将在 Fortran 2015 中得到补救。如本文档所述(第 6 页,标题“已批准对标准的更改”), “应该取消对程序中语句出现的限制error stop
pure
” 。
Fortran 2008 标准error stop
在一些新的并行计算特性的上下文中包含了该声明。它会发出错误信号并尽快停止所有进程。目前,过程中既不允许stop
也error stop
不允许语句pure
,因为它们显然不是线程安全的。实际上,在发生内部错误的情况下,这是不必要的限制。
根据您的编译器,您可能需要耐心等待实现。我知道英特尔已经在他们的 ifort 编译器中实现了它。(“F2015:在 PURE/ELEMENTAL 程序中解除对 STOP 和 ERROR STOP 的限制”)
选择
对于另一种方法,您可以看一下这个问题,但在您的情况下,这可能有点棘手,因为您必须更改do concurrent
关键字,而不仅仅是pure
.
(正确答案结束)
如果弄脏手是一种选择...
与此同时,你可以做一些残忍的事情,比如
pure subroutine internal_error(error_msg)
! Try hard to produce a runtime error, regardless of compiler flags.
! This is useful in pure subprograms where you want to produce an error,
! preferably with a traceback.
!
! Though far from pretty, this solution contains all the ugliness in this
! single subprogram.
!
! TODO: replace with ERROR STOP when supported by compiler
implicit none
character(*), intent(in) :: error_msg
integer, dimension(:), allocatable :: molested
allocate(molested(2))
allocate(molested(2))
molested(3) = molested(4)
molested(1) = -10
molested(2) = sqrt(real(molested(1)))
deallocate(molested)
deallocate(molested)
molested(3) = molested(-10)
end subroutine internal_error
如果有人问,你没有从我这里得到这个。