Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-12 Thread terrance savitsky
The comment of Doug Bates on this thread motivated me to better examine opportunities for algorithm improvements in my R package c++ code written under Rcpp and RcppArmadillo. I performed a few changes: firstly, i removed all general matrix inverse computations, replacing many with triangular so

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-12 Thread Honglang Wang
And actually, i got three different types of errors: 1)Error in betahat(ker, inv[l], cbind(inv[1:ll], inv[(ll + 1):(2 * ll)]), : unused argument(s) (n = 1) Calls: system.time ... apply -> FUN -> betahat -> .External -> cpp_exception Execution halted 2)Error in betahat(ker, inv[l], cbind(inv[1:

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-11 Thread Honglang Wang
Dear All, I have modified the code as following. Since I found the old code had the memory allocation issue, I calculated each term of the matrix with dim 4. The speed is pretty much good now. But since I am not familiar with Rcpp, the code sometimes will get error, like *** caught segfault *** a

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-11 Thread Honglang Wang
I tried out that if the dim of the matrix less than or equal to 4, inv() and solve() have the same speed. Best wishes! Honglang Wang Office C402 Wells Hall Department of Statistics and Probability Michigan State University 1579 I Spartan Village, East Lansing, MI 48823 wangh...@msu.edu On Mon

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-10 Thread c s
Hi Honglang, I recommend looking into using the solve() function in Armadillo, instead of inv(). Using solve() will be considerably faster. More details here: http://arma.sourceforge.net/docs.html#solve On Thursday, December 6, 2012, Honglang Wang wrote: > The following is a full example altho

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-06 Thread Honglang Wang
Thanks Douglas, I will study your code and Eigen carefully. Best wishes! Honglang Wang Office C402 Wells Hall Department of Statistics and Probability Michigan State University 1579 I Spartan Village, East Lansing, MI 48823 wangh...@msu.edu On Thu, Dec 6, 2012 at 1:33 PM, Douglas Bates wrote

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-06 Thread Dirk Eddelbuettel
On 6 December 2012 at 12:33, Douglas Bates wrote: | // [[Rcpp::depends(RcppEigen)]] | #include | | typedef Eigen::MatrixXd Mat; | typedef Eigen::MapMMat; | typedef Eigen::HouseholderQRQR; | typedef Eigen::VectorXd Vec; | typedef Eigen::Map

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-06 Thread Douglas Bates
I don't know as much about Armadillo as I do about Eigen so I will cheat and write using RcppEigen instead of RcppArmadillo. Page 6 of the Eigen tutorial at http://eigen.tuxfamily.org/dox/ discusses decompositions and solving linear systems of equations. One of the simplest ways of solving a weig

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-05 Thread Darren Cook
> this part will always make your code crawl along: >> arma::vec betahat = arma::inv(Inv)*arma::trans(D)*W*y; I'd be very interested to see the before/after version of your code code, Honglang. With timings. It'll make a good blog post or academic paper (depending on just how many learnings you

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-05 Thread Tim Triche, Jr.
this part will always make your code crawl along: On Wed, Dec 5, 2012 at 11:09 AM, Honglang Wang wrote: > arma::vec betahat = arma::inv(Inv)*arma::trans(D)*W*y; > first time I wrote a GLM engine, I wrote it the way statistics books illustrate it (i.e. actually invert things to do IWLS) and it

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-05 Thread Honglang Wang
I am sorry. I think you are right. I know little about computation. And it will be great that you write what you would like to wrote to explain to me. I am really learning a lot from this discussion. Before I even have not heard of profiling of a program. Thanks. Best wishes! Honglang Wang Offic

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-05 Thread Honglang Wang
The following is a full example although I don't know whether it's minimal or not: library(Rcpp) library(RcppArmadillo) sourceCpp("betahat_mod.cpp") #The following is data generation. n=200 m=20 p=2 t=runif(m*n,min=0, max=1) X1=rnorm(m*n,0,1) X1=as.matrix(1+2*exp(t)+X1) X2=rnorm(m*n

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-05 Thread Douglas Bates
On Tue, Dec 4, 2012 at 9:39 PM, Honglang Wang wrote: > Yes, the main issue for my coding is the allocation of memory. And I have > fixed one of the biggest memory allocation issue: 4000 by 4000 diagonal > matrix. And since I am not familiar with Rcpp and RcppArmadillo, I have no > idea how to reu

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-05 Thread Christian Gunning
Can you post a minimal full example? -Christian On Tue, Dec 4, 2012 at 8:39 PM, Honglang Wang wrote: > Yes, the main issue for my coding is the allocation of memory. And I have > fixed one of the biggest memory allocation issue: 4000 by 4000 diagonal > matrix. And since I am not familiar with Rc

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Honglang Wang
Yes, the main issue for my coding is the allocation of memory. And I have fixed one of the biggest memory allocation issue: 4000 by 4000 diagonal matrix. And since I am not familiar with Rcpp and RcppArmadillo, I have no idea how to reuse the memory. I hope I can have some materials to learn this.

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Tim Triche, Jr.
I have the book on my desk as it happens, and it is a great one, but the appendix is better than I remembered, so I parceled it out: http://www.flaver.com/BoydVandenberghe_appendixC_numericalLinearAlgebra.pdf (hope this helps a few other people) I bought the text because I like being able to writ

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Kasper Daniel Hansen
On Tue, Dec 4, 2012 at 9:55 PM, Christian Gunning wrote: > What exactly do these timings show? A single call you your function? > How many calls? > > Building on Romain's point: -- a portion of your function's runtime is > in memory allocation > (and you have a lot of allocations here). > If you'

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Christian Gunning
What exactly do these timings show? A single call you your function? How many calls? Building on Romain's point: -- a portion of your function's runtime is in memory allocation (and you have a lot of allocations here). If you're calling your function thousands or millions of times, then it might

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Dirk Eddelbuettel
On 4 December 2012 at 20:39, Yan Zhou wrote: | You cannot expect C++ to magically make your code faster. If speed is of | concern, you need profiling to find the bottleneck instead of blind guessing. I Precisely. As I told Honglang just yesterday on this very list: Some general advice: when

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Romain Francois
Le 04/12/12 21:27, Honglang Wang a écrit : Since the "ker" function defined in R is very simple and vectorized, I thought it was fine to call this function in C++. And I tried to code this function inside C++, and I found it became ever slower: from 47.310s to 49.536s. Hard to say what is going

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Yan Zhou
You cannot expect C++ to magically make your code faster. If speed is of concern, you need profiling to find the bottleneck instead of blind guessing. I am not quite sure how to profile an Rcpp R program. Maybe someone else can help. But my intuition is that your program simply need that much ti

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Honglang Wang
Since the "ker" function defined in R is very simple and vectorized, I thought it was fine to call this function in C++. And I tried to code this function inside C++, and I found it became ever slower: from 47.310s to 49.536s. // [[Rcpp::depends(RcppArmadillo)]] #include using namespace Rcpp;

Re: [Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Romain Francois
You are calling back to R using Function. This is expensive. What is ker ? Can you implement it in C++. This is a wild guess, but that is I think where the bottleneck is. Romain Le 04/12/12 20:14, Honglang Wang a écrit : Dear All, I have tried out the first example by using RcppArmadillo, bu

[Rcpp-devel] How to increase the coding efficiency

2012-12-04 Thread Honglang Wang
Dear All, I have tried out the first example by using RcppArmadillo, but I am not sure whether the code is efficient or not. And I did the comparison of the computation time. 1) R code using for loop in R: 87.22s 2) R code using apply: 77.86s 3) RcppArmadillo by using for loop in C++: 53.102s 4) R