Running multiple scripts on separate cores in parallel

I have four scripts - all running a MILP problem using intlinprog. I want to run the four scripts simulataneously to produce their results in the workspace. I cannot run it as parfor problems as there exisits a 'break' in the intlinprog loop.
I have tried :
parpool(4)
parfor K = 1 : 4
if K == 1; test1; end
if K == 2; test2; end
if K == 3; test3; end
if K == 4; test4; end
end
However, it informs me:
Error using test2
Transparency violation error.
See Workspace Transparency in MATLAB Statements.
Error in runCodeScript (line 2)
parfor K = 1 : 2

2 件のコメント

Jan
Jan 2022 年 8 月 11 日
編集済み: Jan 2022 年 8 月 11 日
Scripts share the worksape with the caller.
Convert the scripts to functions instead.
Koren Murphy
Koren Murphy 2022 年 8 月 11 日
Quick fix thank you!

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

 採用された回答

Matt J
Matt J 2022 年 8 月 11 日

0 投票

fcn=@(i) run("test"+i);
for K=1:n
F(K)=parfeval(fcn,0,K);
end;
wait(F)

5 件のコメント

Koren Murphy
Koren Murphy 2022 年 8 月 11 日
How does the first line work? A loop?
Walter Roberson
Walter Roberson 2022 年 8 月 11 日
The first line is defining an anonymous function that takes one parameter. It does not do any looping itself.
The for K line starts the looping, and inside the loop asks parfeval to start executing fcn(K) on a worker.
Matt J
Matt J 2022 年 8 月 11 日
The first line is just an anonymous function that runs the i-th script.
Koren Murphy
Koren Murphy 2022 年 8 月 11 日
Thank you Walter - so I would need to create a vector of the fucntion names to implement as fcn so all 4 run simulanteously on different workers?
Walter Roberson
Walter Roberson 2022 年 8 月 11 日
No,
parfeval(fcn,0,K)
means to ask a worker to execute function fcn on a worker, and on the worker it will be passed the parameter K . Looking at the function source:
fcn=@(i) run("test"+i);
that would take the numeric value passed in, convert it to character vector, append it to test and ask to run() the result. So when parfeval(fcn,0,3) was executed, run("test3") would be executed, thus asking the script test3 to execute.
You would only need a vector (cell array) of function handles if the names of the scripts to execute were not predictable.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeParallel Computing Fundamentals についてさらに検索

製品

リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by