[Rcpp-devel] How much speedup for matrix operations?
Douglas Bates
bates at stat.wisc.edu
Wed Nov 6 19:24:57 CET 2013
Another thing you can do in compiled code that is difficult to do in R is
to avoid the allocate/free sequences when calling a code segment thousands
of times. R's functional semantics (i.e. the expectation that functions do
not modify their arguments) is great - until you really want to overwrite a
value and you can't. With Eigen and Armadillo and even with raw BLAS calls
you can pass the array that will be the result as well as the arguments.
On Wed, Nov 6, 2013 at 12:19 PM, Simon Zehnder <szehnder at uni-bonn.de> wrote:
> As Romain already mentioned, there could be rather a memory issue -
> copying is one issue. Very often time gets lost in loads and writes with
> cache misses. How large are your matrices? Have you done a memory-profiling?
>
> Best
> Simon
>
> On 06 Nov 2013, at 19:04, Xavier Robin <xavier at cbs.dtu.dk> wrote:
>
> > On 11/6/13 6:38 PM, Romain Francois wrote:
> >> This very much depends on the code but there is a good chance that
> RcppArmadillo will generate code making less data copies, etc ...
> >>
> >> Hard to say without seeing the code.
> >>
> >> Romain
> >>
> > Most of the code (or at least the slow, highly repeated parts) look like:
> >
> >> A <- t(c + t(W) %*% X)
> >> E <- (exp(A) * (1 - 1 / A) + 1 / A) / (exp(A) - 1)
> >> E[abs(A) < sqrt(.Machine$double.eps) * 2 ] <- 0.5
> >>
> >> B <- t(b + W %*% t(E))
> >> X2 <- 1 / (1 + exp(-B))
> >>
> >> A2 <- t(c + t(W) %*% X2)
> >> E2 <- (exp(A2) * (1 - 1 / A2) + 1 / A2) / (exp(A2) - 1)
> >> E2[abs(A2) < sqrt(.Machine$double.eps) * 2 ] <- 0.5
> >>
> >> delta <- (t(X) %*% E - t(X2) %*% E2)
> >> W <- W + delta
> >
> > Where b and c are vectors, W and X matrices. All this is encapsulated in
> a function, that is called a few thousand times in a for loop, with some
> sanity checks. (But it didn't appear to have much impact on the speed... if
> I remove the matrix operations so it does nothing, it executes nearly
> instantly). I understand from Dirk and Douglas that it probably isn't going
> to make a huge difference, though (not by orders).
> >
> > Thanks,
> > Xavier
> >
> > --
> > Xavier Robin, PhD
> > Cellular Signal Integration Group (C-SIG) - http://www.lindinglab.org
> > Center for Biological Sequence Analysis (CBS) - http://www.cbs.dtu.dk
> > Department of Systems Biology - Technical University of Denmark (DTU)
> > Anker Engelundsvej, Building 301, DK-2800 Lyngby, DENMARK.
> >
> > _______________________________________________
> > 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
>
> _______________________________________________
> 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/20131106/b48d2b47/attachment-0001.html>
More information about the Rcpp-devel
mailing list