When using "inv()" in a thread-based parallel environment, can multiple threads be applied to a single instance of "inv()"?
3 次查看(过去 30 天)
显示 更早的评论
I have 48 threads and 256 GB of RAM available to invert as large an impedance matrix as possible, which will be painful. I do need the mobility matrix to then find the impedance matrix of the unforced partition by inverting only that partition. I do not want to miss an oppurtunity to relieve the pain. The documentation says that the "inv()" command is fully compatiable with a thread based environment; however, I can only take this to mean that the "inv()" command may be used on individual threads and that there is not necessarily a way to use multiple threads to apply to a single instance of the "inv()" command. If not, I will probably implment a heirarchical blocked matrix approach.
0 个评论
采纳的回答
Mike Croucher
2025-6-26
The inv function is multithreaded. You don't need to do anything to make use of multithreading here.
Here are more details:
When the documentation says that a function can be used in a thread-based environment, it means that you can run it on a thread-based worker. That is, you could do something like
parfor i=1:10
A = somefunctionToBuildA();
result{i} = inv(A)
end
and it would work on a ThreadPool. I.e. Parallel computing in MATLAB: Have you tried ThreadPools yet? » The MATLAB Blog - MATLAB & Simulink
It's about inverting many matrices in parallel rather than using multiple threads to invert one matrix.
The parallelism you are interested here is different. You want to think about built in Multithreading MATLAB Multicore - MATLAB & Simulink. Many functions are implicitly multithreaded and inv is probably one of them. There is not a definitive list of such functions but there are ways to find out.
By default, MATLAB will use a number of threads equal to the number of cores on your machine. You can check with the maxNumCompThreads function. On my machine:
>> maxNumCompThreads()
ans =
8
Let's do an experiment to see if inv is muiltthreaded.
>> a = rand(10000);
>> tic;b = inv(a);toc
Elapsed time is 10.543774 seconds.
I now tell MATLAB to use 1 thread and repeat the calculation
>> maxNumCompThreads(1);
>> tic;b = inv(a);toc
Elapsed time is 34.646851 seconds.
That's more than 3x slower than when I used 8 threads. So, I conclude that inv is making use of multithreading.
5 个评论
John D'Errico
2025-6-26
编辑:John D'Errico
2025-6-26
Yes. If you were doing many computations on many different matrices, you would probably get most benefit from assigning blocks of perhaps at most 2 cores to each sub-problem. But even there, 2 cores is only twice as fast as one core on a single matrix. Therefore, you would arguably do best to process each sub-problem on individual cores, using a parfor loop. This would seem simplest, and might give the best overall throughput.
Raymond Norris
2025-6-30
@John D'Errico, regarding "With 4 cores, now it was using between 4 and 6 cores. Again, it looks like MATLAB uses a spare extra core or so on the side itself."
My guess is that the OS isn't affinitizing the cores to a specific 4, but instead hopping those 4 around to 6 (but never more than 4 at a time?).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!