How to run two scripts rapidly on different workers/cores
4 次查看(过去 30 天)
显示 更早的评论
I have a piece of code which is structured in the following way and updates a
BIGVAR = 0;
for i = 1:nt
% Updates BIGVAR with dB_1
script_1.m
% Updates BIGVAR with dB_2
script_2.m
BIGVAR = BIGVAR + dB_1 + dB_2;
end
While it would be nice to execute these scripts as functions, for transparency I would prefer not to call them in such a way. The number of arguments is large and the outputs as well. While this might not be best practice it keeps things simple. The two scripts do not interact.
both script_1 and script_2 take roughly .5 s to execute. nt is equal to a large number, and so it may take a day to finish execution. What I want to do is instead have code like
for i = 1:nt
% Updates BIGVAR with dB_1
send_to_worker_1 script_1.m
% Updates BIGVAR with dB_2
send_to_worker_2 script_2.m
BIGVAR = BIGVAR + dB_1 + dB_2;
end
Using a parfor loop
parfor i = 1:2
run(script{i})
end
gives me issues with UndefinedFunction errors where some variables are not accessible.
Using j = batch('script_1'); wait(j); load(j) does what I want, but the execution time for this is an order of magnitude larger than the run time of the script itself. Is there an effective way of simply sending the scripts where I want them to execute?
0 个评论
回答(1 个)
Prateekshya
2024-10-11
Hello Chris,
You can use parfeval to execute functions asynchronously on parallel workers. This allows you to run your scripts in parallel without the overhead of batch. For more information on parfeval please follow this link:
I hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!