[Rcpp-devel] RcppArmadillo matrix with writeable auxiliary memory

Steven Varga steven.varga at gmail.com
Tue Nov 26 03:45:10 CET 2013


Hello,

I am having trouble creating arma::mat with external writeable auxiliary
memory as class member;
ie:

struct Example {
         Example( double *ptr ){
// ..... see provided minimal working example
 }

arma::mat ExtPtr; // this is what I would like to have
}

What is it that I am doing wrong? Can someone point to the right direction
which preferably would use externally created memory location, with
encapsulated data structures?

----------------------------------------------------------------------------------------------
 example: save it to 'arma_example.cpp', then:
sourceCpp('arma_example.cpp'); arma_example();
----------------------------------------------------------------------------------------------
/*
 * Contact: Steven Varga
 *          steven.varga at gmail.com
 *          2013 Toronto, On Canada
 */

#include <stdlib.h>
#include <RcppArmadillo.h>
using namespace std;

// [[Rcpp::depends(RcppArmadillo)]]
struct Example {

    Example(double *ptr)
    /*
    * Initilizationlist wont do
    * error: invalid initialization of non-const reference of type
    * ‘arma::mat& {aka arma::Mat<double>&}’ from an rvalue of type
    * ‘arma::mat {aka arma::Mat<double>
    : A(arma::mat(ptr, 3,2, false, true) ){
    */
    {
        cout<<" creating arma mat, then assigning it to member B " <<endl;
        B=arma::mat(ptr+6, 3,2, false, true ); // same area as original B
        cout<<"resetting B to 'zeros' should affect datablock outside of
'Example' struct " <<endl;
        B.zeros(); // reset to zero
        cout<<" printing B inside of Example: "<<endl;
        B.print();
        cout<<" but B is a copy only, can I have a reference, or B with
original ptr?"<<endl<<endl;
    }

    //arma::mat& A;
    arma::mat B;
};


// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void  arma_example() {


    double* p = static_cast<double*>( malloc( 30*sizeof(double) ));
    arma::mat M(p, 3,10, false, true ); // this works nicely

    bzero(p, 30*sizeof(double)); // reset M
    M.zeros(); // same as above, but cleaner

    arma::mat A(p, 3,1, false, true );   // these work as advertised we
have A,B
    arma::mat B(p+6, 3,2, false, true ); // inside ptr area shifted slightly

    A.ones(); B.ones(); // lets paint ptr with 'ones'
    cout<<"area A and B are ones, rest are zeros as expected"<<endl;
    M.print();

    cout<< "Now I would like to have the above; encapsulating A,B inside
'Example' "
    << "keeping 'M' and ptr outside of class 'Example' "
    << "by passing 'ptr' in constructor "
    << "initializer list fails to create reference to 'A' from temporary
arma::mat "
    << "when using operator '=' B is initilized with the COPY of ptr
"<<endl;
    Example E(p);

    cout<<"This is B outside of struct 'Example', expected to be zeros, see
struct Example sonstructor"<<endl;
    B.print(); // but B is still 'ones' because '=' copied memory from ptr
    // passing B by reference would solve the problem; but that requires
    // having B outside of struct Example; and I preferred hiding it; but
using
    // RcppArmadillo/matlab syntax inside struct Example on ptr for matrix
multiplication
    // and such

    // CONTEXT:
    // neural network implementation with weights, gradients parameter
unrolling,
    // and efficiently passing them to optimizer.
    // size: layer[n]*layer[n-1]*layers* sizeof(double)
    // roughly:
    // ~ 2* 1000*1000*6*8 = 2 * 6*8*10^6 ~= 100 * 10^6 = 100Mbyte
    // gets called 10^6~10^7 times per epoch, depending on minibatch size
    // epoch := 40 ~ 4000 depending on optimizer
    free(p);
}
// EOF
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131125/efd05e2a/attachment-0001.html>


More information about the Rcpp-devel mailing list