[R] Calling LAPACK functions directly from R
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Apr 12 08:27:43 CEST 2007
This was much more appropriate for R-devel, so please move any followup
there.
On Wed, 11 Apr 2007, William Constantine wrote:
> I am interested in tapping into LAPACK functionality directly from R.
> Using R-2.4.1 for Windows, I was able to do so ala:
>
> dyn.load("bin/Rlapack.dll")
> is.loaded("dstebz") # returns TRUE
>
> N <- 100
> NW <- 4
> n.tapers <- 5
> tpW <- (2 * pi * NW)/N
> otNmo <- 1:N
> D <- as.double(cos(tpW) * ((N - 1 - 2 * (0:(N - 1)))/2)^2)
> E <- as.double((otNmo * (N - otNmo))/2)
>
> z <- .Fortran("dstebz",
> "I", "B", as.integer(N), double(1), double(1),
> as.integer(N - n.tapers + 1), as.integer(N), double(1),
> D, E, integer(1), integer(1), double(N), integer(N), integer(N),
> double(4 * N), integer(3 * N), integer(1))[13:15]
>
> I then extended this approach in developing an R package
> where I added the following to the appropriate .First.lib():
>
> lapack.path <- file.path(R.home(), ifelse(.Platform$OS.type == "windows",
> file.path("bin", "Rlapack"), file.path("lib", "libRlapack")))
> dyn.load(paste(lapack.path,.Platform$dynlib.ext, sep=""))
>
> which loads the LAPACK shared objects in lib/libRlapack.so for LINUX/UNIX
> and bin/Rlapack.dll for Windows.
>
> Q1: Is there a better or more robust way of loading LAPACK symbols
> into R, (e.g., one that is not platform dependent)?
Why do you want to do that? All uses of LAPACK in R itself are via small
C wrappers that make life a lot easier.
> ---------------------------------------------------------------------------------------------------------------------
>
> My R package does not currently contain any C or FORTRAN code. However,
> it has been suggested to me that creating a src/Makevars file containing
> the line:
>
> PKG_LIBS=$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
>
> is a better means of loading LAPACK symbols as it
> would eliminate the need to use dyn.load() as shown in my .First.lib().
> In Windows, however, this suggestion fails and results (for example)
> in the above code returning a "missing dstebz symbol" error. My understanding
> is that one need only create such a src/Makevar if they are interested in
> tapping into LAPACK functionality from their src/ C or FORTRAN code
> and so am doubtful of this suggestion.
>
> Q2: Given that I have no C/FORTRAN code in my package, am I
> correct to assume that creating a Makevars files in such a way
> does not eliminate the need to use dyn.load() as in the above?
>
> Q3: For future releases of R, should I expect the path of the LAPACK
> library to remain as they are noted above in my .First.lib() example?
It may not work even now. Buiilds of R do not necessarily contain
LAPACK code (you can link to an external library) even on Windows.
The portable way to link to LAPACK is to use a C wrapper as described in
'Writing R Extensions'.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list