Le 31/03/10 15:25, Douglas Bates a écrit :
On Tue, Mar 30, 2010 at 9:12 AM, Romain Francois
<[email protected]> wrote:
Le 30/03/10 15:36, Douglas Bates a écrit :
On Tue, Mar 30, 2010 at 1:15 AM, Romain Francois
<[email protected]> wrote:
Hi,
If you want to compile the code that is here
http://dirk.eddelbuettel.com/code/rcpp.armadillo.html directly from R
CMD SHLIB, you can do this:
rom...@naxos /tmp $ export PKG_CXXFLAGS=`Rscript -e
"RcppArmadillo:::CxxFlags()"`
rom...@naxos /tmp $ export PKG_LIBS=`Rscript -e "RcppArmadillo:::LdFlags()"`
rom...@naxos /tmp $ R CMD SHLIB armaex.cpp
The way that RcppArmadillo:::LdFlags() is currently defined there is a
possibility of getting different Lapack and BLAS libraries when using
R or calls to RcppArmadillo. R uses the libraries defined by the
macros LAPACK_LIBS, BLAS_LIBS and FLIBS in the file
file.path(R.home("etc"), "Makeconf") # evaluate this in R
It is not certain that those will be the same as the libraries used by
Armadillo. If they are not shared objects you run the risk of getting
two copies of Lapack, etc. and possibly different BLAS, leading to
different performance characteristics and perhaps different results.
Hmmm. Interesting. I did not think of that problem.
On my machine, I have :
Rscript -e "RcppArmadillo:::LdFlags()"
/Library/Frameworks/R.framework/Resources/library/Rcpp/lib/x86_64/libRcpp.a
-L/usr/lib -larmadillo
so linking against Rcpp "user" library and armadillo... but when I
manually set PKG_LIBS to just Rcpp user library, it still works, so we
actually do not need to link against armadillo library. armadillo itself
does not __need__ to get compiled into a library because it is all
templates.
I'm pretty sure that Dirk's FastLM test would fail if you did not link
to -larmadillo library (and thence to -llapack and -lblas). Dirk uses
things like an inv method for a matrix and that will eventually call
Lapack routines.
I haven't looked at the code for Armadillo but I did check that the
armadillo library links to the lapack and blas libraries.
$ ldd /usr/lib/libarmadillo.so
linux-vdso.so.1 => (0x00007fffaf7ff000)
libblas.so.3gf => /usr/lib/libblas.so.3gf (0x00007f01e5593000)
liblapack.so.3gf => /usr/lib/liblapack.so.3gf (0x00007f01e49a0000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f01e468f000)
libm.so.6 => /lib/libm.so.6 (0x00007f01e440b000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007f01e41f4000)
libc.so.6 => /lib/libc.so.6 (0x00007f01e3e84000)
libgfortran.so.3 => /usr/lib/libgfortran.so.3 (0x00007f01e3b98000)
/lib64/ld-linux-x86-64.so.2 (0x00007f01e5a41000)
I've attached an R file that contains the code that is on Dirk's
RcppArmadillo page. The file compiles, links and load the code and run
the function. (similar to what inline does but inline does not work here
because it includes Rcpp.h too early, that's another story)
The key thing is that this is not linking against armadillo library or
blas or lapack. I suppose the blas functions are retrieved from R's blas
and lapack.
$ Rscript armaex.R
Le chargement a nécessité le package : inline
Le chargement a nécessité le package : methods
Le chargement a nécessité le package : Rcpp
g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include
-I/Library/Frameworks/R.framework/Resources/include/x86_64
-I/usr/local/include
-I/Library/Frameworks/R.framework/Resources/library/Rcpp/lib/x86_64
-I/Library/Frameworks/R.framework/Resources/library/RcppArmadillo/include -I/usr/include
-fPIC -g -O3 -Wall -pipe -Wno-variadic-macros -c armaex.cpp -o armaex.o
g++-4.2 -arch x86_64 -dynamiclib -Wl,-headerpad_max_install_names
-undefined dynamic_lookup -single_module -multiply_defined suppress
-L/usr/local/lib -o armaex.so armaex.o
/Library/Frameworks/R.framework/Resources/library/Rcpp/lib/x86_64/libRcpp.a
-F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework
-Wl,CoreFoundation
$coefficients
[,1]
[1,] -2.353325
[2,] 2.199970
$stderr
[,1]
[1,] 0.23066284
[2,] 0.08983455
(on OSX I need to use otool instead of ldd for some reason: )
$ otool -L armaex.so
armaex.so:
armaex.so (compatibility version 0.0.0, current version 0.0.0)
/Library/Frameworks/R.framework/Versions/2.11/Resources/lib/libR.dylib
(compatibility version 2.11.0, current version 2.11.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
(compatibility version 150.0.0, current version 550.13.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current
version 7.9.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current
version 125.0.0)
and :
$ otool -L
/Library/Frameworks/R.framework/Versions/2.11/Resources/lib/libR.dylib
/lib/libR.dylib
/Library/Frameworks/R.framework/Versions/2.11/Resources/lib/libR.dylib:
/Library/Frameworks/R.framework/Versions/2.11/Resources/lib/libR.dylib
(compatibility version 2.11.0, current version 2.11.0)
/Library/Frameworks/R.framework/Versions/2.11/Resources/lib/libRblas.dylib
(compatibility version 0.0.0, current version 0.0.0)
/Library/Frameworks/R.framework/Versions/2.11/Resources/lib/libgfortran.2.dylib
(compatibility version 3.0.0, current version 3.0.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
(compatibility version 150.0.0, current version 476.19.0)
/Library/Frameworks/R.framework/Versions/2.11/Resources/lib/libreadline.5.2.dylib
(compatibility version 5.0.0, current version 5.2.0)
/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version
1.2.3)
/usr/lib/libicucore.A.dylib (compatibility version 1.0.0, current
version 36.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current
version 111.1.4)
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version
7.0.0)
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version
1.0.0)
so I suppose the reason why this works here is that blas and lapack from
R are used.
Maybe we should remove the -L/usr/lib -larmadillo business in
RcppArmadillo so that there is no confusion. We used to compile the
package just fine without them, I think Dirk added the flags for some
windows related reason.
I would however strongly recommend you to have this in a package.
Romain
Le 26/03/10 19:00, Abhisek a écrit :
Hi Dirk,
I was referring to this page:
http://dirk.eddelbuettel.com/code/rcpp.armadillo.html
i was trying to run the code there.
here is my understanding so far (im new to linux and c++ so i apologize
for seeming rather slow!):
1. The Rcpp package helps C++ and R recognize each others stuff (like
variables).
2. The RcppArmadillo package extends this functionality to objects used
in armadillo (such as matrices and vectors e.g. colvec).
3. In FastLM, armadillo is being included but not RcppArmadillo. but
that is ok because there is a loop in there which converts an vector as
defined in armadillo to one which Rcpp recognizes. And it is this
vector which which gets passed back to R through the Rcpp interface.
also, it uses cfunction which is different from what im doing here as i
think you mentioned earlier (it worked by the way!)
what i tried to do was as follows:
a) to run the code as in the link above and include Rcpp and
RcppArmadillo and I was getting the errors as described in my initial post.
b) then i tried the same code but only including Rcpp and armadillo.
if my reasoning above is correct, part (b) should not run as Rcpp would
not recognize stuff like arma::colvec. but, i get the exact same error
as above!
so that suggests, that while compiling the code, the compiler is not
even getting to the point in procedure b when it should encounters an
type it doesnt recognize. which means that somehow, armadillo is giving
problems. but that is as far as i can understand. either that or my
understanding is way off.
appreciate your patience and help!
abhisek
On Fri, Mar 26, 2010 at 4:20 PM, Dirk Eddelbuettel<[email protected]
<mailto:[email protected]>> wrote:
On 26 March 2010 at 15:32, Abhisek wrote:
| Hi Dirk,
|
| Just so that I understand. I copied the code in the RcppArmadillo
section
| on your website and pasted and saved it.
What code? What page?
| Are you saying I should change
| #include<RcppArmadillo> to #include<Rcpp> and #include<armadillo>
?
No that is not what I said. I said
You can't just look at FastLM() and think your code will run
like it.
RcppArmadillo __does not export its classes__. It is an
implementation
reference -- it gives you guidance as to how to implement C++
packages of
your own against other libraries.
If you "just" want to use Armadillo and have Rcpp help you, look
at the
installed example 'FastLM' in the Rcpp package. In other words
ignore
RcppArmadillo for now.
So look at /usr/local/lib/Rcpp/examples/FastLM/ and the files there.
Run
fastLMviaArmadillo.r
and look at it. That should work.
You can also look at the _entire RcppArmadillo package_ and see how the
pieces fit together, including src/Makevars.
But don't just take semi-random shortcuts and expect that to work. If we
could make it any simpler, we would. There is a reason RcppArmadilli is
package and not just a file.
Dirk
| best,
| abhisek
|
| On Fri, Mar 26, 2010 at 1:40 PM, Dirk Eddelbuettel<[email protected]
<mailto:[email protected]>> wrote:
|
|>
|> Hi Abhisek,
|>
|> On 26 March 2010 at 12:28, Abhisek wrote:
|> | I downloaded the stuff just last friday so it should be the latest
|> version
|> | of Rcpp as well as Armadillo. I used install.packages() for
Rcpp and
|> | RcppArmadillo. Armadillo is 0.9.4. Im using xubuntu karmic
koala.
|>
|> Ok, that is all good. And if RcppArmadillo installed you have
proper
|> libraries, Armadillo included.
|>
|> I think part of your problem is a misunderstanding of an issue
we may not
|> have made particularly clear: You can't just look at FastLM()
and think
|> your code will run like it. RcppArmadillo __does not export its
classes__.
|> It is an implementation reference -- it gives you guidance as to
how to
|> implement C++ packages of your own against other libraries.
|>
|> If you "just" want to use Armadillo and have Rcpp help you, look
at the
|> installed example 'FastLM' in the Rcpp package. In other words
ignore
|> RcppArmadillo for now.
|>
|> The example (in 0.7.10) uses an idiom we no longer like and I am
about to
|> change that for an upcoming 0.7.11 release, but it should works
as is and
|> just use inline -- so no messing with makefile etc.
|>
|> Try that and see how it goes and keep us posted.
|>
|> Cheers, Dirk
|>
|>
|> | i also had some problem using Armadillo which I posted on the
Armadillo
|> | discussion board. Conrad got back to me. Here is the discussion:
|> | https://sourceforge.net/apps/phpbb/arma/viewtopic.php?f=1&t=4
<https://sourceforge.net/apps/phpbb/arma/viewtopic.php?f=1&t=4>
|> |
|> | so it may be that armadillo is the problem but im really not sure.
|> |
|> | Many thanks
|> | Abhisek
|> |
|> | On Fri, Mar 26, 2010 at 12:17 PM, Dirk Eddelbuettel
<[email protected]<mailto:[email protected]>>
|> wrote:
|> |
|> |>
|> |> (resending, this time to list I omitted last time, sorry.
--Dirk)
|> |>
|> |> On 26 March 2010 at 11:10, Abhisek wrote:
|> |> | Hi,
|> |> |
|> |> | Ive been trying to make RcppArmadillo work using the
example file
|> |> (fastLM).
|> |> | I copied and pasted the code and saved it as
RcppArmadilloEg.cpp (not
|> to
|> |> use
|> |> | fastLM but to figure out how to work RcppArmadillo). Then
i tried
|> using
|> |> the
|> |> | command R CMD SHLIB RcppArmadilloEg.cpp but an error came
up saying
|> that
|> |> | RcppArmadillo.h not found.
|> |> |
|> |> | So i tried doing a symbolic link from /usr/local/include
to the
|> directory
|> |> | where RcppArmadillo.h and RcppArmadilloDefines.h is
stored. I also
|> did a
|> |> | symbolic link analogously for RcppArmadillo.so. When i
ran R CMD
|> SHLIB
|> |> | again I got the following error:
|> |> |
|> |> | abhi...@shieks:~/Desktop/Personal/Test$ R CMD SHLIB
|> RcppArmadilloEg.cpp
|> |> | g++ -I/usr/share/R/include
|> |> | -I/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib
-fpic -g
|> -O2
|> |> -c
|> |> | RcppArmadilloEg.cpp -o RcppArmadilloEg.o
|> |>
|> |> So far so good: one -I for R as usual, one for Rcpp.
|> |>
|> |> | In file included from
|> |> |
|>
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/RcppCommon.h:215,
|> |> | from
|> |> |
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/Rcpp.h:28,
|> |> | from RcppArmadilloEg.cpp:1:
|> |> |
|> |>
|>
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/Rcpp/internal/wrap.h:
|> |> | In function ‘SEXPREC*
|> |> Rcpp::internal::wrap_dispatch_unknown_iterable(const
|> |> | T&, Rcpp::traits::false_type) [with T = arma::Col<double>]’:
|> |> |
|> |>
|>
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/Rcpp/internal/wrap.h:491:
|> |> | instantiated from ‘SEXPREC*
|> Rcpp::internal::wrap_dispatch_unknown(const
|> |> T&,
|> |> | Rcpp::traits::false_type) [with T = arma::Col<double>]’
|> |> |
|> |>
|>
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/Rcpp/internal/wrap.h:509:
|> |> | instantiated from ‘SEXPREC*
|> |> | Rcpp::internal::wrap_dispatch_unknown_importable(const T&,
|> |> | Rcpp::traits::false_type) [with T = arma::Col<double>]’
|> |> |
|> |>
|>
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/Rcpp/internal/wrap.h:526:
|> |> | instantiated from ‘SEXPREC*
Rcpp::internal::wrap_dispatch(const T&,
|> |> | Rcpp::traits::wrap_type_unknown_tag) [with T =
arma::Col<double>]’
|> |> |
|> |>
|>
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/Rcpp/internal/wrap.h:541:
|> |> | instantiated from ‘SEXPREC* Rcpp::wrap(const T&) [with T =
|> |> | arma::Col<double>]’
|> |> |
|>
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/Rcpp/Named.h:54:
|> |> | instantiated from ‘Rcpp::Named::Named(const std::string&,
const T&)
|> [with
|> |> T
|> |> | = arma::colvec]’
|> |> | RcppArmadilloEg.cpp:19: instantiated from here
|> |> |
|> |>
|>
/home/abhisek/R/i486-pc-linux-gnu-library/2.10/Rcpp/lib/Rcpp/internal/wrap.h:394:
|> |> | error: cannot convert ‘const arma::Col<double>’ to
‘SEXPREC*’ in
|> |> | initialization
|> |> | make: *** [RcppArmadilloEg.o] Error 1
|> |> |
|> |> |
|> |> | could someone provide some advice?
|> |>
|> |> Which Rcpp version do you have installed?
|> |>
|> |> Which Armadillo version do you have installed?
|> |>
|> |> I have not tried the free-floating direct compile you try
here. Maybe
|> the
|> |> defines for RcppArmadillo that we use are missing here --
hard to say.
|> |>
|> |> Could you try to create your own little package but copying
the
|> |> RcppArmadillo
|> |> files, changes DESCRIPTION to what you do and changes the
FastLM code
|> in
|> |> C++
|> |> and R?
|> |>
|> |> Dirk
|> |>
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5
require( inline )
require( Rcpp )
cxxflags <- capture.output( RcppArmadillo:::CxxFlags() )
ldflags <- capture.output( Rcpp:::LdFlags() )
code <- '
#include <RcppArmadillo.h>
extern "C" {
SEXP file10d63af1 ( SEXP ys, SEXP Xs );
}
SEXP file10d63af1 ( SEXP ys, SEXP Xs ) {
Rcpp::NumericVector yr(ys); // creates Rcpp vector
from SEXP
Rcpp::NumericMatrix Xr(Xs); // creates Rcpp matrix from SEXP
int n = Xr.nrow(), k = Xr.ncol();
arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids
extra copy
arma::colvec y(yr.begin(), yr.size(), false);
arma::colvec coef = solve(X, y); // fit model y ~ X
arma::colvec resid = y - X*coef; // residuals
double sig2 = arma::as_scalar( trans(resid)*resid/(n-k) );
// std.error of estimate
arma::colvec stderrest = sqrt( sig2 * diagvec( arma::inv(arma::trans(X)*X))
);
Rcpp::Pairlist res(Rcpp::Named( "coefficients", coef),
Rcpp::Named( "stderr", stderrest));
return res;
}
'
writeLines( code, "armaex.cpp" )
Sys.setenv( PKG_LIBS = ldflags )
Sys.setenv( PKG_CXXFLAGS = cxxflags )
unlink( "armaex.so" )
system( "R CMD SHLIB armaex.cpp" )
dyn.load( "armaex.so" )
fx <- function( ... ){
.Call( "file10d63af1", ... )
}
fx( log(trees$Volume), cbind(rep(1,31), log(trees$Girth) ) )
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel