|
| 1 | +--- |
| 2 | +layout: book |
| 3 | +title: Arrays |
| 4 | +permalink: /learn/best_practices/arrays |
| 5 | +--- |
| 6 | + |
| 7 | +Arrays are a central object in Fortran. The creation of dynamic sized arrays |
| 8 | +is discussed in the [allocatable arrays arrays](./allocatable_arrays.html). |
| 9 | + |
| 10 | +To pass arrays to procedures four ways are available |
| 11 | + |
| 12 | +1. *assumed-shape* arrays |
| 13 | +4. *assumed-rank* arrays |
| 14 | +2. *explicit-shape* arrays |
| 15 | +3. *assumed-size* arrays |
| 16 | + |
| 17 | +The preferred way to pass arrays to procedures is as *assumed-shape* arrays |
| 18 | + |
| 19 | +```fortran |
| 20 | +subroutine f(r) |
| 21 | + real(dp), intent(out) :: r(:) |
| 22 | + integer :: n, i |
| 23 | + n = size(r) |
| 24 | + do i = 1, n |
| 25 | + r(i) = 1.0_dp / i**2 |
| 26 | + end do |
| 27 | +end subroutine f |
| 28 | +``` |
| 29 | + |
| 30 | +Higher dimensional arrays can be passed in a similar way. |
| 31 | + |
| 32 | +```fortran |
| 33 | +subroutine g(A) |
| 34 | + real(dp), intent(in) :: A(:, :) |
| 35 | + ... |
| 36 | +end subroutine g |
| 37 | +``` |
| 38 | + |
| 39 | +The array is simply passed by |
| 40 | + |
| 41 | +```fortran |
| 42 | +real(dp) :: r(5) |
| 43 | +call f(r) |
| 44 | +``` |
| 45 | + |
| 46 | +In this case no array copy is done, which has the advantage that the shape and size |
| 47 | +information is automatically passed along and checked at compile and optionally at |
| 48 | +runtime. |
| 49 | +Similarly, array strides can be passed without requiring a copy of the array but as |
| 50 | +*assumed-shape* discriptor: |
| 51 | + |
| 52 | +```fortran |
| 53 | +real(dp) :: r(10) |
| 54 | +call f(r(1:10:2)) |
| 55 | +call f(r(2:10:2)) |
| 56 | +``` |
| 57 | + |
| 58 | +This should always be your default way of passing arrays in and out of subroutines. |
| 59 | +Avoid passing arrays as whole slices, as it obfuscates the actual intent of the code: |
| 60 | + |
| 61 | +```fortran |
| 62 | +real(dp) :: r(10) |
| 63 | +call f(r(:)) |
| 64 | +``` |
| 65 | + |
| 66 | +In case more general arrays should be passed to a procedure the *assumed-rank* |
| 67 | +functionality introduced in the Fortran 2018 standard can be used |
| 68 | + |
| 69 | +```fortran |
| 70 | +subroutine h(r) |
| 71 | + real(dp), intent(in) :: r(..) |
| 72 | + select rank(r) |
| 73 | + rank(1) |
| 74 | + ! ... |
| 75 | + rank(2) |
| 76 | + ! ... |
| 77 | + end select |
| 78 | +end subroutine h |
| 79 | +``` |
| 80 | + |
| 81 | +The actual rank can be queried at runtime using the ``select rank`` construct. |
| 82 | +This easily allows to create more generic functions that have to deal with |
| 83 | +differnet array ranks. |
| 84 | + |
| 85 | +*Explicit-shape* arrays can be useful for returning data from functions. |
| 86 | +Most of their functionality can be provided by *assumed-shape* and *assumed-rank* |
| 87 | +arrays but they find frequent use for interfacing with C or in legacy Fortran |
| 88 | +procedures, therefore they will be discussed briefly here. |
| 89 | + |
| 90 | +To use *explicit-shape* arrays, the dimension has to be passed explicitly as dummy |
| 91 | +argument like in the example below |
| 92 | + |
| 93 | +``` fortran |
| 94 | +subroutine f(n, r) |
| 95 | + integer, intent(in) :: n |
| 96 | + real(dp), intent(out) :: r(n) |
| 97 | + integer :: i |
| 98 | + do i = 1, n |
| 99 | + r(i) = 1.0_dp / i**2 |
| 100 | + end do |
| 101 | +end subroutine |
| 102 | +``` |
| 103 | + |
| 104 | +For high-dimensional arrays additional indices have to be passed. |
| 105 | + |
| 106 | +``` fortran |
| 107 | +subroutine g(m, n, A) |
| 108 | + integer, intent(in) :: m, n |
| 109 | + real(dp), intent(in) :: A(m, n) |
| 110 | + ... |
| 111 | +end subroutine |
| 112 | +``` |
| 113 | + |
| 114 | +The routines can be invoked by |
| 115 | + |
| 116 | +``` fortran |
| 117 | +real(dp) :: r(5), s(3, 4) |
| 118 | +call f(size(r), r) |
| 119 | +call g(size(s, 1), size(s, 2), s) |
| 120 | +``` |
| 121 | + |
| 122 | +Note that the shape is not checked, therefore the following would be valid code |
| 123 | +with will potentially yield incorrect results: |
| 124 | + |
| 125 | +```fortran |
| 126 | +real(dp) :: s(3, 4) |
| 127 | +call g(size(s), 1, s) ! s(12, 1) in g |
| 128 | +call g(size(s, 2), size(s, 1), s) ! s(4, 3) in g |
| 129 | +``` |
| 130 | + |
| 131 | +In this case the memory layout is preserved but the shape is changed. |
| 132 | +Also, *explicit-shape* arrays require contiguous memory and will create temporary |
| 133 | +arrays in case non-contiguous array strides are passed. |
| 134 | + |
| 135 | +To return an array from a function with *explicit-shape* use |
| 136 | + |
| 137 | +``` fortran |
| 138 | +function f(n) result(r) |
| 139 | + integer, intent(in) :: n |
| 140 | + real(dp) :: r(n) |
| 141 | + integer :: i |
| 142 | + do i = 1, n |
| 143 | + r(i) = 1.0_dp / i**2 |
| 144 | + end do |
| 145 | +end function |
| 146 | +``` |
| 147 | + |
| 148 | +Finally, there are *assumed-size* arrays, which provide the least compile and runtime |
| 149 | +checking and can be found be found frequently in legacy code, they should be avoided |
| 150 | +in favour of *assumed-shape* or *assumed-rank* arrays. |
| 151 | +An *assumed-size* array dummy argument is identified by an asterisk as the last dimension, |
| 152 | +this disables the usage of this array with many intrinsic functions, like ``size`` or |
| 153 | +``shape``. |
| 154 | + |
| 155 | +To check for the correct size and shape of an *assumed-shape* array the ``size`` and |
| 156 | +``shape`` intrinsic functions can be used to query for those properties |
| 157 | + |
| 158 | +```fortran |
| 159 | +if (size(r) /= 4) error stop "Incorrect size of 'r'" |
| 160 | +if (any(shape(r) /= [2, 2])) error stop "Incorrect shape of 'r'" |
| 161 | +``` |
| 162 | + |
| 163 | +Note that ``size`` returns the total size of all dimensions, to obtain the shape of |
| 164 | +a specific dimension add it as second argument to the function. |
| 165 | + |
| 166 | +Arrays can be initialized by using an array constructor |
| 167 | + |
| 168 | +```fortran |
| 169 | +integer :: r(5) |
| 170 | +r = [1, 2, 3, 4, 5] |
| 171 | +``` |
| 172 | + |
| 173 | +The array constructor can be annoted with the type of the constructed array |
| 174 | + |
| 175 | +```fortran |
| 176 | +real(dp) :: r(5) |
| 177 | +r = [real(dp) :: 1, 2, 3, 4, 5] |
| 178 | +``` |
| 179 | + |
| 180 | +Implicit do loops can be used inside an array constructor as well |
| 181 | + |
| 182 | +```fortran |
| 183 | +integer :: i |
| 184 | +real(dp) :: r(5) |
| 185 | +r = [(real(i**2, dp), i = 1, size(r))] |
| 186 | +``` |
| 187 | + |
| 188 | +In order for the array to start with different index than 1, do: |
| 189 | + |
| 190 | +```fortran |
| 191 | +subroutine print_eigenvalues(kappa_min, lam) |
| 192 | + integer, intent(in) :: kappa_min |
| 193 | + real(dp), intent(in) :: lam(kappa_min:) |
| 194 | +
|
| 195 | + integer :: kappa |
| 196 | + do kappa = kappa_min, ubound(lam, 1) |
| 197 | + print *, kappa, lam(kappa) |
| 198 | + end do |
| 199 | +end subroutine print_eigenvalues |
| 200 | +``` |
0 commit comments