Stan memory management

Hi,

Is there documentation anywhere on empty_nested and recover_memory? I assume I’m tripping these calls because I’m attempting to put these tests in the same binary, and they aren’t cleaning up correctly after themselves - figured I’d ask here before going over the code.

The following files and fixtures inside trigger an exception because the SetUp of the test fixture calls recover_memory() while empty_nested() is false.

mat/functor/idas_integrator_test.cpp
idas_ivp_system_yy0
foward_sensitivity_theta_chemical
forward_sensitivity_theta_prey
error_handling

rev/mat/functor/cvodes_ode_data_prim_test.cpp
recover_exception

rev/mat/functor/cvodes_ode_data_rev_test.cpp
Basically everything

rev/mat/functor/integrate_dae_test.cpp
idas_ivp_system_yy0
forward_sensitivity_theta
inconsistent_ic_error

rev/mat/fun/append_row_test.cpp
append_row_matrix
append_row_row_vector

rev/mat/fun/append_col_test.cpp
append_col_matrix
append_col_row_vector

rev/mat/fun/matrix_exp_multiply_test.cpp
matrix_multiply_exp__vd__segfault

rev/mat/fun/gp_periodic_cov_test.cpp
everything

rev/mat/fun/gp_exp_quad_cov_test.cpp
everything

rev/mat/fun/cov_exp_quad_test.cpp
everything

rev/mat/err/check_consistent_sizes_test.cpp
rev/mat/err/check_pos_semidefinite_test.cpp

I think the tests are sloppy written in that regard. If you put things together that needs clean-up.

You would have to call recover_memory_nested() until empty_nested() is true. Then you can call recover_memory().

Should I fix it in my monorepo branch or submit an issue to stan math and wait? If I fix it myself, is there any reason I should not change recover_memory to recursively call recover_memory_nested()?

Are these memory arenas thread safe, i.e. can I run unit tests in parallel, or should I force them to run serially using a mutex or equivalent method?

We should probably fix that in Stan-math… so please file an issue there.

Nothing is thread-safe in Stan-math unless you define STAN_THREADS during compilation… but doing that would require proper initialisation per thread; I am not sure we want that in the tests… rather we test things with and without STAN_THREADS (at least at the moment).

1 Like