Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple issues with expose_stan_functions. #458

Closed
dmenne opened this issue Oct 10, 2017 · 13 comments
Closed

Multiple issues with expose_stan_functions. #458

dmenne opened this issue Oct 10, 2017 · 13 comments

Comments

@dmenne
Copy link

dmenne commented Oct 10, 2017

Summary:

Multiple issues with expose_stan_functions. This is a summary of

http://discourse.mc-stan.org/t/error-expose-stan-functions/2119/6

Description:

  1. expose_stan_functions does not work with a stanc model passed. Works partially with a file
  2. when used with file, compile error

Reproducible Steps:

  1. Run the example "Not run" in documentation of expose_stan_function
Error in get_model_strcode(file, NULL) : 
      file must be a character string or connection

Your version works when the filename is given (checked), but does does not retrieve the mc-string you use in the example. Alternatively, remove the example in the documentation.

function (file, ...) 
{
    model_code <- get_model_strcode(file, NULL)
    model_cppname <- legitimate_model_name(basename(file), obfuscate_name = TRUE)

Something like this, probably

function (file, mc_code, ...) 
{
    model_code <- get_model_strcode(file, mc_code)
  1. Also tried with RStudio on Ubuntu in addition to Windows

– Put fibonacci example in the documentation in a file “a.stan” (since passing text currently fails)

library(rstan)
st = expose_stan_functions("a.stan")
---------------------------------------------------------

In file included from file37284ff74b42.cpp:6:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/src/stan/model/standalone_functions_header.hpp:4:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math.hpp:4:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core.hpp:12:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5:
In file included from /usr/local/lib/R/site-library/StanHeaders/include/stan/math/rev/core/var.hpp:7:
In file included from /usr/local/lib/R/site-library/BH/include/boost/math/tools/config.hpp:13:
In file included from /usr/local/lib/R/site-library/BH/include/boost/config.hpp:39:
/usr/local/lib/R/site-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
        ^
file37284ff74b42.cpp:13:19: error: target of using declaration conflicts with declaration already in scope
using stan::math::lgamma;
                  ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/scal/fun/lgamma.hpp:36:19: note: target of using declaration
    inline double lgamma(double x) {
                  ^
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:261:13: note: conflicting declaration
__MATHCALL (lgamma,, (_Mdouble_));
            ^
/usr/include/math.h:66:25: note: expanded from macro '__MATHCALL'
  __MATHDECL (_Mdouble_,function,suffix, args)
                        ^
/usr/include/math.h:68:22: note: expanded from macro '__MATHDECL'
  __MATHDECL_1(type, function,suffix, args); \
                     ^
/usr/include/math.h:76:31: note: expanded from macro '__MATHDECL_1'
  extern type __MATH_PRECNAME(function,suffix) args __THROW
                              ^
/usr/include/math.h:79:42: note: expanded from macro '__MATH_PRECNAME'
#define __MATH_PRECNAME(name,r) __CONCAT(name,r)
                                         ^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:88:23: note: expanded from macro '__CONCAT'
#define __CONCAT(x,y)   x ## y
                        ^
file37284ff74b42.cpp:34:46: error: parameter declarator cannot be qualified
fibonacci(const int& x, std::ostream* &Rcpp::Rcout);
                                       ~~~~~~^
file37284ff74b42.cpp:58:91: error: call to 'fibonacci' is ambiguous
        return stan::math::promote_scalar<fun_return_scalar_t__>((logical_lte(x,2) ? 1 : (fibonacci((x - 1), pstream__) + fibonacci((x - 2), pstream__)) ));
                                                                                          ^~~~~~~~~
file37284ff74b42.cpp:34:1: note: candidate function
fibonacci(const int& x, std::ostream* &Rcpp::Rcout);
^
file37284ff74b42.cpp:38:1: note: candidate function
fibonacci(const int& x, std::ostream* pstream__) {
^
file37284ff74b42.cpp:58:123: error: call to 'fibonacci' is ambiguous
        return stan::math::promote_scalar<fun_return_scalar_t__>((logical_lte(x,2) ? 1 : (fibonacci((x - 1), pstream__) + fibonacci((x - 2), pstream__)) ));
                                                                                                                          ^~~~~~~~~
file37284ff74b42.cpp:34:1: note: candidate function
fibonacci(const int& x, std::ostream* &Rcpp::Rcout);
^
file37284ff74b42.cpp:38:1: note: candidate function
fibonacci(const int& x, std::ostream* pstream__) {
^
file37284ff74b42.cpp:80:50: error: parameter declarator cannot be qualified
int fibonacci(const int& x, std::ostream*& Rcpp::Rcout);
                                           ~~~~~~^
file37284ff74b42.cpp:81:62: error: parameter declarator cannot be qualified
RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP, SEXP Rcpp::RcoutSEXP) {
                                                       ~~~~~~^
file37284ff74b42.cpp:86:77: error: no type named 'RcoutSEXP' in namespace 'Rcpp'
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                                      ~~~~~~^
file37284ff74b42.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);
                                                          ~~~~~~^
file37284ff74b42.cpp:86:70: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                                     ^~~~~~~~~~~~~~~~~
file37284ff74b42.cpp:86:71: note: add a pair of parentheses to declare a variable
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                                      ^
                                                                      (
file37284ff74b42.cpp:87:53: error: reference to 'Rcout' is ambiguous
    rcpp_result_gen = Rcpp::wrap(fibonacci(x, Rcpp::Rcout));
                                              ~~~~~~^
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:92:28: note: candidate found by name lookup is 'Rcpp::Rcout'
    static Rostream<true>  Rcout;
                           ^
file37284ff74b42.cpp:86:65: note: candidate found by name lookup is 'Rcpp::Rcout'
    Rcpp::traits::input_parameter< std::ostream*& >::type Rcpp::Rcout(Rcpp::RcoutSEXP);
                                                                ^
file37284ff74b42.cpp:93:17: error: redefinition of 'sourceCpp_1_fibonacci'
RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP, SEXP pstream__SEXP) {
                ^
file37284ff74b42.cpp:81:17: note: previous definition is here
RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP, SEXP Rcpp::RcoutSEXP) {
                ^
In file included from file37284ff74b42.cpp:4:
In file included from /usr/local/lib/R/site-library/Rcpp/include/Rcpp.h:27:
In file included from /usr/local/lib/R/site-library/Rcpp/include/RcppCommon.h:160:
In file included from /usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:25:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/internal/Exporter.h:31:28: error: cannot initialize a member subobject of type 'std::basic_ostream<char> *' with an lvalue of type 'SEXP' (aka 'SEXPREC *')
                    Exporter( SEXP x ) : t(x){}
                                         ^ ~
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:87:41: note: in instantiation of member function 'Rcpp::traits::Exporter<std::basic_ostream<char> *>::Exporter' requested here
            ::Rcpp::traits::Exporter<T> exporter(x);
                                        ^
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:152:26: note: in instantiation of function template specialization 'Rcpp::internal::as<std::basic_ostream<char> *>' requested here
        return internal::as<T>(x, typename traits::r_type_traits<T>::r_category());
                         ^
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/InputParameter.h:34:38: note: in instantiation of function template specialization 'Rcpp::as<std::basic_ostream<char> *>' requested here
        inline operator T() { return as<T>(x) ; }
                                     ^
file37284ff74b42.cpp:99:47: note: in instantiation of member function 'Rcpp::InputParameter<std::basic_ostream<char> *>::operator std::basic_ostream<char> *' requested here
    rcpp_result_gen = Rcpp::wrap(fibonacci(x, pstream__));
                                              ^
2 warnings and 11 errors generated.
make: *** [file37284ff74b42.o] Fehler 1
clang++ -I/usr/share/R/include -DNDEBUG   -I"/usr/local/lib/R/site-library/Rcpp/include/"  -I"/usr/local/lib/R/site-library/RcppEigen/include/"  -I"/usr/local/lib/R/site-library/RcppEigen/include/unsupported"  -I"/usr/local/lib/R/site-library/rstan/include/boost_not_in_BH" -I"/usr/local/lib/R/site-library/BH/include" -I"/usr/local/lib/R/site-library/StanHeaders/include/src/"  -I"/usr/local/lib/R/site-library/StanHeaders/include/"  -I"/usr/local/lib/R/site-library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_NO_CXX11_RVALUE_REFERENCES  -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/rstan/include" -I"/tmp/Rtmp3pDkcc/sourceCpp-x86_64-pc-linux-gnu-0.12.13"    -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c file37284ff74b42.cpp -o file37284ff74b42.o
/usr/lib/R/etc/Makeconf:168: die Regel für Ziel „file37284ff74b42.o“ scheiterte
Fehler in Rcpp::sourceCpp(code = paste(code, collapse = "\n"), ...) : 
  Error 1 occurred building shared library.

RStan Version:

rstan * 2.17.1 2017-09-21 Github (39f9c1a)

R Version:

Operating System:
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

@bgoodri
Copy link
Contributor

bgoodri commented Oct 10, 2017

We went back to the old expose_stan_functions. I think it should be working.

@dmenne
Copy link
Author

dmenne commented Oct 11, 2017

To misquote Lenin: thinking is good, testing is better

devtools::install_github(“stan-dev/rstan/rstan/rstan@develop”)

> library(rstan)
.....
> mc <- 
+     '
+ functions {
+ int fibonacci(int x);
+ int fibonacci(int x) {
+ if (x <= 0) reject("x must be positive");
+ return x <= 2 ? 1 : fibonacci(x - 1) + fibonacci(x - 2);
+ }
+ }
+ '
> cppcode <- stanc(model_code = mc, model_name = "Fibonacci")
> ## Not run: 
> expose_stan_functions(cppcode)
Error in get_model_strcode(file, NULL) : 
  file must be a character string or connection
> 

@dmenne
Copy link
Author

dmenne commented Oct 17, 2017

Using your #422

a.stan is your Fibonacci-Example code put in a file. For reasons why not used directly, see original post

Windows:

devtools::install_github("stan-dev/rstan/rstan/rstan@feature/issue-422-standalone-functions-k")
library(rstan)
expose_stan_functions(file = "a.stan")

------------------------------------------------

> expose_stan_functions(file = "a.stan")
C:/RTools/mingw_64/bin/g++  -I"C:/R/include" -DNDEBUG   -I"C:/R/library/Rcpp/include/"  -I"C:/R/library/RcppEigen/include/"  -I"C:/R/library/RcppEigen/include/unsupported"  -I"C:/R/library/rstan/include/boost_not_in_BH" -I"C:/R/library/BH/include" -I"C:/R/library/StanHeaders/include/src/"  -I"C:/R/library/StanHeaders/include/"  -I"C:/R/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_NO_CXX11_RVALUE_REFERENCES  -I"C:/R/library/Rcpp/include" -I"C:/R/library/rstan/include" -I"C:/Users/Dieter/AppData/Local/Temp/Rtmp8q2nhM/sourceCpp-x86_64-w64-mingw32-0.12.13"   -I"d:/Compiler/gcc-4.9.3/local330/include"     -O3 -mtune=native -march=native -Wno-unused-variable -Wno-deprecated-declarations -Wno-unused-local-typedefs -Wno-unused-function -c file283c6f9049b.cpp -o file283c6f9049b.o
In file included from C:/R/library/BH/include/boost/config.hpp:39:0,
                 from C:/R/library/BH/include/boost/random/additive_combine.hpp:22,
                 from C:/R/library/rstan/include/exporter.h:2,
                 from file283c6f9049b.cpp:1:
C:/R/library/BH/include/boost/config/compiler/gcc.hpp:186:0: warning: "BOOST_NO_CXX11_RVALUE_REFERENCES" redefined
 #  define BOOST_NO_CXX11_RVALUE_REFERENCES
 ^
<command-line>:0:0: note: this is the location of the previous definition
file283c6f9049b.cpp: In function 'SEXPREC* sourceCpp_1_fibonacci(SEXP, SEXP)':
file283c6f9049b.cpp:104:17: error: redefinition of 'SEXPREC* sourceCpp_1_fibonacci(SEXP, SEXP)'
 RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP, SEXP pstream__SEXP) {
                 ^
file283c6f9049b.cpp:92:17: note: 'SEXPREC* sourceCpp_1_fibonacci(SEXP, SEXP)' previously defined here
 RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP, SEXP pstream__SEXP) {
                 ^
make: *** [file283c6f9049b.o] Error 1
Warnmeldung:
Ausführung von Kommando 'make -f "C:/R/etc/x64/Makeconf" -f "C:/R/share/make/winshlib.mk" -f "C:/Users/Dieter/Documents/.R/Makevars" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_2.dll" WIN=64 TCLBIN=64 OBJECTS="file283c6f9049b.o"' ergab Status 2 
Error in Rcpp::sourceCpp(code = paste(code, collapse = "\n"), ...) : 
  Error 1 occurred building shared library.

Ubuntu


  expose_stan_functions(file = "a.stan")
In file included from file2a5634d801df.cpp:1:
In file included from /usr/local/lib/R/site-library/rstan/include/exporter.h:2:
In file included from /usr/local/lib/R/site-library/BH/include/boost/random/additive_combine.hpp:22:
In file included from /usr/local/lib/R/site-library/BH/include/boost/config.hpp:39:
/usr/local/lib/R/site-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
        ^
file2a5634d801df.cpp:104:17: error: redefinition of 'sourceCpp_4_fibonacci'
RcppExport SEXP sourceCpp_4_fibonacci(SEXP xSEXP, SEXP pstream__SEXP) {
                ^
file2a5634d801df.cpp:92:17: note: previous definition is here
RcppExport SEXP sourceCpp_4_fibonacci(SEXP xSEXP, SEXP pstream__SEXP) {
                ^
1 warning and 1 error generated.
make: *** [file2a5634d801df.o] Fehler 1
clang++ -I/usr/share/R/include -DNDEBUG   -I"/usr/local/lib/R/site-library/Rcpp/include/"  -I"/usr/local/lib/R/site-library/RcppEigen/include/"  -I"/usr/local/lib/R/site-library/RcppEigen/include/unsupported"  -I"/usr/local/lib/R/site-library/rstan/include/boost_not_in_BH" -I"/usr/local/lib/R/site-library/BH/include" -I"/usr/local/lib/R/site-library/StanHeaders/include/src/"  -I"/usr/local/lib/R/site-library/StanHeaders/include/"  -I"/usr/local/lib/R/site-library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DBOOST_NO_CXX11_RVALUE_REFERENCES  -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/rstan/include" -I"/tmp/RtmpOSZQyn/sourceCpp-x86_64-pc-linux-gnu-0.12.13"    -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c file2a5634d801df.cpp -o file2a5634d801df.o
/usr/lib/R/etc/Makeconf:168: die Regel für Ziel „file2a5634d801df.o“ scheiterte
Fehler in Rcpp::sourceCpp(code = paste(code, collapse = "\n"), ...) : 
  Error 1 occurred building shared library.

@sakrejda
Copy link
Contributor

@dmenne you also need the matching rstan branch (currently a PR on rstan).

@dmenne
Copy link
Author

dmenne commented Oct 17, 2017

So this is not enough?

devtools::install_github("stan-dev/rstan/rstan/rstan@feature/issue-422-standalone-functions-k")

@sakrejda
Copy link
Contributor

I'm not sure what's going on with this, please try this:

  1. Run with a specific cacheDir, for example: expose_stan_functions('a.stan', cacheDir='.')
  2. Find two files in the cache directory post them here:
    a) something like sourceCpp-x86_64-pc-linux-gnu-0.12.12/file1e8a6ab56566.cpp
    b) and something like sourceCpp-x86_64-pc-linux-gnu-0.12.12/sourcecpp_1e8a55e87ddc/file1e8a940078f.cpp

The first file is the c++ generated by Stan and the second is the c++ generated by Rcpp, without those troubleshooting is sort of hopeless.

@sakrejda
Copy link
Contributor

@dmenne so locally with both branches correctly installed:

rstan::expose_stan_functions('gah.stan', cacheDir='.')
fibonacci(5, rstan::OUT)
fibonacci(9, rstan::OUT)

Gets me this in the terminal:

> rstan::expose_stan_functions('gah.stan', cacheDir='.')
> fibonacci(5, rstan::OUT)
[1] 5
> fibonacci(9, rstan::OUT)
[1] 34

I think you're having an installation problem. So try reinstalling StanHeaders and then reinstalling rstan itself. We may need @bgoodri for help with this

@dmenne
Copy link
Author

dmenne commented Oct 17, 2017

Does the example from the documentation of expose_stan_functions run ok on your installation ?

The Windows-Version
The ubuntu-Version

@bgoodri
Copy link
Contributor

bgoodri commented Oct 17, 2017 via email

@dmenne
Copy link
Author

dmenne commented Oct 17, 2017

Strange. I work with RStan every day and have a current package on CRAN with it (breathteststan), so the installation cannot be totally foobar; and Ubuntu runs on an external Web server totally idependently.+

I re-installed StanHeader, but only from CRAN 2.16.0-1. No change. But maybe I have to get a github version? Which one?

@sakrejda
Copy link
Contributor

sakrejda commented Oct 17, 2017 via email

@dmenne
Copy link
Author

dmenne commented Oct 17, 2017

@bgoodri

Bingo!

fibonacci(10)
55

Even works with the example in the docs! Thanks and over.

Sorry about my impatience I needed this for a pending release of breathteststan

@dmenne dmenne closed this as completed Oct 17, 2017
@dmenne
Copy link
Author

dmenne commented Oct 17, 2017

@sakrejda Sorry, I just deleted them on my server, because I thought the issure was resolved. You still need them?

Just recovered them. Have to go not yet checked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants