Hello all. I'm developing a package for R holding a Gibbs sampler, which tends to have better performance when written in C than in R. During each iteration in the Gibbs sampler, I need the inverse of a symmetric matrix. For this, I wish to use lapack, as is concisely suggested in "Writing R extensions", since this will have better performance than I could ever write myself. After some twiddling I have got my code to compile by including "R_ext/Lapack.h" and using "F77_CALL(dpotrf)", but unfortunately, I don't get this to link properly. I get this message: " testc.o:testc.c:(.text+0x255): undefined reference to `dpotrf_'" which seems logical to me as far as my understanding of C reaches, but I don't know how to resolve it. I'm quite sure I need some extra parameters in my makefile, but as I come from a world where all these complexities are happily abstracted away for me by an IDE, I have no actual clue on how to surmount this. However: when I'm done with all my code, I wish to build a package for publication on CRAN, so I want to be sure that not only I can build it on my system, but it will also work well when distributed to other computers (if I understand the package process well, source files are compiled and linked during installation of the package), so I would also like to know how to do this. It should not be relevant, but either way: I'm doing all this on a Windows 7 machine, though the package will probably be used on Linux-based servers eventually. Finally: I have found no comprehensive list of the functions available to an R package developer, nor, strangely, questions about that. Does such a thing exist, or are we up to hoping we find what we are looking for in the header files? If it does not exist already, I would surely be willing to work on it. Thanks for any input. Nick Sabbe -- ping: nick.sabbe at ugent.be link: http://biomath.ugent.be wink: A1.056, Coupure Links 653, 9000 Gent ring: 09/264.59.36 -- Do Not Disapprove
On Fri, Oct 22, 2010 at 4:30 AM, Nick Sabbe <nick.sabbe at ugent.be> wrote:> Hello all.> I'm developing a package for R holding a Gibbs sampler, which tends to have > better performance when written in C than in R. > During each iteration in the Gibbs sampler, I need the inverse of a > symmetric matrix.You may want to check that. The first rule of numerical linear algebra is that you hardly ever need to invert a matrix. If you need to solve a linear system, you use a factorization, as below and then use one of the solvers based on that factorization. If the matrix is small calculating the inverse is not a big problem. If it is large and you do so within each iteration of your sampler then you are probably wasting time.> For this, I wish to use lapack, as is concisely suggested in "Writing R > extensions", since this will have better performance than I could ever write > myself. > > After some twiddling I have got my code to compile by including > "R_ext/Lapack.h" and using "F77_CALL(dpotrf)", but unfortunately, I don't > get this to link properly. > I get this message: " testc.o:testc.c:(.text+0x255): undefined reference to > `dpotrf_'" which seems logical to me as far as my understanding of C > reaches, but I don't know how to resolve it. I'm quite sure I need some > extra parameters in my makefile, but as I come from a world where all these > complexities are happily abstracted away for me by an IDE, I have no actual > clue on how to surmount this.Go back to section 1.2.1 of "Writing R Extensions" that deals with "Using Makevars".> However: when I'm done with all my code, I wish to build a package for > publication on CRAN, so I want to be sure that not only I can build it on my > system, but it will also work well when distributed to other computers (if I > understand the package process well, source files are compiled and linked > during installation of the package), so I would also like to know how to do > this. > > It should not be relevant, but either way: I'm doing all this on a Windows 7 > machine, though the package will probably be used on Linux-based servers > eventually. > > Finally: I have found no comprehensive list of the functions available to an > R package developer, nor, strangely, questions about that. Does such a thing > exist, or are we up to hoping we find what we are looking for in the header > files? If it does not exist already, I would surely be willing to work on > it.Do you mean other than the section on "The R API" in the "Writing R Extensions" manual?> Thanks for any input. > > Nick Sabbe > -- > ping: nick.sabbe at ugent.be > link: http://biomath.ugent.be > wink: A1.056, Coupure Links 653, 9000 Gent > ring: 09/264.59.36 > > -- Do Not Disapprove > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
On Fri, 2010-10-22 at 05:30 -0400, Nick Sabbe wrote:> Hello all. > > I'm developing a package for R holding a Gibbs sampler, which tends to have > better performance when written in C than in R. > During each iteration in the Gibbs sampler, I need the inverse of a > symmetric matrix. > For this, I wish to use lapack, as is concisely suggested in "Writing R > extensions", since this will have better performance than I could ever write > myself. > > After some twiddling I have got my code to compile by including > "R_ext/Lapack.h" and using "F77_CALL(dpotrf)", but unfortunately, I don't > get this to link properly. > I get this message: " testc.o:testc.c:(.text+0x255): undefined reference to > `dpotrf_'" which seems logical to me as far as my understanding of C > reaches, but I don't know how to resolve it. I'm quite sure I need some > extra parameters in my makefile, but as I come from a world where all these > complexities are happily abstracted away for me by an IDE, I have no actual > clue on how to surmount this.Yes. You need to ensure that your program is linked with a Lapack library where the symbol dpotrf_ is defined. In an R package, using a 'Makevars' file in your 'src' directory is used to specify additional linking. See "Writing R Extensions" section 1.2.1 Using ?Makevars?. If you are compiling your code with a custom Makefile, then you will need to add an argument to the linker/compiler specifying a Lapack library to be linked. For example, here is a command that dynamically links the R Lapack and Blas libraries to a pre-compiled 'test.o' on my system: gcc -std=gnu99 -shared -L/usr/local/lib -o test.so test.o -L/usr/local/lib/R/lib -lRlapack -L/usr/local/lib/R/lib -lRblas -lgfortran -lm> However: when I'm done with all my code, I wish to build a package for > publication on CRAN, so I want to be sure that not only I can build it on my > system, but it will also work well when distributed to other computers (if I > understand the package process well, source files are compiled and linked > during installation of the package), so I would also like to know how to do > this.It might be a good idea to build your package simultaneously with your code, and use a Makevars file.> It should not be relevant, but either way: I'm doing all this on a Windows 7 > machine, though the package will probably be used on Linux-based servers > eventually. > > Finally: I have found no comprehensive list of the functions available to an > R package developer, nor, strangely, questions about that. Does such a thing > exist, or are we up to hoping we find what we are looking for in the header > files? If it does not exist already, I would surely be willing to work on > it.Three of the R manuals (R-exts, R-ints, R-admin; http://cran.r-project.org/doc/manuals/) partially describe some aspect of the R API, where "Writing R Extensions" is the most comprehensive. Function descriptions are generally not biolerplate. For good or bad, a look into the C headers is often necessary to use an R API function. -Matt> Thanks for any input. > > Nick Sabbe > -- > ping: nick.sabbe at ugent.be > link: http://biomath.ugent.be > wink: A1.056, Coupure Links 653, 9000 Gent > ring: 09/264.59.36 > > -- Do Not Disapprove > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Matthew S. Shotwell Graduate Student Division of Biostatistics and Epidemiology Medical University of South Carolina