mldivide take too much time

Hi guys,
I am solving compressible NS using FEM , and I wrote my solver using MATLAB.
currently I have a problem with the running time ( 90x30 grid ). One time step takes about 22 seconds, 20 seconds of them are in the inverse line ! I dont know why it takes this much time !.

19 Comments

mldivide is probably the fastest you can get to solve such equations in MATLAB. Maybe try to review your algorithm and see if there is a way to reduce the dimensions of the matrices involved in this step.
Computers are not infinitely fast. People seem to think they are, but not so. Perhaps you watch too much TV, where the computers are always able to do amazing things in a fraction of a pico-second, when the user only had to click a couple of keys on the keyboard.
Since we cannot see into your computer, we cannot offer any useful advice. If you are unhappy with the speed of your code, then you could also get a faster computer, or change the overall algorithm (mldivide is, as has been said, already about as fast as it can be) or use less data (expect even worse results, since using less data is never a good idea.)
Changing the overall algorithm may mean to completely change what you are doing. That is, if mldivide is taking too much time for you, then restructure things so it needs to call mldivide less often.
Or, the answer may simply be to sit down with a good book and a cup of coffee, while the existing code finishes its work. What good book could I suggest, one that would be most appropriate here? Any good text on numerical methods in computing would be a good start, since it would help you to understand the algorithms involved.
And it took all of some 20 sec...hardly a crisis it would seem unless this is something that has to be done thousands of times.
Hi yusuf, what is the size of globalmat, and is is real double precision numbers?
If this is inside a loop, do not xlsread() within the loop if you can avoid it. If you are reading the same thing each time, read it once outside the loop and then pass it down to each routine that needs it.
" do not xlsread() within the loop..."
Good advice and with 12 calls it's likely at least some of those might be avoided but...altogether they're 8% of the total while 90% is in builtins, overhead, etc, ... Dunno if there's some way to get a feel for what those might be, more specifically, Walter?
W/ no code to look at, wonder if there's some issue like not preallocationg or the like going on.
@Yusuf--have you looked at all the lint suggestion in the code editor for things it can suggest and eliminated all those? If not, there's good chance there are some worthwhile suggestions there.
Thank you guys for your responses , I want to quote each of your comments but i dont know how to do so.
I am doing the inverse operation for at least 100 times, so I need to speed it up.
I am using xlsread 12 time as there are 12 files that I am reading and they are being read once.
my matrix is 11284 x 11284 , it is big :( .
Could you confirm that globamat is 11284 by 11284, and that globalrhs is 11284 by 1 ?
If you experiment with
globalmat = rand(11284);
globalrhs = rand(11284,1);
tic; globalmat\globalrhs; toc
then what kind of timings do you get? I get around 8 seconds on my (2013) system.
Will globalmat have any special properties such as being a band matrix? In some cases there are faster algorithms. mldivide can figure out some of the patterns, but not always, and it takes time to probe the patterns that could be avoided if you know ahead of time that the pattern is there.
" am doing the inverse operation for at least 100 times,"
The biggest change likely would be to find a way to recast the implementation to avoid the explicit inverse.
You're not going to speed up builtin \ operator as it's already code.
Walter , I am getting
Elapsed time is 17.162946 seconds.
I tried different MATLAB version, some gave me less inverse time but rest of the lines were taking much time so the total time is the same.
My system is not especially fast: it is a 3.5 GHz i7 system. Your system appears to take pretty close to twice the time for the \ operator, suggesting that you might be running at roughly 1.8 GHz.. though I guess in theory you might be up around 2.0 GHz if you have an i3 processor.
This might be time to obtain access to a faster system, perhaps use a cloud computing facility.
You did not answer my questions about the size of the matrix, and about whether it has any special patterns ?
I have Ryzen 2600 , 12 thread , I think it is fast enough.
yes my matrix is 11284 x 11284 , rhs for sure is 11284x1 .
i think it is symmetric
No walter , i didnt know that there is such thing, thank you so much ! .
Do you think this may help me ?
Apparently it can make a big difference.
If your matrix is real-valued then if it is symmetric then it is also Hermitian -- equal to its complex transpose.
In such a case if the diagonal is all positive or all negative, then you might be able to use cholesky; if the diagonal does not meet that condition then LDL is used.
thank you Walter, for helping, it decreased the time to 50% :)
Thanks for all of you

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 18 Mar 2020
To summarize: the most important issue was that the poster had a Ryzen, which is at a disadvantage when using the Intel high performance Math library MKL. Fortunately a user has posted information on how to speed it up; https://www.mathworks.com/matlabcentral/answers/396296-what-if-anything-can-be-done-to-optimize-performance-for-modern-amd-cpu-s#answer_401963 and when the poster did that, they doubled performance.
Precise Simulation
Precise Simulation on 5 May 2020
You can also try the MUMPS linear solver which is also is up to 30-50% faster than mldivide (Umfpack) in serial mode (and also supports parallel), and is available as pre-compiled Matlab Mex files with the FEATool Multiphysics toolbox

Categories

Tags

Asked:

on 14 Mar 2020

Community Treasure Hunt

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

Start Hunting!