首页 > 解决方案 > Fortran 中的稀疏数组

问题描述

我对 Fortran 很陌生(一个月左右)。在我主要使用 Python 编写脚本之前。我目前正在编写一个代码,我需要一些方法来存储一个稀疏数组。该数组是正方形的,大约为 3E6×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

标签: fortransparse-matrix

解决方案


有许多数据结构可以很好地处理稀疏数据。我发现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

推荐阅读