Writing new Stan C++ code: share your tips!

My colleagues and I are trying to add some C++ functionality to Stan (specifically we want to write some new functions for solving steady state problems). At first we will just use the new functions for our own problems but we are keen to eventually add to Stan’s math library if it seems like they would be helpful for other people.

We’d like to copy as many as possible best practices from people who have more experience of writing Stan-compatible C++. There is a lot of great information already available for getting started, but we’d really like to learn some more informal things - what kind of setups and tools do you use, what are some common pitfalls, what do you wish you had known about sooner?

I have listed some semi-specific questions below but mostly I thought this might be a good place for people to share their experiences.

Debugging

I found on this page that it’s possible to build the Stan math library with support for debuggers. Do you use this functionality a lot, and if so, how? Do you debug through an IDE or use something like gdb from the terminal?

Windows

One of our team members would prefer if possible to work in Visual studio on windows as this is their most familiar development environment. I understand from here that there are some specific problems with setting up the required toolchain on windows, but I’d like to know what other relevant consideration there are and , if someone is actively doing Stan C++ development on windows, if they have any handy tips.

Compilation

When we first tried working on the math libary we found that we spent a lot of time waiting for code to compile, as the whole system needs to be rebuilt after any change. I think we can get around this problem to some extent by putting prototype code in external header files as described here, but I guess at some point it will pop up again. Do you have any tips for either avoiding or speeding up the building process?

3 Likes

Nice! Yes so on the Stan math docs site we have a few contributor docs with splainers of best practices that might be useful

http://mc-stan.org/math/getting_started.html

For writing functions that will go into Stan models I’d say a good starting place is to write the function in Stan, then look at the output C++ for the function. You will want to the signature to exactly match how the function is defined there. From there you can follow the contributor docs for making specializations that use reverse mode

2 Likes

For the compilation speed: Use clang++ and turn off most optimisations. That should help. clang++ is a lot faster with templated code.

Is this steady-state thing for ODEs? I am just curious.

1 Like

Thanks for these tips!

We are trying to model steady states of metabolic networks - our main problem is to find the steady state metabolite concentrations for a given set of kinetic parameters and boundary conditions. Our current approach is to assume an initial concentration and solve the Initial Value Problem of the constructed system of non-linear ODEs. We simulate for a reasonably long period and as long as the concentrations don’t change within our tolerances we assume this to be the steady state concentration. We think there is some room to improve on this method since we’re not interested in the trajectory of how to get to the solution.

You can read more at the project’s documentation site and github, or check out a bare-bones Stan example here : we’re always keen for discussion and feedback!

1 Like

Can they use clang interface inside Visual Studio?

Sorry for delay - the answer is yes!