Cannot resolve the "cannot allocate vector of size … " error

Ah, I see the problem. Delete this line from your ~/.R/Makevars.win:

CXX14=$(BINPREF)g++ -O2 -mtune=native

There are actually two of those, which override

CXX14 = C:/Rtools/mingw64/bin/g++ -m$(WIN)

Dang, that didn’t work.

I deleted and Makevars.win is now

CXX14 = C:/Rtools/mingw64/bin/g++ -m$(WIN)
CXX14FLAGS=-O3 -Wno-unused-variable -Wno-unused-function -v
CXX11FLAGS=-O3 -Wno-unused-variable -Wno-unused-function

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

CXX14FLAGS=-O3 -march=native
CXX14 = g++ -m$(WIN) -std=c++1y
CXX11FLAGS=-O3 -march=native

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

And when I run the example, I get this output

> example(stan_model, package = "rstan", run.dontrun = TRUE, verbose = TRUE)
Found file = ‘C:/Program Files/R/R-4.0.0/library/rstan/help/stan_model’ 
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.19.3, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
For improved execution time, we recommend calling
Sys.setenv(LOCAL_CPPFLAGS = '-march=corei7 -mtune=corei7')
although this causes Stan to throw an error on a few processors.
'envir' chosen:<environment: R_GlobalEnv>
encoding = "UTF-8" chosen
--> parsed 4 expressions; now eval(.)ing them:
has srcrefs:
List of 4
 $ : 'srcref' int [1:8] 8 1 8 84 1 84 8 8
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000002d821872510> 
 $ : 'srcref' int [1:8] 9 1 9 56 1 56 9 9
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000002d821872510> 
 $ : 'srcref' int [1:8] 10 1 10 45 1 45 10 10
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000002d821872510> 
 $ : 'srcref' int [1:8] 11 1 11 46 1 46 11 11
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000002d821872510> 

>>>> eval(expression_nr. 1 )
		 =================

stn_md> stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
curr.fun: symbol <-
 .. after ‘expression(stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}')’

>>>> eval(expression_nr. 2 )
		 =================

stn_md> mod <- stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL '73fc79f8b1915e8208c736914c86d1a1' FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model '73fc79f8b1915e8208c736914c86d1a1'.
curr.fun: symbol <-
 .. after ‘expression(mod <- stan_model(model_code = stancode, verbose = TRUE))’

>>>> eval(expression_nr. 3 )
		 =================

stn_md> fit <- sampling(mod, data = list(y_mean = 0))
Error: cannot allocate vector of size 23300.9 Gb

You didn’t delete the second one.

That’s what I get for feeding the kids lunch and trying to do this! However, still the error. Makevars.win is now

CXX14 = C:/Rtools/mingw64/bin/g++ -m$(WIN)
CXX14FLAGS=-O3 -Wno-unused-variable -Wno-unused-function -v
CXX11FLAGS=-O3 -Wno-unused-variable -Wno-unused-function

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

CXX14FLAGS=-O3 -march=native
CXX11FLAGS=-O3 -march=native

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

and after restarting R

Restarting R session...

> example(stan_model, package = "rstan", run.dontrun = TRUE, verbose = TRUE)
Found file = ‘C:/Program Files/R/R-4.0.0/library/rstan/help/stan_model’ 
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.19.3, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
For improved execution time, we recommend calling
Sys.setenv(LOCAL_CPPFLAGS = '-march=corei7 -mtune=corei7')
although this causes Stan to throw an error on a few processors.
'envir' chosen:<environment: R_GlobalEnv>
encoding = "UTF-8" chosen
--> parsed 4 expressions; now eval(.)ing them:
has srcrefs:
List of 4
 $ : 'srcref' int [1:8] 8 1 8 84 1 84 8 8
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000002472f456de8> 
 $ : 'srcref' int [1:8] 9 1 9 56 1 56 9 9
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000002472f456de8> 
 $ : 'srcref' int [1:8] 10 1 10 45 1 45 10 10
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000002472f456de8> 
 $ : 'srcref' int [1:8] 11 1 11 46 1 46 11 11
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x000002472f456de8> 

>>>> eval(expression_nr. 1 )
		 =================

stn_md> stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
curr.fun: symbol <-
 .. after ‘expression(stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}')’

>>>> eval(expression_nr. 2 )
		 =================

stn_md> mod <- stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL '73fc79f8b1915e8208c736914c86d1a1' FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model '73fc79f8b1915e8208c736914c86d1a1'.
curr.fun: symbol <-
 .. after ‘expression(mod <- stan_model(model_code = stancode, verbose = TRUE))’

>>>> eval(expression_nr. 3 )
		 =================

stn_md> fit <- sampling(mod, data = list(y_mean = 0))
Error: cannot allocate vector of size 18662.6 Gb

Can you delete all the CXX14FLAGS except for the first one and then evaluate

example(stan_model, package = "rstan", run.dontrun = TRUE)

in an interactive session? However, you are running it as a script is eating the compiler invocation.

I have to close out of Rstudio and re-open for the changes to take effect. The example runs if I have nothing in Makevars.win and with just

CXX14FLAGS=-O3 -Wno-unused-variable -Wno-unused-function -v
CXX11FLAGS=-O3 -Wno-unused-variable -Wno-unused-function

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

CXX14FLAGS=-O3 -march=native
CXX11FLAGS=-O3 -march=native

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

My final question is if it’s OK to not have the g++ stuff in Makevars.win for other stan models?

I guess so.

Thanks for all your help, Ben. Does any of this apply to my problem? Note, we got to where Martin split the topic.
Right now my Makevars has the following line:
CXX14 = C:/Rtools/mingw64/bin/g++ -m$(WIN)
Again, not sure what the correct path should be, see my last comment from a day ago.

Do you have such a directory? You might have named it C:/Rtools40 or something.

Yes, I only have rtools40 so should the line look like this?
C:/rtools40/mingw64/bin/g++ -m$(WIN)

Tried to run the example you pointed me towards.

RStudio asks me: Building R package from source requires installation of additional build tools. Do you want to install the additional tools now?

I clicked no, as we got those installed already.

The example ran fine, warm up sampling etc. all good.

Yet, my own model still gives me the "cannot allocate vector error. :)

OK, can you change

CXX14 = C:/rtools40/mingw64/bin/g++ -m$(WIN)
```
to
```
CXX14 = C:/rtools40/mingw64/bin/g++ -m$(WIN) -v
```
and call `stan_model` on the model that is not working with `verbose = TRUE` and post the part that pertains to how `g++` is being called?

Didn’t get as far as you probably thought:

library(rstan)
rstan_options(auto_write = TRUE)
Sys.setenv(LOCAL_CPPFLAGS = ‘-march=native’)
options(mc.cores = parallel::detectCores())
datafitX ← stan(file=“STAN/flights_model/flights_model.stan”,data=c(“J”,“y”,“n”), iter = 10000, verbose = TRUE, chains = 4,control=list(adapt_delta=0.8, max_treedepth =10))

TRANSLATING MODEL ‘flights_model’ FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model ‘flights_model’.

CHECKING DATA AND PREPROCESSING FOR MODEL ‘flights_model’ NOW.

COMPILING MODEL ‘flights_model’ NOW.
Error: cannot allocate vector of size 12965.5 Gb

It looks like you’re passing three characters as data, rather than data itself. Does it differ if you use: data=list(J = J, y = y, n = n)?

Didn’t notice it not making it past the compile stage, this wouldn’t help

Stan dose not work with R-4.0.0. on Win10.
What should I do so that Stan works fine.

I updated Rtools and R.
I also cannot fined the Makevars file in the pass ~/document/.R.
Thus, using Notepad, I made it by my hands in two ways,
one is without extension and the other is with win extension.

The error “cannot allocate vector of size … ” also occured when I run my original Stan model.

I also run the follwoing codes. from R studio cosole. I listed the codes and results , separately.
Codes

cat("CXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarations", file = "~/.R/Makevars.win", append = TRUE)
readLines("~/.Renviron")
readLines("~/.R/Makevars")
example(cxxfunction, package = "inline", run.dontrun = TRUE)



library(rstan)  
  stanmodelcode <- "
data {
  int<lower=0> N;
  real y[N];
} 

parameters {
  real mu;
} 

model {
  target += normal_lpdf(mu | 0, 10);
  target += normal_lpdf(y  | mu, 1);
} 
"
  
  y <- rnorm(20) 
  dat <- list(N = 20, y = y); 
  fit <- stan(model_code = stanmodelcode, model_name = "example", 
              data = dat, iter = 2012, chains = 3, verbose = TRUE,
              sample_file = file.path(tempdir(), 'norm.csv')) 

Results
> cat(“CXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarations”, file = “~/.R/Makevars.win”, append = TRUE)
> readLines("~/.Renviron")
[1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""
> readLines("~/.R/Makevars")
character(0)
> example(cxxfunction, package = “inline”, run.dontrun = TRUE)

cxxfnc> # default plugin
cxxfnc> fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
cxxfnc+ 	return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
cxxfnc+ ' )

cxxfnc> fx( 2L, 5 )
[1] 10

cxxfnc> # Rcpp plugin
cxxfnc> if( require( Rcpp ) ){
cxxfnc+ 
cxxfnc+ 	fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
cxxfnc+ 		return wrap( as<int>(x) * as<double>(y) ) ;
cxxfnc+ 	', plugin = "Rcpp" )
cxxfnc+ 	fx( 2L, 5 )
cxxfnc+ 
cxxfnc+         ## equivalent shorter form using rcpp()
cxxfnc+ 	fx <- rcpp(signature(x = "integer", y = "numeric"),
cxxfnc+                    ' return wrap( as<int>(x) * as<double>(y) ) ; ')
cxxfnc+ 
cxxfnc+ }

cxxfnc> # RcppArmadillo plugin
cxxfnc> if( require( RcppArmadillo ) ){
cxxfnc+ 	
cxxfnc+ 	fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
cxxfnc+ 		int dim = as<int>( x ) ;
cxxfnc+ 		arma::mat z = as<double>(y) * arma::eye<arma::mat>( dim, dim ) ;
cxxfnc+ 		return wrap( arma::accu(z) ) ;
cxxfnc+ 	', plugin = "RcppArmadillo" )
cxxfnc+ 	fx( 2L, 5 )
cxxfnc+ 	
cxxfnc+ 	
cxxfnc+ }
[1] 10
> 
> 
> 
> library(rstan)  
>   stanmodelcode <- "
+ data {
+   int<lower=0> N;
+   real y[N];
+ } 
+ 
+ parameters {
+   real mu;
+ } 
+ 
+ model {
+   target += normal_lpdf(mu | 0, 10);
+   target += normal_lpdf(y  | mu, 1);
+ } 
+ "
>   
>   y <- rnorm(20) 
>   dat <- list(N = 20, y = y); 
>   fit <- stan(model_code = stanmodelcode, model_name = "example", 
+               data = dat, iter = 2012, chains = 3, verbose = TRUE,
+               sample_file = file.path(tempdir(), 'norm.csv')) 

TRANSLATING MODEL 'example' FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model 'example'.
COMPILING THE C++ CODE FOR MODEL 'example' NOW.
OS: x86_64, mingw32; rstan: 2.19.3; Rcpp: 1.0.4.6; inline: 0.3.15 
 >> setting environment variables: 
PKG_LIBS =  -L"C:/Users/81909/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders
PKG_CPPFLAGS =   -I"C:/Users/81909/Documents/R/win-library/4.0/Rcpp/include/"  -I"C:/Users/81909/Documents/R/win-library/4.0/RcppEigen/include/"  -I"C:/Users/81909/Documents/R/win-library/4.0/RcppEigen/include/unsupported"  -I"C:/Users/81909/Documents/R/win-library/4.0/BH/include" -I"C:/Users/81909/Documents/R/win-library/4.0/StanHeaders/include/src/"  -I"C:/Users/81909/Documents/R/win-library/4.0/StanHeaders/include/"  -I"C:/Users/81909/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG  -D_REENTRANT  -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -include stan/math/prim/mat/fun/Eigen.hpp  -std=c++1y
 >> Program source :

   1 : 
   2 : // includes from the plugin
   3 : // [[Rcpp::plugins(cpp14)]]
   4 : 
   5 : // user includes
   6 : #define STAN__SERVICES__COMMAND_HPP#include <boost/integer/integer_log2.hpp>
   7 : #include <rstan/rstaninc.hpp>
   8 : // Code generated by Stan version 2.19.1
   9 : 
  10 : #include <stan/model/model_header.hpp>
  11 : 
  12 : namespace model31e475206b_example_namespace {
  13 : 
  14 : using std::istream;
  15 : using std::string;
  16 : using std::stringstream;
  17 : using std::vector;
  18 : using stan::io::dump;
  19 : using stan::math::lgamma;
  20 : using stan::model::prob_grad;
  21 : using namespace stan::math;
  22 : 
  23 : static int current_statement_begin__;
  24 : 
  25 : stan::io::program_reader prog_reader__() {
  26 :     stan::io::program_reader reader;
  27 :     reader.add_event(0, 0, "start", "model31e475206b_example");
  28 :     reader.add_event(16, 14, "end", "model31e475206b_example");
  29 :     return reader;
  30 : }
  31 : 
  32 : class model31e475206b_example : public prob_grad {
  33 : private:
  34 :         int N;
  35 :         std::vector<double> y;
  36 : public:
  37 :     model31e475206b_example(stan::io::var_context& context__,
  38 :         std::ostream* pstream__ = 0)
  39 :         : prob_grad(0) {
  40 :         ctor_body(context__, 0, pstream__);
  41 :     }
  42 : 
  43 :     model31e475206b_example(stan::io::var_context& context__,
  44 :         unsigned int random_seed__,
  45 :         std::ostream* pstream__ = 0)
  46 :         : prob_grad(0) {
  47 :         ctor_body(context__, random_seed__, pstream__);
  48 :     }
  49 : 
  50 :     void ctor_body(stan::io::var_context& context__,
  51 :                    unsigned int random_seed__,
  52 :                    std::ostream* pstream__) {
  53 :         typedef double local_scalar_t__;
  54 : 
  55 :         boost::ecuyer1988 base_rng__ =
  56 :           stan::services::util::create_rng(random_seed__, 0);
  57 :         (void) base_rng__;  // suppress unused var warning
  58 : 
  59 :         current_statement_begin__ = -1;
  60 : 
  61 :         static const char* function__ = "model31e475206b_example_namespace::model31e475206b_example";
  62 :         (void) function__;  // dummy to suppress unused var warning
  63 :         size_t pos__;
  64 :         (void) pos__;  // dummy to suppress unused var warning
  65 :         std::vector<int> vals_i__;
  66 :         std::vector<double> vals_r__;
  67 :         local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
  68 :         (void) DUMMY_VAR__;  // suppress unused var warning
  69 : 
  70 :         try {
  71 :             // initialize data block variables from context__
  72 :             current_statement_begin__ = 3;
  73 :             context__.validate_dims("data initialization", "N", "int", context__.to_vec());
  74 :             N = int(0);
  75 :             vals_i__ = context__.vals_i("N");
  76 :             pos__ = 0;
  77 :             N = vals_i__[pos__++];
  78 :             check_greater_or_equal(function__, "N", N, 0);
  79 : 
  80 :             current_statement_begin__ = 4;
  81 :             validate_non_negative_index("y", "N", N);
  82 :             context__.validate_dims("data initialization", "y", "double", context__.to_vec(N));
  83 :             y = std::vector<double>(N, double(0));
  84 :             vals_r__ = context__.vals_r("y");
  85 :             pos__ = 0;
  86 :             size_t y_k_0_max__ = N;
  87 :             for (size_t k_0__ = 0; k_0__ < y_k_0_max__; ++k_0__) {
  88 :                 y[k_0__] = vals_r__[pos__++];
  89 :             }
  90 : 
  91 : 
  92 :             // initialize transformed data variables
  93 :             // execute transformed data statements
  94 : 
  95 :             // validate transformed data
  96 : 
  97 :             // validate, set parameter ranges
  98 :             num_params_r__ = 0U;
  99 :             param_ranges_i__.clear();
 100 :             current_statement_begin__ = 8;
 101 :             num_params_r__ += 1;
 102 :         } catch (const std::exception& e) {
 103 :             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
 104 :             // Next line prevents compiler griping about no return
 105 :             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
 106 :         }
 107 :     }
 108 : 
 109 :     ~model31e475206b_example() { }
 110 : 
 111 : 
 112 :     void transform_inits(const stan::io::var_context& context__,
 113 :                          std::vector<int>& params_i__,
 114 :                          std::vector<double>& params_r__,
 115 :                          std::ostream* pstream__) const {
 116 :         typedef double local_scalar_t__;
 117 :         stan::io::writer<double> writer__(params_r__, params_i__);
 118 :         size_t pos__;
 119 :         (void) pos__; // dummy call to supress warning
 120 :         std::vector<double> vals_r__;
 121 :         std::vector<int> vals_i__;
 122 : 
 123 :         current_statement_begin__ = 8;
 124 :         if (!(context__.contains_r("mu")))
 125 :             stan::lang::rethrow_located(std::runtime_error(std::string("Variable mu missing")), current_statement_begin__, prog_reader__());
 126 :         vals_r__ = context__.vals_r("mu");
 127 :         pos__ = 0U;
 128 :         context__.validate_dims("parameter initialization", "mu", "double", context__.to_vec());
 129 :         double mu(0);
 130 :         mu = vals_r__[pos__++];
 131 :         try {
 132 :             writer__.scalar_unconstrain(mu);
 133 :         } catch (const std::exception& e) {
 134 :             stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable mu: ") + e.what()), current_statement_begin__, prog_reader__());
 135 :         }
 136 : 
 137 :         params_r__ = writer__.data_r();
 138 :         params_i__ = writer__.data_i();
 139 :     }
 140 : 
 141 :     void transform_inits(const stan::io::var_context& context,
 142 :                          Eigen::Matrix<double, Eigen::Dynamic, 1>& params_r,
 143 :                          std::ostream* pstream__) const {
 144 :       std::vector<double> params_r_vec;
 145 :       std::vector<int> params_i_vec;
 146 :       transform_inits(context, params_i_vec, params_r_vec, pstream__);
 147 :       params_r.resize(params_r_vec.size());
 148 :       for (int i = 0; i < params_r.size(); ++i)
 149 :         params_r(i) = params_r_vec[i];
 150 :     }
 151 : 
 152 : 
 153 :     template <bool propto__, bool jacobian__, typename T__>
 154 :     T__ log_prob(std::vector<T__>& params_r__,
 155 :                  std::vector<int>& params_i__,
 156 :                  std::ostream* pstream__ = 0) const {
 157 : 
 158 :         typedef T__ local_scalar_t__;
 159 : 
 160 :         local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
 161 :         (void) DUMMY_VAR__;  // dummy to suppress unused var warning
 162 : 
 163 :         T__ lp__(0.0);
 164 :         stan::math::accumulator<T__> lp_accum__;
 165 :         try {
 166 :             stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
 167 : 
 168 :             // model parameters
 169 :             current_statement_begin__ = 8;
 170 :             local_scalar_t__ mu;
 171 :             (void) mu;  // dummy to suppress unused var warning
 172 :             if (jacobian__)
 173 :                 mu = in__.scalar_constrain(lp__);
 174 :             else
 175 :                 mu = in__.scalar_constrain();
 176 : 
 177 :             // model body
 178 : 
 179 :             current_statement_begin__ = 12;
 180 :             lp_accum__.add(normal_log(mu, 0, 10));
 181 :             current_statement_begin__ = 13;
 182 :             lp_accum__.add(normal_log(y, mu, 1));
 183 : 
 184 :         } catch (const std::exception& e) {
 185 :             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
 186 :             // Next line prevents compiler griping about no return
 187 :             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
 188 :         }
 189 : 
 190 :         lp_accum__.add(lp__);
 191 :         return lp_accum__.sum();
 192 : 
 193 :     } // log_prob()
 194 : 
 195 :     template <bool propto, bool jacobian, typename T_>
 196 :     T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
 197 :                std::ostream* pstream = 0) const {
 198 :       std::vector<T_> vec_params_r;
 199 :       vec_params_r.reserve(params_r.size());
 200 :       for (int i = 0; i < params_r.size(); ++i)
 201 :         vec_params_r.push_back(params_r(i));
 202 :       std::vector<int> vec_params_i;
 203 :       return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
 204 :     }
 205 : 
 206 : 
 207 :     void get_param_names(std::vector<std::string>& names__) const {
 208 :         names__.resize(0);
 209 :         names__.push_back("mu");
 210 :     }
 211 : 
 212 : 
 213 :     void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
 214 :         dimss__.resize(0);
 215 :         std::vector<size_t> dims__;
 216 :         dims__.resize(0);
 217 :         dimss__.push_back(dims__);
 218 :     }
 219 : 
 220 :     template <typename RNG>
 221 :     void write_array(RNG& base_rng__,
 222 :                      std::vector<double>& params_r__,
 223 :                      std::vector<int>& params_i__,
 224 :                      std::vector<double>& vars__,
 225 :                      bool include_tparams__ = true,
 226 :                      bool include_gqs__ = true,
 227 :                      std::ostream* pstream__ = 0) const {
 228 :         typedef double local_scalar_t__;
 229 : 
 230 :         vars__.resize(0);
 231 :         stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
 232 :         static const char* function__ = "model31e475206b_example_namespace::write_array";
 233 :         (void) function__;  // dummy to suppress unused var warning
 234 : 
 235 :         // read-transform, write parameters
 236 :         double mu = in__.scalar_constrain();
 237 :         vars__.push_back(mu);
 238 : 
 239 :         double lp__ = 0.0;
 240 :         (void) lp__;  // dummy to suppress unused var warning
 241 :         stan::math::accumulator<double> lp_accum__;
 242 : 
 243 :         local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
 244 :         (void) DUMMY_VAR__;  // suppress unused var warning
 245 : 
 246 :         if (!include_tparams__ && !include_gqs__) return;
 247 : 
 248 :         try {
 249 :             if (!include_gqs__ && !include_tparams__) return;
 250 :             if (!include_gqs__) return;
 251 :         } catch (const std::exception& e) {
 252 :             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
 253 :             // Next line prevents compiler griping about no return
 254 :             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
 255 :         }
 256 :     }
 257 : 
 258 :     template <typename RNG>
 259 :     void write_array(RNG& base_rng,
 260 :                      Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
 261 :                      Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
 262 :                      bool include_tparams = true,
 263 :                      bool include_gqs = true,
 264 :                      std::ostream* pstream = 0) const {
 265 :       std::vector<double> params_r_vec(params_r.size());
 266 :       for (int i = 0; i < params_r.size(); ++i)
 267 :         params_r_vec[i] = params_r(i);
 268 :       std::vector<double> vars_vec;
 269 :       std::vector<int> params_i_vec;
 270 :       write_array(base_rng, params_r_vec, params_i_vec, vars_vec, include_tparams, include_gqs, pstream);
 271 :       vars.resize(vars_vec.size());
 272 :       for (int i = 0; i < vars.size(); ++i)
 273 :         vars(i) = vars_vec[i];
 274 :     }
 275 : 
 276 :     static std::string model_name() {
 277 :         return "model31e475206b_example";
 278 :     }
 279 : 
 280 : 
 281 :     void constrained_param_names(std::vector<std::string>& param_names__,
 282 :                                  bool include_tparams__ = true,
 283 :                                  bool include_gqs__ = true) const {
 284 :         std::stringstream param_name_stream__;
 285 :         param_name_stream__.str(std::string());
 286 :         param_name_stream__ << "mu";
 287 :         param_names__.push_back(param_name_stream__.str());
 288 : 
 289 :         if (!include_gqs__ && !include_tparams__) return;
 290 : 
 291 :         if (include_tparams__) {
 292 :         }
 293 : 
 294 :         if (!include_gqs__) return;
 295 :     }
 296 : 
 297 : 
 298 :     void unconstrained_param_names(std::vector<std::string>& param_names__,
 299 :                                    bool include_tparams__ = true,
 300 :                                    bool include_gqs__ = true) const {
 301 :         std::stringstream param_name_stream__;
 302 :         param_name_stream__.str(std::string());
 303 :         param_name_stream__ << "mu";
 304 :         param_names__.push_back(param_name_stream__.str());
 305 : 
 306 :         if (!include_gqs__ && !include_tparams__) return;
 307 : 
 308 :         if (include_tparams__) {
 309 :         }
 310 : 
 311 :         if (!include_gqs__) return;
 312 :     }
 313 : 
 314 : }; // model
 315 : 
 316 : }  // namespace
 317 : 
 318 : typedef model31e475206b_example_namespace::model31e475206b_example stan_model;
 319 : 
 320 : /**
 321 :  * Define Rcpp Module to expose stan_fit's functions to R.
 322 :  */
 323 : RCPP_MODULE(stan_fit4model31e475206b_example_mod){
 324 :   Rcpp::class_<rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example,
 325 :                boost::random::ecuyer1988> >("stan_fit4model31e475206b_example")
 326 :     // .constructor<Rcpp::List>()
 327 :     .constructor<SEXP, SEXP, SEXP>()
 328 :     // .constructor<SEXP, SEXP>()
 329 :     .method("call_sampler",
 330 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::call_sampler)
 331 :     .method("param_names",
 332 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::param_names)
 333 :     .method("param_names_oi",
 334 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::param_names_oi)
 335 :     .method("param_fnames_oi",
 336 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::param_fnames_oi)
 337 :     .method("param_dims",
 338 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::param_dims)
 339 :     .method("param_dims_oi",
 340 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::param_dims_oi)
 341 :     .method("update_param_oi",
 342 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::update_param_oi)
 343 :     .method("param_oi_tidx",
 344 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::param_oi_tidx)
 345 :     .method("grad_log_prob",
 346 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::grad_log_prob)
 347 :     .method("log_prob",
 348 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::log_prob)
 349 :     .method("unconstrain_pars",
 350 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::unconstrain_pars)
 351 :     .method("constrain_pars",
 352 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::constrain_pars)
 353 :     .method("num_pars_unconstrained",
 354 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::num_pars_unconstrained)
 355 :     .method("unconstrained_param_names",
 356 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::unconstrained_param_names)
 357 :     .method("constrained_param_names",
 358 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::constrained_param_names)
 359 :     .method("standalone_gqs",
 360 :             &rstan::stan_fit<model31e475206b_example_namespace::model31e475206b_example, boost::random::ecuyer1988>::standalone_gqs)
 361 :   ;
 362 : }
 363 : 
 364 : // declarations
 365 : extern "C" {
 366 : SEXP file31e450a57d4( ) ;
 367 : }
 368 : 
 369 : // definition
 370 : 
 371 : SEXP file31e450a57d4(  ){
 372 :  return Rcpp::wrap("example");
 373 : }
 374 : 
 375 : 
Compilation argument:
 C:/PROGRA~1/R/R-40~1.0/bin/x64/R CMD SHLIB file31e450a57d4.cpp 2> file31e450a57d4.cpp.err.txt 
C:\Rtools\mingw64\bin\g++ -m64 -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG   -I"C:/Users/81909/Documents/R/win-library/4.0/Rcpp/include/"  -I"C:/Users/81909/Documents/R/win-library/4.0/RcppEigen/include/"  -I"C:/Users/81909/Documents/R/win-library/4.0/RcppEigen/include/unsupported"  -I"C:/Users/81909/Documents/R/win-library/4.0/BH/include" -I"C:/Users/81909/Documents/R/win-library/4.0/StanHeaders/include/src/"  -I"C:/Users/81909/Documents/R/win-library/4.0/StanHeaders/include/"  -I"C:/Users/81909/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG  -D_REENTRANT  -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -include stan/math/prim/mat/fun/Eigen.hpp  -std=c++1y -I"C:/Users/81909/Documents/R/win-library/4.0/RcppArmadillo/include" -I"C:/Users/81909/Documents/R/win-library/4.0/Rcpp/include"   -march=core2     -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarationsCXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarations -c file31e450a57d4.cpp -o file31e450a57d4.o
sh: C:Rtoolsmingw64bing++: command not found
make: *** [C:/PROGRA~1/R/R-40~1.0/etc/x64/Makeconf:229: file31e450a57d4.o] Error 127
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'file31e450a57d4.cpp.err.txt': No such file or directory

The slashes are pointed the wrong way in CXX14. It should be

CXX14 = C:/Rtools/mingw64/bin/g++

Note, my model and code work on R 3.6

In this thread, you said

On the other hand, in the first post in the thread, it is described

CXX14 = C:\Rtools\mingw64\bin\g++ -m$(WIN)

I am comfused…

Now, Stan works.
My Makevars.win file in the file .R is the following.

CXX14 = C:/Rtools/mingw64/bin/g++

CXX14 = C:/rtools40/mingw64/bin/g++ -m$(WIN) -v
CXX14FLAGS += -mtune=native -march=native -Wno-ignored-attributes -Wno-deprecated-declarations

I was comfused the code
CXX14 = C:/Rtools/mingw64/bin/g++

with

CXX14 = C:/rtools40/mingw64/bin/g++ -m$(WIN) -v
and I guess it is the reason why Stan does not work.

Anyway,
Thank you.

If the example compiles but not your model, are you able to test other stan models?

For example, does it work with:

stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
out = stan(model_code = stancode,data=list(y_mean=0))

If it’s specific to your model, you’ll probably need to post some code and data that causes the error for you so that we can see if it reproduces on other systems