Rstan on Windows

@Kasper_Fischer

You should see the content of the file “Makevars.win”.
Something like:
CXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2

If you have multiple lines starting with “CXX14FLAGS” in this file, this may be the cause of your problem.

There are multiple lines. Should I delete everything but the last line then?

It works when I do that, but I just wanted to make sure it is the correct way to do it.

@Kasper_Fischer
You probably also have lines starting with CXX11FLAGS … ?
Can you copy/paste here what you have exactly in the Makevars.win file ?

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=corei7 -mtune=corei7

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=corei7 -mtune=corei7

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=corei7 -mtune=corei7

@Kasper_Fischer
Just remove the two first sets of lines and keep the last two lines.

Yes I think you are correct I can put in a patch for that. We just need to look for multiple CXX14FLAGS etc and take the last one

@Kasper_Fischer and @Rno before you changed your Makevars do you remember if you had multiple definitions of any of CXXFLAGS , CXX11FLAGS, or CXX14FLAGS. I’m pretty sure that’s the issue but want to verify

I had multple definitions of CXX14FLAGS. The problems goes away after I deleted the other statements.

Alrighty ty for letting me know I’ll submit a patch!

1 Like

@stevebronder

I think the problem occurs when people run several time the following code provided on the “Rstan Getting Started” page.

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, ifelse(.Platform$OS.type == "windows", "Makevars.win", "Makevars"))
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS=-O3 -march=native -mtune=native",
if( grepl("^darwin", R.version$os)) "CXX14FLAGS += -arch x86_64 -ftemplate-depth-256" else 
  if (.Platform$OS.type == "windows") "CXX11FLAGS=-O3 -march=corei7 -mtune=corei7" else
    "CXX14FLAGS += -fPIC",
file = M, sep = "\n", append = TRUE)

A simple patch to avoid that would be to add brackets around the lines after

if (!file.exists(M))

such as

if (!file.exists(M)) {
  file.create(M)
  cat("\nCXX14FLAGS=-O3 -march=native -mtune=native",
  if( grepl("^darwin", R.version$os)) "CXX14FLAGS += -arch x86_64 -ftemplate-depth-256" else 
  if (.Platform$OS.type == "windows") "CXX11FLAGS=-O3 -march=corei7 -mtune=corei7" else
    "CXX14FLAGS += -fPIC",
  file = M, sep = "\n", append = TRUE)
}
2 Likes

@Kasper_Fischer glad it’s working now, sorry for the hassle! @Rno thanks a lot for helping out with this!

@jonah Your are welcome !

Hi @bgoodri @jonah @stevebronder @Rno @paul.buerkner @bbbales2

Thanks for all of the hard work in ensuring those of us on Windows could run rstan and brms again. As luck would have it, I ran into this error today, while doing a demo on why Brms/RStan was superior to Winbugs in a data set that had problematic multi-collinearity among the predictors in a mixed effects model. Now the demo is working well with R 4.x and RStan 2.21.x

Do we know the exact reason RStan broke with R 4.0.2 and RStan 2.21.x update?

Sree

1 Like

There were a half-dozen. Mostly we had been suggesting people utilize -march=native, which turned out to be a bad idea due to some changes between 2.19.x and 2.21.x.

3 Likes

The rstan url isn’t working again.

1 Like

@bgoodri The R 4 rstan link is out of date again. (edit: just adding the @ so maybe Discourse will send an e-mail)

Try now

The instructions given in the original post above do work for me, however I still get the following warning when fitting a model (e.g. using rstan::stan()):

In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

My session info:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[...]

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.21.2         ggplot2_3.3.2        StanHeaders_2.21.0-6

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         pillar_1.4.6       compiler_4.0.2     prettyunits_1.1.1  tools_4.0.2       
 [6] pkgbuild_1.1.0     jsonlite_1.7.0     lifecycle_0.2.0    tibble_3.0.3       gtable_0.3.0      
[11] pkgconfig_2.0.3    rlang_0.4.7        cli_2.0.2          rstudioapi_0.11    parallel_4.0.2    
[16] curl_4.3           loo_2.3.1          gridExtra_2.3      withr_2.2.0        dplyr_1.0.0       
[21] generics_0.0.2     vctrs_0.3.2        stats4_4.0.2       grid_4.0.2         tidyselect_1.1.0  
[26] glue_1.4.1         inline_0.3.15      R6_2.4.1           processx_3.4.3     fansi_0.4.1       
[31] callr_3.4.3        purrr_0.3.4        magrittr_1.5       codetools_0.2-16   matrixStats_0.56.0
[36] scales_1.1.1       ps_1.3.3           ellipsis_0.3.1     assertthat_0.2.1   colorspace_1.4-1  
[41] V8_3.2.0           RcppParallel_5.0.2 munsell_0.5.0      crayon_1.3.4

My Makevars file content (as recommended in the original post):

CXX14FLAGS=-O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2

The issue persists even when adding the line

CXX14 = <path_to_g++>

to the Makevars file.

I got a similar weird path when trying rstan::get_CXX() on my Windows install. Rstan still worked though.

It’s not that bad. You can probably avoid that warning by having

CXX14 = C:/rtools40/usr/mingw_$(WIN)/bin/g++

in your Makevars.