Controlling new parallel feature "Automatically create a parallel pool when parallel keywords are executed"

16 次查看(过去 30 天)
Hi all,
is there a possibility to control this feature (e.g. temporarily deactivate or read the settings value) from within a script?
Background is that I have a script that contains a parfor loop. For this parfor loop to work on workers there needs to be done some initializing though before entering the loop. This initialization is omitted when the matlabpool is automatically created when reaching the parfor loop.
This means I either need to start the matlabpool before reaching the loop so I can do the initialization (which I don't want to do by default) or (as an ugly workaround) I could add a dummy parfor loop to trigger the automatic startup of the matlabpool due to the new setting early.
Is there a proper way to control the preference (without user interaction)?
Regards Eduard

采纳的回答

Edric Ellis
Edric Ellis 2014-1-15
Unfortunately, there's no API to modify the parallel preferences. I think you have essentially two options here, and these are:
  1. Turn off auto-opening pools in the parallel preferences UI. Then you have essentially the same situation as existed in R2013a.
  2. Use the optional "number of workers" argument in your PARFOR loop - see the documentation for more.
As an example of the second approach, you could do something like this
if wantToGoParallel
M = Inf;
else
M = 0;
end
parfor (idx = 1:N, M)
doStuff(...);
end
  2 个评论
Friedrich
Friedrich 2014-1-15
Actually one could change one entry in the parallel.settings file in the prefdir. There is one sections in that file which reads:
<settings name="pool">
<key name="AutoCreate">
<bool>
<value>1</value>
</bool>
</key>
Change the value 1 to 0 to disable it. So a bit fopen, fseek, fwrite does the job.
Eduard
Eduard 2014-1-15
Thanks Edric, option two sounds like a reasonable solution. I'll consider this.
Option one is not possible since the script should run out of the box and I can't make sure everyone who uses it always remembers to turn of that option.
@Friedrich: Even if that would work, it sounds very hacky to me. Actually I doubt this file is read interactively when starting the parallel pool; probably only at MATLAB startup? Anyway I'll definitely not start messing around with settings files inside scripts.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by