Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
C or Fortran pcg function callable from MATLAB

Subject: C or Fortran pcg function callable from MATLAB

From: evan um

Date: 10 Jul, 2009 17:10:20

Message: 1 of 3

Hi all,

I tested a built-in MATLAB preconditioned conjugate gradient function (pcg) to solve my large sparse matrix equations. The results are accurate, but I feel that it is not fast enough. Alternatively, I think that I need to create an MEX-file for an external C or Fortran cg routine.

Can I expect a meaningful speedup in this way? I believe that other people already faced such a problem with pcg and already prepared an mex-generating script for an external cg function, but I couldn't find yet any results or suggestions about it.

If I use MATLAB Parallel Tool Box, does it automatically implement MATLAB pcg function using parallel matrix-vector multiplication algorithms?

In advance, thank you for all helps.

Evan

Subject: C or Fortran pcg function callable from MATLAB

From: Rune Allnor

Date: 10 Jul, 2009 19:07:28

Message: 2 of 3

On 10 Jul, 19:10, "evan um" <eva...@gmail.com> wrote:
> Hi all,
>
> I tested a built-in MATLAB preconditioned conjugate gradient function (pcg) to solve my large sparse matrix equations. The results are accurate, but I feel that it is not fast enough.

Don't 'feel'. Measure. Leave the computer to do its
thing over night or over weekends.

> Alternatively, I think that I need to create an MEX-file for an external C or Fortran cg routine.
>
> Can I expect a meaningful speedup in this way?

The general rule is that MEX'ing speeds up programs
by a factor 10 or more, PROVIDED the matlab code is
not dominated by BLAS/LAPACK operations.

However, that speed doesn't come for free. You will
need to know all the efficient algorithms, the data
structures, all the peculiarities of both your
programming language and compiler, and also how to
battle numerical inaccuracies.

Since you asked the question in the first place, you
probably don't know enough to pull all this off, so
just stick with matlab.

Rune

Subject: C or Fortran pcg function callable from MATLAB

From: Bruno Luong

Date: 11 Jul, 2009 07:04:03

Message: 3 of 3

"evan um" <evanum@gmail.com> wrote in message <h37sls$nl5$1@fred.mathworks.com>...
> Hi all,
>
> I tested a built-in MATLAB preconditioned conjugate gradient function (pcg) to solve my large sparse matrix equations. The results are accurate, but I feel that it is not fast enough. Alternatively, I think that I need to create an MEX-file for an external C or Fortran cg routine.
>
> Can I expect a meaningful speedup in this way? I believe that other people already faced such a problem with pcg and already prepared an mex-generating script for an external cg function, but I couldn't find yet any results or suggestions about it.
>

In my experience, for the iterative method, most of the CPU time will be used performing in matrix-vector-product. It has very little importance about what language that the wrapper is written. The two efficient aspects to look for accelerate iterative algorithm is (1) find a fast maxtrix-vector-product library (2) find a good method that requires less number of iterations for the same accuracy (eg better preconditioner). For the first point, it will be hard to beat Matlab. For the second point, you just need to look for the right algorithm for your problem. I wouldn't bother to create MEX.

Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us