我对Fortran非常陌生(一个月左右)。在我主要使用Python编写脚本之前。我目前正在编写代码,在其中需要一些方法来存储稀疏数组。该阵列是正方形的,大约为3E6 x 3E6。最初,我尝试使用8位实数存储完整的数组,但遇到“虚拟内存不足”的情况。
为了减少内存消耗,我编写了一组非常简单的例程,并编写了一种类型来将矩阵存储为行,列和值三元组。通过使用另一个数组复制到然后取消分配和分配,我可以存储此三元格式的值。该代码结果非常慢。当然,它随着我存储在稀疏数组中的值的数量而变慢。
我注意到,在Fortran 2003及更高版本中,有一种非常简单的方法可以使用vec = [vec(:), val]
追加一个元素来进行重新分配。这需要使用-assume realloc_lhs
进行编译。不幸的是,这和我之前写的版本一样慢。
一个问题是,尽管我知道矩阵有多大,但我只知道每行(或每列)非零元素的数量上限。我还没有利用这些信息,所以才尝试重新分配。
我已附上使用-assume realloc_lhs
的当前版本。我感谢提供更好解决方案的任何技巧或提示。
TYPE sprs
INTEGER :: n, len
REAL, ALLOCATABLE :: val(:)
INTEGER, ALLOCATABLE :: irow(:)
INTEGER, ALLOCATABLE :: icol(:)
END TYPE sprs
!===================================================================================================
SUBROUTINE store_sprs(val,irow,icol,matrix)
!===================================================================================================
!
IMPLICIT NONE
!
! Arguments:
!-----------
REAL, INTENT(IN) :: val
INTEGER, INTENT(IN) :: irow
INTEGER, INTENT(IN) :: icol
TYPE(sprs), INTENT(INOUT) :: matrix
!
! Locals:
!--------
IF (ABS(val)>=1.0E-50) THEN
CALL add2list_re(val, matrix.val)
CALL add2list_int(irow, matrix.irow)
CALL add2list_int(icol, matrix.icol)
ENDIF
END SUBROUTINE store_sprs
SUBROUTINE add2list_re(val,vec)
!===================================================================================================
IMPLICIT NONE
!
! Arguments:
!-----------
REAL, INTENT(IN) :: val
REAL, ALLOCATABLE, INTENT(INOUT) :: vec(:)
!
! Locals:
!--------
vec = [vec(:), val]
END SUBROUTINE add2list_re
!===================================================================================================
SUBROUTINE add2list_int(val,vec)
!===================================================================================================
IMPLICIT NONE
!
! Arguments:
!-----------
INTEGER, INTENT(IN) :: val
INTEGER, ALLOCATABLE, INTENT(INOUT) :: vec(:)
!
! Locals:
!--------
vec = [vec(:), val]
END SUBROUTINE add2list_int
[许多数据结构可以很好地处理稀疏数据。我发现C++ std::Vector模型可以很好地工作。
此模型为多余的元素分配空间,并跟踪实际使用的元素数。在大多数情况下,添加元素时,不需要新的内存分配。当需要更多空间时,分配的空间将增加一倍。这导致O(log(n))分配和更好的性能。
例如,可以使用矩阵类和矩阵元素类在Fortran中实现,
module sparse_matrix
implicit none
private
public :: dp
public :: SparseElement
public :: Sparse
integer, parameter :: dp=selected_real_kind(15,300)
type SparseElement
integer :: irow
integer :: icol
real(dp) :: val
end type
type Sparse
! Only the first no_elements elements will be in use.
integer :: no_elements
type(SparseElement), allocatable :: elements_(:)
contains
procedure, public :: elements
procedure, public :: add_element
end type
interface Sparse
module procedure new_Sparse
end interface
contains
! Initialise the Sparse matrix to an empty matrix.
function new_Sparse() result(this)
implicit none
type(Sparse) :: this
this%no_elements = 0
allocate(this%elements_(0))
end function
! Return only the elements which are in use.
function elements(this) result(output)
implicit none
class(Sparse), intent(in) :: this
type(SparseElement), allocatable :: output(:)
output = this%elements_(:this%no_elements)
end function
! Add an element to the array, automatically allocating memory if needed.
subroutine add_element(this,element)
implicit none
class(Sparse), intent(inout) :: this
type(SparseElement), intent(in) :: element
type(SparseElement), allocatable :: temp(:)
this%no_elements = this%no_elements+1
! This is where memory is added.
! If this%elements_ would overflow, its length is doubled.
if (this%no_elements>size(this%elements_)) then
allocate(temp(2*this%no_elements))
temp(:this%no_elements-1) = this%elements_
this%elements_ = temp
endif
this%elements_(this%no_elements) = element
end subroutine
end module
program main
use sparse_matrix
implicit none
type(Sparse) :: matrix
type(SparseElement), allocatable :: elements(:)
integer :: i
! Initialise the matrix.
matrix = Sparse()
! Add some elements to the matrix.
call matrix%add_element(SparseElement(1,1,1.0_dp))
call matrix%add_element(SparseElement(3,5,7.0_dp))
call matrix%add_element(SparseElement(100,200,300.0_dp))
! Retrieve the elements from the matrix.
elements = matrix%elements()
do i=1,size(elements)
write(*,*) elements(i)%irow, elements(i)%icol, elements(i)%val
enddo
end program