how to run two matlab scripts in parallel?
조회 수: 149 (최근 30일)
이전 댓글 표시
I need to run two scripts in parallel and simultaneously, run the first script and the second script at the same time.
댓글 수: 0
채택된 답변
KSSV
2017년 1월 2일
You can use parfor to run two functions simultaneously. Eg:
funcs1 = {@fun1, @fun2} ; % let fun1, fun2 be two functions
arguments = {inp1,inp2 ;inp1,inp2} ; % write the inputs of each function
solutions = cell(1,2); % initialize the solution
% use of parfor
parfor ii = 1:2
solutions1{ii}=funcs1{ii}(arguments1{ii,:});
end
M = solutions1{1} ; N = solutions1{2} ; % assign the results
댓글 수: 0
추가 답변 (4개)
José-Luis
2017년 1월 2일
편집: José-Luis
2017년 1월 2일
Another alternative is to start two sessions of Matlab and run the different scripts there.
댓글 수: 4
Hamza Ashraf
2020년 9월 26일
Can you explain it with code i dont know how to use spmd with labsend and lab receive
Austin Bond
2018년 4월 17일
should the 'arguments' and 'solutions' variables really be 'arguments1' and 'solutions1'?
Sean de Wolski
2018년 4월 17일
Just call batch directly or right-click on the script in the current folder browser and select "Run as batch job"
댓글 수: 0
MathWorks Support Team
2022년 9월 2일
You can use Parallel Computing Toolbox to run two MATLAB functions simultaneously. (If you have scripts, then first wrap them up as functions as described here in the doc Create Functions in Files). The parfeval function allows you to run two functions simultaneously on a parallel pool. Each call to parfeval causes the specified function to be invoked on a worker with the provided inputs. You also need to specify up front how many outputs you want. Your code might end up like this:
numOut = 1; % Both functions here have only a single output
f1 = parfeval(@myFirstFunction, numOut, in1, in2); % request myFirstFunction(in1,in2)
f2 = parfeval(@mySecondFunction, numOut, in3); % request mySecondFunction(in3)
out1 = fetchOutputs(f1); % this waits for myFirstFunction to complete and then gets the result
out2 = fetchOutputs(f2);
If you need to process the results as soon as either function completes, you could use fetchNext instead of fetchOutputs.
댓글 수: 1
Walter Roberson
2022년 9월 2일
These days, you can also use parfeval() and background threads without needing the Parallel Computing Toolbox; see backgroundPool
참고 항목
카테고리
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!