# Error running Stan model with rstan 2.21 in Windows

**URL:** <https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499>\
**Category:** RStan\
**Created:** [July 9, 2020, 11:53pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499 "2020-07-09T23:53:20Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [July 16, 2020, 2:57pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/21 "2020-07-16T14:57:56Z")

</div>

> [@paul.buerkner](#):
>
> `--recurse-submodules`

It is trying to get the submodules and is messing up. Possibly

```no-highlight
path_rstan <- tempfile(pattern = "git2r-")
git2r::clone("http://github.com/stan-dev/rstan", path_rstan, 
              branch = "develop", bare = FALSE)
utils::install.packages(file.path(path_rstan, "rstan", "rstan"), 
                        type = "source", repos = NULL)

```

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [July 16, 2020, 4:36pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/22 "2020-07-16T16:36:01Z")

</div>

Got some promising results. Using RStan 2.21 from CRAN, I get the TBB errors just from `example(stan_model, run.dontrun=T)`. But when I install the github version from the syntax above, the example model runs fine as well as a larger factor analysis model of mine.

I can test this on a couple of other windows machines tomorrow (it’s midnight here), so let me know if there are other models or configs to test

---

<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:** [July 16, 2020, 4:40pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/23 "2020-07-16T16:40:13Z")

</div>

The linker error should be fixed on GitHub by setting the `LOCAL_LIBS` environmental variable. But the known unknown is the problems that @MauritsM (the OP) and other Windows users (on other threads) have had with models that compile, link, and subsequently fail with something that looks like a stackoverflow.

---

<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:** [July 16, 2020, 7:06pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/24 "2020-07-16T19:06:05Z")

</div>

@MauritsM this HMM seems to be work in serial or in parallel on @mike-lawrence’s Windows computer, so we need to figure out what the difference is. Can you do

> [@Rstan on Windows](https://discourse.mc-stan.org/t/rstan-on-windows/16673):
>
> As you all have noticed, there are a large number of people who are having problems with rstan 2.21.x on Windows. This thread is to try to bring those threads together. Eliminate all -march=native clauses from CXX14FLAGS in the ~/.R/Makevars.win or ~/.R/Makevars files, which you can do by calling file.edit("~/.R/Makevars.win") and file.edit("~/.R/Makevars"). Even though the startup message says to consider adding it to Sys.setenv(LOCAL\_CPPFLAGS), do not do that. Note that the temporary URLs be…

and then tell me if you spot a material difference in the Compilation argument compared to:

> Compilation argument:  
> C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file382c169e3b14.cpp 2\> file382c169e3b14.cpp.err.txt  
> “C:/rtools40/mingw64/bin/“g++ -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.2/include” -DNDEBUG -I"C:/Users/Mike/Documents/R/win-library/4.0/Rcpp/include/” -I"C:/Users/Mike/Documents/R/win-library/4.0/RcppEigen/include/" -I"C:/Users/Mike/Documents/R/win-library/4.0/RcppEigen/include/unsupported" -I"C:/Users/Mike/Documents/R/win-library/4.0/BH/include" -I"C:/Users/Mike/Documents/R/win-library/4.0/StanHeaders/include/src/" -I"C:/Users/Mike/Documents/R/win-library/4.0/StanHeaders/include/" -I"C:/Users/Mike/Documents/R/win-library/4.0/RcppParallel/include/" -I"C:/Users/Mike/Documents/R/win-library/4.0/rstan/include" -DEIGEN\_NO\_DEBUG -DBOOST\_DISABLE\_ASSERTS -DBOOST\_PENDING\_INTEGER\_LOG2\_HPP -DSTAN\_THREADS -DBOOST\_NO\_AUTO\_PTR -include “C:/Users/Mike/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp” -std=c++1y -march=core2 -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c file382c169e3b14.cpp -o file382c169e3b14.o

---

<div class="post-metadata">

**Author:** ![MauritsM](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mauritsm/32/430_2.png) [@MauritsM](https://discourse.mc-stan.org/u/MauritsM)\
**Post date:** [July 16, 2020, 9:30pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/25 "2020-07-16T21:30:03Z")

</div>

Hi Ben,

after installing StanHeaders and rstan from the thread you posted the model suddenly seems to sample fine. I’m not sure what’s different now, unfortunately.

I noticed that, when running with `cores > 1` I can no longer see the progress of the chains in RStudio. When I click on the Viewer tab I get the following message:

`The process cannot access the file because it is being used by another process`

The second thing I noticed is that `print(fit)` is now very slow. It takes minutes to print the summary of the samples, where this used to be a couple of seconds.

I captured the compilation argument when compiling the model (see below). Hopefully this is still useful to you. This seems to be pretty much the same as Mike except for some small changes in Makevars.

```
Compilation argument:
 C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file2d786bde218d.cpp 2> file2d786bde218d.cpp.err.txt 
"C:/rtools40/mingw64/bin/"g++ -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG -I"C:/Users/mista/Documents/R/win-library/4.0/Rcpp/include/" -I"C:/Users/mista/Documents/R/win-library/4.0/RcppEigen/include/" -I"C:/Users/mista/Documents/R/win-library/4.0/RcppEigen/include/unsupported" -I"C:/Users/mista/Documents/R/win-library/4.0/BH/include" -I"C:/Users/mista/Documents/R/win-library/4.0/StanHeaders/include/src/" -I"C:/Users/mista/Documents/R/win-library/4.0/StanHeaders/include/" -I"C:/Users/mista/Documents/R/win-library/4.0/RcppParallel/include/" -I"C:/Users/mista/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include "C:/Users/mista/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp" -std=c++1y -march=core2 -O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c file2d786bde218d.cpp -o file2d786bde218d.o

```

---

<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:** [July 16, 2020, 9:42pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/26 "2020-07-16T21:42:26Z")

</div>

> [@MauritsM](#):
>
> I noticed that, when running with `cores > 1` I can no longer see the progress of the chains in RStudio

I definitely could see the progress in the Viewer tab. All the chains write to the same file but I don’t know why it couldn’t be read. Is `print(fit)` slow on all models? I noticed it took a little while but figured that was due to having 10000 iterations.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [July 16, 2020, 9:43pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/27 "2020-07-16T21:43:40Z")

</div>

My computer is crashing with @MauritsM 's model.

It was working and then I added `CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function` to my ~/Documents/.R.

You want a login or want me to keep messing with this to figure out what’s going on?

---

<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:** [July 16, 2020, 9:47pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/28 "2020-07-16T21:47:00Z")

</div>

Is it the case that `-march=native` is necessary and sufficient for the crash? This would suggest that it is processor-dependent in addition to Windows-dependent.

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [July 16, 2020, 9:51pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/29 "2020-07-16T21:51:21Z")

</div>

The March thing is not working ok on mingw. People should avoid using it on windows. The link I posted above is what can be used according to a colleague of mine.

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [July 16, 2020, 9:57pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/30 "2020-07-16T21:57:21Z")

</div>

I can confirm that removing `-march=native` and following your advice from above solves the issue for me, at least when using the winbuilder binaries Ben posted in [Rstan on Windows](https://discourse.mc-stan.org/t/rstan-on-windows/16673)

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [July 16, 2020, 9:57pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/31 "2020-07-16T21:57:58Z")

</div>

When I remove `-march=native` I have a `-march=core2` in my build and the model runs.

I think you can get segfaults trying to execute instructions your cpu doesn’t support. I wonder if there’s a way to scan the object file to figure this out?

---

<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:** [July 16, 2020, 10:01pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/32 "2020-07-16T22:01:32Z")

</div>

I think we just have to avoid `-march=native` on Windows for now. With `g++-4.9` I assumed the processor tuning was due to having processors that were not in production when `g++-4.9` had its final release, but perhaps there is something that messes up processor identification on Windows more generally.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [July 16, 2020, 10:11pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/33 "2020-07-16T22:11:54Z")

</div>

Is that all the debugging we need to do now? Do we just watch the forums and make sure this combo of things solves the Windows issues?

---

<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:** [July 16, 2020, 10:22pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/34 "2020-07-16T22:22:13Z")

</div>

This is probably enough to move forward but I would like to hear about any other problems people are encountering on Windows.

---

<div class="post-metadata">

**Author:** ![tadej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tadej/32/5607_2.png) [@tadej](https://discourse.mc-stan.org/u/tadej)\
**Post date:** [July 17, 2020, 8:19am UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/35 "2020-07-17T08:19:04Z")

</div>

I have encountered the `-march=native`issue before. It is a longstanding bug in MinGW.

If a CPU supports AVX instruction set `-march=native`allows use of these instuctions. Eigen than uses certain AVX instructions that require 32-bit aligment of memory locations they touch. Windows provides stack that is 16-bit aligned and compiler does not do any extra alignment, so executing these instructions crashes.

I also found a workaround that works for me, even if I have no idea why. If I skip the make and compile by invoking g++ manually, so that everything is compiled in a single invocation and no object files are generated things work for me. No idea how portable this workaround is. Other options are to use either clang or Linux.

---

<div class="post-metadata">

**Author:** ![MauritsM](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mauritsm/32/430_2.png) [@MauritsM](https://discourse.mc-stan.org/u/MauritsM)\
**Post date:** [July 17, 2020, 10:21am UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/36 "2020-07-17T10:21:56Z")

</div>

The viewer also doesn’t seem to work for other models either. Do you have any idea what could cause that?

`print(fit)` was quick for the 8 schools model and some other models over here - it might be that for the HMM model I posted there is simply a lot more data to print (and as you mentioned, 10K iterations surely slows that down as well).

EDIT: after some more testing on different machines I get the feeling that this model has always been slow to print and I simply remembered incorrectly. It’s quicker on my Mac but my Mac is way more powerful than my Windows machine. On my Windows machine at work (which runs 2.19) the model is also slow to print, so please don’t spend any more time on this.

---

<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:** [July 17, 2020, 4:11pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/37 "2020-07-17T16:11:41Z")

</div>

What version of RStudio are you using?

---

<div class="post-metadata">

**Author:** ![MauritsM](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mauritsm/32/430_2.png) [@MauritsM](https://discourse.mc-stan.org/u/MauritsM)\
**Post date:** [July 17, 2020, 6:37pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/38 "2020-07-17T18:37:04Z")

</div>

1.3.959

---

<div class="post-metadata">

**Author:** ![MauritsM](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mauritsm/32/430_2.png) [@MauritsM](https://discourse.mc-stan.org/u/MauritsM)\
**Post date:** [July 17, 2020, 6:59pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/39 "2020-07-17T18:59:20Z")

</div>

Oh, I see there’s a new version - I’ll try updating and will post back.

---

<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:** [July 17, 2020, 7:29pm UTC](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499/40 "2020-07-17T19:29:58Z")

</div>

It still won’t show the progress with multiple cores. Something has changed in RStudio.

[Previous page](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499.md?page=1)

[Next page](https://discourse.mc-stan.org/t/error-running-stan-model-with-rstan-2-21-in-windows/16499.md?page=3)
