When I have the parallel computing toolbox installed and use parfor
in my code, MATLAB starts the pool automatically once it reaches the parfor
loop. This however makes it difficult to debug at times, which is why I would like to prevent MATLAB from opening a pool in certain situations. So, how can I tell MATLAB not to open a pool? Obviously I could go through my code and remove all parfor
loops and replace them with normal for
loops, but this is tedious and I might forget to undo my changes.
edit: To specify, I ideally would like the parfor loop to behave exactly like a for when setting a control or variable or something. That is, I should for example also be able to place breakpoints in the for-loop.
delete( poolobj ) shuts down the parallel pool associated with the object poolobj , and destroys the communicating job that comprises the pool. Subsequent parallel language features will automatically start a new parallel pool, unless your parallel preferences disable this behavior.
When you run a function with parallel enabled, MATLAB® automatically opens a parallel pool of workers. MATLAB runs the computation across the available workers.
What Is a Parallel Pool? A parallel pool is a set of MATLAB® workers on a compute cluster or desktop. By default, a parallel pool starts automatically when needed by parallel language features such as parfor . You can specify the default pool size and cluster in your parallel preferences.
Depending on your preferences, MATLAB can start a parallel pool automatically. To enable this feature, select Parallel > Parallel Preferences in the Environment group on the Home tab, and then select Automatically create a parallel pool. Set your solver to use parallel processing.
Under Home->parallel->parallel preferences you can deselect the check box "Automatically create a parallel pool (if one doesn't already exist) when parallel keywords are executed." This makes all the parfor
loops behave as a normal for
loop.
I'll get back to you if I figure out a way to do this in the code as opposed to using the check box.
Update turns out it is indeed possible to change the settings through code, although I would not recommend this, as it involves changing MATLAB's preference file. This is taken from the Undocumented MATLAB blog by Yair Altman.
ps = parallel.Settings;
ps.Pool
ans =
PoolSettings with properties:
AutoCreate: 1
RestartOnClusterChange: 1
RestartOnPreferredNumWorkersChange: 1
IdleTimeout: 30
PreferredNumWorkers: 12
where you need to change the AutoCreate
switch to 0
.
As alternative I'd suggest wrapping everything inside your parfor
in a function, thus calling
parfor 1:N
output = function(..)
end
Now modify your script/function to have a Parallel
switch on top:
if Parallel
parfor 1:N
output = function(..)
end
else
for 1:N
output = function(..)
end
end
You can edit and debug the function
itself and set your switch on top of your program to execute in parallel or serial.
As well as the normal syntax
parfor i = 1:10
you can also use
parfor (i = 1:10, N)
where N
is the maximum number of workers to be used in the loop. N
can be a variable set by other parts of the code, so you can effectively turn on and off parallelism by setting the variable N
to 1 or 0.
Edit: to be clear, this only controls the number of workers on which the code is executed (and if N is zero, whether a pool is started at all). If no pool exists, the code will execute on the client. Nevertheless, the code remains a parfor
loop, which does not have the same semantics as a for
loop - there are restrictions on the loop code for parfor
loops that do not exist for for
loops, and there is no guarantee on the order in which the loop iterations are executed.
When you use parfor
, you're doing more than just saying "speed this up please". You're saying to MATLAB "I can guarantee to you that the iterations of this loop are independent, and can be executed in any order, so you will be OK if you try to parallelize it". Because you've guaranteed that, MATLAB is able to speed things up by using different semantics than it would do for a for
loop.
The only way to completely get for
loop behaviour is to use for
, and if you need to switch back and forth for debugging purposes you'll need to comment and uncomment the for
/parfor
(or perhaps use an if
/else
block, switching between a for
and a parfor
depending on some variable).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With