# How to use Stanc3 compiler optimization flag --O --O1 in cmdstanr?

**URL:** <https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365>\
**Category:** CmdStan\
**Tags:** stanc3, cmdstanr\
**Created:** [February 15, 2022, 5:23pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365 "2022-02-15T17:23:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michelle](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@Michelle](https://discourse.mc-stan.org/u/Michelle)\
**Post date:** [February 15, 2022, 5:23pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/1 "2022-02-15T17:23:32Z")

</div>

Dear Stan community,

I just read the new Stan 2.29.0 release note [Release of CmdStan 2.29 – The Stan Blog](https://blog.mc-stan.org/2022/02/15/release-of-cmdstan-2-29/) and found these auto-formatter, canonicalizer and optimization are very interesting. And I was not aware of these features previously. May I ask if one can apply this in cmdstanr? like some flags when compiling the model? Except this nice demo [Stan-to-C++ optimizations](https://rok-cesnovar.github.io/stanc3js-demo/optimization.html) from @rok_cesnovar, is there a way for R or Python users to automatically apply these features? Thank you very much.

Michelle

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [February 15, 2022, 7:07pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/2 "2022-02-15T19:07:29Z")

</div>

Hi,

I apologize for the lack of a cmdstanr example. Here you go:

```r
library(cmdstanr)

n <- 25000
k <- 10
X <- matrix(rnorm(n * k), ncol = k)
y <- rbinom(n, size = 1, prob = plogis(3 * X[,1] - 2 * X[,2] + 1))
mdata <- list(k = k, n = n, y = y, X = X)

mod_opt <- cmdstan_model("lr.stan", stanc_options = list("O1"))
fit_opt <- mod_opt$sample(data = mdata, chains = 1, refresh = 500)

```

This was used with the following model:

```stan
data {
  int<lower=1> k;
  int<lower=0> n;
  matrix[n, k] X;
  array[n] int y;
}
parameters {
  vector[k] beta;
  real alpha;
}
model {
  target += std_normal_lpdf(beta | );
  target += std_normal_lpdf(alpha | );
  target += bernoulli_logit_lpmf(y | alpha + X* beta);
}

```

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [February 15, 2022, 7:10pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/3 "2022-02-15T19:10:25Z")

</div>

Also a quick script to see the comparison in terms of speed:

```r
library(cmdstanr)

n <- 25000
k <- 10
X <- matrix(rnorm(n * k), ncol = k)
y <- rbinom(n, size = 1, prob = plogis(3 * X[,1] - 2 * X[,2] + 1))
mdata <- list(k = k, n = n, y = y, X = X)

mod_no_opt <- cmdstan_model("lr.stan", exe_file = "no_opt_lr")
mod_opt <- cmdstan_model("lr.stan", exe_file = "opt_lr", stanc_options = list("O1"))

fit_no_opt <- mod_no_opt$sample(data = mdata, chains = 1, refresh = 500)

fit_opt <- mod_opt$sample(data = mdata, chains = 1, refresh = 500)

print(fit_no_opt$time()$total)
print(fit_opt$time()$total)

```

The `exe_file` is there so we can make 2 executables from the same model file.

```nohighlight
The results are:
Chain 1 Iteration: 1 / 2000 [0%] (Warmup) 
Chain 1 Iteration: 500 / 2000 [25%] (Warmup) 
Chain 1 Iteration: 1000 / 2000 [50%] (Warmup) 
Chain 1 Iteration: 1001 / 2000 [50%] (Sampling) 
Chain 1 Iteration: 1500 / 2000 [75%] (Sampling) 
Chain 1 Iteration: 2000 / 2000 [100%] (Sampling) 
Chain 1 finished in 17.8 seconds.
Running MCMC with 1 chain...

Chain 1 Iteration: 1 / 2000 [0%] (Warmup) 
Chain 1 Iteration: 500 / 2000 [25%] (Warmup) 
Chain 1 Iteration: 1000 / 2000 [50%] (Warmup) 
Chain 1 Iteration: 1001 / 2000 [50%] (Sampling) 
Chain 1 Iteration: 1500 / 2000 [75%] (Sampling) 
Chain 1 Iteration: 2000 / 2000 [100%] (Sampling) 
Chain 1 finished in 10.6 seconds.
[1] 17.94077
[1] 10.78738

```

To not get everyone’s hopes too high, I would not expect as huge gains every time :)

---

<div class="post-metadata">

**Author:** ![Michelle](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@Michelle](https://discourse.mc-stan.org/u/Michelle)\
**Post date:** [February 16, 2022, 3:24pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/4 "2022-02-16T15:24:15Z")

</div>

This is amazing. Thank you very much for developing this. I also liked so much the online demo which is a nice tool for me to check what I could improve/canonicalize on my dirty Stan code. It’s also like a home tutor to learn some advanced features of Stan based on my own model. :)

May I ask if I could blindly apply this -O1 to all models or it is better to always compare?

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [February 16, 2022, 4:21pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/5 "2022-02-16T16:21:34Z")

</div>

I would recommend comparison if you are able, especially while the optimizations are still being developed. If you run into any incorrect behavior or compilation problems when using it that don’t occur without `O1`, we would really love to hear about it

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [February 16, 2022, 4:38pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/6 "2022-02-16T16:38:52Z")

</div>

> [@Michelle](#):
>
> May I ask if I could blindly apply this -O1 to all models

We have found at least one model which segfaults with -O1, so I would not use it yet blindly.

---

<div class="post-metadata">

**Author:** ![Michelle](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@Michelle](https://discourse.mc-stan.org/u/Michelle)\
**Post date:** [March 1, 2022, 5:02pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/7 "2022-03-01T17:02:02Z")

</div>

> [@rok\_cesnovar](#):
>
> ```no-highlight
> mod_no_opt <- cmdstan_model("lr.stan", exe_file = "no_opt_lr")
> mod_opt <- cmdstan_model("lr.stan", exe_file = "opt_lr", stanc_options = list("O1"))
> 
> ```

Hi @rok_cesnovar, just to feedback on what I played with this optimization flag. I could reproduce your example, no\_opt took 14.9s, whereas opt took 20.3s on my machine. However, the option `exe_file` seems not working and giving me error like

```no-highlight
Error in self$compile(...) : unused argument (exe_file = "no_opt_lr")

```

So I have to delete the .exe file and recompile every time I switch between opt and no\_opt. Do you have any idea where I am wrong? I think this `exe_file` is very convenient and would like to use it. Thanks a lot.

Btw, I also tried on my hierarchical variate-covariate model with AR(1) residual, it took 262.8s with opt and 285.3 without opt. All tests are using the same seed.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [March 1, 2022, 5:08pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/8 "2022-03-01T17:08:55Z")

</div>

> [@Michelle](#):
>
> So I have to delete the .exe file and recompile every time I switch between opt and no\_opt. Do you have any idea where I am wrong? I think this `exe_file` is very convenient and would like to use it. Thanks a lot.

This was added just recently. Run:

```r
remotes::install_github("stan-dev/cmdstanr")

```

and then try again.

> [@Michelle](#):
>
> Btw, I also tried on my hierarchical variate-covariate model with AR(1) residual, it took 262.8s with opt and 285.3 without opt. All tests are using the same seed.

Thanks for the feedback!

---

<div class="post-metadata">

**Author:** ![Michelle](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@Michelle](https://discourse.mc-stan.org/u/Michelle)\
**Post date:** [March 1, 2022, 6:11pm UTC](https://discourse.mc-stan.org/t/how-to-use-stanc3-compiler-optimization-flag-o-o1-in-cmdstanr/26365/9 "2022-03-01T18:11:09Z")

</div>

> [@rok\_cesnovar](#):
>
> `remotes::install_github("stan-dev/cmdstanr")`

It works, just it seems that I have to specify the extension as .exe otherwise it will tell me `Error in rethrow_call(c_processx_exec, command, c(command, args), pty, : file not found. `
