Clear Filters
Clear Filters

How can i run 2 functions in parallel with MATLAB parallel Computing Toolbox? looking for an easy example code!

57 views (last 30 days)
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!

Accepted Answer

Edric Ellis
Edric Ellis on 30 Mar 2015
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 Comment
Shubham Jha
Shubham Jha on 23 Jun 2017
Edited: Shubham Jha on 23 Jun 2017
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

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing Fundamentals 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!