[英]fortran 2003 seg fault with overridden type-bound procedure
I'm trying to use some of the object-oriented features of Fortran 2003 and I'm running into some trouble. 我试图使用Fortran 2003的某些面向对象功能,但遇到了一些麻烦。 First off, I have an abstract data type sparse_matrix: 首先,我有一个抽象数据类型sparse_matrix:
type, abstract :: sparse_matrix
(some data)
contains
procedure(matrix_vector_multiply), deferred :: matvec
end type sparse_matrix
abstract interface
subroutine matrix_vector_multiply(A,x,y)
import :: sparse_matrix
class (sparse_matrix), intent(in) :: A
real(kind=8), dimension(:) :: x,y
end subroutine matrix_vector_multiply
end abstract interface
then I have a concrete data type which inherits from sparse_matrix 那么我有一个具体的数据类型继承自sparse_matrix
type, extends(sparse_matrix) :: csr_sparse_matrix
(some more data)
contains
procedure :: matvec => csr_matvec
end type csr_sparse_matrix
and the actual implementation of matvec: 和matvec的实际实现:
subroutine csr_matvec(A,x,y)
class (csr_sparse_matrix), intent(in) :: A
real(kind=8), dimension(:) :: x,y
(do stuff)
end subroutine csr_matvec
Later, I want to use matvec in a different module, and I don't care which dynamic type I've got: 以后,我想在不同的模块中使用matvec,而不必关心我拥有哪种动态类型:
subroutine solve(A,x,b,tolerance)
class (sparse_matrix), intent(in) :: A
real(kind=8), dimension(:), intent(in) :: b
real(kind=8), dimension(:), intent(out) :: x
real(kind=8), intent(in) :: tolerance
real(kind=8), dimension( A%nrow ) :: z
call A%matvec(b,z)
(more stuff)
end subroutine solver
My understanding is that everything should work fine, no matter what dynamic type A is, so long as the subroutine matvec
has been overridden in the child data type. 我的理解是,只要子程序matvec
已在子数据类型中被覆盖,无论什么动态类型A,一切都应该正常工作。
This code compiles just fine, but it seg faults when I run it. 这段代码可以很好地编译,但是在我运行它时会出现段错误。 When I change the declaration of the matrix in the solve
procedure as follows: 当我在solve
过程中更改矩阵的声明时,如下所示:
class (csr_sparse_matrix), intent(in) :: A
it works just fine. 它工作正常。 Likewise, if I leave A as a sparse_matrix but use 同样,如果我将A保留为sparse_matrix,但使用
select type(A)
type is (csr_sparse_matrix)
(the same stuff)
end select
then everything runs fine too. 然后一切都很好。 But this defeats the whole purpose of overriding the procedures of an abstract type in the first place -- the program shouldn't care what kind of matrix I'm using, as long as it can perform a matrix-vector multiplication. 但这首先挫败了覆盖抽象类型的过程的全部目的-程序不关心我使用的是哪种矩阵,只要它可以执行矩阵向量乘法即可。
Anyhow, I'm sure it's just a matter of some attribute that I forgot to include, but I can't figure out what it is. 无论如何,我确定这只是我忘了包含的某些属性的问题,但我不知道它是什么。
So it was the compiler after all; 因此,毕竟是编译器。 I was using gfortran 4.6.3, whereas abstract data types were only properly supported as of gfortran 4.7. 我使用的是gfortran 4.6.3,而从gfortran 4.7开始仅支持抽象数据类型。 Unfortunately, this makes my code not so portable, as the long-term support releases of several linux distributions only include version 4.6. 不幸的是,这使我的代码无法移植,因为几个linux发行版的长期支持版本仅包括4.6版。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.