Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How do parfor, matlabpool and spmd, and vectorization work?

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Binlin Wu

Date: 21 Apr, 2011 19:07:21

Message: 1 of 10

I'm using matlab2010b, with a dual core computer.

1. When parfor is used, without using matlabpool open, it runs in one lab. Right? Is is using one CPU core? If so, how does the parallel computing work for parfor? Is it done by using the time-division idea of the traditional multithreading programming. Or multiple cores can be used without creating labs with matlabpool?

2. In the following test, parfor itself doesn't speed up. But I want to check something else. I put parfor in matlabpool.

matlabpool open 2
parfor i=1:length(funList)
% call the function
        labBarrier
funList{i}(dataList{i});
end

function y = fun1(data1)
fprintf('labindex = %d\n',labindex)
time1=clock;
fprintf('%02d : %02d : %02f\n',time1(4),time1(5),time1(6))
disp('run f1');
pause(5)

function y = fun2(data2)
fprintf('labindex = %d\n',labindex)
time2=clock;
fprintf('%02d : %02d : %02f\n',time2(4),time2(5),time2(6))
disp('run f2')
pause(5)

The result is:

labindex = 1
14 : 19 : 24.975000
run f1
labindex = 1
14 : 19 : 24.991000
run f2
f1,f2 done

Here parallel computing helps to speed up. It shows labindex of two functions 1. So the other lab is free? One lab must correspond to one cpu core, right? If it's just using one lab/cpu core, then how matlabpool makes it faster? Actually, I noticed both cpu cores are busy, with each 50% cpu. Am I right that both functions are using both labs and both cores, by breaking the work into pieces? Then why labindex only shows labindex = 1?


3. Parfor in Matlab2006b is different from the higher version. Right? I'm wondering how parallelism is achieved for parfor in matlab R2006b, which is only single threaded and can't utilize the multi-core CPU.


4. When

spmd
    labBarrier
    fun1(data1);
end

The result shows the function is run twice in two labs, once in each lab. The time it takes is about the same as running one function without spmd. Is this a repetition work? Does this mean that I can only benefit if I manually split the work into two labs,

such as

spmd
        labBarrier
funList{labindex}(dataList{labindex})
end

?

5. Is vectorization parallel computing? It seems vectorization is just a way to use memory more efficiently to speed up. It seems using matrix is still faster than using loop with pre-allocation. Why is that?

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Steven_Lord

Date: 22 Apr, 2011 14:08:33

Message: 2 of 10



"Binlin Wu" <cunyalen@gmail.com> wrote in message
news:iopv99$mln$1@fred.mathworks.com...
> I'm using matlab2010b, with a dual core computer.
>
> 1. When parfor is used, without using matlabpool open, it runs in one lab.
> Right?

No. As described in the documentation:

http://www.mathworks.com/help/toolbox/distcomp/brb2x2l-1.html

if you run a PARFOR loop without a MATLABPOOL open, the loop runs on the
client. Without the MATLABPOOL there are no labs.

> Is is using one CPU core?

Maybe, maybe not. It depends on what you're doing in the loop. Some
functions in MATLAB are multithreaded, some are not. But don't worry about
that -- the question "Are all my cores being used?" is the wrong question
IMO. The right question is "Is my code fast enough for my purpose?"

> If so, how does the parallel computing work for parfor?

Read the Introduction section of the page I linked above for a high-level
description.

> Is it done by using the time-division idea of the traditional
> multithreading programming. Or multiple cores can be used without creating
> labs with matlabpool?

To your latter question, the answer is "It depends on what you're doing."

> 2. In the following test, parfor itself doesn't speed up. But I want to
> check something else. I put parfor in matlabpool.
>
> matlabpool open 2 parfor i=1:length(funList)
> % call the function
> labBarrier
> funList{i}(dataList{i}); end
>
> function y = fun1(data1)
> fprintf('labindex = %d\n',labindex)
> time1=clock;
> fprintf('%02d : %02d : %02f\n',time1(4),time1(5),time1(6))
> disp('run f1');
> pause(5)
>
> function y = fun2(data2)
> fprintf('labindex = %d\n',labindex)
> time2=clock;
> fprintf('%02d : %02d : %02f\n',time2(4),time2(5),time2(6))
> disp('run f2')
> pause(5)
>
> The result is:
>
> labindex = 1
> 14 : 19 : 24.975000
> run f1
> labindex = 1
> 14 : 19 : 24.991000
> run f2
> f1,f2 done
>
> Here parallel computing helps to speed up. It shows labindex of two
> functions 1. So the other lab is free?

You're running two iterations, and MATLAB chose (for whatever reason) not to
split those tasks among different labs, probably because it doesn't make
sense to do so.

> One lab must correspond to one cpu core, right?

A lab is an instance of MATLAB. That's all.

> If it's just using one lab/cpu core, then how matlabpool makes it faster?
> Actually, I noticed both cpu cores are busy, with each 50% cpu. Am I right
> that both functions are using both labs and both cores, by breaking the
> work into pieces? Then why labindex only shows labindex = 1?

Stop focusing on the cores!

IF you're solving a problem where each loop iteration is independent and IF
you're solving a problem where each loop iteration takes a "significant"
amount of work (and yes, I'm leaving "significant" undefined here) and IF
you have a large number of loop iterations, then you should try to use
PARFOR. If your loop iterations are not independent, or if you have a loop
body that doesn't require a lot of computational effort, or if you have a
small number of loop iterations, then PARFOR may not be for you. The
independent loop iterations is a requirement for PARFOR; after all if
iteration 2 requires the results from iteration 1 you CAN'T perform them in
parallel. Even if you _could_ perform them in parallel, sometimes the
overhead of assigning out the work to the labs would outweigh any benefit
you gained by running the loop bodies in parallel if the loop bodies are
computationally inexpensive or there are only a few of them.

> 3. Parfor in Matlab2006b is different from the higher version. Right?

That's correct; PARFOR changed in release R2007b.

http://www.mathworks.com/help/toolbox/distcomp/rn/bq7xin2-1.html#bq7xipz

> I'm wondering how parallelism is achieved for parfor in matlab R2006b,
> which is only single threaded and can't utilize the multi-core CPU.

I don't really want to get into those implementation details, in part
because this isn't my area and I'm not entirely certain.

> 4. When
> spmd
> labBarrier
> fun1(data1);
> end
>
> The result shows the function is run twice in two labs, once in each lab.
> The time it takes is about the same as running one function without spmd.
> Is this a repetition work? Does this mean that I can only benefit if I
> manually split the work into two labs,

SPMD stands for "single program multiple data" -- your function does in fact
run once on each lab. See the "When to use SPMD" section of this
documentation page:

http://www.mathworks.com/help/toolbox/distcomp/brukbno-2.html

BTW, why are you using labBarrier in your SPMD code?

> such as
>
> spmd labBarrier
> funList{labindex}(dataList{labindex})
> end
>
> ?
>
> 5. Is vectorization parallel computing?

I would say the answer is no, not the way those terms are usually defined in
the context of MATLAB.

> It seems vectorization is just a way to use memory more efficiently to
> speed up. It seems using matrix is still faster than using loop with
> pre-allocation. Why is that?

When you call a vectorized function on a vector instead of calling that same
function for each individual element, you're making 1 function call instead
of numel(theVector) function calls. Calling a function does have some
overhead associated with it, so reducing the number of calls may (depending
on what the function is doing) save you some time.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Binlin Wu

Date: 27 Apr, 2011 20:09:20

Message: 3 of 10

Hi Steven,

Thank you very much for your detailed explanation, comments, references etc. I really appreciate it. That makes a lot of things much more clear to me. I have further questions.

1. parfor loop

No labs also means no workers. 1 worker = 1 lab. 1 lab is assigned to 1 worker. Or we can say 1 worker is working in 1 lab. Right?

"Part of the parfor body is executed on the MATLAB client (where the parfor is issued) and part is executed in parallel on MATLAB workers."
"You use the function matlabpool to reserve a number of MATLAB workers for executing a subsequent parfor-loop."

It seems client is separate from workers. And workers can only be created using matlabpool open.

If this is the case, then parfor alone can't do parallel computing. It's just the same as for loop. Right?

(# Maybe this is not the case for the R2006b version, where there must be a different parallelism method. Or maybe without matlabpool open, parfor in recent version will become the same as in R2006b.)


2. Lab, worker, core

I'm using labBarrier for a particular project. "labBarrier - block until all labs have entered the barrier". I want multiple workers start the work at exactly the same time (at least <0.1ms error). I think labbarrier can help. This is one particular case where I care cores much. Because if different work are assigned to different cores PHYSICALLY, this parallelism is physically real, compared to the time division method. With the default conf, I can create at most 2 workers, one worker per core. When I assign a job to a worker (labindex), the work is done on one core. Correct me if I'm wrong.

I won't focus on cores any more before one last question.

"If the local configuration is your default and does not specify the number of workers, this starts one worker per core (maximum of eight) on your local MATLAB client machine."

I have a dual core computer. But even using myConf, I can't create 16 labs. I can only create a maximum of 8 workers. "The ClusterSize for a local scheduler must be between 1 and 8". 8 workers will share the two cores automatically based on the availability, and we don't have control about which workers use which cores, right?

"Local" means THIS computer, right? Does that mean I can't create more than 8 workers on one computer?


3. For my question previous #2, you said "You're running two iterations, and MATLAB chose (for whatever reason) not to split those tasks among different labs, probably because it doesn't make sense to do so."

If the two iterations are done in one lab, do they get done sequentially? But actually, the two functions are not run squentially. That we can tell using tictoc. Each function delays 5s. It will be 10s if run sequantially. Does this mean two workers working in lab 1 at the same time (in parallel)? If this is true, my understand in #1 must be incorrect.

tictoc times are shown as follows.

---------------------------------------------
tic
funList{1}(dataList{1});
toc

Elapsed time is 4.998234 seconds.
---------------------------------------------

tic
funList{1}(dataList{1});
funList{2}(dataList{2});
toc

Elapsed time is 10.000547 seconds.
---------------------------------------------
tic
parfor (i=1:length(funList),2)
funList{i}(dataList{i});
end
toc

Elapsed time is 10.002345 seconds.

---------------------------------------------
matlabpool open 2
tic
parfor (i=1:length(funList),2)
%labindex
funList{i}(dataList{i});
end
toc
matlabpool close

Elapsed time is 5.360232 seconds.
labindex = 1 for both iterations.
---------------------------------------------


4. Vetectorization

Even an assignment, A(i) = i, is actually calling an "equal operator" function, right?


Thank you for your time and knowledge!
Binlin

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Steven_Lord

Date: 28 Apr, 2011 13:47:33

Message: 4 of 10



"Binlin Wu" <cunyalen@gmail.com> wrote in message
news:ip9t5g$fvo$1@fred.mathworks.com...
> Hi Steven,
>
> Thank you very much for your detailed explanation, comments, references
> etc. I really appreciate it. That makes a lot of things much more clear to
> me. I have further questions.
>
> 1. parfor loop
>
> No labs also means no workers. 1 worker = 1 lab. 1 lab is assigned to 1
> worker. Or we can say 1 worker is working in 1 lab. Right?

In general, the terms "labs" and "workers" mean basically the same thing.

> "Part of the parfor body is executed on the MATLAB client (where the
> parfor is issued) and part is executed in parallel on MATLAB workers."
> "You use the function matlabpool to reserve a number of MATLAB workers for
> executing a subsequent parfor-loop."
>
> It seems client is separate from workers. And workers can only be created
> using matlabpool open.

That's correct. Look at this picture from the Getting Started section of the
Parallel Computing Toolbox documentation:

http://www.mathworks.com/help/toolbox/distcomp/brjw1e5-1.html

> If this is the case, then parfor alone can't do parallel computing. It's
> just the same as for loop. Right?

When run without a MATLABPOOL, PARFOR is treated like a FOR loop, yes.

> (# Maybe this is not the case for the R2006b version, where there must be
> a different parallelism method. Or maybe without matlabpool open, parfor
> in recent version will become the same as in R2006b.)

I'm not certain.

> 2. Lab, worker, core
>
> I'm using labBarrier for a particular project. "labBarrier - block until
> all labs have entered the barrier". I want multiple workers start the work
> at exactly the same time (at least <0.1ms error).

I don't think labBarrier (or any other method) can guarantee that all your
workers start at exactly the same time. What if the OS on one of the
machines on which you're running your workers decides at the moment it
receives the signal "okay, everybody's here, start" to phone home and check
for updates rather than starting your work immediately?

> I think labbarrier can help. This is one particular case where I care
> cores much. Because if different work are assigned to different cores
> PHYSICALLY, this parallelism is physically real, compared to the time
> division method. With the default conf, I can create at most 2 workers,
> one worker per core. When I assign a job to a worker (labindex), the work
> is done on one core. Correct me if I'm wrong.

I'm not going to answer, because I'm not completely sure I understand what
you're asking. I think you're probably going to want to contact the Parallel
Computing Toolbox Technical Support staff for this one.

> I won't focus on cores any more before one last question.
> "If the local configuration is your default and does not specify the
> number of workers, this starts one worker per core (maximum of eight) on
> your local MATLAB client machine."
>
> I have a dual core computer. But even using myConf, I can't create 16
> labs. I can only create a maximum of 8 workers.

With the local scheduler that comes with Parallel Computing Toolbox that's
correct -- up to 8 labs, all on the same machine.

http://www.mathworks.com/products/parallel-computing/description1.html

If you want to start more than 8 labs, or you want to start labs on multiple
machines and have them work together, you need MATLAB Distributed Computing
Server.

http://www.mathworks.com/products/distriben/

> "The ClusterSize for a local scheduler must be between 1 and 8". 8 workers
> will share the two cores automatically based on the availability, and we
> don't have control about which workers use which cores, right?

I believe the answer to your question is yes.

> "Local" means THIS computer, right? Does that mean I can't create more
> than 8 workers on one computer?

Yes and see above.

> 3. For my question previous #2, you said "You're running two iterations,
> and MATLAB chose (for whatever reason) not to split those tasks among
> different labs, probably because it doesn't make sense to do so."
> If the two iterations are done in one lab, do they get done sequentially?
> But actually, the two functions are not run squentially. That we can tell
> using tictoc. Each function delays 5s. It will be 10s if run sequantially.
> Does this mean two workers working in lab 1 at the same time (in
> parallel)? If this is true, my understand in #1 must be incorrect.

LABINDEX is not the right tool to use for this investigation, as Edric
commented in this other thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/306207#831053

> 4. Vetectorization
>
> Even an assignment, A(i) = i, is actually calling an "equal operator"
> function, right?

If A is an object, that will invoke the object's SUBSASGN method.

http://www.mathworks.com/help/techdoc/matlab_oop/br09eqz.html

You SHOULD NOT try to overload SUBSASGN for a built-in class (yes, it is
theoretically possible) unless you know EXACTLY what you're doing and why.
That type of change could easily cause all sorts of problems with other code
that expects indexed assignment to work as it does out of the box.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Edric M Ellis

Date: 28 Apr, 2011 14:11:18

Message: 5 of 10

"Steven_Lord" <slord@mathworks.com> writes:

> "Binlin Wu" <cunyalen@gmail.com> wrote in message
> news:ip9t5g$fvo$1@fred.mathworks.com...
>> [... stuff about PARFOR ...]
>> (# Maybe this is not the case for the R2006b version, where there must be a
>> different parallelism method. Or maybe without matlabpool open, parfor in
>> recent version will become the same as in R2006b.)
>
> I'm not certain.

The pre-R2007b thing called "PARFOR" was a different beast to that
introduced in R2007b, FYI the release notes for that change are here:

http://www.mathworks.com/help/toolbox/distcomp/rn/bq7xin2-1.html#bq7xipz

The "old PARFOR" is intended for use in a parallel context (i.e. one
where NUMLABS returns some value > 1); the "new PARFOR" is intended for
use from a client MATLAB session with a MATLABPOOL open for its use.

>> 2. Lab, worker, core
>>
>> I'm using labBarrier for a particular project. "labBarrier - block until all
>> labs have entered the barrier". I want multiple workers start the work at
>> exactly the same time (at least <0.1ms error).
>
> I don't think labBarrier (or any other method) can guarantee that all your
> workers start at exactly the same time. What if the OS on one of the machines on
> which you're running your workers decides at the moment it receives the signal
> "okay, everybody's here, start" to phone home and check for updates rather than
> starting your work immediately?

Further to this, the contract of labBarrier is this: no lab shall leave
the call to labBarrier until all labs have entered. There is definitely
no guarantee that the labs will all leave the barrier simultaneously.

>> I think labbarrier can help. This is one particular case where I care cores
>> much. Because if different work are assigned to different cores PHYSICALLY,
>> this parallelism is physically real, compared to the time division
>> method. With the default conf, I can create at most 2 workers, one worker per
>> core. When I assign a job to a worker (labindex), the work is done on one
>> core. Correct me if I'm wrong.

Parallel Computing Toolbox does not force different workers to execute
on different cores (even though this is technically possible using "core
affinity" mechanisms provided by your OS). You can force this yourself
either via the Task Manager in Windows, or something like taskset on
Linux.

What we've found when we've investigated this is that typically the OS
does a good job of migrating the worker processes between cores as it
sees fit.

Cheers,

Edric.

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Binlin Wu

Date: 28 Apr, 2011 18:54:18

Message: 6 of 10

"When run without a MATLABPOOL, PARFOR is treated like a FOR loop, yes."

I'm getting different results for parfor and for without matlabpool open under R2010b. Wonder why.

clear all
tic
for i=1:100000
M(i)=i;
end
toc

Elapsed time is 21.082800 seconds.

----------------

clear all
tic
parfor i=1:100000
M(i)=i;
end
toc

Elapsed time is 0.160598 seconds.


Thank you!

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Binlin Wu

Date: 28 Apr, 2011 21:02:06

Message: 7 of 10

Edric M Ellis <eellis@mathworks.com> wrote in message <ytwmxjacvp5.fsf@uk-eellis-deb5-64.dhcp.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> writes:
>
> > "Binlin Wu" <cunyalen@gmail.com> wrote in message
> > news:ip9t5g$fvo$1@fred.mathworks.com...
> >> [... stuff about PARFOR ...]
> >> (# Maybe this is not the case for the R2006b version, where there must be a
> >> different parallelism method. Or maybe without matlabpool open, parfor in
> >> recent version will become the same as in R2006b.)
> >
> > I'm not certain.
>
> The pre-R2007b thing called "PARFOR" was a different beast to that
> introduced in R2007b, FYI the release notes for that change are here:
>
> http://www.mathworks.com/help/toolbox/distcomp/rn/bq7xin2-1.html#bq7xipz
>
> The "old PARFOR" is intended for use in a parallel context (i.e. one
> where NUMLABS returns some value > 1); the "new PARFOR" is intended for
> use from a client MATLAB session with a MATLABPOOL open for its use.
>


"Old matlab" is single threaded. What do these NUMLABS mean? How do they work? Was it specifically for multi computers?


> >> 2. Lab, worker, core
> >>
> >> I'm using labBarrier for a particular project. "labBarrier - block until all
> >> labs have entered the barrier". I want multiple workers start the work at
> >> exactly the same time (at least <0.1ms error).
> >
> > I don't think labBarrier (or any other method) can guarantee that all your
> > workers start at exactly the same time. What if the OS on one of the machines on
> > which you're running your workers decides at the moment it receives the signal
> > "okay, everybody's here, start" to phone home and check for updates rather than
> > starting your work immediately?
>
> Further to this, the contract of labBarrier is this: no lab shall leave
> the call to labBarrier until all labs have entered. There is definitely
> no guarantee that the labs will all leave the barrier simultaneously.
>
> >> I think labbarrier can help. This is one particular case where I care cores
> >> much. Because if different work are assigned to different cores PHYSICALLY,
> >> this parallelism is physically real, compared to the time division
> >> method. With the default conf, I can create at most 2 workers, one worker per
> >> core. When I assign a job to a worker (labindex), the work is done on one
> >> core. Correct me if I'm wrong.
>
> Parallel Computing Toolbox does not force different workers to execute
> on different cores (even though this is technically possible using "core
> affinity" mechanisms provided by your OS). You can force this yourself
> either via the Task Manager in Windows, or something like taskset on
> Linux.
>
> What we've found when we've investigated this is that typically the OS
> does a good job of migrating the worker processes between cores as it
> sees fit.
>



When people say implicit and explicit multithreading, what do they mean?

In this thread (http://www.mathworks.com/matlabcentral/newsreader/view_thread/262753), Duane says "implicit parallelism (the kind that just happens for you automatically when running on a multi-core/multi-processor system) and explicit parallelism via parfor and the Parallel Computing Toolbox". Another article here also explains about it. (https://docs.google.com/viewer?url=http%3A%2F%2Fweb.eecs.utk.edu%2F~luszczek%2Fpubs%2Fparallelmatlab.pdf)

Do they mean mean this? Multithreading is including multicore working in parallel and multi computational threads. Implicit multithreading refers to multicores working in parallel, while explicit multithreading refers to multi computational threads. Am I right? That means on a single core computer, there is no such thing as implicit multithreading.

Is implicit threading achieved with maxNumCompThreads? Correct me if I'm wrong. In the old version, Matlab makes computational threads in one matlab instance. But they will be run sequentially on single-core computer. If there are other cores available, it will run other threads in parallel. We don't have control about which threads to run in parallel, because that depends on core availability.

In the new matlab, multiple instances of matlab are created, explicitly. Each instance is actually a single thread. These threads are managed by windows. These instances/threads are running in parallel for sure. They may be run on one core, may be multicore in parallel.

We can benefit from explicit multithreading, no matter on a single core or multi-core computer. But multicore working in parallel at the same time will definitely further help. Am I right?


> Cheers,
>
> Edric.


Thank you.
Binlin

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Binlin Wu

Date: 28 Apr, 2011 21:10:21

Message: 8 of 10

http://www.mathworks.com/support/solutions/en/data/1-4PG4AN/?solution=1-4PG4AN

These MATLAB functions that benefit from multithreaded computation. Without creating multiple workers, do they still benefit from multithreading? Implicit multithreading?

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Steven_Lord

Date: 29 Apr, 2011 13:26:14

Message: 9 of 10



"Binlin Wu" <cunyalen@gmail.com> wrote in message
news:ipcd4q$nfu$1@fred.mathworks.com...
> "When run without a MATLABPOOL, PARFOR is treated like a FOR loop, yes."
>
> I'm getting different results for parfor and for without matlabpool open
> under R2010b. Wonder why.
>
> clear all
> tic
> for i=1:100000
> M(i)=i;
> end
> toc
>
> Elapsed time is 21.082800 seconds.
>
> ----------------
>
> clear all
> tic
> parfor i=1:100000
> M(i)=i;
> end
> toc
>
> Elapsed time is 0.160598 seconds.

I believe PARFOR does some analysis to figure out how long M will be and
preallocates it to that length. Your original code does NOT preallocate M
and so has to reallocate memory each and every iteration through the loop.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How do parfor, matlabpool and spmd, and vectorization work?

From: Mike Karr

Date: 2 May, 2011 11:30:41

Message: 10 of 10

Steven_Lord wrote:
>
>
> "Binlin Wu" <cunyalen@gmail.com> wrote in message
> news:ipcd4q$nfu$1@fred.mathworks.com...
>> "When run without a MATLABPOOL, PARFOR is treated like a FOR loop, yes."
>>
>> I'm getting different results for parfor and for without matlabpool
>> open under R2010b. Wonder why.
>>
>> clear all
>> tic
>> for i=1:100000
>> M(i)=i;
>> end
>> toc
>>
>> Elapsed time is 21.082800 seconds.
>>
>> ----------------
>>
>> clear all
>> tic
>> parfor i=1:100000
>> M(i)=i;
>> end
>> toc
>>
>> Elapsed time is 0.160598 seconds.
>
> I believe PARFOR does some analysis to figure out how long M will be and
> preallocates it to that length. Your original code does NOT preallocate
> M and so has to reallocate memory each and every iteration through the
> loop.
>

Steve is right that M is being pre-allocated, but it doesn't really
directly "figure out how long M will be". I think what is really going
on here is that by using parfor, you are telling MATLAB that you don't
care about the order of evaluation of the various iterations. For
example, if the pool is open, segments of 1:100000 are sent of to
various workers and executed in who knows what order. And in the serial
loops that executed in the workers, or even in the serial loop that is
executed in the main MATLAB process when the pool is not open, MATLAB is
free to execute the iterations in any order, in particular, backwards.
Executing the above loop backward has the effect of pre-allocating M.
In other words, try out the timing for:

for i=100000:-1:1
M(i)=i;
end

Beware that MATLAB doesn't always run serial loops backward.

hth,
mike

Tags for this Thread

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.

Contact us