How to use parpool for independent expressions

1 Ansicht (letzte 30 Tage)
Ole
Ole am 23 Sep. 2021
Kommentiert: Raymond Norris am 24 Sep. 2021
How to evaluate independent expressions in local parpool on laptop.
When I try the code below, the parpool is idle.
How to force matlab to evaluate the expressions in parallel.
x = 1; y =2; z=3;
parpool('local',4)
s = x + y + z;
a = 2*x + z;
w = 3*y + 2*z;
f = 3*x + 2*z;
  1 Kommentar
Raymond Norris
Raymond Norris am 24 Sep. 2021
Let me ask you this. Using a pool of workers (i.e. processes) cancels out any implicit threadedness. So will there be any value in doing as your requesting?
To see the impact, try the following:
maxNumCompThreads(1);
tic
< run code >
toc
maxNumCompThreads('auto');
tic
< run code >
toc
Next, run your code using a parallel pool of workers and parfeval (as Mohammad has suggested) and see how close it gets to your timings with maxNumCompThreads set to 'auto'. You might see it's a wash.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mohammad Sami
Mohammad Sami am 24 Sep. 2021
You can use the parfeval function to make these calculations on a worker thread.
x = 1; y =2; z=3;
p=parpool('local',4)
Fs = parfeval(p,@(x,y,z) x + y + z,1,x,y,z);
Fa = parfeval(p,@(x,z)2*x + z,1,x,z);
Fw = parfeval(p,@(y,z)3*y + 2*z,1,y,z);
Ff = parfeval(p,@(x,z)3*x + 2*z,1,x,z);
s = Fs.fetchOutputs;
a = Fa.fetchOutputs;
w = Fw.fetchOutputs;
f = Ff.fetchOutputs;
To avoid copying of data to workers from main thread if you have large datasets, you can use thread based parpool.

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by