Thread Subject: Parallel execution

Subject: Parallel execution

From: Geoffroy

Date: 2 Feb, 2012 16:09:12

Message: 1 of 3

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.

Thank you

Subject: Parallel execution

From: Edric M Ellis

Date: 3 Feb, 2012 07:42:11

Message: 2 of 3

"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.

Subject: Parallel execution

From: Geoffroy

Date: 3 Feb, 2012 15:29:11

Message: 3 of 3

Thank you for your answer. My loops are mostly same time.

Edric M Ellis <eellis@mathworks.com> wrote in message <ytwy5skz7l8.fsf@uk-eellis0l.dhcp.mathworks.com>...
> "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.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
parallel proces... Geoffroy 2 Feb, 2012 11:14:11
workers Geoffroy 2 Feb, 2012 11:14:11
rssFeed for this Thread

Contact us at files@mathworks.com