You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
mldivide take too much time
7 views (last 30 days)
Show older comments
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
Ameer Hamza
on 14 Mar 2020
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.
John D'Errico
on 14 Mar 2020
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.
David Goodmanson
on 14 Mar 2020
Hi yusuf, what is the size of globalmat, and is is real double precision numbers?
Walter Roberson
on 14 Mar 2020
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.
dpb
on 14 Mar 2020
" 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.
yusuf albadry
on 14 Mar 2020
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 :( .
Walter Roberson
on 14 Mar 2020
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.
dpb
on 14 Mar 2020
" 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.
yusuf albadry
on 14 Mar 2020
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.
Walter Roberson
on 14 Mar 2020
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.
Walter Roberson
on 14 Mar 2020
You did not answer my questions about the size of the matrix, and about whether it has any special patterns ?
yusuf albadry
on 14 Mar 2020
I have Ryzen 2600 , 12 thread , I think it is fast enough.
yusuf albadry
on 14 Mar 2020
yes my matrix is 11284 x 11284 , rhs for sure is 11284x1 .
i think it is symmetric
Walter Roberson
on 15 Mar 2020
Did you happen to follow the guidelines to speed up ryzen ?
yusuf albadry
on 15 Mar 2020
No walter , i didnt know that there is such thing, thank you so much ! .
Do you think this may help me ?
Walter Roberson
on 15 Mar 2020
Apparently it can make a big difference.
Walter Roberson
on 15 Mar 2020
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.
yusuf albadry
on 15 Mar 2020
thank you Walter, for helping, it decreased the time to 50% :)
Thanks for all of you
Answers (2)
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
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
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)