[Rcpp-devel] Fwd: RcppArmadillo matrix with writeable auxiliary memory

Steven Varga steven.varga at gmail.com
Thu Nov 28 05:59:32 CET 2013


update: came across Simon's
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-June/006080.htmlpost,
where Romain suggested  c++11 move constructor;  which is supported
in the latest Armadillo release;

for the record here are the tested working solutions, if found to be useful
for others and there is a way to add these to RcppArmadillo examples, I can
send the code in required format.

thanks again for everyone's contribution,
steve
/*
----------------------------------------------------------  code c++11
-------------------------------------------------------------------------
 */

#include <R.h>
#include <stdlib.h>
#include <RcppArmadillo.h>
#include <utility>


using namespace std;

// [[Rcpp::depends(RcppArmadillo)]]


// suggested by Simon, pointer solution, this has a disadvantage of extra
de referencing
// which is ok inside class methods, but somewhat awkward when matrix is
public
// has the advantage that doesn't need c++11 and latest Armadillo
struct Example {

    Example(double *ptr) {

        A= new arma::mat(ptr, 3,2, false, true ); // same area as original B
                arma::mat &a = *A; // dereference B

                a(1,1) = 1; // convenient matlab like syntax
    }

        ~Example(){ delete A; }
    arma::mat *A;
};

// this is moving constructor works only with c++11 and latest Armadillo
// much nicer syntax; the 'view' is encapsulated, and invisible;
struct Example1 {

    Example1(double *ptr ) : A(arma::mat(ptr, 3,2, false, true ) ){
               A(1,1) = 3;
    }

    arma::mat A;
};

// this is moving constructor works only with c++11 and latest Armadillo
// much nicer syntax, referenced matrix constructed with move contructor
struct Example2 {

    Example2(arma::mat m ) : A(m){ // 'view' is not visible from the inside
               A(2,1) = 4;
    }

    arma::mat &A;

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

    arma::mat M(3,10); // implicit memory allocation; of course this can be
done many other ways
                                 // Dirk suggestions using bigmemory and or
R matrix passed with Rcpp proxy
                                 // depending on needs

    M.zeros(); // paint with zeros
    M.print(); cout<<endl<<endl;

// this is the main part: arma::mat submatrix has read write access to M
without copy!;
// inside of Example? classes one can operate on some part of M
        Example E(M.memptr());
        M.print();cout<<"-------------"<<endl;

        Example1 E1( M.memptr() );
        M.print();  cout<<"-------------"<<endl;

        Example2 E2( arma::mat( M.memptr(), 3,2, false, true ) );
        M.print();cout<<"-------------"<<endl;
}
// EOF







On Tue, Nov 26, 2013 at 7:38 PM, Steven Varga <steven.varga at gmail.com>wrote:

> Hello Simon,
>
> my apologies contacting you off the list; I try to avoid confusion.
>
> I came across your previous post:
> http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-June/006080.htmlwhere you seemed to have similar issues to mine: creating an arma::mat with
> external storage, then bumping into '=' operator which does deep copy;
>
> I just noticed that the newest version of Armadillo supports c++11 move
> constructor; did you have a chance to try it?
>
> Steve
>
>
> On Tue, Nov 26, 2013 at 6:19 AM, <szehnder at uni-bonn.de> wrote:
>
>> Steve,
>>
>> If you want to use a variable being an arma::mat obejct with varying
>> memory, you could use an arma::mat pointer. Otherwise if memory stays the
>> same but should be reusable in R, Dirk's suggestion initialising memory in
>> R and passing it to C++ is the most practicable way and very fast.
>>
>> Best
>> Simon
>> Gesendet über den BlackBerry® Service von E-Plus.
>>
>> -----Original Message-----
>> From: Steven Varga <steven.varga at gmail.com>
>> Sender: rcpp-devel-bounces at lists.r-forge.r-project.orgDate: Mon, 25 Nov
>> 2013 23:23:24
>> To: <rcpp-devel at lists.r-forge.r-project.org>
>> Reply-To: steven.varga at gmail.com
>> Subject: Re: [Rcpp-devel] RcppArmadillo matrix with writeable auxiliary
>>         memory
>>
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131127/9b20917d/attachment.html>


More information about the Rcpp-devel mailing list