# Custom c++ using cmdstanr?

**URL:** <https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528>\
**Category:** CmdStan\
**Tags:** cmdstanr\
**Created:** [November 27, 2020, 10:41pm UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528 "2020-11-27T22:41:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [November 27, 2020, 10:41pm UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/1 "2020-11-27T22:41:06Z")

</div>

Is this possible somehow? Spent a bit of time coding a function and then found out complex number autodiff hasn’t made it to rstan, would like to test it…

---

<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:** [November 29, 2020, 10:51pm UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/2 "2020-11-29T22:51:28Z")

</div>

It is possible. Its not as friendly as we would like it to be, but can be done. Not at my computer right now, but can show an example tomorrow. You need to use cmdstans user\_header make flag essentialy.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [November 29, 2020, 10:59pm UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/3 "2020-11-29T22:59:05Z")

</div>

Yeah we haven’t yet done anything in cmdstanr to make this easier than how you would have to do it with cmdstan itself, but since cmdstan supports this it should be possible to already do it via cmdstanr too. The [Using External C++ Code](https://mc-stan.org/docs/2_25/cmdstan-guide/stanc.html#using-external-c-code) section of the cmdstan guide has the details for getting it to work with cmdstan.

---

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [November 30, 2020, 8:46am UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/4 "2020-11-30T08:46:52Z")

</div>

have had a few guesses at the syntax but the closest I think I got was this

> ```
> cmdstan_model('test.stan',include_paths=getwd(),
> cpp_options=list(STANCFLAGS='--allow-undedefined', USER_HEADER='<syl2.hpp>'))
> 
> ```

Which gives me "unknown option ‘–allow-undedefined’ " and then a long list of options, among them, --allow-undefined ;)

---

<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:** [November 30, 2020, 9:24am UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/5 "2020-11-30T09:24:53Z")

</div>

For STANCFLAGS we have a separate argument `stanc_options`.

So something like this should be close (you had a typo in allow-undefined):

```
cmdstan_model('test.stan',include_paths=getwd(),
   cpp_options=list(USER_HEADER="syl2.hpp"), 
   stanc_options = list("allow-undefined")
)

```

Although I think you need to specify the full path for the user\_header. Will check in an hour or so.

---

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [November 30, 2020, 9:52am UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/6 "2020-11-30T09:52:46Z")

</div>

hah! I was staring at that for a while wondering what was wrong, not seeing the typo… though even with the full path it can’t seem to find the external code:

```
cmdstan_model('test.stan',include_paths=getwd(),
  cpp_options=list(USER_HEADER=file.path(getwd(),"syl2.hpp")), 
  stanc_options = list("allow-undefined")
)

Compiling Stan program...
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: 
C:/Users/Driver/AppData/Local/Temp/RtmpWo65Np/model-50442d747fa.o:model-50442d747fa.hpp:(.text$_ZNK20test_model_namespace10test_model8log_probILb0ELb0EdEET1_RSt6vectorIS2_SaIS2_EERS3_IiSaIiEEPSo[_ZNK20test_model_namespace10test_model8log_probILb0ELb0EdEET1_RSt6vectorIS2_SaIS2_EERS3_IiSaIiEEPSo]+0x135): undefined reference to `Eigen::Matrix<boost::math::tools::promote_args<double, double, float, float, float, float>::type, -1, -1, 0, -1, -1> test_model_namespace::syl<double, double>(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, std::ostream*)'
```

---

<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:** [November 30, 2020, 10:05am UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/7 "2020-11-30T10:05:12Z")

</div>

A drawback of the cmdstan’s approach to external C++ files is that the function need to be put inside the same namespace as the model.

So whatever you have in your external file, you need to place it in

```cpp
namespace test_model_namespace {

// .. contents of the file

}

```

The namespace is always: `"name of the model"+"_model_namespace"`.

What rstan does here is it takes the C++ file of the model, places the include inside the namespace and then compiles the model. While that approach works, I honestly dont like it very much. A cleaner solution would be to handle that in stanc compiler (see [https://github.com/stan-dev/stanc3/issues/712](https://github.com/stan-dev/stanc3/issues/712)).  
Maybe there is a way around this in the meantime.

---

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [November 30, 2020, 10:26am UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/8 "2020-11-30T10:26:26Z")

</div>

still no go unfortunately. Can’t be sure that it’s even finding the syl2.hpp file, the error is the same no matter which filename I have. A simpler way of doing all this would indeed be very nice!

---

<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:** [November 30, 2020, 11:24am UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/9 "2020-11-30T11:24:38Z")

</div>

Ok, yeah, this uncovered a cmdstan bug. `USER_HEADER` only works with the deprecated `--allow_undefined`. Argh…

Would you mind creating an issue for this on cmdstan?

This works:

```R
library(cmdstanr)
model_code <-
"
functions {
  real my_fun(real r);
}
transformed data {
  real s = my_fun(0.0);
}
parameters {
  real y;
}
model {
  y ~ normal(0, 1);
}
"
stan_file <- write_stan_file(model_code, basename = "test.stan")
hpp_code <-
"
#include <iostream>

namespace test_model_namespace {

double my_fun(double r, std::ostream* = nullptr) {
  return r+1.0;
}

}
"
hpp_file <- "test.hpp"
write(hpp_code, file = hpp_file)
mod <- cmdstan_model(stan_file,include_paths=getwd(),
              cpp_options=list(USER_HEADER=file.path(getwd(), hpp_file)),
              stanc_options = list("allow_undefined")
)
fit <- mod$sample()

```

What you need to be careful with is the std::ostream\* argument. Those are required for any external C++ function in Stan.

---

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [November 30, 2020, 7:03pm UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/10 "2020-11-30T19:03:00Z")

</div>

Thanks. I filed the issue [https://github.com/stan-dev/cmdstan/issues/952](https://github.com/stan-dev/cmdstan/issues/952)

A new error now:  
error: ‘Eigen’ has not been declared  
Maybe I’ll wait for rstan, since the function is kind of useless without it anyway…

---

<div class="post-metadata">

**Author:** ![satpal](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/satpal/32/18639_2.png) [@satpal](https://discourse.mc-stan.org/u/satpal)\
**Post date:** [October 8, 2021, 12:41pm UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/11 "2021-10-08T12:41:44Z")

</div>

Hi,

The above example by @rok_cesnovar worked for earlier versions (cmdstan 2.26.1; cmdstanr 0.4.0) but throws following error when I try using the more recent versions (cmdstan 2.27.0; cmdstanr 0.4.0.9000).

```
Error in processx::run(command = stanc_cmd(), args = c(stan_file, "--info", : 
  System command 'stanc.exe' failed, exit status: 1, stderr (last 10 lines):
E> -------------------------------------------------
E> 1:  
E> 2: functions {
E> 3: real my_fun(real r);
E> ^
E> 4:    
E> 5: }
E> -------------------------------------------------
E> 
E> Some function is declared without specifying a definition.
Type .Last.error.trace to see where the error occurred

```

.Last.error.trace

```
Stack trace:

 1. mod$sample()
 2. self$variables()
 3. cmdstanr:::model_variables(self$stan_file(), self$include_paths())
 4. processx::run(command = stanc_cmd(), args = c(stan_file, "--info", ...
 5. throw(new_process_error(res, call = sys.call(), echo = echo, ...

 x System command 'stanc.exe' failed, exit status: 1, stderr (last 10 lines):
E> -------------------------------------------------
E> 1:  
E> 2: functions {
E> 3: real my_fun(real r);
E> ^
E> 4:    
E> 5: }
E> -------------------------------------------------
E> 
E> Some function is declared without specifying a definition. 

```

---

<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:** [October 8, 2021, 5:58pm UTC](https://discourse.mc-stan.org/t/custom-c-using-cmdstanr/19528/12 "2021-10-08T17:58:52Z")

</div>

Hey!

See this comment [Hoping for some guidance / help with implementing custom log likelihood and gradient for research project (details below) - #14 by rok\_cesnovar](https://discourse.mc-stan.org/t/hoping-for-some-guidance-help-with-implementing-custom-log-likelihood-and-gradient-for-research-project-details-below/24598/14)
