I have tried linking C++ code using the Template Numerical Toolkit
version 0.8.3 (see http://math.nist.gov/tnt/) with R-0.50-a3 (see
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html). I expected this to
be very difficult. It was very easy. I was pleasantly surprised.
Nice work by the developers all around.
A bit of background for the R crowd: TNT, being developed by Roldan
Pozo at the National Institute for Standards and Technology, is the
successor to lapack++
As the name indicates, all the representations in TNT are templated.
The advantages of using it with R would be that one could obtain the
execution speed of compiled linear algebra code without the
corresponding hassle of keeping track of all the dimensions associated
with matrix representations in Fortran. All of us really love to
write those calls that require
A, LDA, M, N
for each matrix being passed but some of us in the baby boomer
generation are getting on in age and we lose track of the calling
sequence at about the 12th argument :-)
TNT creates all matrices as copies of the original content. Because
it is written in C++, it gracefully handles the releasing of the
storage when the identifier goes out of scope.
Here is a sample run on a Linux system
$ cat R_link.cc
// An example of linking the Template Numerical Toolkit 0.8.3 with R-0.50
//
#include "tnt.h"
#include "vec.h"
#include "fmat.h"
extern "C" {
static double *
copy_contents(double *dest, Fortran_matrix<double> A)
{
double *dd = dest, *src = &A(1,1);
int nn = A.num_rows() * A.num_cols();
while (nn--) { *dd++ = *src++; }
return dest;
}
void my_mat_sum(double * A, double * B, long int * dims) {
Fortran_matrix<double>
AA(dims[0], dims[1], A), // creates a copy
BB(dims[0], dims[1], B),
CC = AA + BB;
cout << "A on input: " << AA << endl;
cout << "B on input: " << BB << endl;
cout << "Sum: " << CC << endl;
copy_contents(A, CC);
}
}
$ g++ -c -fpic R_link.cc
$ ld -shared -o R_link.so R_link.o -lstdc++
$ R
R : Copyright 1997, Robert Gentleman and Ross Ihaka
Version 0.50 Alpha-3 (August 8, 1997)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type "license()" for details.
[Previously saved workspace restored]
> A
[,1] [,2] [,3]
[1,] -0.2713707 -1.1715863 0.8884861
[2,] -1.7103328 0.6957497 -0.7603538 > B
[,1] [,2] [,3]
[1,] -1.573575 0.1554325 -1.1013854
[2,] -1.151815 0.1632211 -0.4710143 > A+B
[,1] [,2] [,3]
[1,] -1.844946 -1.0161538 -0.2128993
[2,] -2.862148 0.8589708 -1.2313680 > dyn.load("R_link.so")
> .C("my_mat_sum", A, B, as.integer(dim(A)))[[1]]
A on input: 2 3
-0.271371 -1.17159 0.888486
-1.71033 0.69575 -0.760354
B on input: 2 3
-1.57357 0.155432 -1.10139
-1.15181 0.163221 -0.471014
Sum: 2 3
-1.84495 -1.01615 -0.212899
-2.86215 0.858971 -1.23137
[,1] [,2] [,3]
[1,] -1.844946 -1.0161538 -0.2128993
[2,] -2.862148 0.8589708 -1.2313680 > q()
Save workspace image? [y/n/c]: n
--
Douglas Bates bates@stat.wisc.edu
Statistics Department 608/262-2598
University of Wisconsin - Madison http://www.stat.wisc.edu/~bates/
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To:
r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-