Skip to content

Instantly share code, notes, and snippets.

@tpapp
Created September 8, 2015 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpapp/bfb17b0a44f845ccaf14 to your computer and use it in GitHub Desktop.
Save tpapp/bfb17b0a44f845ccaf14 to your computer and use it in GitHub Desktop.
rstan error
> stanmodelcode <- "
data {
int<lower=0> N;
real y[N];
}
parameters {
real mu;
}
model {
mu ~ normal(0, 10);
y ~ normal(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, sample_file = 'norm.csv',
+ verbose = TRUE)
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, linux-gnu; rstan: 2.7.0.1; Rcpp: 0.12.0; inline: 0.3.14
>> setting environment variables:
PKG_LIBS =
PKG_CPPFLAGS = -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include/" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/RcppEigen/include/" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/RcppEigen/include/unsupported" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/rstan/include/boost_not_in_BH" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/BH/include" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/StanHeaders/include/src/" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/StanHeaders/include/" -I"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/rstan/include" -O3 -Wno-unused-function -Wno-uninitialized -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG
>> Program source :
1 :
2 : // includes from the plugin
3 :
4 :
5 : // user includes
6 : #define STAN__SERVICES__COMMAND_HPP// Code generated by Stan version 2.7
7 :
8 : #include <stan/model/model_header.hpp>
9 :
10 : namespace model16f81be8ff0c_example_namespace {
11 :
12 : using std::istream;
13 : using std::string;
14 : using std::stringstream;
15 : using std::vector;
16 : using stan::io::dump;
17 : using stan::math::lgamma;
18 : using stan::model::prob_grad;
19 : using namespace stan::math;
20 :
21 : typedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;
22 : typedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;
23 : typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;
24 :
25 : static int current_statement_begin__;
26 : class model16f81be8ff0c_example : public prob_grad {
27 : private:
28 : int N;
29 : vector<double> y;
30 : public:
31 : model16f81be8ff0c_example(stan::io::var_context& context__,
32 : std::ostream* pstream__ = 0)
33 : : prob_grad(0) {
34 : current_statement_begin__ = -1;
35 :
36 : static const char* function__ = "model16f81be8ff0c_example_namespace::model16f81be8ff0c_example";
37 : (void) function__; // dummy call to supress warning
38 : size_t pos__;
39 : (void) pos__; // dummy call to supress warning
40 : std::vector<int> vals_i__;
41 : std::vector<double> vals_r__;
42 : context__.validate_dims("data initialization", "N", "int", context__.to_vec());
43 : N = int(0);
44 : vals_i__ = context__.vals_i("N");
45 : pos__ = 0;
46 : N = vals_i__[pos__++];
47 : context__.validate_dims("data initialization", "y", "double", context__.to_vec(N));
48 : validate_non_negative_index("y", "N", N);
49 : y = std::vector<double>(N,double(0));
50 : vals_r__ = context__.vals_r("y");
51 : pos__ = 0;
52 : size_t y_limit_0__ = N;
53 : for (size_t i_0__ = 0; i_0__ < y_limit_0__; ++i_0__) {
54 : y[i_0__] = vals_r__[pos__++];
55 : }
56 :
57 : // validate data
58 : check_greater_or_equal(function__,"N",N,0);
59 :
60 : double DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
61 : (void) DUMMY_VAR__; // suppress unused var warning
62 :
63 :
64 : // initialize transformed variables to avoid seg fault on val access
65 :
66 : try {
67 : } catch (const std::exception& e) {
68 : stan::lang::rethrow_located(e,current_statement_begin__);
69 : // Next line prevents compiler griping about no return
70 : throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
71 : }
72 :
73 : // validate transformed data
74 :
75 : // set parameter ranges
76 : num_params_r__ = 0U;
77 : param_ranges_i__.clear();
78 : ++num_params_r__;
79 : }
80 :
81 : ~model16f81be8ff0c_example() { }
82 :
83 :
84 : void transform_inits(const stan::io::var_context& context__,
85 : std::vector<int>& params_i__,
86 : std::vector<double>& params_r__,
87 : std::ostream* pstream__) const {
88 : stan::io::writer<double> writer__(params_r__,params_i__);
89 : size_t pos__;
90 : (void) pos__; // dummy call to supress warning
91 : std::vector<double> vals_r__;
92 : std::vector<int> vals_i__;
93 :
94 : if (!(context__.contains_r("mu")))
95 : throw std::runtime_error("variable mu missing");
96 : vals_r__ = context__.vals_r("mu");
97 : pos__ = 0U;
98 : context__.validate_dims("initialization", "mu", "double", context__.to_vec());
99 : double mu(0);
100 : mu = vals_r__[pos__++];
101 : try {
102 : writer__.scalar_unconstrain(mu);
103 : } catch (const std::exception& e) {
104 : throw std::runtime_error(std::string("Error transforming variable mu: ") + e.what());
105 : }
106 :
107 : params_r__ = writer__.data_r();
108 : params_i__ = writer__.data_i();
109 : }
110 :
111 : void transform_inits(const stan::io::var_context& context,
112 : Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
113 : std::ostream* pstream__) const {
114 : std::vector<double> params_r_vec;
115 : std::vector<int> params_i_vec;
116 : transform_inits(context, params_i_vec, params_r_vec, pstream__);
117 : params_r.resize(params_r_vec.size());
118 : for (int i = 0; i < params_r.size(); ++i)
119 : params_r(i) = params_r_vec[i];
120 : }
121 :
122 :
123 : template <bool propto__, bool jacobian__, typename T__>
124 : T__ log_prob(vector<T__>& params_r__,
125 : vector<int>& params_i__,
126 : std::ostream* pstream__ = 0) const {
127 :
128 : T__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
129 : (void) DUMMY_VAR__; // suppress unused var warning
130 :
131 : T__ lp__(0.0);
132 : stan::math::accumulator<T__> lp_accum__;
133 :
134 : // model parameters
135 : stan::io::reader<T__> in__(params_r__,params_i__);
136 :
137 : T__ mu;
138 : (void) mu; // dummy to suppress unused var warning
139 : if (jacobian__)
140 : mu = in__.scalar_constrain(lp__);
141 : else
142 : mu = in__.scalar_constrain();
143 :
144 :
145 : // transformed parameters
146 :
147 : // initialize transformed variables to avoid seg fault on val access
148 :
149 : try {
150 : } catch (const std::exception& e) {
151 : stan::lang::rethrow_located(e,current_statement_begin__);
152 : // Next line prevents compiler griping about no return
153 : throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
154 : }
155 :
156 : // validate transformed parameters
157 :
158 : const char* function__ = "validate transformed params";
159 : (void) function__; // dummy to suppress unused var warning
160 :
161 : // model body
162 : try {
163 : current_statement_begin__ = 12;
164 : lp_accum__.add(normal_log<propto__>(mu, 0, 10));
165 : current_statement_begin__ = 13;
166 : lp_accum__.add(normal_log<propto__>(y, mu, 1));
167 : } catch (const std::exception& e) {
168 : stan::lang::rethrow_located(e,current_statement_begin__);
169 : // Next line prevents compiler griping about no return
170 : throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
171 : }
172 :
173 : lp_accum__.add(lp__);
174 : return lp_accum__.sum();
175 :
176 : } // log_prob()
177 :
178 : template <bool propto, bool jacobian, typename T_>
179 : T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
180 : std::ostream* pstream = 0) const {
181 : std::vector<T_> vec_params_r;
182 : vec_params_r.reserve(params_r.size());
183 : for (int i = 0; i < params_r.size(); ++i)
184 : vec_params_r.push_back(params_r(i));
185 : std::vector<int> vec_params_i;
186 : return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
187 : }
188 :
189 :
190 : void get_param_names(std::vector<std::string>& names__) const {
191 : names__.resize(0);
192 : names__.push_back("mu");
193 : }
194 :
195 :
196 : void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
197 : dimss__.resize(0);
198 : std::vector<size_t> dims__;
199 : dims__.resize(0);
200 : dimss__.push_back(dims__);
201 : }
202 :
203 : template <typename RNG>
204 : void write_array(RNG& base_rng__,
205 : std::vector<double>& params_r__,
206 : std::vector<int>& params_i__,
207 : std::vector<double>& vars__,
208 : bool include_tparams__ = true,
209 : bool include_gqs__ = true,
210 : std::ostream* pstream__ = 0) const {
211 : vars__.resize(0);
212 : stan::io::reader<double> in__(params_r__,params_i__);
213 : static const char* function__ = "model16f81be8ff0c_example_namespace::write_array";
214 : (void) function__; // dummy call to supress warning
215 : // read-transform, write parameters
216 : double mu = in__.scalar_constrain();
217 : vars__.push_back(mu);
218 :
219 : if (!include_tparams__) return;
220 : // declare and define transformed parameters
221 : double lp__ = 0.0;
222 : (void) lp__; // dummy call to supress warning
223 : stan::math::accumulator<double> lp_accum__;
224 :
225 :
226 : try {
227 : } catch (const std::exception& e) {
228 : stan::lang::rethrow_located(e,current_statement_begin__);
229 : // Next line prevents compiler griping about no return
230 : throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
231 : }
232 :
233 : // validate transformed parameters
234 :
235 : // write transformed parameters
236 :
237 : if (!include_gqs__) return;
238 : // declare and define generated quantities
239 :
240 : double DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
241 : (void) DUMMY_VAR__; // suppress unused var warning
242 :
243 :
244 : // initialize transformed variables to avoid seg fault on val access
245 :
246 : try {
247 : } catch (const std::exception& e) {
248 : stan::lang::rethrow_located(e,current_statement_begin__);
249 : // Next line prevents compiler griping about no return
250 : throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
251 : }
252 :
253 : // validate generated quantities
254 :
255 : // write generated quantities
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 "model16f81be8ff0c_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_gqs__) return;
292 : }
293 :
294 :
295 : void unconstrained_param_names(std::vector<std::string>& param_names__,
296 : bool include_tparams__ = true,
297 : bool include_gqs__ = true) const {
298 : std::stringstream param_name_stream__;
299 : param_name_stream__.str(std::string());
300 : param_name_stream__ << "mu";
301 : param_names__.push_back(param_name_stream__.str());
302 :
303 : if (!include_gqs__ && !include_tparams__) return;
304 :
305 : if (!include_gqs__) return;
306 : }
307 :
308 : }; // model
309 :
310 : } // namespace
311 :
312 : typedef model16f81be8ff0c_example_namespace::model16f81be8ff0c_example stan_model;
313 :
314 : #include <rstan/rstaninc.hpp>
315 : /**
316 : * Define Rcpp Module to expose stan_fit's functions to R.
317 : */
318 : RCPP_MODULE(stan_fit4model16f81be8ff0c_example_mod){
319 : Rcpp::class_<rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example,
320 : boost::random::ecuyer1988> >("stan_fit4model16f81be8ff0c_example")
321 : // .constructor<Rcpp::List>()
322 : .constructor<SEXP, SEXP>()
323 : // .constructor<SEXP, SEXP>()
324 : .method("call_sampler",
325 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::call_sampler)
326 : .method("param_names",
327 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::param_names)
328 : .method("param_names_oi",
329 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::param_names_oi)
330 : .method("param_fnames_oi",
331 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::param_fnames_oi)
332 : .method("param_dims",
333 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::param_dims)
334 : .method("param_dims_oi",
335 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::param_dims_oi)
336 : .method("update_param_oi",
337 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::update_param_oi)
338 : .method("param_oi_tidx",
339 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::param_oi_tidx)
340 : .method("grad_log_prob",
341 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::grad_log_prob)
342 : .method("log_prob",
343 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::log_prob)
344 : .method("unconstrain_pars",
345 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::unconstrain_pars)
346 : .method("constrain_pars",
347 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::constrain_pars)
348 : .method("num_pars_unconstrained",
349 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::num_pars_unconstrained)
350 : .method("unconstrained_param_names",
351 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::unconstrained_param_names)
352 : .method("constrained_param_names",
353 : &rstan::stan_fit<model16f81be8ff0c_example_namespace::model16f81be8ff0c_example, boost::random::ecuyer1988>::constrained_param_names)
354 : ;
355 : }
356 :
357 : // declarations
358 : extern "C" {
359 : SEXP file16f87f6a7b5f( ) ;
360 : }
361 :
362 : // definition
363 :
364 : SEXP file16f87f6a7b5f( ){
365 : return Rcpp::wrap("example");
366 : }
367 :
368 :
Compilation argument:
/usr/lib/R/bin/R CMD SHLIB file16f87f6a7b5f.cpp 2> file16f87f6a7b5f.cpp.err.txt
g++ -I/usr/share/R/include -DNDEBUG -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include/" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/RcppEigen/include/" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/RcppEigen/include/unsupported" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/rstan/include/boost_not_in_BH" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/BH/include" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/StanHeaders/include/src/" -isystem"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/StanHeaders/include/" -I"/home/tamas/R/x86_64-pc-linux-gnu-library/3.2/rstan/include" -O3 -Wno-unused-function -Wno-uninitialized -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -fpic -O3 -flto -ffat-lto-objects -c file16f87f6a7b5f.cpp -o file16f87f6a7b5f.o
g++ -shared -L/usr/lib/R/lib -Wl,-z,relro -o file16f87f6a7b5f.so file16f87f6a7b5f.o -L/usr/lib/R/lib -lR
*** caught segfault ***
address 0x20, cause 'memory not mapped'
Traceback:
1: .Call(Module__classes_info, xp)
2: Module(module, mustStart = TRUE)
3: .getModulePointer(x)
4: <S4 object of class "Module">$stan_fit4model16f81be8ff0c_example
5: <S4 object of class "Module">$stan_fit4model16f81be8ff0c_example
6: eval(expr, envir, enclos)
7: eval(call("$", mod, paste("stan_fit4", model_cppname, sep = "")))
8: .local(object, ...)
9: sampling(sm, data, pars, chains, iter, warmup, thin, seed, init, check_data = TRUE, sample_file = sample_file, diagnostic_file = diagnostic_file, verbose = verbose, algorithm = match.arg(algorithm), control = control, check_unknown_args = FALSE, cores = cores, open_progress = open_progress, ...)
10: sampling(sm, data, pars, chains, iter, warmup, thin, seed, init, check_data = TRUE, sample_file = sample_file, diagnostic_file = diagnostic_file, verbose = verbose, algorithm = match.arg(algorithm), control = control, check_unknown_args = FALSE, cores = cores, open_progress = open_progress, ...)
11: stan(model_code = stanmodelcode, model_name = "example", data = dat, iter = 2012, chains = 3, sample_file = "norm.csv", verbose = TRUE)
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment