The problem with memory arises when I calculate the likelihood. Calling the likelihood for the first time allocates 1.3 GB, which won't be released later on. The next time the likelihood is called around 0.18 GB memory leaks. The numbers depend on the choice of accuracy parameters, especially with lSampleBoost and IntkAccuracyBoost. For standard settings, it is around 10 MB. When running the MCMC normally with action = 0, this memory leak does not occur. The initial 1.3 GB are allocated and then the used memory stays constant. I don't understand why this leak occurs in my case, but not when running the MCMC.
Am I doing something wrong in the way I call the likelihood function?
I wrote a little test subroutine, which shows the described memory leak. It is a simple loop changing one parameter (A_s in this case) slightly and then calculating the likelihood. After calling the likelihood, the used memory is printed calling another function called Memory. This is the subroutine calling the likelihood:
Code: Select all
subroutine TChainSampler_Tests(this, Params)
class(TChainSampler) :: this
class(ParamSet) :: Params
real(mcp) :: Likelihood
integer :: i, MemGB
integer :: which_param = 17 ! This refers to A_s
print*, 'Allocation'
call this%Memory(MemGB)
print*, 'Start Sampling'
do i=1, 100
print*, i
Params%P(which_param) = Params%P(which_param)+ BaseParams%PWidth(which_param)
Likelihood = this%LogLike(Params)
call this%Memory(MemGB)
end do
end subroutine TChainSampler_Tests
Code: Select all
subroutine TSamplingAlgorithm_Memory(this, valueRSS)
! use ifport !if on intel compiler
class(TSamplingAlgorithm) :: this
integer, intent(out) :: valueRSS
character(len=200):: filename=' '
character(len=80) :: line
character(len=8) :: pid_char=' '
integer :: pid
logical :: ifxst
valueRSS=-1 ! return negative number if not found
!--- get process ID
pid=getpid()
write(pid_char,'(I8)') pid
filename='/proc/'//trim(adjustl(pid_char))//'/status'
!--- read system file
inquire (file=filename,exist=ifxst)
if (.not.ifxst) then
write (*,*) 'system file does not exist'
return
endif
open(unit=100, file=filename, action='read')
do
read (100,'(a)',end=120) line
if (line(1:6).eq.'VmRSS:') then
read (line(7:),*) valueRSS
exit
endif
enddo
120 continue
close(100)
write(*,*) valueRSS , 'Used memory'
return
end subroutine TSamplingAlgorithm_Memory
Also: I have tested this with gfortran 7.4, as well as different ifort versions and the problem occurs with all compilers/versions. I have also tried different likelihoods with Planck, unbinned Planck and without Planck. I tried this with the recent May 2020 version (CAMB 1.1.2) and the previous July 2019 version(CAMB 1.0.7) of CosmoMC.