On 5/24/12 3:23 PM, Jiqiang Guo wrote:
> Currently, if we get rstan installed, we can do what stan can do in R.
That's great. Can you read the data from R
or are you relying on the main that's already there?
I put in the flag in stanc to suppress generating a main
function.
> That is, we would end up with a csv file for samples etc.
> I will go ahead trying to hook up the mcmc::chains object and return
> analysis and samples from c++ to R.
Do you mean reading the mcmc::chains object from the CSV
file? That'd be great. It's a real challenge with the
arbitrarily dimensioned arrays and the need to swap row-order
and column order.
I've been tapping Matt for auto-correlation, but I'm about
to finish the changes to the model to make writing to the
chains object easier.
> I would like to bring up the installation of rstan by talking about what I did to
> make it work. That might bring up better ways to do it.
I'd talk to Ben about this -- I know next to
nothing about R. I've inserted some opinions/guesses
below.
> rstan have two places that need stan:
>
> First, stanc. As we need to compile stanc once, I think we should just
> compile it during installation (in the case of installation from source).
Agreed.
I'm not sure how portable executables are across platforms,
but we could also distribute a pre-made version.
The other pre-req for all of this is having
the whole C++ tool chain in place. What you need is
documented in an appendix in the manual.
> (The other way is just to compile it after the package is loaded, which adds
> time to compile model -- only once but every session.)
>
> So I have source code for stanc in R as follows:
>
> rstan/src/stanc.hpp
> rstan/src/stanc.cpp
Did you put it in a namespace? If not, I'd suggest
stan::stanc if that's not too much trouble in interacting
with R.
> and Makevars files in rstan/src, which tells R how to compile them during
> installation.
Great.
> Currently in Makevars, I have
This you'll need to run by Daniel!
> PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` -L$(STAN_HOME)/bin -lstanc
> PKG_CPPFLAGS = -I$(STAN_HOME)/lib -I$(STAN_HOME)/src -I./include/
>
> Note that the libstanc is
>
> CXX_ARGS="-O3 -Wall -fpic -pipe -g" # this is just copied from what are used in R CMD SHLIB
>
> g++ -I src -I lib $CXX_ARGS -c src/stan/gm/grammars/var_decls_grammar_inst.cpp -o
> bin/stan/gm/grammars/var_decls_grammar_inst.o
> g++ -I src -I lib $CXX_ARGS -c src/stan/gm/grammars/whitespace_grammar_inst.cpp -o
> bin/stan/gm/grammars/whitespace_grammar_inst.o
> g++ -I src -I lib $CXX_ARGS -c src/stan/gm/grammars/program_grammar_inst.cpp -o
> bin/stan/gm/grammars/program_grammar_inst.o
> g++ -I src -I lib $CXX_ARGS -c src/stan/gm/grammars/statement_grammar_inst.cpp -o
> bin/stan/gm/grammars/statement_grammar_inst.o
> g++ -I src -I lib $CXX_ARGS -c src/stan/gm/grammars/expression_grammar_inst.cpp -o
> bin/stan/gm/grammars/expression_grammar_inst.o
> g++ -I src -I lib $CXX_ARGS -c src/stan/gm/ast_def.cpp -o bin/stan/gm/ast_def.o
>
> g++ -shared -o ./bin/libstanc.so src/stan/agrad/agrad.o src/stan/math/matrix.o src/stan/agrad/matrix.o \
> bin/stan/gm/grammars/statement_grammar_inst.o bin/stan/gm/grammars/expression_grammar_inst.o
> bin/stan/gm/ast_def.o \
> bin/stan/gm/grammars/var_decls_grammar_inst.o \
> bin/stan/gm/grammars/whitespace_grammar_inst.o \
> bin/stan/gm/grammars/program_grammar_inst.o
>
> So I am suggesting we have libstanc.a (or libstanc.so).
Won't the usage in R determine whether you need a .a or .so/.dll?
> In order to make it loadable, I did
>
> LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$STAH_HOME/bin.
> Second, we need Stan for compiling models. I need stan's head files and binary libraries (libstan).
You'll also need the library headers in stan/lib.
Should we be calling those something else?
> This step is easier. For the time being, I am just using the STAN_HOME and assuming that
> libstan is in LD_LIBRARY_PATH.
The other issue is whether this environment var works cross platform?
> Simply put, I need to set environment variables STAN_HOME and LD_LIBRARY_PATH
> to allow the installation of rstan from source assuming that stan is built with libstan
> and libstanc in STAN_HOME/bin.
It would be better if there's a way we could code
that without an environment variable. In my experience,
they really confuse users (including me). Could that
path just be specified in an R command somewhere?
> By the way, rstan is in branch rstan. We could try to use it now.
I don't know how others feel, but I'm fine with you just
merging back into the main branch. There's no competing
version of rstan to contend with. The only issue is whether
we want to include whatever form it's in in the first release.
- Bob