Skip to content

Commit

Permalink
cache chunk size can now be user-input.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwilliams committed Dec 4, 2016
1 parent a37538e commit d48b74b
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/cache_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ module cache_module
private
integer :: n = 0 !! size of `x`
integer :: m = 0 !! size of `f`
type(fx),dimension(:),allocatable :: c !! the cache of f(x)
type(fx),dimension(:),allocatable :: c !! the cache of `f(x)`
integer :: chunk_size = 100 !! for resizing vectors
!! in the [[unique]] function
contains
private
procedure,public :: initialize => initialize_cache
Expand All @@ -44,21 +46,34 @@ module cache_module
!*******************************************************************************

!*******************************************************************************
subroutine initialize_cache(me,isize,n,m)
!>
! Initialize the cache. Must be called first before use.

subroutine initialize_cache(me,isize,n,m,chunk_size)

implicit none

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

call me%destroy()

allocate(me%c(0:isize-1))
me%n = n
me%m = m

if (present(chunk_size)) then
me%chunk_size = chunk_size
else
me%chunk_size = 100
end if

end subroutine initialize_cache
!*******************************************************************************

Expand Down Expand Up @@ -163,12 +178,12 @@ subroutine put_in_cache(me,i,x,f,ifs)
implicit none

class(function_cache),intent(inout) :: me
integer,intent(in) :: i !! index in the hash table
real(wp),dimension(:),intent(in) :: x !! independant variable vector (dimension n)
real(wp),dimension(:),intent(in) :: f !! function vector `f(x)` (dimension m)
integer,dimension(:),intent(in) :: ifs !! elements of `f` to add (should all be >0, <=m)
integer,intent(in) :: i !! index in the hash table
real(wp),dimension(:),intent(in) :: x !! independant variable vector (dimension `n`)
real(wp),dimension(:),intent(in) :: f !! function vector `f(x)` (dimension `m`)
integer,dimension(:),intent(in) :: ifs !! elements of `f` to add (should all be `>0, <=m`)

real(wp),parameter :: null = huge(1.0_wp) !! an unusual value
real(wp),parameter :: null = huge(1.0_wp) !! an unusual value to initialize arrays

if (allocated(me%c)) then
if (i<=size(me%c)) then
Expand All @@ -182,7 +197,8 @@ subroutine put_in_cache(me,i,x,f,ifs)
! this x is already present in this location.
! so merge the new f,ifs into what is already there.
if (allocated(me%c(i)%f)) then
me%c(i)%ifs = unique([me%c(i)%ifs,ifs],chunk_size=100)
me%c(i)%ifs = unique([me%c(i)%ifs,ifs],&
chunk_size=me%chunk_size)
else
allocate(me%c(i)%f(me%m))
me%c(i)%f = null ! initialize to an unusual value
Expand Down

0 comments on commit d48b74b

Please sign in to comment.