## How to Classify the Variable for parfor program?

on 19 Jun 2012

### Walter Roberson (view profile)

Hi Everybody,

I am priya Now doing one research. I want execute my code parallel using parfor. But not able to Run It. I got a Error Msg. ??? Error: The variable mcode in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".

This is my sample code

```parfor i1=1:1:n
disp(i1);
??? Error: The variable mcoder,mcodeg,mcodeb,a,e,gcoder,gcodeg,gcodeb in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
if (var(i1) < 25)
label(i1)=0;
mcoder(e)=mranr(i1);
mcodeg(e)=mrang(i1);
mcodeb(e)=mranb(i1);
mgacode=cat(3,mcoder,mcodeg,mcodeb);
e=e+1;
else
label(i1)=1;
[a b c d er]=rand1(value1r(i1,:),value2,value3,value4);
[a b c d eg]=rand1(value1g(i1,:),value2,value3,value4);
[a b c d eb]=rand1(value1b(i1,:),value2,value3,value4);
??? Error using ==>
parallel_function at 598
Error in ==>
parallel_function>make_general_channel/channel_general
at 872
Subscripted assignment
dimension mismatch.
% following code  mismatch error
gcoder(a,:) = [a b c d er];
gcodeg(a,:) = [a b c d eg];
gcodeb(a,:) = [a b c d eb];
gcode=cat(3,gcoder,gcodeg,gcodeb);
a=a+1;
err=(er+eg+eb)/3;
if (err > llim)
eno=eno+1;
end
end
end
```

My code Run properly without parfor. But With Parfor I got so many error msg.

I am a new matlab user. How can I modify that code please any one help me my problem.

Thank You so much.

## Products

### Walter Roberson (view profile)

on 25 Jun 2012

You are indexing arrays at "e", which is not initialized within the parfor loop. The value of "e" at any one point thus depends upon what the previous iterations of the loop have done. That makes indexing by "e" dependent on the sequential execution of the loops. "parfor" loops are executed in an unspecified order (currently they are launched in reverse order!), so any indexing within "parfor" must depend only on the loop variable and constants.

The same kind of problem occurs for your indexing by "a": parfor cannot know that your calls to rand1() are going to produce values that are dependent upon the loop variable "i1" so that it can be sure that indexing by "a" is not going to overlap locations with other iterations of the parfor loop. Indeed, considering that you do not even pass "i1" to rand1(), it seems quite unlikely that "a" will turn out to be unique for each "i1" value, so the parfor loop iterations would almost certainly attempt to write to the same locations within gcode* arrays.

Your output arrays need to be indexed at "i1" to prevent clashes with other iterations.

It is allowed for you to use a cell array that you index at "i1" and only sometimes store results in, and it is allowed for you to delete the unused (empty) cells after the end of the "parfor" loop. This would have the effect of allowing data to be added only "sometimes", with the end order being in increasing loop index order like you had for your "for" loop.

PRIYANGA

### PRIYANGA (view profile)

on 28 Jun 2012

Sir,

<<What is the line

e=size(1,nrr1);

intended to mean ?>>

THis line is preallocation of e value. I mean parfor loop Show the comment The e,a, mgacoder,mgacodeg,mgacodeb, gacoder,gacodeg,gacodeb cannot be classified. So I don't know how to classify the variable. So I will try to this preallocation MEthod. But I know this is Wrong. But Realy I don't know how to classify the variable Sir. Please Kindly classify that Variable Sir. The following code is preallocate of variable. e=size(1,nrr1); a=size(1,nrr1); mgacoder=cell(1,nrr1); mgacodeg=cell(1,nrr1); mgacodeb=cell(1,nrr1); gacoder=cell(1,nrr1); gacodeg=cell(1,nrr1); gacodeb=cell(1,nrr1);

This method is wrong or not? how to classify that variable properly in PARFOR method Sir.

Walter Roberson

### Walter Roberson (view profile)

on 28 Jun 2012

size() is normally a key MATLAB function which takes as its first argument an expression (almost always given as a variable name), and as its second argument a dimension number, and it returns the number of elements that expression has along that dimension. For example,

foo = rand(3,5); size(foo,1) %3 because foo is 3 elements along dimension #1 size(foo,2) %5 because foo is 5 elements along dimension #2

Now, the expression "1" is a scalar expression, whose dimensions are 1 x 1. The nrr1'th dimension of a scalar is also 1 (all trailing dimensions are treated as being 1). So size(1,nrr1) is going to be the same as just 1, and so you are initializing e to 1, which is probably not an efficient pre-allocation.

If your "e" variable is intended to be a vector or array, then your expression mgacoder{e} is going to have problems. If your "e" is intended to be initialized to 1 (which is what happens to get done now) then remember that is being done in every "parfor" iteration, and since you never use any other value of "e" after your "e=e+1" statement (remember, your parfor is going to re-initialize "e" for each iteration of "i1"), there is no point in having "e" in that context at all and you might as well just code

```mgacoder{1}=meanranr(i1);
mgacodeg{1}=meanrang(i1);
mgacodeb{1}=meanranb(i1);
```

Then later in your code exactly the same thing occurs for your use of the variable "a".

Meanwhile, your variable "eno" is not initialized in the loop but is incremented in some circumstances. This is in violation of what I said before:

```Each parfor iteration must be independent of what is being done in every other iteration. No using information that would have to be calculated in a different iteration.
```

The only person who has enough information to fix your code is you, as you have not posted indicating what the overall code is intended to do, or how the sections are intended to work. Two short comments total is not enough for us to know what your program does or how you think it should work.

I remind you of my earlier answer:

```It is allowed for you to use a cell array that you index at "i1" and only sometimes store results in, and it is allowed for you to delete the unused (empty) cells after the end of the "parfor" loop.
```

Are your cell arrays indexed at "i1" ?

PRIYANGA

### PRIYANGA (view profile)

on 2 Jul 2012

Thank you so much you help Sir. I was change array indexing value 'e' to 'i1' that's all, my code was running parallel. thank you so much your idea and help sir.

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