How can i run 2 functions in parallel with MATLAB parallel Computing Toolbox? looking for an easy example code!
조회 수: 26 (최근 30일)
이전 댓글 표시
Hey people,
i have already implemented several smaller issues with Matlab. Now i have to face a real challenge i cannot solve so far. Its about parallel computing a thing i have no experience with yet. I'd be glad if somebody with some experience about parallel computing could give me some kind of example code. i am sure i can make it with some support but i havent found anything like that on the internet yet. i just would like to run 2 functions in parallel, more exact my goal is to realize the following pseudocode:
while (termination criterion is not met)
Activate Function_I
Activate Function_II
while(Function_I or Function_II are active)
if (Function_I or Function_II produce a new best solution)
kill Function_I and Function_II
end if
end while
end(while)
plz give me some hints how to realize it! id be very glad. i am working with MATLAB R2010b
Thanks a lot!
댓글 수: 0
채택된 답변
Edric Ellis
2015년 3월 30일
In R2010b, your options are to use either parfor or spmd. (In R2013b and later, there's parfeval which gives more flexibility, but is a little more difficult to use). With either parfor or spmd, you will need to arrange so that your two functions can make a partial attempt at a solution, and then continue - this is because you need to check whether either has completed yet. This will necessarily make things less efficient. You might do something like this:
parpool(2);
spmd
done = false;
state = []; % state used by Function_I or Function_II
while ~done
% Run Function_I/Function_II for a while
if labindex == 1
[state, gotSolution] = Function_I(state);
elseif labindex == 2
[state, gotSolution] = Function_II(state);
end
% Check to see if either has completed using GOP which
% combines the results of 'gotSolution' from each lab
done = gop(@any, gotSolution);
end
end
% access solution in 'state'
In R2013b and later, you could simply use parfeval to invoke Function_I / Function_II and then use fetchNext to work out when one of them has completed, a bit like this:
parpool(2);
f1 = parfeval(@Function_I, 1);
f2 = parfeval(@Function_II, 1);
% fetchNext waits for one of the functions to complete,
% and also gets the result
[idx, result] = fetchNext([f1, f2]);
% We're done, so we can cancel f1 and f2 (one will actually already be complete)
cancel([f1, f2]);
댓글 수: 1
Shubham Jha
2017년 6월 23일
편집: Shubham Jha
2017년 6월 23일
Can u justify what is 'state' here? As because I am facing problem in calling my Function_I & Function_II. There are arguments needed to be passed with these functions. So 8th and 10th lines of your first solution show syntactical errors.
I am doing like this:- Function_I: subalgo1(A,C,n) Function_II: subalgo2(A,C,n)
parpool(2);
spmd
done = false;
state = []; % state used by Function_I or Function_II
while ~done
% Run Function_I/Function_II for a while
if labindex == 1
[state, gotSolution] = subalgo1(A,C,n)(state);
elseif labindex == 2
[state, gotSolution] = subalgo2(A,C,n)(state);
end
% Check to see if either has completed using GOP which
% combines the results of 'gotSolution' from each lab
done = gop(@any, gotSolution);
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Parallel Computing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!