series computing is faster than parallel computing using parfor

20 Ansichten (letzte 30 Tage)
KAVI PRADHAP
KAVI PRADHAP am 10 Sep. 2023
Kommentiert: Sam Marshalik am 11 Sep. 2023
Hi,
I am converting a code to from normal for loop to parallel for "parfor". while running the code in series it runs faster than the parallization. I have refered some documentation from matlab converting for to parfor. But not sure because of what it is taking much time in parallel processing, it not possible debug tool inside parfor also. How to troubleshoot this.
  4 Kommentare
Stephen23
Stephen23 am 10 Sep. 2023
"how to identify what is the problem here?"
So far you have not explained what the problem is.
Sam Marshalik
Sam Marshalik am 11 Sep. 2023
That's a great resource, Stephen, thank you for including that.
When thinking about whether I should use parallel, I think of the following 2 considerations:
Overhead cost of using parallel: As several people have pointed out, using parallelization involves overhead cost. This overhead cost is the cost of starting parallel workers (you are essentially launching additional MATLAB enginers to offload work to), sending data to the parallel workers, collecting data from those workers, and more. If the computation itself is small, then the overhead cost will neglect any kind of speed up you may get from parallel.
Multi-threading: Your code may already be using implicit parallelization called multi-threading. Some functions in MATLAB automatically do implicit parallelization behind the scenes without you invoking it or potentially even knowing about it. Parallel workers by default have access to a single thread, so if your code makes heavy use of multi-threading, you will see a slow down. An easy way to tell if you are using multi-threading is to run your code in serial (for-loop) and monitor your CPU usage. If at some point in time, all of your CPU cores light up, then that means you are using multi-threaded functions. If that is the case, you can continue running in serial or consider playing with NumThreads (parallel profile property you can change) and giving your parallel workers access to more threads.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Matt J
Matt J am 10 Sep. 2023
Assuming you are on a local parpool, it is never clear whether parfor will be faster than a serial loop. You are dividing the loop iterations over more workers than in the serial case, but on the other hand, each operation within the loop now has less cores to multithread with.
  2 Kommentare
KAVI PRADHAP
KAVI PRADHAP am 10 Sep. 2023
Is there a way or proceduce to troubleshoot the code to improve it.?
Matt J
Matt J am 10 Sep. 2023
It's all very situation specific. We would need to see the code, or a representiative, simplified example of it.

Melden Sie sich an, um zu kommentieren.


John D'Errico
John D'Errico am 10 Sep. 2023
There is no assurance that a parallel code will be faster than any specific serial code. ABSOLUTELY NONE. In fact, I can easily give you an example where at best, the two might be the same. This is because MATLAB already makes good use of all of your cores on some computations.
That is, suppose you wanted to multiply two matrices, but essentially do it many times. MATLAB will already parallelize this process, as long as the matrices are large enough. So it is already using all of your cores. But if you force MATLAB to use only one core per multiply, then you make each multiply slower than it would have been otherwise.
Such processes may give little or no gain by making them parallel. I would look to see if your computation in the serial form is already using multiply cores most of the time. If it is, then going parallel may not gain you much at all.
At the same time, if you are not that expert at MATLAB, the odds are huge that you can see a significant gain, simply by writing better, more efficient code. The parallel toolbox is sometimes the lazy way to writing faster code, a crutch you can use to avoid becoming a better coder. That may not be true here for you, but we cannot know. What I do know is you seem not sure how to even debug it, or know where the time is going, so that suggests you need better coding skills in MATLAB.
My advice is first, spend some serious time on improving the basic code.

Walter Roberson
Walter Roberson am 10 Sep. 2023
Suppose you have a bunch of envelopes to address and a list of places to address them to. Now consider:
Scenario A (serial)
  • you repeat: pull in a new envelope and copy the next item in the address list to the new envelope
Scenario B (parallel)
  • you repeat: next worker, copy down this next address. When I say "go", pull in an envelope and copy that address to the envelope, and let me know when you are done
Which Scenario is going to get finished sooner?
Answer: if the time to "pull in an envelope" or to write an address on an envelope are large enough compared to the time that it takes you to tell the workers which address to deal with, then you might come out ahead with the parallel version due to the fact that numerous workers can be doing those time-consuming things simultaneously. But if the times to do those things are inexpensive enough then the serial version would be faster because it takes time to tell each worker what to do and to get results back from each worker.
parfor can definitely be slower than serial -- it is a fairly common situation actually, especially if the matrices to be copied for each iteration are "large" compared to the amount of work to be done on them.

Kategorien

Mehr zu Parallel Computing Fundamentals finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by