Parallel Code
Show older comments
Hi
Can I run this code in parallel?
A1=F(A1,B1)
A2=F(A2,B2)
F is a function, A1 B1 are unrelated to A2 B2
How can I do this ? ( now it runs one after the other)
Thanks
Shani
Answers (1)
Edric Ellis
on 17 Apr 2012
If you have Parallel Computing Toolbox, you could do this:
% first start up 2 local workers
matlabpool open local 2
% Make cell arrays of inputs
Ain = {A1, A2}; Bin = {B1, B2};
parfor ii=1:2
% Return outputs as a cell array
Aout{ii} = F(Ain{ii}, Bin{ii});
end
5 Comments
hamid
on 14 Sep 2014
thanks edric
i write your above code and suppose function F do this compute
Aout(ii)=Ain(ii)*rand+Bin(ii)*rand
but in every run i get same answer.why when i use two rand always i get same output? even when i use spmd both in every run i get same answers.
please say why this occur.
blessing
Edric Ellis
on 15 Sep 2014
Just like MATLAB itself, parallel workers start up in a deterministic random state as described in the documentation. That page also tells you how you can control this state.
excuse me i can not understand mr.edric. i ask my question again with a example. i write function f that return a random number and a script with the name of spmdtest. in spmdtest i want run function f in parallel.
my question is that why in every run i see same output? at first run output was lab1: c=.9713 lab2 c=.2951 and in second run again output was lab1: c=.9713 lab2 c=.2951 and in every run output is this. while i think,in every run i should get different output.
please explain what is this problem. and how i can solve it. and this is need that to say i visit above link that you put, but i cannt understand how i can slove my problem and in my matlab verson 2010 there is no function with name parpool. thanks
function [ c ] = F( )
c=rand
end
matlabpool open
spmd(2)
F();
end
matlabpool close
Edric Ellis
on 15 Sep 2014
You should not open and close the pool each time - you should open the pool once and leave it open. Each time the pool opens, you are creating new workers with a fresh random number generator state.
hamid
on 16 Sep 2014
oh yes. thank you. now i can do my work
god bless you
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!