-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_dp_manager.f90
More file actions
240 lines (177 loc) · 8.51 KB
/
result_dp_manager.f90
File metadata and controls
240 lines (177 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
!> manager of `resultdp` (todo: xref) across the fortran-python interface
module m_result_dp_manager
use kind_parameters, only: dp
use m_error_v, only: errorv
use m_result_dp, only: resultdp
use m_result_int, only: resultint
use m_result_none, only: resultnone
implicit none
private
type(resultdp), dimension(:), allocatable :: instance_array
logical, dimension(:), allocatable :: instance_available
! todo: think about ordering here, alphabetical probably easiest
public :: build_instance, finalise_instance, get_available_instance_index, get_instance, set_instance_index_to, &
ensure_instance_array_size_is_at_least
contains
subroutine build_instance(data_v_in, error_v_in, res)
!! Build an instance
real(kind=dp), intent(in), optional :: data_v_in
!! Data
class(ErrorV), intent(in), optional :: error_v_in
!! Error message
type(ResultInt) , intent(out) :: res
!! Result i.e. index of the built instance (within a result type)
type(ResultNone) :: res_build
call ensure_instance_array_size_is_at_least(1)
! ! TODO: switch to
! instance_index = get_available_instance_index()
call get_available_instance_index(res)
if (res % is_error()) then
! Already hit an error, quick return
return
end if
call instance_array(res%data_v) % build( &
data_v_in=data_v_in, error_v_in=error_v_in, res=res_build &
)
if (.not. (res_build % is_error())) then
! All happy
return
end if
! Error occured
!
! Free the slot again
instance_available(res % data_v) = .true.
! Bubble the error up.
! This is a good example of where stacking errors would be nice.
! It would be great to be able to say,
! "We got an instance index,
! but when we tried to build the instance,
! the following error occured...".
! (Stacking error messages like this
! would even let us do stack traces in a way...)
res = ResultInt(error_v=res_build%error_v)
end subroutine build_instance
subroutine finalise_instance(instance_index)
!! Finalise an instance
integer, intent(in) :: instance_index
!! Index of the instance to finalise
type(ResultNone) :: res_check_index_claimed
res_check_index_claimed = check_index_claimed(instance_index)
if(res_check_index_claimed%is_error()) return
call instance_array(instance_index) % finalise()
instance_available(instance_index) = .true.
end subroutine finalise_instance
subroutine get_available_instance_index(res_available_instance_index)
!! Get a free instance index
! TODO: think through whether race conditions are possible
! e.g. while returning a free index number to one Python call
! a different one can be looking up a free instance index at the same time
! and something goes wrong (maybe we need a lock)
type(ResultInt), intent(out) :: res_available_instance_index
!! Available instance index
integer :: i
do i = 1, size(instance_array)
if (instance_available(i)) then
instance_available(i) = .false.
res_available_instance_index = ResultInt(data_v=i)
return
end if
end do
res_available_instance_index = ResultInt( &
error_v=ErrorV( &
code=1, &
message="No available instances" &
! TODO: add total number of instances to the error message
! as that is useful information when debugging
! (requires a int_to_str function first)
) &
)
end subroutine get_available_instance_index
! Change to pure function when we update check_index_claimed to be pure
function get_instance(instance_index) result(inst)
integer, intent(in) :: instance_index
!! Index in `instance_array` of which to set the value equal to `val`
type(ResultDP) :: inst
!! Instance at `instance_array(instance_index)`
type(ResultNone) :: res_check_index_claimed
res_check_index_claimed = check_index_claimed(instance_index)
if(res_check_index_claimed%is_error()) then
inst = ResultDP(error_v=res_check_index_claimed%error_v)
else
inst = instance_array(instance_index)
end if
end function get_instance
subroutine set_instance_index_to(instance_index, val)
integer, intent(in) :: instance_index
!! Index in `instance_array` of which to set the value equal to `val`
type(ResultDP), intent(in) :: val
type(ResultNone) :: res_check_index_claimed
res_check_index_claimed = check_index_claimed(instance_index)
if(res_check_index_claimed%is_error()) instance_array(instance_index) = val
end subroutine set_instance_index_to
function check_index_claimed(instance_index) result(res_check_index_claimed)
!! Check that an index has already been claimed
!!
!! Stops execution if the index has not been claimed.
integer, intent(in) :: instance_index
!! Instance index to check
type(ResultNone) :: res_check_index_claimed
character(len=:), allocatable :: msg
! msg initialisation to avoid compiler warning
msg = ""
if (instance_available(instance_index)) then
! TODO: Switch to using Result here
! Use `ResultNone` which is a Result type
! that doesn't have a `data` attribute
! (i.e. if this succeeds, there is no data to check,
! if it fails, the result_dp attribute will be set).
! So the code would be something like
! res = ResultNone(ResultDP(code=1, message="Index ", instance_index, " has not been claimed"))
! print *, "Index ", instance_index, " has not been claimed"
! error stop 1
write(msg,fmt="(A, I0, A)") "Index ", instance_index," has not been claimed"
res_check_index_claimed = ResultNone(error_v=ErrorV(code=1, message=msg))
end if
if (instance_index < 1) then
! TODO: Switch to using Result here
! Use `ResultNone` which is a Result type
! that doesn't have a `data` attribute
! (i.e. if this succeeds, there is no data to check,
! if it fails, the result_dp attribute will be set).
! So the code would be something like
! res = ResultNone(ResultDP(code=2, message="Requested index is ", instance_index, " which is less than 1"))
! print *, "Requested index is ", instance_index, " which is less than 1"
! error stop 1
write(msg,fmt="(A, I0, A)") "Requested index is ", instance_index, " which is less than 1"
res_check_index_claimed = ResultNone(error_v=ErrorV(code=2, message=msg))
end if
! ! Here, result becomes
! ! Now that I've thought about this, it's also clear
! ! that we will only use functions
! ! or subroutines with a result type that has `intent(out)`.
! ! We will no longer have subroutines that return nothing
! ! (like this one currently does).
! res = ResultNone()
res_check_index_claimed = ResultNone()
end function check_index_claimed
subroutine ensure_instance_array_size_is_at_least(n)
!! Ensure that `instance_array` and `instance_available` have at least `n` slots
integer, intent(in) :: n
type(ResultDP), dimension(:), allocatable :: tmp_instances
logical, dimension(:), allocatable :: tmp_available
if (.not. allocated(instance_array)) then
allocate (instance_array(n))
allocate (instance_available(n))
! Race conditions ?
instance_available = .true.
else if (size(instance_available) < n) then
allocate (tmp_instances(n))
tmp_instances(1:size(instance_array)) = instance_array
call move_alloc(tmp_instances, instance_array)
allocate (tmp_available(n))
tmp_available(1:size(instance_available)) = instance_available
tmp_available(size(instance_available) + 1:size(tmp_available)) = .true.
call move_alloc(tmp_available, instance_available)
end if
end subroutine ensure_instance_array_size_is_at_least
end module m_result_dp_manager