Skip to content

Commit 87fbe8c

Browse files
committed
use allocatable to move large local variables to heap
1 parent 6211c22 commit 87fbe8c

File tree

7 files changed

+58
-36
lines changed

7 files changed

+58
-36
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Changes from version 1.5.1 to 1.6.0
99
`n.ahead` to define constant (series-specific) `u` for future predictions,
1010
thus avoiding the need for the `newdata` if model is otherwise
1111
time-invariant.
12+
* Adjustments to Fortran codes to deal with optimizations of the flang
13+
compiler which lead to importance sampling to crash due to stack overflow.
1214

1315
Changes from version 1.5.0 to 1.5.1
1416
* Added explicit alias for KFAS-package due to changes in roxygen2.

src/covmeanw.f90

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,31 @@ subroutine covmeanwprotect(x,w,m,n,k,meanx,covx)
2929

3030
implicit none
3131
integer, intent(in) :: m, n, k
32-
integer :: t,i
32+
integer :: t,i,j,l
3333
double precision, intent(in), dimension(m,n,k) :: x
3434
double precision, intent(in), dimension(k) :: w
3535
double precision, intent(inout), dimension(m,n) :: meanx
3636
double precision, intent(inout), dimension(m,m,n) :: covx
37-
double precision, dimension(m,n,k) :: x2
37+
double precision, dimension(:,:), allocatable :: x2
3838

3939
external dgemm
40-
41-
42-
do i = 1, k
43-
meanx = meanx + x(:,:,i)*w(i)
44-
end do
45-
do i = 1, k
46-
x2(:,:,i) = sqrt(w(i))*(x(:,:,i) - meanx)
40+
do i = 1, k
41+
do j = 1, m
42+
do l = 1,n
43+
meanx(j, l) = meanx(j,l)+x(j,l,i)*w(i)
44+
end do
45+
end do
4746
end do
48-
47+
allocate(x2(m,k))
4948
do t = 1, n
50-
call dgemm('n','t',m,m,k,1.0d0,x2(:,t,:),m,x2(:,t,:),m,0.0d0,covx(:,:,t),m)
49+
do i = 1, k
50+
do j = 1, m
51+
x2(j, i) = sqrt(w(i))*(x(j,t,i) - meanx(j,t))
52+
end do
53+
end do
54+
call dgemm('n','t',m,m,k,1.0d0,x2,m,x2,m,0.0d0,covx(:,:,t),m)
5155
end do
52-
56+
deallocate(x2)
5357
end subroutine covmeanwprotect
5458

5559
subroutine varmeanw(x,w,m,n,k,meanx,varx,var)

src/isample.f90

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ subroutine isample(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, dist, &
3030
double precision, intent(inout), dimension(n,p) :: theta
3131
double precision, dimension(p,p,n) :: ht
3232
double precision, intent(inout), dimension(simdim,n,3 * nsim * antithetics + nsim) :: sim
33-
double precision, dimension(p,(3 * nsim * antithetics + nsim)*(5-simwhat)) :: tsim
3433
double precision, dimension(n,p) :: ytilde
3534
double precision, dimension(n) :: tmp
36-
double precision, dimension(3 * nsim * antithetics + nsim) :: w
35+
double precision, intent(inout), dimension(3 * nsim * antithetics + nsim) :: w
3736
double precision :: diff
3837
double precision, external :: ddot
3938
double precision :: lik
39+
double precision, dimension(:,:), allocatable :: tsim
4040

4141
external approx, simgaussian
4242

@@ -112,6 +112,8 @@ subroutine isample(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, dist, &
112112
end do
113113

114114
else
115+
116+
allocate(tsim(p,(3 * nsim * antithetics + nsim)*(5-simwhat)))
115117
do j=1,p
116118
select case(dist(j))
117119
case(2) !poisson
@@ -165,9 +167,7 @@ subroutine isample(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, dist, &
165167
end do
166168
end select
167169
end do
168-
169-
170+
deallocate(tsim)
170171
end if
171-
172172

173173
end subroutine isample

src/isamplefilter.f90

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ subroutine isamplefilter(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, d
3030
double precision, intent(inout), dimension(n,p) :: theta
3131
double precision, dimension(p,p,n) :: ht
3232
double precision, intent(inout), dimension(simdim,n,3 * nsim * antithetics + nsim) :: sim
33-
double precision, dimension(p,(3 * nsim * antithetics + nsim)*(5-simwhat)) :: tsim
33+
double precision, intent(inout), dimension(n,3 * nsim * antithetics + nsim) :: w
3434
double precision, dimension(n,p) :: ytilde
3535
double precision, dimension(n) :: tmp
36-
double precision, dimension(n,3 * nsim * antithetics + nsim) :: w
3736
double precision, external :: ddot
3837

3938
integer, dimension(n,p) :: ymiss2
4039
double precision, dimension(p,n,nsim) :: epsplus2
4140
double precision, dimension(r,n,nsim) :: etaplus2
4241
double precision, dimension(m,nsim) :: aplus12
43-
double precision, dimension(simdim,n,3 * nsim * antithetics + nsim) :: sim2
4442
double precision :: diff
4543
double precision :: lik
46-
44+
double precision, dimension(:,:), allocatable :: tsim
45+
double precision, dimension(:,:,:), allocatable :: sim2
4746
external approx, simgaussian
47+
allocate(sim2(simdim,n,3 * nsim * antithetics + nsim))
4848

4949
ht=0.0d0
5050
ytilde=0.0d0
@@ -155,6 +155,7 @@ subroutine isamplefilter(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, d
155155
end do
156156

157157
else
158+
allocate(tsim(p,(3 * nsim * antithetics + nsim)*(5-simwhat)))
158159
do j=1,p
159160
select case(dist(j))
160161
case(2) !poisson
@@ -208,9 +209,10 @@ subroutine isamplefilter(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, d
208209
end do
209210
end select
210211
end do
212+
deallocate(tsim)
211213
end if
212214
sim(:,i+1,:) = sim2(:,i+1,:)
213215
end do
214216
maxiter=maxitermax
215-
217+
deallocate(sim2)
216218
end subroutine isamplefilter

src/ngfilter.f90

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ subroutine ngfilter(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, theta,
3535
double precision, intent(inout), dimension((p-1)*smoothy+1,(p-1)*smoothy+1,(n-1)*smoothy+1) :: yvar
3636
double precision, intent(inout), dimension((p-1)*smooths+1,(n-1)*smooths+1) :: thetahat
3737
double precision, intent(inout), dimension((p-1)*smooths+1,(p-1)*smooths+1,(n-1)*smooths+1) :: thetavar
38-
double precision, dimension(smootha*m+(1-smootha)*p,n,4*nsim) :: sim
39-
double precision, dimension(smootha*p+(1-smootha),n,4*nsim*smootha+(1-smootha)) :: osim
40-
double precision, dimension(n,4*nsim) :: w
4138
double precision, dimension(p,m) :: pm
42-
double precision, external :: ddot
39+
double precision, dimension(:,:,:), allocatable :: sim
40+
double precision, dimension(:,:,:), allocatable :: osim
41+
double precision, dimension(:,:), allocatable :: w
4342

43+
double precision, external :: ddot
4444
external isamplefilter, covmeanwprotect, dgemv, dsymm, dgemm, covmeanw
45+
46+
allocate(w(n,4*nsim))
47+
allocate(sim(smootha*m+(1-smootha)*p,n,4*nsim))
4548

4649
if(smootha.EQ.1) then
4750

@@ -67,6 +70,7 @@ subroutine ngfilter(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, theta,
6770
end if
6871

6972
if(smoothy.EQ.1) then
73+
allocate(osim(smootha*p+(1-smootha),n,4*nsim*smootha+(1-smootha)))
7074
do t = 1, n
7175
do j = 1,p
7276
call dgemv('t',m,4*nsim,1.0d0,sim(:,t,:),m,zt(j,:,(t-1)*timevar(1)+1),1,0.0d0,osim(j,t,:),1)
@@ -90,6 +94,7 @@ subroutine ngfilter(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, theta,
9094
do t=1,n
9195
call covmeanw(osim(:,t,:),w(t,:),p,1,4*nsim,yhat(:,t),yvar(:,:,t))
9296
end do
97+
deallocate(osim)
9398
end if
9499
else
95100
call isamplefilter(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, dist, &
@@ -129,6 +134,7 @@ subroutine ngfilter(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, theta,
129134
call covmeanw(sim(:,t,:),w(t,:),p,1,4*nsim,yhat(:,t),yvar(:,:,t))
130135
end do
131136
end if
132-
133137
end if
138+
deallocate(sim)
139+
deallocate(w)
134140
end subroutine ngfilter

src/ngloglik.f90

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ subroutine ngloglik(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, p,m, &
3131
double precision, intent(inout), dimension(p,n,nsim) :: epsplus
3232
double precision, intent(inout), dimension(r,n,nsim) :: etaplus
3333
double precision, intent(inout) :: lik
34-
double precision, dimension(p,n,nsim2) :: tsim
3534
double precision, dimension(n) :: tmp
36-
double precision, dimension(nsim2) :: w
3735
double precision, intent(inout) :: diff
36+
double precision, dimension(:), allocatable :: w
37+
double precision, dimension(:,:,:), allocatable :: tsim
3838
double precision, external :: ddot
3939

4040
external approx, marginalxx, dpoisf, dnormf, dbinomf, dgammaf, dnbinomf, simgaussian
@@ -93,7 +93,8 @@ subroutine ngloglik(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, p,m, &
9393
end do
9494

9595
if(sim .EQ. 1) then
96-
96+
allocate(w(nsim))
97+
allocate(tsim(p, n, nsim2))
9798
w=1.0d0
9899
info2 = 0
99100

@@ -151,7 +152,8 @@ subroutine ngloglik(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, p,m, &
151152
info = info2
152153
return
153154
end if
154-
155+
deallocate(tsim)
156+
deallocate(w)
155157
end if
156158

157159
end subroutine ngloglik

src/ngsmooth.f90

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ subroutine ngsmooth(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, theta,
3535
double precision, intent(inout), dimension((p-1)*smoothy+1,(p-1)*smoothy+1,(n-1)*smoothy+1) :: yvar
3636
double precision, intent(inout), dimension((p-1)*smooths+1,(n-1)*smooths+1) :: thetahat
3737
double precision, intent(inout), dimension((p-1)*smooths+1,(p-1)*smooths+1,(n-1)*smooths+1) :: thetavar
38-
double precision, dimension(smootha*m+(1-smootha)*p,n,4*nsim) :: sim
39-
double precision, dimension(smootha*p+(1-smootha),n,4*nsim*smootha+(1-smootha)) :: osim
40-
double precision, dimension(4*nsim) :: w
4138
double precision, dimension(p,m) :: pm
39+
double precision, dimension(:,:,:), allocatable :: sim
40+
double precision, dimension(:,:,:), allocatable :: osim
41+
double precision, dimension(:), allocatable :: w
4242
double precision, external :: ddot
43-
4443
external isample, covmeanwprotect, dgemv, dsymm, dgemm, covmeanw
44+
45+
allocate(sim(smootha*m+(1-smootha)*p,n,4*nsim))
46+
allocate(w(4*nsim))
4547

4648
if(smootha.EQ.1) then
4749

@@ -66,6 +68,7 @@ subroutine ngsmooth(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, theta,
6668
end if
6769

6870
if(smoothy.EQ.1) then
71+
allocate(osim(smootha*p+(1-smootha),n,4*nsim*smootha+(1-smootha)))
6972
do t = 1, n
7073
do j = 1,p
7174
call dgemv('t',m,4*nsim,1.0d0,sim(:,t,:),m,zt(j,:,(t-1)*timevar(1)+1),1,0.0d0,osim(j,t,:),1)
@@ -86,6 +89,8 @@ subroutine ngsmooth(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, theta,
8689
end select
8790
end do
8891
call covmeanw(osim,w,p,n,4*nsim,yhat,yvar)
92+
93+
deallocate(osim)
8994
end if
9095
else
9196
call isample(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, dist, &
@@ -120,5 +125,6 @@ subroutine ngsmooth(yt, ymiss, timevar, zt, tt, rtv, qt, a1, p1,p1inf, u, theta,
120125
end if
121126

122127
end if
123-
128+
deallocate(sim)
129+
deallocate(w)
124130
end subroutine ngsmooth

0 commit comments

Comments
 (0)