Thread Subject: Parfor counts backward?

Subject: Parfor counts backward?

From: Yoav

Date: 7 Oct, 2009 15:36:02

Message: 1 of 3

Hi,

When using a parfor loop without the matlabpool open it is supposed to default to a regular for loop (see documentation). This works fine but it seems to be counting from the END of the numbering array to the beginning. For example, run the following code without opening a matlab pool:

parfor x = 1:10
    disp(x);
end

And instead of seeing 1, 2, 3...10 (as you would with a for loop) you see 10, 9, 8... 1.

Any idea why it happens this way? It shouldn't matter much for results collection but still a curious behaviour.

Cheers,

Yoav

Subject: Parfor counts backward?

From: Steven Lord

Date: 7 Oct, 2009 15:54:06

Message: 2 of 3


"Yoav" <yoav.farkash@utoronto.ca> wrote in message
news:haich2$38a$1@fred.mathworks.com...
> Hi,
>
> When using a parfor loop without the matlabpool open it is supposed to
> default to a regular for loop (see documentation). This works fine but it
> seems to be counting from the END of the numbering array to the beginning.
> For example, run the following code without opening a matlab pool:
>
> parfor x = 1:10
> disp(x);
> end
>
> And instead of seeing 1, 2, 3...10 (as you would with a for loop) you see
> 10, 9, 8... 1.
>
> Any idea why it happens this way? It shouldn't matter much for results
> collection but still a curious behaviour.

http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/brb2x2l-1.html#brb2x57

You should not depend on the order in which the iterations in your PARFOR
loop are executed; each iteration of the loop body must be independent of
the others.

http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/brb2x2l-1.html#brdew7t-1

I believe this is designed to encourage that, but you shouldn't assume that
it will always be the case that the loop runs in this particular order.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Parfor counts backward?

From: Mike Karr

Date: 16 Oct, 2009 11:41:16

Message: 3 of 3

Steven Lord wrote:
> "Yoav" <yoav.farkash@utoronto.ca> wrote in message
> news:haich2$38a$1@fred.mathworks.com...
>> Hi,
>>
>> When using a parfor loop without the matlabpool open it is supposed to
>> default to a regular for loop (see documentation). This works fine but it
>> seems to be counting from the END of the numbering array to the beginning.
>> For example, run the following code without opening a matlab pool:
>>
>> parfor x = 1:10
>> disp(x);
>> end
>>
>> And instead of seeing 1, 2, 3...10 (as you would with a for loop) you see
>> 10, 9, 8... 1.
>>
>> Any idea why it happens this way? It shouldn't matter much for results
>> collection but still a curious behaviour.
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/brb2x2l-1.html#brb2x57
>
> You should not depend on the order in which the iterations in your PARFOR
> loop are executed; each iteration of the loop body must be independent of
> the others.
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/brb2x2l-1.html#brdew7t-1
>
> I believe this is designed to encourage that, but you shouldn't assume that
> it will always be the case that the loop runs in this particular order.

Steve is absolutely right that you should not depend on the order. But
running the loop backward was not designed to encourage that. A more
complete story is that when parfor goes truly parallel, it in fact sends
"chunks" to workers in increasing e.g., for 1:10 the chunks might be
1:3, 4:6, 7:8, 9:10. But then in each worker, the loops are run
backward, achieving the effect of a pre-allocation (which I won't
discuss here; there are many references to it in this newsgroup).

In other words, the parfor implementation does not merely encourage
order-independence, it _relies_ on it. When parfor is run on a single
processor, the fact that the serial loop there goes backward is fallout
from the general case.

fyi,
mike

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com