How to run two scripts rapidly on different workers/cores

3 views (last 30 days)
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?

Answers (1)

Prateekshya
Prateekshya on 11 Oct 2024
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!

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!