How to use parpool for different expressions ?

3 ビュー (過去 30 日間)
Ole
Ole 2020 年 10 月 9 日
回答済み: Raymond Norris 2020 年 10 月 12 日
This is my first time trying to use parallel computing.
Would this code pass Dx1 Dx2 Dx3 Dx4 to different cores and then continue with Dx5 to speed computation ?
And can 'subs' in Dx5 be used by more than one core?
I have 4 cores on my computer.
x1 = linspace(-1,1,10000);
S = @(x) exp(sin(x));
syms x
D1 = diff(S(x), x,1);
D2 = diff(D1, x,1);
D3 = diff(D2, x,1);
D4 = diff(D3, x,1);
D5 = diff(D4, x,1);
parpool
Dx1 = double(subs(vpa(D1), x, x1));
Dx2 = double(subs(vpa(D2), x, x1));
Dx3 = double(subs(vpa(D3), x, x1));
Dx4 = double(subs(vpa(D4), x, x1));
Dx5 = double(subs(vpa(D5), x, x1));

回答 (1 件)

Raymond Norris
Raymond Norris 2020 年 10 月 12 日
Hi Ole,
The parpool command will start a pool of workers (in this case most likely on your 4-core machine). Conceptually, think of this as now 5 MATLAB processes running, with one of them being your MATLAB Desktop and the other 4 as headless compute engines. The true parallelization then comes from leveraging the MATLAB parallel language constructs, such as parfor and spmd.
In pratice, it's possible that anyone of the functions you've called (double, subs, and vpa) could call a parallel constructor and therefore be dispearsed onto other cores, but in this case, it's unlikely. Instead, you'll want to explicitly call them yourself. For instance, I might rewrite your example as such:
x1 = linspace(-1,1,10000);
S = @(x) exp(sin(x));
syms x
D1 = diff(S(x), x,1);
D2 = diff(D1, x,1);
D3 = diff(D2, x,1);
D4 = diff(D3, x,1);
D5 = diff(D4, x,1);
DN = {D1; D2; D3; D4; D5};
D = nan(5,10000);
parfor didx = 1:5
D(didx,:) = double(subs(vpa(DN{didx}), x, x1));
end
Notice I left off the call to parpool? In fact, parfor will start a parpool if one hasn't already started. What the code above now does is assign each call to double (and thefore subs and vpa) to its own core.
Thanks,
Raymond

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by