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:
matlabpool classified variables in parfor

Subject: matlabpool classified variables in parfor

From: HenryW

Date: 7 May, 2009 15:06:36

Message: 1 of 4

I can't seem to get the variables done right in a parfor loop. My loop has several two dimensional arrays.
As I go through the loop
parfor iCo = 1:lCo,
ic=Co(iCo);
I use the ic column of these arrays.
But I use them as data, e.g.
p = Dcq(:,ic);
And I change a value, e.g.
Dcq(jc,ic)=1;
I tried several different ways but could not figure out how to avoid the error caused by the variable not being classified by matlab.
   thanks for any hings.

Subject: matlabpool classified variables in parfor

From: Edric M Ellis

Date: 7 May, 2009 17:06:03

Message: 2 of 4

HenryW <hwolkowicz@uwaterloo.ca> writes:

> I can't seem to get the variables done right in a parfor loop. My loop has
> several two dimensional arrays.
> As I go through the loop
> parfor iCo = 1:lCo,
> ic=Co(iCo);
> I use the ic column of these arrays.
> But I use them as data, e.g.
> p = Dcq(:,ic);
> And I change a value, e.g.
> Dcq(jc,ic)=1;
> I tried several different ways but could not figure out how to avoid the error
> caused by the variable not being classified by matlab. thanks for any hings.

I'm not 100% sure I've understood, but it seems that you're talking about a
situation a bit like this:

N = 12;
data = rand( N );

parfor ii = 1:N
    col = data( :, ii ); % (1)
    col = col - max( col );
    data( :, ii ) = col; % (2)
    data( ii, ii ) = min( col ); % (3)
end

In this example, lines (1) and (2) are fine, line (3) is an error. The reason
for this is that lines (1) and (2) define how the variable "data" is being
sliced. Once this has been determined for a given variable within a given parfor
loop, you cannot index into that variable in any other way. So, line (1) defines
the slicing of "data" to be "(:, ii)" - and any other indexing expression is an
error.

There's more about this in the doc here:

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

Cheers,

Edric.

Subject: matlabpool classified variables in parfor

From: HenryW

Date: 7 May, 2009 17:47:40

Message: 3 of 4

Yes, that is my problem, e.g.
parfor ii = 1:N
col = data( :, ii ); % (1)
col = col - max( col );
data( :, ii ) = col; % (2)
data( ii, ii ) = min( col ); % (3)
end
where I uses data on the right and then on the left.
However, it is always the same column ii that is being used and changed. So it will not affect the other workers?
Is there any way that I can do this?
 thanks

Subject: matlabpool classified variables in parfor

From: Edric M Ellis

Date: 8 May, 2009 09:05:13

Message: 4 of 4

HenryW <hwolkowicz@uwaterloo.ca> writes:

> Yes, that is my problem, e.g.
> parfor ii = 1:N
> col = data( :, ii ); % (1)
> col = col - max( col );
> data( :, ii ) = col; % (2)
> data( ii, ii ) = min( col ); % (3)
> end
> where I uses data on the right and then on the left.

It's fine to use sliced data on the right and the left, but you must be careful
to index with the same list of subscripts each time.

So, if you pick out a column from "data", you must always update the whole
column, like this:

parfor ii = 1:N
    col = data( :, ii );
    % Now that I've selected "col", I can do anything with it.
    col(1) = 7;
    col(2) = 42;
    
    % Must update data with the same subscripts as before, even if I only
    % actually wish to update one or two entries.
    data( :, ii ) = col;
end

Cheers,

Edric.

Tags for this Thread

No tags are associated with 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