Running four different independent scripts in parallel

17 ビュー (過去 30 日間)
Ghazwan
Ghazwan 2017 年 4 月 12 日
コメント済み: Ghazwan 2017 年 4 月 14 日
Hello, I have a four cores machine, and I need to run a matlab code on each one of them. Say, I want the four files to run on each core in parallel not sequentially.The files do not interact with each other and they are all in the same folder.
Could anyone please tell me how to do that?
Say my files are: Pump1.m , Pump2.m, Pump3.m, Pump4.m
Thank you so much
  2 件のコメント
Sebastian Castro
Sebastian Castro 2017 年 4 月 12 日
To answer whether this is possible, it would help to know what those separate files do.
Are they scripts or functions? Do those functions accept inputs and/or return outputs? Do the scripts create workspace variables, MAT-files, other files?
The fact that the files do not interact is a good start as far as parallel computing feasibility.
Ghazwan
Ghazwan 2017 年 4 月 13 日
They are scripts. They return outputs. And yes, they create variables in the workspace. Thank you

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2017 年 4 月 13 日
編集済み: Walter Roberson 2017 年 4 月 13 日
parpool(4)
parfor K = 1 : 4
if K == 1; Pump1; end
if K == 2; Pump2; end
if K == 3; Pump3; end
if K == 4; Pump4; end
end
You can also use spmd and test labindex()
It is also possible to generalize to a series of functions whose handles are given. However if you do that then you need to be sure to add those files to the parallel pool; otherwise the workers will not be able to find them through the handle. The code I show above is not pretty but it has the advantage that parfor can see the files clearly and so would know to make them available.
Note: if the routines do arithmetic computation on reasonable sized matrices, you might find that the overall result is slower than if you had executed them in sequence. By default each parallel worker only gets access to one core, but MATLAB tries to run "sufficiently large" array calculations in parallel, and that parallel operation can end up being more efficient than doing several unrelated things at the same time.
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 4 月 13 日
Above you wrote,
"They are scripts. They return outputs. And yes, they create variables in the workspace."
That is a problem. Scripts that are run within a parfor can, at most, assign to variables that were plainly assigned to earlier in the parfor. You should turn the scripts into functions that return appropriate values (if you care about the values outside of the parfor)
Ghazwan
Ghazwan 2017 年 4 月 14 日
It works now. Thank you so much for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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