# Saving executables compiled using RStan

**URL:** <https://discourse.mc-stan.org/t/saving-executables-compiled-using-rstan/2072>\
**Category:** Interfaces\
**Tags:** cmdstan, rstan\
**Created:** [September 30, 2017, 6:16am UTC](https://discourse.mc-stan.org/t/saving-executables-compiled-using-rstan/2072 "2017-09-30T06:16:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [September 30, 2017, 6:16am UTC](https://discourse.mc-stan.org/t/saving-executables-compiled-using-rstan/2072/1 "2017-09-30T06:16:02Z")

</div>

Operating System: Linux  
Interface Version: Rstan 2.16.2, cmdstan 2.17  
Compiler/Toolkit: g++

Hi, I have been fitting models with a fairly large number of parameters (~40,000). I am not having a problem fitting the models, they just take a lot of draws. My problem is that the draws take up a lot of space! I have some good hardware. Storage is not an issue, but given memory constraints I can only run 4 chains using Rstan, even though I have 16 cores.

This seems to be because Rstan is preparing to load all the chains into the fit object after sampling is done. In my setting I would rather run lots of chains and worry about building the fit object later (maybe I will use virtual memory, or just look at the chains for certain subsets of parameters). Fortunantly, cmdstan affords exactly this. I can run 16 chains no problem!

The downside to cmdstan for me is losing Rstan’s friendly compilation interface which automatically recompiles the model if necessary, doesn’t expose me to a bunch of makefiles and so on.

I can’t seem to find a way to save the compiled stan program in the Rstan interface. Am I missing it?

Thanks!

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 30, 2017, 3:34pm UTC](https://discourse.mc-stan.org/t/saving-executables-compiled-using-rstan/2072/2 "2017-09-30T15:34:24Z")

</div>

> [@groceryheist](#):
>
> I can’t seem to find a way to save the compiled stan program in the Rstan interface.

You can call `stan_model` to compile the Stan program and then call `sampling`, but I don’t think that is going to help you much.

If you call `stan` or `sampling` specifying `pars = character()` and `sample_file` as some path, then you are basically doing CmdStan. Then you have the problem of how to read the draws off the disk with the available memory. The `read_stan_csv` function does not have an option to read a subset of the parameters.

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [September 30, 2017, 4:40pm UTC](https://discourse.mc-stan.org/t/saving-executables-compiled-using-rstan/2072/3 "2017-09-30T16:40:14Z")

</div>

Thanks. That should do what I want. I think I can hack around the limitations of read\_stan\_csv. Would it be cool if I add a pars= argument to read\_cmd\_stan?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 30, 2017, 4:42pm UTC](https://discourse.mc-stan.org/t/saving-executables-compiled-using-rstan/2072/4 "2017-09-30T16:42:17Z")

</div>

Yeah and an include flag. The read\_stan\_csv function (should, and probably will for rstan3) take in a stanmodel so that it knows the dimensions of things. Then it would be easier to include / exclude containers of parameters.

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [September 30, 2017, 5:18pm UTC](https://discourse.mc-stan.org/t/saving-executables-compiled-using-rstan/2072/5 "2017-09-30T17:18:25Z")

</div>

Sounds good. It turns out that when I set pars=character() Rstan still does the huge allocation. I guess this is a bug.

In my case I can put `pars=c(‘param1’), and the allocation is much smaller and I should be able to run all the chains.

I will still need to hack read\_stan\_csv to fully examine all my parameters in the end.

Thanks again bgoodri.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 30, 2017, 6:04pm UTC](https://discourse.mc-stan.org/t/saving-executables-compiled-using-rstan/2072/6 "2017-09-30T18:04:48Z")

</div>

It might be `pars = NULL` or `pars = NA` or something. I never use that feature, so I forget. It is probably documented.
