|
"Geoffroy " <grsabourin@gmail.com> writes:
> I'm looking for a way to start two for loops in parallel. This is the
> situation
>
> first loop for i=1:length(A) ............ end
>
> second loop for i=1:length(B) ............ end
>
> The interior operations of each loop are not independent. It is not
> possible to execute each loop in parallel. On the other hand each loop
> are totally independent. How I can start execution of one loop on a
> worker and the other loop on an other worker.
You could approach things in one of two ways. Firstly, you could use an
SPMD block, something like this:
matlabpool local 2
spmd
if labindex == 1
result = firstForLoop();
elseif labindex == 2
result = secondForLoop();
end
end
result{1} % result of firstForLoop()
result{2} % result of secondForLoop();
This runs a sychronous SPMD block on an open MATLABPOOL. You could do
things asynchronously more like this using the BATCH function to submit
jobs:
matlabpool close % if it was open
j1 = batch( @firstForLoop, 1, {} ); % 1 output, no inputs
j2 = batch( @secondForLoop, 1, {} );
wait(j1); result1 = j1.Tasks.OutputArguments
wait(j2); result2 = j2.Tasks.OutputArguments
It really depends on how long your FOR loops take.
Cheers,
Edric.
|