Skip to content

Commit d48b74b

Browse files
committed
cache chunk size can now be user-input.
1 parent a37538e commit d48b74b

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/cache_module.f90

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ module cache_module
3030
private
3131
integer :: n = 0 !! size of `x`
3232
integer :: m = 0 !! size of `f`
33-
type(fx),dimension(:),allocatable :: c !! the cache of f(x)
33+
type(fx),dimension(:),allocatable :: c !! the cache of `f(x)`
34+
integer :: chunk_size = 100 !! for resizing vectors
35+
!! in the [[unique]] function
3436
contains
3537
private
3638
procedure,public :: initialize => initialize_cache
@@ -44,21 +46,34 @@ module cache_module
4446
!*******************************************************************************
4547

4648
!*******************************************************************************
47-
subroutine initialize_cache(me,isize,n,m)
49+
!>
50+
! Initialize the cache. Must be called first before use.
51+
52+
subroutine initialize_cache(me,isize,n,m,chunk_size)
4853

4954
implicit none
5055

5156
class(function_cache),intent(inout) :: me
5257
integer,intent(in) :: isize !! the size of the hash table
5358
integer,intent(in) :: n !! number of independant variables (x)
5459
integer,intent(in) :: m !! number of functions (f)
60+
integer,intent(in),optional :: chunk_size !! chunk size to speed up reallocation
61+
!! of arrays. A good value is a guess for
62+
!! the actual number of elements of `f` that
63+
!! will be saved per value of `x` [default is 100]
5564

5665
call me%destroy()
5766

5867
allocate(me%c(0:isize-1))
5968
me%n = n
6069
me%m = m
6170

71+
if (present(chunk_size)) then
72+
me%chunk_size = chunk_size
73+
else
74+
me%chunk_size = 100
75+
end if
76+
6277
end subroutine initialize_cache
6378
!*******************************************************************************
6479

@@ -163,12 +178,12 @@ subroutine put_in_cache(me,i,x,f,ifs)
163178
implicit none
164179

165180
class(function_cache),intent(inout) :: me
166-
integer,intent(in) :: i !! index in the hash table
167-
real(wp),dimension(:),intent(in) :: x !! independant variable vector (dimension n)
168-
real(wp),dimension(:),intent(in) :: f !! function vector `f(x)` (dimension m)
169-
integer,dimension(:),intent(in) :: ifs !! elements of `f` to add (should all be >0, <=m)
181+
integer,intent(in) :: i !! index in the hash table
182+
real(wp),dimension(:),intent(in) :: x !! independant variable vector (dimension `n`)
183+
real(wp),dimension(:),intent(in) :: f !! function vector `f(x)` (dimension `m`)
184+
integer,dimension(:),intent(in) :: ifs !! elements of `f` to add (should all be `>0, <=m`)
170185

171-
real(wp),parameter :: null = huge(1.0_wp) !! an unusual value
186+
real(wp),parameter :: null = huge(1.0_wp) !! an unusual value to initialize arrays
172187

173188
if (allocated(me%c)) then
174189
if (i<=size(me%c)) then
@@ -182,7 +197,8 @@ subroutine put_in_cache(me,i,x,f,ifs)
182197
! this x is already present in this location.
183198
! so merge the new f,ifs into what is already there.
184199
if (allocated(me%c(i)%f)) then
185-
me%c(i)%ifs = unique([me%c(i)%ifs,ifs],chunk_size=100)
200+
me%c(i)%ifs = unique([me%c(i)%ifs,ifs],&
201+
chunk_size=me%chunk_size)
186202
else
187203
allocate(me%c(i)%f(me%m))
188204
me%c(i)%f = null ! initialize to an unusual value

0 commit comments

Comments
 (0)