|
| 1 | +!> Manager of `ResultDP` (TODO: xref) across the Fortran-Python interface |
| 2 | +module m_result_dp_manager |
| 3 | + |
| 4 | + use kind_parameters, only: dp |
| 5 | + use m_error_v, only: ErrorV |
| 6 | + use m_result_dp, only: ResultDP |
| 7 | + use m_result_none, only: ResultNone |
| 8 | + |
| 9 | + implicit none(type, external) |
| 10 | + private |
| 11 | + |
| 12 | + type(ResultDP), dimension(:), allocatable :: instance_array |
| 13 | + logical, dimension(:), allocatable :: instance_available |
| 14 | + |
| 15 | + ! TODO: think about ordering here, alphabetical probably easiest |
| 16 | + public :: build_instance, finalise_instance, get_available_instance_index, get_instance, set_instance_index_to, & |
| 17 | + ensure_instance_array_size_is_at_least |
| 18 | + |
| 19 | +contains |
| 20 | + |
| 21 | + function build_instance(data_v_in, error_v_in) result(instance_index) |
| 22 | + !! Build an instance |
| 23 | + |
| 24 | + real(kind=dp), intent(in), optional :: data_v_in |
| 25 | + !! Data |
| 26 | + |
| 27 | + class(ErrorV), intent(in), optional :: error_v_in |
| 28 | + !! Error message |
| 29 | + |
| 30 | + integer :: instance_index |
| 31 | + !! Index of the built instance |
| 32 | + |
| 33 | + type(ResultNone) :: res_build |
| 34 | + |
| 35 | + call ensure_instance_array_size_is_at_least(1) |
| 36 | + call get_available_instance_index(instance_index) |
| 37 | + res_build = instance_array(instance_index) % build(data_v_in=data_v_in, error_v_in=error_v_in) |
| 38 | + |
| 39 | + ! TODO: check build has no error |
| 40 | + |
| 41 | + end function build_instance |
| 42 | + |
| 43 | + subroutine finalise_instance(instance_index) |
| 44 | + !! Finalise an instance |
| 45 | + |
| 46 | + integer, intent(in) :: instance_index |
| 47 | + !! Index of the instance to finalise |
| 48 | + |
| 49 | + call check_index_claimed(instance_index) |
| 50 | + |
| 51 | + call instance_array(instance_index) % finalise() |
| 52 | + instance_available(instance_index) = .true. |
| 53 | + |
| 54 | + end subroutine finalise_instance |
| 55 | + |
| 56 | + subroutine get_available_instance_index(available_instance_index) |
| 57 | + !! Get a free instance index |
| 58 | + |
| 59 | + ! TODO: think through whether race conditions are possible |
| 60 | + ! e.g. while returning a free index number to one Python call |
| 61 | + ! a different one can be looking up a free instance index at the same time |
| 62 | + ! and something goes wrong (maybe we need a lock) |
| 63 | + |
| 64 | + integer, intent(out) :: available_instance_index |
| 65 | + !! Available instance index |
| 66 | + |
| 67 | + integer :: i |
| 68 | + |
| 69 | + do i = 1, size(instance_array) |
| 70 | + |
| 71 | + if (instance_available(i)) then |
| 72 | + |
| 73 | + instance_available(i) = .false. |
| 74 | + available_instance_index = i |
| 75 | + ! TODO: switch to returning a Result type |
| 76 | + ! res = ResultInt(data=i) |
| 77 | + return |
| 78 | + |
| 79 | + end if |
| 80 | + |
| 81 | + end do |
| 82 | + |
| 83 | + ! TODO: switch to returning a Result type with an error set |
| 84 | + ! res = ResultInt(ResultDP(code=1, message="No available instances")) |
| 85 | + error stop 1 |
| 86 | + |
| 87 | + end subroutine get_available_instance_index |
| 88 | + |
| 89 | + ! Change to pure function when we update check_index_claimed to be pure |
| 90 | + function get_instance(instance_index) result(inst) |
| 91 | + |
| 92 | + integer, intent(in) :: instance_index |
| 93 | + !! Index in `instance_array` of which to set the value equal to `val` |
| 94 | + |
| 95 | + type(ResultDP) :: inst |
| 96 | + !! Instance at `instance_array(instance_index)` |
| 97 | + |
| 98 | + call check_index_claimed(instance_index) |
| 99 | + inst = instance_array(instance_index) |
| 100 | + |
| 101 | + end function get_instance |
| 102 | + |
| 103 | + subroutine set_instance_index_to(instance_index, val) |
| 104 | + |
| 105 | + integer, intent(in) :: instance_index |
| 106 | + !! Index in `instance_array` of which to set the value equal to `val` |
| 107 | + |
| 108 | + type(ResultDP), intent(in) :: val |
| 109 | + |
| 110 | + call check_index_claimed(instance_index) |
| 111 | + instance_array(instance_index) = val |
| 112 | + |
| 113 | + end subroutine set_instance_index_to |
| 114 | + |
| 115 | + subroutine check_index_claimed(instance_index) |
| 116 | + !! Check that an index has already been claimed |
| 117 | + !! |
| 118 | + !! Stops execution if the index has not been claimed. |
| 119 | + |
| 120 | + integer, intent(in) :: instance_index |
| 121 | + !! Instance index to check |
| 122 | + |
| 123 | + if (instance_available(instance_index)) then |
| 124 | + ! TODO: Switch to using Result here |
| 125 | + ! Use `ResultNone` which is a Result type |
| 126 | + ! that doesn't have a `data` attribute |
| 127 | + ! (i.e. if this succeeds, there is no data to check, |
| 128 | + ! if it fails, the result_dp attribute will be set). |
| 129 | + ! So the code would be something like |
| 130 | + ! res = ResultNone(ResultDP(code=1, message="Index ", instance_index, " has not been claimed")) |
| 131 | + print *, "Index ", instance_index, " has not been claimed" |
| 132 | + error stop 1 |
| 133 | + end if |
| 134 | + |
| 135 | + if (instance_index < 1) then |
| 136 | + ! TODO: Switch to using Result here |
| 137 | + ! Use `ResultNone` which is a Result type |
| 138 | + ! that doesn't have a `data` attribute |
| 139 | + ! (i.e. if this succeeds, there is no data to check, |
| 140 | + ! if it fails, the result_dp attribute will be set). |
| 141 | + ! So the code would be something like |
| 142 | + ! res = ResultNone(ResultDP(code=2, message="Requested index is ", instance_index, " which is less than 1")) |
| 143 | + print *, "Requested index is ", instance_index, " which is less than 1" |
| 144 | + error stop 1 |
| 145 | + end if |
| 146 | + |
| 147 | + ! ! Here, result becomes |
| 148 | + ! ! Now that I've thought about this, it's also clear |
| 149 | + ! ! that we will only use functions |
| 150 | + ! ! or subroutines with a result type that has `intent(out)`. |
| 151 | + ! ! We will no longer have subroutines that return nothing |
| 152 | + ! ! (like this one currently does). |
| 153 | + ! res = ResultNone() |
| 154 | + |
| 155 | + end subroutine check_index_claimed |
| 156 | + |
| 157 | + subroutine ensure_instance_array_size_is_at_least(n) |
| 158 | + !! Ensure that `instance_array` and `instance_available` have at least `n` slots |
| 159 | + |
| 160 | + integer, intent(in) :: n |
| 161 | + |
| 162 | + type(ResultDP), dimension(:), allocatable :: tmp_instances |
| 163 | + logical, dimension(:), allocatable :: tmp_available |
| 164 | + |
| 165 | + if (.not. allocated(instance_array)) then |
| 166 | + |
| 167 | + allocate (instance_array(n)) |
| 168 | + |
| 169 | + allocate (instance_available(n)) |
| 170 | + ! Race conditions ? |
| 171 | + instance_available = .true. |
| 172 | + |
| 173 | + else if (size(instance_available) < n) then |
| 174 | + |
| 175 | + allocate (tmp_instances(n)) |
| 176 | + tmp_instances(1:size(instance_array)) = instance_array |
| 177 | + call move_alloc(tmp_instances, instance_array) |
| 178 | + |
| 179 | + allocate (tmp_available(n)) |
| 180 | + tmp_available(1:size(instance_available)) = instance_available |
| 181 | + tmp_available(size(instance_available) + 1:size(tmp_available)) = .true. |
| 182 | + call move_alloc(tmp_available, instance_available) |
| 183 | + |
| 184 | + end if |
| 185 | + |
| 186 | + end subroutine ensure_instance_array_size_is_at_least |
| 187 | + |
| 188 | +end module m_result_dp_manager |
0 commit comments