Skip to content

Commit

Permalink
remove with_stack hack (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonProtter authored Jan 12, 2025
1 parent 4ac16c3 commit b67978c
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions _literate/DCM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,17 @@ _, s_bold = get_eqidx_tagged_vars(fitmodel, "measurement"); # get bold signal
# Prepare the DCM. This function will setup the computation of the Dynamic Causal Model. The last parameter specifies that wer are using fMRI time series as opposed to LFPs.
(state, setup) = setup_sDCM(dfsol[:, String.(Symbol.(s_bold))], fitmodel, perturbedfp, csdsetup, priors, hyperpriors, indices, pmean, "fMRI");

## HACK: on machines with very small amounts of RAM, Julia can run out of stack space while compiling the code called in this loop
## this should be rewritten to abuse the compiler less, but for now, an easy solution is just to run it with more allocated stack space.
with_stack(f, n) = fetch(schedule(Task(f, n)));

# We are now ready to run the optimization procedure!
# That is we loop over run_sDCM_iteration! which will alter `state` after each optimization iteration. It essentially computes the Variational Laplace estimation of expectation and variance of the tunable parameters.
with_stack(5_000_000) do # 5MB of stack space
for iter in 1:max_iter
state.iter = iter
run_sDCM_iteration!(state, setup)
print("iteration: ", iter, " - F:", state.F[end] - state.F[2], " - dF predicted:", state.dF[end], "\n")
if iter >= 4
criterion = state.dF[end-3:end] .< setup.tolerance
if all(criterion)
print("convergence\n")
break
end
for iter in 1:max_iter
state.iter = iter
run_sDCM_iteration!(state, setup)
print("iteration: ", iter, " - F:", state.F[end] - state.F[2], " - dF predicted:", state.dF[end], "\n")
if iter >= 4
criterion = state.dF[end-3:end] .< setup.tolerance
if all(criterion)
print("convergence\n")
break
end
end
end
Expand Down

0 comments on commit b67978c

Please sign in to comment.