series computing is faster than parallel computing using parfor
8 views (last 30 days)
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.
Matt J on 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.
John D'Errico on 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 on 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.