[Rcpp-devel] use of auxiliary functions

Romain Francois romain at r-enthusiasts.com
Wed Aug 11 21:59:57 CEST 2010


Hi,

For one thing, the construct :

arma::mat B = Rcpp::as<  arma::mat>( A ) ;

probably makes copies along the way. I'm not sure what right now, but we 
might a different construct, something like:

arma::mat B ;
Rcpp::borrow<arma::mat>( B, A ) ;

where borrow would have this signature perhaps

template <typename T>
void borrow( T& target, SEXP origin ) ;


This would avoid some of these unnecessary copies. Not sure borrow is 
the right verb though.

Also, the compiler might implicitely inline dum1 and dum2 because they 
are so simple, so the performance might be the same, I'm not sure about 
that though ...

Romain



Le 11/08/10 21:30, baptiste auguie a écrit :
> I think I understand the principle, however with my best effort I
> cannot find a test case. Here I thought I'd be passing an arbitrarily
> large arma matrix to some function, but again the timing is not
> convincing (in fact, I had to stop the execution once with the dummy2
> version),
>
> using namespace Rcpp ;
> using namespace RcppArmadillo ;
>
> extern "C" {
>
>    arma::mat dum1(arma::mat B)
>    {
>      return (B*38);
>    }
>
>    arma::mat dum2(const arma::mat&  B)
>    {
>      return (B*38);
>    }
>
>    RCPP_FUNCTION_2(NumericMatrix, dummy1, NumericMatrix A, int N) {
>      arma::mat B = Rcpp::as<  arma::mat>( A ) ;
>      return wrap(dum1(repmat(B, N, N)));
>    }
>
>    RCPP_FUNCTION_2(NumericMatrix, dummy2, NumericMatrix A, int N) {
>      arma::mat B = Rcpp::as<  arma::mat>( A ) ;
>      return wrap(dum2(repmat(B, N, N)));
>    }
>
> }
>
> }
>
> Regards,
>
> baptiste
>
>
> On 11 August 2010 19:21, Davor Cubranic<cubranic at stat.ubc.ca>  wrote:
>> On August 11, 2010 02:48:23 am romain at r-enthusiasts.com wrote:
>>> Le 10 août 2010 à 22:10, baptiste auguie
>> <baptiste.auguie at googlemail.com>  a écrit :
>>>> OK, thanks. I have not been able to produce a minimal code that
>>>> would exhibit an improved performance using this
>>>> passing-by-reference idea.
>>>
>>> Hi,
>>>
>>> It would usually make a difference when copying the object that is
>>> passed by value is "expensive" to copy. Rcpp objects are very cheap
>>> to copy and both copies refer to the same actual data (the same
>>> SEXP).
>>>
>>> For internal code my recommendation would be to always use pass by
>>> reference and do an explicit call to clone when a real copy
>>> (different SEXP) is needed.
>>
>> In other words, if you were passing large arma::mat's, then const
>> references should make a real difference.
>>
>> Davor
>>
> _______________________________________________
> 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
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/aAyra4 : highlight 0.2-2
|- http://bit.ly/94EBKx : inline 0.3.6
`- http://bit.ly/aryfrk : useR! 2010



More information about the Rcpp-devel mailing list