-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.F90
391 lines (366 loc) · 15.3 KB
/
stack.F90
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
!Generic implementation of a stack (OO Fortran 2003).
!This implementation is not derived from the GFC::Base.
!AUTHOR: Dmitry I. Lyakh (Liakh): [email protected]
!REVISION: 2016/03/16
!Copyright (C) 2014-2016 Dmitry I. Lyakh (Liakh)
!Copyright (C) 2014-2016 Oak Ridge National Laboratory (UT-Battelle)
!This file is part of ExaTensor.
!ExaTensor is free software: you can redistribute it and/or modify
!it under the terms of the GNU Lesser General Public License as published
!by the Free Software Foundation, either version 3 of the License, or
!(at your option) any later version.
!ExaTensor is distributed in the hope that it will be useful,
!but WITHOUT ANY WARRANTY; without even the implied warranty of
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!GNU Lesser General Public License for more details.
!You should have received a copy of the GNU Lesser General Public License
!along with ExaTensor. If not, see <http://www.gnu.org/licenses/>.
!DESCRIPTION:
! 1) Allocate a stack object via Fortran allocate;
! 2) Clean the stack at any point via .clean;
! 3) Push/pop any objects to the stack via .push and .pop;
! 4) Clean the stack at the end via .clean (if it is not empty);
! 5) Deallocate the stack object via Fortran deallocate;
! 6) While the stack is in scope, .state can return info on it.
!NOTES:
! a) An object pushed to the stack can be of any scalar type/class.
! b) It is illegal to push a NULL object.
! c) So far, it is illegal to push non-scalar objects (arrays).
module stack
use dil_basic
implicit none
private
!PARAMETERS:
!Generic:
integer, private:: CONS_OUT=6 !default output
logical, private:: VERBOSE=.true. !verbosity for errors
logical, private:: DEBUG=.true. !debugging mode
!Error codes:
integer(INTD), parameter, public:: STACK_SUCCESS=0 !success
integer(INTD), parameter, public:: STACK_ERROR=-1 !unclassified error
integer(INTD), parameter, public:: STACK_NOT_INITIALIZED=1 !error: stack is not initialized
integer(INTD), parameter, public:: STACK_EMPTY=2 !error: stack is empty
integer(INTD), parameter, public:: STACK_NOT_EMPTY=3 !error: stack is not empty
integer(INTD), parameter, public:: STACK_MEM_ALLOC_FAIL=4 !error: memory allocation failed
integer(INTD), parameter, public:: STACK_MEM_FREE_FAIL=5 !error: memory deallocation failed
integer(INTD), parameter, public:: STACK_CORRUPTED=6 !error: stack got corrupted
!DERIVED TYPES:
!Polymorphic stack entry:
type, private:: stack_entry_t
class(*), pointer, private:: item !stored item
class(stack_entry_t), pointer, private:: prev=>NULL() !pointer to the previous stored item
logical, private:: pointed_only !flag that distinguishes allocated items from associated
end type stack_entry_t
!Stack:
type, public:: stack_t
integer(INTL), private:: num_items=0_INTL !total number of items in the stack
class(stack_entry_t), pointer, private:: last=>NULL() !pointer to the last item
logical, private:: corrupted=.false. !set to .TRUE. when the stack is corrupted
contains
procedure, public:: clean=>stack_clean !cleans the stack (pops out all items)
procedure, public:: state=>stack_state !returns the current state of the stack
procedure, public:: push=>stack_push !adds an item to the stack
procedure, public:: pop=>stack_pop !extracts the last item from the stack
end type stack_t
!VISIBILITY:
private stack_clean
private stack_state
private stack_push
private stack_pop
contains
!----------------------------------------
subroutine stack_clean(this,ierr)
!Cleans a stack (pops out and deletes all items).
implicit none
class(stack_t), intent(inout):: this !inout: stack
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: i,errc
class(stack_entry_t), pointer:: sp
if(this%num_items.eq.0.and.(.not.(this%corrupted.or.associated(this%last)))) then
errc=STACK_SUCCESS
else
if(this%corrupted) then
errc=STACK_CORRUPTED
else
if(this%num_items.gt.0.and.associated(this%last)) then
errc=STACK_SUCCESS; sp=>this%last
do while(associated(sp))
this%last=>sp%prev; nullify(sp%prev)
if(.not.sp%pointed_only) then
deallocate(sp%item,STAT=i); if(i.ne.0) errc=STACK_MEM_FREE_FAIL
else
nullify(sp%item)
endif
deallocate(sp,STAT=i); if(i.ne.0) errc=STACK_MEM_FREE_FAIL
this%num_items=this%num_items-1
sp=>this%last
enddo
if(this%num_items.ne.0) then
this%corrupted=.true.; if(errc.eq.STACK_SUCCESS) errc=STACK_CORRUPTED
endif
else
this%corrupted=.true.; errc=STACK_CORRUPTED
endif
endif
endif
if(present(ierr)) ierr=errc
return
end subroutine stack_clean
!----------------------------------------------------------
subroutine stack_state(this,num_items,ierr,test_it)
!Returns the current state of the stack.
implicit none
class(stack_t), intent(inout):: this !in: stack
integer(INTL), intent(out):: num_items !out: total number of items in the stack
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
logical, intent(in), optional:: test_it !in: if .true., the stack linking will be tested
integer(INTD):: errc
integer(INTL):: i
class(stack_entry_t), pointer:: sp
num_items=-1
if(.not.this%corrupted) then
errc=STACK_SUCCESS
if(this%num_items.gt.0.and.associated(this%last)) then
if(present(test_it)) then
if(test_it) then
sp=>this%last
do i=1,this%num_items-1
if(associated(sp%prev)) then
sp=>sp%prev
else
this%corrupted=.true.; errc=STACK_CORRUPTED
exit
endif
enddo
if(errc.eq.STACK_SUCCESS.and.associated(sp%prev)) then
this%corrupted=.true.; errc=STACK_CORRUPTED
endif
nullify(sp)
endif
endif
if(errc.eq.STACK_SUCCESS) num_items=this%num_items
elseif(this%num_items.lt.0.or.associated(this%last)) then
this%corrupted=.true.; errc=STACK_CORRUPTED
endif
else
errc=STACK_CORRUPTED
endif
if(present(ierr)) ierr=errc
return
end subroutine stack_state
!-------------------------------------------------------
subroutine stack_push(this,item,ierr,point_only)
!Adds an item to a stack. An item can be an arbitrary object.
!It can either be cloned (default) or pointed to (point_only=TRUE).
implicit none
class(stack_t), intent(inout):: this !in: stack
class(*), intent(in), target:: item !in: item
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
logical, intent(in), optional:: point_only !in: if TRUE, the %item field will be just associated with <item> (default: false)
integer(INTD):: errc
logical:: only_point
class(stack_entry_t), pointer:: sp
if(.not.this%corrupted) then
errc=STACK_SUCCESS
if(present(point_only)) then; only_point=point_only; else; only_point=.false.; endif
if(this%num_items.gt.0.and.associated(this%last)) then !not the first item
sp=>this%last; call add_new_entry(errc)
if(errc.eq.STACK_SUCCESS) then
this%last%prev=>sp
elseif(errc.eq.STACK_CORRUPTED) then
if(associated(this%last)) this%last%prev=>sp
else
this%last=>sp
endif
sp=>NULL()
elseif(this%num_items.eq.0.and.(.not.associated(this%last))) then !first item
call add_new_entry(errc)
if(errc.eq.STACK_SUCCESS) then
this%last%prev=>NULL()
elseif(errc.eq.STACK_CORRUPTED) then
if(associated(this%last)) this%last%prev=>NULL()
else
this%last=>NULL()
endif
else
this%corrupted=.true.; errc=STACK_CORRUPTED
endif
else
errc=STACK_CORRUPTED
endif
if(present(ierr)) ierr=errc
return
contains
subroutine add_new_entry(erc)
integer(INTD), intent(out):: erc
erc=0; this%last=>NULL(); allocate(this%last,STAT=erc) !allocate a new stack entry as the last one
if(erc.eq.0) then
if(only_point) then
this%last%item=>item; this%last%pointed_only=.true.
this%num_items=this%num_items+1
else
allocate(this%last%item,source=item,STAT=erc)
if(erc.eq.0) then
this%last%pointed_only=.false.
this%num_items=this%num_items+1
else
deallocate(this%last,STAT=erc)
if(erc.eq.0) then
erc=STACK_MEM_ALLOC_FAIL
else
this%corrupted=.true.; erc=STACK_CORRUPTED
endif
endif
endif
else
this%last=>NULL(); erc=STACK_MEM_ALLOC_FAIL
endif
if(erc.eq.0) erc=STACK_SUCCESS
return
end subroutine add_new_entry
end subroutine stack_push
!-------------------------------------------
subroutine stack_pop(this,item,ierr)
!Pops up the last item out of the stack.
implicit none
class(stack_t), intent(inout):: this !in: stack
class(*), intent(out), pointer:: item !out: item
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: i,errc
class(stack_entry_t), pointer:: sp
errc=0; item=>NULL()
if(.not.this%corrupted) then
if(this%num_items.gt.0.and.associated(this%last)) then
if(associated(this%last%item)) then
item=>this%last%item; this%last%item=>NULL()
sp=>this%last%prev; this%last%prev=>NULL()
deallocate(this%last,STAT=i)
if(i.eq.0) then
this%num_items=this%num_items-1; this%last=>sp
errc=STACK_SUCCESS
else
this%num_items=this%num_items-1; this%last=>sp
errc=STACK_MEM_FREE_FAIL
endif
sp=>NULL()
else !NULL pointers cannot be stored in the stack as items
this%corrupted=.true.; errc=STACK_CORRUPTED
endif
elseif(this%num_items.lt.0.or.associated(this%last)) then
this%corrupted=.true.; errc=STACK_CORRUPTED
else
errc=STACK_EMPTY
endif
else
errc=STACK_CORRUPTED
endif
if(present(ierr)) ierr=errc
return
end subroutine stack_pop
end module stack
!----------------------------------------------------------------------
!TESTING PART:
!----------------------------------------------------------------------
module stack_test
use dil_basic
use stack
use timers, only: thread_wtime
implicit none
private
!VISIBILITY:
public dil_test_stack
contains
function dil_test_stack(perf,dev_out) result(ierr)
implicit none
integer(INTD):: ierr !out: error code (0:success)
real(8), intent(out):: perf !out: performance index
integer(INTD), intent(in), optional:: dev_out !in: default output device
!-----------------------------------------------------
integer(INTD), parameter:: MAX_ACTIONS=1000000
type base_t
integer(INTD):: int_field
real(8):: real8_field
end type base_t
type, extends(base_t):: derv_t
logical:: lgc_field
type(base_t), pointer:: base_ptr
end type derv_t
integer(INTD):: jo,i,pushed,popped
integer(INTL):: n
type(base_t), target:: base
type(derv_t):: derv
type(stack_t), pointer:: my_stack
class(*), pointer:: item
class(base_t), pointer:: bp
real(8):: rn,tm,sm
ierr=0_INTD; perf=0d0
if(present(dev_out)) then; jo=dev_out; else; jo=6_INTD; endif
base=base_t(3_INTD,-1.134d0)
derv%base_t=base_t(-11_INTD,-1.134d0); derv%lgc_field=.true.; derv%base_ptr=>base
allocate(my_stack,STAT=ierr); if(ierr.ne.0) then; call test_quit(1_INTD); return; endif
call my_stack%clean(ierr); if(ierr.ne.STACK_SUCCESS) then; call test_quit(2_INTD); return; endif
pushed=0; popped=0; sm=0d0; tm=thread_wtime()
do i=1,MAX_ACTIONS
nullify(item)
call random_number(rn)
if(rn.gt.2d-1) then !push (more often)
if(rn.gt.6d-1) then
call my_stack%push(base,ierr,point_only=.false.)
else
call my_stack%push(derv,ierr,point_only=.false.)
endif
if(ierr.ne.STACK_SUCCESS) then; call test_quit(3_INTD); return; endif
pushed=pushed+1
else !pop (less often)
call my_stack%state(n,ierr,test_it=.false.)
if(ierr.ne.STACK_SUCCESS) then; call test_quit(4_INTD); return; endif
call my_stack%pop(item,ierr)
if(.not.((n.gt.0.and.ierr.eq.STACK_SUCCESS.and.associated(item)).or.(n.le.0.and.ierr.eq.STACK_EMPTY))) then
call test_quit(5_INTD); return
endif
if(ierr.eq.STACK_SUCCESS) then
select type(item)
class is(base_t)
bp=>item; sm=sm+bp%real8_field
class default
call test_quit(6_INTD); return
end select
popped=popped+1
endif
endif
enddo
ierr=STACK_SUCCESS
do while(ierr.eq.STACK_SUCCESS)
call my_stack%pop(item,ierr)
if(ierr.eq.STACK_SUCCESS) then
popped=popped+1; if(.not.associated(item)) then; call test_quit(7_INTD); return; endif
select type(item)
class is(base_t)
bp=>item; sm=sm+bp%real8_field
class default
call test_quit(8_INTD); return
end select
endif
enddo
if(ierr.ne.STACK_EMPTY) then; call test_quit(9_INTD); return; endif
if(pushed.ne.popped) then; call test_quit(10_INTD); return; endif
tm=thread_wtime(tm); perf=dble(pushed+popped)/tm
! write(jo,*) pushed,popped,sm !debug
call my_stack%clean()
deallocate(my_stack,STAT=ierr); if(ierr.ne.0) then; call test_quit(11_INTD); return; endif
return
contains
subroutine test_quit(jerr)
integer(INTD), intent(in):: jerr
ierr=jerr
if(ierr.ne.0) then
write(jo,'("#ERROR(stack::dil_test_stack): Test failed: Error code ",i13)') ierr
write(jo,'("Please contact the developer at [email protected]")')
endif
if(associated(my_stack)) then
call my_stack%clean(); deallocate(my_stack)
endif
return
end subroutine test_quit
end function dil_test_stack
end module stack_test