Are there any problems with using jagged-arrays in Fortran with multiple levels of allocation?

后端 未结 1 1271
执念已碎
执念已碎 2021-01-18 22:47

In my Fortran code, I want to use jagged arrays with multiple levels of allocation. An example code of what I mean is

module nonsquare_matrix_mod
implicit n         


        
1条回答
  •  醉梦人生
    2021-01-18 23:36

    Since you are using allocatable arrays only, you won't have any memory leak as it could be the case when using pointers. Whether the jagged array is a reasonable solution for your problem depends very much on the structure of your data. A few points to be noted:

    • Your array won't be contigous, indeed. That has several implication, like worse caching behaviour, when you are accessing subsequent lines.

    • You have to allocate every line via allocate individually. If it happens very often (e.g. inside a loop), that could be a problem, as allocation is a rather "slow" operation.

    • If the lines in your array are really very different in size (and you do not have too many lines), you may save some considerable amount of memory.

    If the length of the lines is set at the time of their creation and not changed afterwards (and you have a good guess of maximally how much elements you will have in your entire array), you could allocate a large buffer array, where you put in the lines, and an index array which contains the position of the first element of that line in the buffer array:

     program nonsquare_matrix_test
      implicit none
    
      integer, parameter :: dp = kind(1.0d0)
      integer, parameter :: maxlines = 50
      integer, parameter :: maxelements = 5000
      real(dp), allocatable :: buffer(:)
      integer, allocatable :: rowindex(:)
      integer :: ii
    
      allocate(buffer(maxelements))
      allocate(rowindex(maxlines + 1))
      rowindex(1) = 1
      do ii = 1, maxlines
        rowindex(ii + 1)  = rowindex(ii) + ii
      end do
      ! ...  
      ! Omitting the part which fills up the array
      ! ...
      ! Accessing a given line (e.g. line 5)
      print *, "Content of line 5:"
      print *, buffer(rowindex(5):rowindex(6)-1)
    
    end program nonsquare_matrix_test
    

    0 讨论(0)
提交回复
热议问题