I’m running latest rstan from develop branch on GitHub.
A few observations:
When I run stan_demo("birats", chains = 1) I get
Iteration: 1 / 2000 [ 0%] (Warmup)
[1] "Error in sampler$call_sampler(args_list[[i]]) : "
[2] " c++ exception (unknown reason)"
error occurred during calling the sampler; sampling not done
I can successfully run stan_demo("dugongs") sometimes, but other times I get the same as for "birats".
If I put the Fibonacci code in test.stan and call rstan::expose_stan_functions("test.stan") I get a lot of output and eventually it errors out:
In file included from file4d8958ca6820.cpp:6:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/stan/model/standalone_functions_header.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/math/tools/config.hpp:13:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/config.hpp:39:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include/boost/config/compiler/clang.hpp:200:11: warning: 'BOOST_NO_CXX11_RVALUE_REFERENCES' macro redefined [-Wmacro-redefined]
# define BOOST_NO_CXX11_RVALUE_REFERENCES
^
<command line>:6:9: note: previous definition is here
#define BOOST_NO_CXX11_RVALUE_REFERENCES 1
^
file4d8958ca6820.cpp:13:19: error: target of using declaration conflicts with declaration already in scope
using stan::math::lgamma;
^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/stan/math/prim/scal/fun/lgamma.hpp:36:19: note: target of using declaration
inline double lgamma(double x) {
^
/usr/include/math.h:462:15: note: conflicting declaration
extern double lgamma(double);
^
file4d8958ca6820.cpp:34:40: error: parameter declarator cannot be qualified
fib(const int& n, std::ostream* &Rcpp::Rcout);
~~~~~~^
file4d8958ca6820.cpp:58:91: error: call to 'fib' is ambiguous
return stan::math::promote_scalar<fun_return_scalar_t__>((logical_lte(n,2) ? 1 : (fib((n - 1), pstream__) + fib((n - 2), pstream__)) ));
^~~
file4d8958ca6820.cpp:34:1: note: candidate function
fib(const int& n, std::ostream* &Rcpp::Rcout);
^
file4d8958ca6820.cpp:38:1: note: candidate function
fib(const int& n, std::ostream* pstream__) {
^
file4d8958ca6820.cpp:58:117: error: call to 'fib' is ambiguous
return stan::math::promote_scalar<fun_return_scalar_t__>((logical_lte(n,2) ? 1 : (fib((n - 1), pstream__) + fib((n - 2), pstream__)) ));
^~~
file4d8958ca6820.cpp:34:1: note: candidate function
fib(const int& n, std::ostream* &Rcpp::Rcout);
^
file4d8958ca6820.cpp:38:1: note: candidate function
fib(const int& n, std::ostream* pstream__) {
^
file4d8958ca6820.cpp:80:44: error: parameter declarator cannot be qualified
int fib(const int& n, std::ostream*& Rcpp::Rcout);
~~~~~~^
file4d8958ca6820.cpp:81:56: error: parameter declarator cannot be qualified
RcppExport SEXP sourceCpp_1_fib(SEXP nSEXP, SEXP Rcpp::RcoutSEXP) {
~~~~~~^
file4d8958ca6820.cpp:86:77: error: no type named 'RcoutSEXP' in namespace 'Rcpp'
Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
~~~~~~^
file4d8958ca6820.cpp:86:65: error: definition or redeclaration of 'Rcout' not allowed inside a function
Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
~~~~~~^
file4d8958ca6820.cpp:86:70: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
^~~~~~~~~~~~~~~~~
file4d8958ca6820.cpp:86:71: note: add a pair of parentheses to declare a variable
Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
^
(
file4d8958ca6820.cpp:87:34: error: no matching function for call to 'fib'
rcpp_result_gen = Rcpp::wrap(fib(n, Rcpp::Rcout));
^~~
file4d8958ca6820.cpp:38:1: note: candidate function not viable: no known conversion from 'Rostream<true>' to 'std::ostream *' (aka 'basic_ostream<char> *') for 2nd argument; take the address of the argument with &
fib(const int& n, std::ostream* pstream__) {
^
file4d8958ca6820.cpp:80:5: note: candidate function not viable: no known conversion from 'Rostream<true>' to 'std::ostream *&' (aka 'basic_ostream<char> *&') for 2nd argument
int fib(const int& n, std::ostream*& Rcpp::Rcout);
^
file4d8958ca6820.cpp:93:17: error: redefinition of 'sourceCpp_1_fib'
RcppExport SEXP sourceCpp_1_fib(SEXP nSEXP, SEXP pstream__SEXP) {
^
file4d8958ca6820.cpp:81:17: note: previous definition is here
RcppExport SEXP sourceCpp_1_fib(SEXP nSEXP, SEXP Rcpp::RcoutSEXP) {
^
In file included from file4d8958ca6820.cpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include/RcppCommon.h:160:
In file included from /Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include/Rcpp/as.h:25:
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include/Rcpp/internal/Exporter.h:31:28: error: cannot initialize a member subobject of type 'std::__1::basic_ostream<char> *' with an lvalue of type 'SEXP' (aka 'SEXPREC *')
Exporter( SEXP x ) : t(x){}
^ ~
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include/Rcpp/as.h:87:41: note: in instantiation of member function 'Rcpp::traits::Exporter<std::__1::basic_ostream<char> *>::Exporter' requested here
::Rcpp::traits::Exporter<T> exporter(x);
^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include/Rcpp/as.h:152:26: note: in instantiation of function template specialization 'Rcpp::internal::as<std::__1::basic_ostream<char> *>' requested here
return internal::as<T>(x, typename traits::r_type_traits<T>::r_category());
^
/Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include/Rcpp/InputParameter.h:34:38: note: in instantiation of function template specialization 'Rcpp::as<std::__1::basic_ostream<char> *>' requested here
inline operator T() { return as<T>(x) ; }
^
file4d8958ca6820.cpp:99:41: note: in instantiation of member function 'Rcpp::InputParameter<std::__1::basic_ostream<char> *>::operator std::__1::basic_ostream<char> *' requested here
rcpp_result_gen = Rcpp::wrap(fib(n, pstream__));
^
2 warnings and 11 errors generated.
make: *** [file4d8958ca6820.o] Error 1
/usr/local/clang4/bin/clang++ -arch x86_64 -ftemplate-depth-256 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rstan/include/boost_not_in_BH" -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_NO_CXX11_RVALUE_REFERENCES -I"/Library/Frameworks/R.framework/Versions/... <truncated>
Error in Rcpp::sourceCpp(code = paste(code, collapse = "\n"), ...) :
Error 1 occurred building shared library.
If I put the throw_exception Rcpp code in test.cpp and then run Rcpp::sourceCpp("test.cpp") it appears to compile fine. But then if I run throw_exception() I get
Error in throw_exception() : c++ exception (unknown reason)
I suspect that Stan’s sampling algorithms are programmed to stop if an unrecognized exception is thrown, even though in this case it should be recognized. @Bob_Carpenter is there anything we can do on the Stan side to change the fallback behavior to continue sampling?
Can one person with a Mac report this as a bug against the clang build of R for Mac and provide a link to it here? I guess the place to do it is https://stat.ethz.ch/mailman/listinfo/r-sig-mac . The expected behavior when the following file
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int throw_exception() {
std::stringstream errmsg;
errmsg << "this is an exception";
throw std::domain_error(errmsg.str());
return 0;
}
is processed by Rcpp::sourceCpp to is create a function throw_exception that when called in R yields
Error in throw_exception() : this is an exception
But with the clang build of R it seems to say
Error in throw_exception() : c++ exception (unknown reason)
They catch std::exception, but being C++, a function can throw anything as an exception. All the ones we throw come from us, from Boost, or from Eigen, and as far as I know, they all extend std::exception.
If there’s an error during generated quantities, I think we may still not be catching it properly.
Can someone who is encountering this problem try adding -nostdinc++ to CXXFLAGS in ~/.R/Makevars in order to avoid the system C++ headers and then add some -isystem directives to use the C++ headers that come with Att’s version of clang4.
Adding -nostdinc++ -isystem/usr/local/clang4/include/c++/v1 to CXXFLAGS didn’t appear to make any difference. I think you’d have to recompile R from source to get rid of the dependency. Even that might not work, given Kevin Ushey’s diagnosis of the issue.