필터 지우기
필터 지우기

Why is the built-in Cholesky function so much faster than my own implementation?

조회 수: 39 (최근 30일)
Hi,
I am currently investigating the efficiency of matrix-inversion-methods and came across the Cholesky decomposition. I then implemented the cholesky in Matlab and compared it to the built-in chol()-function.
What you can see in the graph below is a Benchmark of my implemented Cholesky decompositions and the chol()-function: - "Cholesky" is the regular Cholesky Decomposition - "Incremental Cholesky" is a method where an old Cholesky decomp of a Matrix A is used to calculate the decomposition of an incremented Matrix B with one extra row and column
Both functions are also implemented in Mex-C which improved performance a little bit.
Here is the code of my Cholesky implementation:
function L = cholMatlab(M)
n = length( M );
L = zeros( n, n );
for i=1:n
L(i, i) = sqrt(M(i, i) - L(i, :)*L(i, :)');
for j=(i + 1):n
L(j, i) = (M(j, i) - L(i,:)*L(j ,:)')/L(i, i);
end
end
end
Can you tell me why the chol()-function performs so much better?
Best regards
Edit: Should have mentioned this: The calculations are based on random full symmetric positive definite matrices.

채택된 답변

John D'Errico
John D'Errico 2015년 11월 15일
And, why are you surprised? chol will have been carefully written using tools like the blas for maximum speed, by professionals who have had many opportunities to learn every trick. As far as your MATLAB code goes, it is basic multiply looped MATLAB code, which is rarely that efficient. Even compiling it will not gain much.
  댓글 수: 2
Luke Skywalker
Luke Skywalker 2015년 11월 15일
I was indeed surprised, as at least the incremental Cholesky, where just one additional row needs to be calculated, could have potentially significantly faster.
Do you se a way to increase speed on my calculations? Is it possible to deploy the blas-tool?
Best
John D'Errico
John D'Errico 2015년 11월 15일
You simply won't come near to the built-in chol using MATLAB code. Accept that as a fact.
In order to do better, you might try writing C code directly, rather than compiling MATLAB code. Then you could insert calls to the blas. Since I cannot even spell C, I can't/won't offer advice in this respect.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Marc
Marc 2015년 11월 15일
I am just guessing and based on the help information for chol() is that it uses sparseness as you increase the size of the matrix.
Several years ago, I ran into a fsolve() problem where an in house fortran based solver wasn't flinching as I increased the size of the matrix but fsolve() after n = 100,000 equations really got bogged down. One of TMW application engineers looked at my problem, tweaked things to take advantage of sparseness and it flew within milliseconds that I could not tell the difference between ML and the in house program.
Some of these guys are ninjas with this stuff....
  댓글 수: 3
John D'Errico
John D'Errico 2015년 11월 15일
sparsity information is ONLY used when you specify your matrix as sparse when you create the matrix. MATLAB does NOT decide on the fly that your matrix can benefit because it appears sparse. You need to specify that. As well, if you convert your matrix to sparse form and it is not truly sparse (i.e., a sparse matrix should have MANY zeros) then code will often take longer to run. So, just blithely using sparse form on all of your matrices will probably result in slower code overall.
Marc
Marc 2015년 11월 15일
You guys may be right. I have no idea. I did not look into what you wrote but in the help file it says that chol() uses only the diagonal and upper triangle, assuming the lower is a complex conjugate transpose of the upper.
Does this mean it is doing "half" the computations that you are doing?
In general, I never disagree with John. His answer above wasn't showing when I typed mine in but I would always defer to him over me.

댓글을 달려면 로그인하십시오.


Greg Bishop
Greg Bishop 2017년 2월 15일
Your code doesn't seem to provide the same answer as chol(A) does.
  댓글 수: 1
John D'Errico
John D'Errico 2017년 2월 15일
This is not an answer. Please don't answer a question, just to add a comment. Use a comment for that.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by