2D AMRCLAW
igetsp.f
Go to the documentation of this file.
1 c
2 c ----------------------------------------------------------
3 c
4  function igetsp (nwords)
5 c
6  use amr_module
7  implicit double precision (a-h,o-z)
8 
9 c
10 c ::::::::::::::::::::::::::: IGETSP ::::::::::::::::::::::::::::
11 c
12 c allocate contiguous space of length nword in main storage array
13 c alloc. user code (or pointer to the owner of this storage)
14 c is mptr. lenf = current length of lfree list.
15 c
16 c ::::::::::::::::::::::::::: IGETSP ::::::::::::::::::::::::::::
17 c
18 
19 !$OMP CRITICAL (MemMgmt)
20 
21 c find first fit from free space list
22 c
23  10 continue
24  itake = 0
25  do 20 i = 1, lenf
26  if (lfree(i,2) .lt. nwords) go to 20
27  itake = i
28  go to 25
29  20 continue
30  go to 900
31 c
32 c anything left?
33 c
34  25 left = lfree(itake,2) - nwords
35  igetsp = lfree(itake,1)
36  iendtake = lfree(itake,1) + nwords
37  if (lendim .lt. iendtake) lendim = iendtake
38 c
39 c the following code which is ignored for now adds the new
40  if (left .le. 0) go to 30
41  lfree(itake,2) = left
42  lfree(itake,1) = iendtake
43  go to 99
44 c
45 c item is totally removed. move next items in list up one.
46 c
47  30 lenf = lenf - 1
48  do 40 i = itake, lenf
49  lfree(i,1) = lfree(i+1,1)
50  40 lfree(i,2) = lfree(i+1,2)
51  go to 99
52 c
53  900 write(outunit,901) nwords
54  write(*,901) nwords
55  901 format(' require ',i10,' words - either none left or not big',
56  1 ' enough space')
57  write(outunit,902) ((lfree(i,j),j=1,2),i=1,lenf)
58  write(*,902) ((lfree(i,j),j=1,2),i=1,lenf)
59  902 format(' free list: ',//,2x,50(i10,4x,i10,/,2x))
60 
61  ! Dynamic memory adjustment
62  ! Attempt to allocate new memory
63  factor = 2.0d0
64  istatus = 1
65  old_memsize = memsize
66  do while (istatus > 0)
67  factor = 0.5d0 * factor
68  if (factor < 0.1d0) then
69  print *, 'Allocation failed, not enough memory'
70  stop
71  endif
72  new_size = ceiling((1.d0+factor) * memsize)
73  iadd_size = ceiling(factor * memsize)
74  call resize_storage(new_size,istatus)
75  enddo
76 
77  if (lfree(lenf-1,1) + lfree(lenf-1,2) - 1 .eq. old_memsize) then
78  ! Merge new block with last free block on list, adjust sentinel to
79  ! reflect new memory size
80  lfree(lenf-1,2) = iadd_size + lfree(lenf-1,2)
81  lfree(lenf,1) = new_size + 2
82  else
83  ! New free block added to end, make new sentinel
84  lfree(lenf,1) = old_memsize+1
85  lfree(lenf,2) = iadd_size
86  lfree(lenf+1,1) = new_size+2
87  lfree(lenf+1,2) = 0
88  lenf = lenf + 1
89  endif
90  go to 10
91 
92  99 lentot = lentot + nwords
93  if (lenmax .lt. lentot) lenmax = lentot
94  if (sprint) write(outunit,100) nwords, igetsp, lentot, lenmax
95  100 format(' allocating ',i8,' words in location ',i8,
96  1 ' lentot, lenmax ', 2i10)
97 
98 !$OMP END CRITICAL (MemMgmt)
99 
100  return
101  end
function igetsp(nwords)
Definition: igetsp.f:4
subroutine resize_storage(new_size, status)