[Rcpp-devel] sourceCpp issue: code vs. file

Rodney Sparapani rsparapa at mcw.edu
Wed May 22 17:40:01 CEST 2013


Hi!

I am investigating some of Armadillo's new sparse matrix capabilities.
But, I ran into a problem with sourceCpp that I don't understand.
Here is an example right out of the Rcpp gallery
<http://gallery.rcpp.org/articles/armadillo-sparse-matrix/>

With code= it works as advertised, but it fails with file=
(and this is true with 4 different R installations that I tried):

suppressMessages(library(Matrix))
i <- c(1,3:8)
j <- c(2,9,6:10)
x <- 7 * (1:7)
A <- sparseMatrix(i, j, x = x)
print(A)

require(RcppArmadillo)

sourceCpp(code='
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp ;

// [[Rcpp::export]]
void convertSparse(S4 mat) { // slight improvement with two non-nested loops
     IntegerVector dims = mat.slot("Dim");
     IntegerVector i = mat.slot("i");
     IntegerVector p = mat.slot("p");
     NumericVector x = mat.slot("x");

     int nrow = dims[0], ncol = dims[1];
     arma::sp_mat res(nrow, ncol);

     // create space for values, and copy
     arma::access::rw(res.values) =
         arma::memory::acquire_chunked<double>(x.size() + 1);
     arma::arrayops::copy(arma::access::rwp(res.values),
                          x.begin(), x.size() + 1);

     // create space for row_indices, and copy -- so far in a lame loop
     arma::access::rw(res.row_indices) =
         arma::memory::acquire_chunked<arma::uword>(x.size() + 1);
     for (int j=0; j<i.size(); j++)
         arma::access::rwp(res.row_indices)[j] = i[j];

     // create space for col_ptrs, and copy -- so far in a lame loop
     arma::access::rw(res.col_ptrs) =
         arma::memory::acquire<arma::uword>(p.size() + 2);
     for (int j=0; j<p.size(); j++)
         arma::access::rwp(res.col_ptrs)[j] = p[j];

     // important: set the sentinel as well
     arma::access::rwp(res.col_ptrs)[p.size()+1] =
         std::numeric_limits<arma::uword>::max();

     // set the number of non-zero elements
     arma::access::rw(res.n_nonzero) = x.size();

     Rcout << "SpMat res:" << res << std::endl;
}')

convertSparse(A)
#Works!
## > SpMat res:[matrix size: 8x10; n_nonzero: 7; density: 8.75%]

##      (0, 1)          7.0000
##      (3, 5)         21.0000
##      (4, 6)         28.0000
##      (5, 7)         35.0000
##      (2, 8)         14.0000
##      (6, 8)         42.0000
##      (7, 9)         49.0000

sourceCpp(verbose=TRUE, rebuild=TRUE, file="~/arma-sp.cxx")
#Fails!
Generated extern "C" functions
--------------------------------------------------------


#include <Rcpp.h>

RcppExport SEXP sourceCpp_72326_convertSparse(SEXP matSEXP) {
BEGIN_RCPP
     Rcpp::RNGScope __rngScope;
     S4 mat = Rcpp::as<S4 >(matSEXP);
     convertSparse(mat);
     return R_NilValue;
END_RCPP
}

Generated R functions
-------------------------------------------------------

`.sourceCpp_72326_DLLInfo` <- 
dyn.load('/tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27/sourceCpp_930.so')

convertSparse <- Rcpp:::sourceCppFunction(function(mat) {}, TRUE, 
`.sourceCpp_72326_DLLInfo`, 'sourceCpp_72326_convertSparse')

rm(`.sourceCpp_72326_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27

/opt/local/lib64/R/bin/R CMD SHLIB -o 'sourceCpp_930.so' --preclean 
'arma-sp.cxx'
Error in 
dyn.load("/tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27/sourceCpp_930.so") 
(from arma-sp.cxx.R#1) :
   unable to load shared object 
'/tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27/sourceCpp_930.so':
   /tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27/sourceCpp_930.so: cannot open 
shared object file: No such file or directory

-- 
Rodney Sparapani, PhD  Center for Patient Care and Outcomes Research
Sr. Biostatistician               http://www.mcw.edu/pcor
4 wheels good, 2 wheels better!   Medical College of Wisconsin (MCW)
WWLD?:  What Would Lombardi Do?   Milwaukee, WI, USA
-------------- next part --------------
suppressMessages(library(Matrix))
i <- c(1,3:8) 
j <- c(2,9,6:10) 
x <- 7 * (1:7)
A <- sparseMatrix(i, j, x = x) 
print(A)

require(RcppArmadillo)

sourceCpp(code='
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp ;

// [[Rcpp::export]]
void convertSparse(S4 mat) { // slight improvement with two non-nested loops
    IntegerVector dims = mat.slot("Dim");
    IntegerVector i = mat.slot("i");
    IntegerVector p = mat.slot("p");
    NumericVector x = mat.slot("x");
    
    int nrow = dims[0], ncol = dims[1];
    arma::sp_mat res(nrow, ncol);

    // create space for values, and copy
    arma::access::rw(res.values) = 
        arma::memory::acquire_chunked<double>(x.size() + 1);
    arma::arrayops::copy(arma::access::rwp(res.values), 
                         x.begin(), x.size() + 1);

    // create space for row_indices, and copy -- so far in a lame loop
    arma::access::rw(res.row_indices) = 
        arma::memory::acquire_chunked<arma::uword>(x.size() + 1);
    for (int j=0; j<i.size(); j++) 
        arma::access::rwp(res.row_indices)[j] = i[j];
    
    // create space for col_ptrs, and copy -- so far in a lame loop
    arma::access::rw(res.col_ptrs) = 
        arma::memory::acquire<arma::uword>(p.size() + 2);
    for (int j=0; j<p.size(); j++) 
        arma::access::rwp(res.col_ptrs)[j] = p[j];

    // important: set the sentinel as well
    arma::access::rwp(res.col_ptrs)[p.size()+1] = 
        std::numeric_limits<arma::uword>::max();
    
    // set the number of non-zero elements
    arma::access::rw(res.n_nonzero) = x.size();

    Rcout << "SpMat res:" << res << std::endl;
}')

convertSparse(A)
#Works!
## > SpMat res:[matrix size: 8x10; n_nonzero: 7; density: 8.75%]

##      (0, 1)          7.0000
##      (3, 5)         21.0000
##      (4, 6)         28.0000
##      (5, 7)         35.0000
##      (2, 8)         14.0000
##      (6, 8)         42.0000
##      (7, 9)         49.0000

sourceCpp(verbose=TRUE, rebuild=TRUE, file="~/arma-sp.cxx")
#Fails!
Generated extern "C" functions 
--------------------------------------------------------


#include <Rcpp.h>

RcppExport SEXP sourceCpp_72326_convertSparse(SEXP matSEXP) {
BEGIN_RCPP
    Rcpp::RNGScope __rngScope;
    S4 mat = Rcpp::as<S4 >(matSEXP);
    convertSparse(mat);
    return R_NilValue;
END_RCPP
}

Generated R functions 
-------------------------------------------------------

`.sourceCpp_72326_DLLInfo` <- dyn.load('/tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27/sourceCpp_930.so')

convertSparse <- Rcpp:::sourceCppFunction(function(mat) {}, TRUE, `.sourceCpp_72326_DLLInfo`, 'sourceCpp_72326_convertSparse')

rm(`.sourceCpp_72326_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27

/opt/local/lib64/R/bin/R CMD SHLIB -o 'sourceCpp_930.so' --preclean 'arma-sp.cxx' 
Error in dyn.load("/tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27/sourceCpp_930.so") (from arma-sp.cxx.R#1) : 
  unable to load shared object '/tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27/sourceCpp_930.so':
  /tmp/RtmpSgRD0G/sourcecpp_f97a2e266e27/sourceCpp_930.so: cannot open shared object file: No such file or directory
-------------- next part --------------
A non-text attachment was scrubbed...
Name: arma-sp.cxx
Type: text/x-c++src
Size: 1478 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130522/cde3bf28/attachment.cxx>


More information about the Rcpp-devel mailing list