parfor, can not be classified help

  • Follow


Hi,
    I'm new to the Parallel Computing toolbox and am having trouble converting some of my code to be compliant to support variable classification within my 'parfor' loop. Could someone help explain what is wrong with this simplified example?

Pop(40,10) = zeros;
[pop_size, pop_cols] = size(Pop);

parfor a = 1 : pop_size
    Child = rand(1,pop_cols);
    Pop(a, :) = Child();
    Pop(a, pop_cols) = inf; %This line causes Pop not to be able to be classified
end

"??? Error: The variable Pop in a parfor cannot be classified.
 See Parallel for Loops in MATLAB, "Overview".

Error in ==> test at 5
parfor a = 1 : pop_size"

In my other program even the prior line with the Child assignment flags the same error. As I'm only effecting individual rows it should be able to be made parallel.

Thanks in advance, Paul.
0
Reply paul.matthews (4) 4/12/2011 5:18:05 PM

"Paul Matthews" wrote in message <io21gc$isj$1@fred.mathworks.com>...
> Hi,
>     I'm new to the Parallel Computing toolbox and am having trouble converting some of my code to be compliant to support variable classification within my 'parfor' loop. Could someone help explain what is wrong with this simplified example?
> 
> Pop(40,10) = zeros;
> [pop_size, pop_cols] = size(Pop);
> 
> parfor a = 1 : pop_size
>     Child = rand(1,pop_cols);
>     Pop(a, :) = Child();
>     Pop(a, pop_cols) = inf; %This line causes Pop not to be able to be classified
> end
> 
> "??? Error: The variable Pop in a parfor cannot be classified.
>  See Parallel for Loops in MATLAB, "Overview".
> 
> Error in ==> test at 5
> parfor a = 1 : pop_size"
> 
> In my other program even the prior line with the Child assignment flags the same error. As I'm only effecting individual rows it should be able to be made parallel.
> 
> Thanks in advance, Paul.



If I rearrange the code it works, but it doesn't answer the question of why in the first place.

Pop(40,10) = zeros;
[pop_size, pop_cols] = size(Pop); % pop_size and pop_cols are broadcast variables

parfor a = 1 : pop_size
    Child = rand(1,pop_cols); %Child is temporary variable
    Child(pop_cols) = inf;
    Pop(a, :) = Child(); % Pop is sliced output variable
    %Pop(a, pop_cols) = inf; %This line causes Pop not to be able to be classified
end
0
Reply paul.matthews (4) 4/12/2011 5:41:05 PM



"Paul Matthews" <paul.matthews@agcocorp.com> wrote in message 
news:io21gc$isj$1@fred.mathworks.com...
> Hi,
>    I'm new to the Parallel Computing toolbox and am having trouble 
> converting some of my code to be compliant to support variable 
> classification within my 'parfor' loop. Could someone help explain what is 
> wrong with this simplified example?
>
> Pop(40,10) = zeros;
> [pop_size, pop_cols] = size(Pop);
>
> parfor a = 1 : pop_size
>    Child = rand(1,pop_cols);
>    Pop(a, :) = Child();
>    Pop(a, pop_cols) = inf; %This line causes Pop not to be able to be 
> classified

I think you're expecting this to be a sliced variable:

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

but it fails the criterion listed in the second bullet "Fixed Index Listing" 
because you're indexing into it with both (a, :) and (a, pop_cols) in the 
loop.

Preallocate Pop to be an appropriately sized random matrix prior to entering 
the PARFOR loop and fill in the particular element whose values you want to 
replace by inf inside the loop, or preallocate the entire array to be Inf 
and fill in all but the last column with random data, or just eliminate the 
PARFOR loop entirely.

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

0
Reply slord (13284) 4/12/2011 5:49:38 PM

"Steven_Lord" <slord@mathworks.com> wrote in message <io23ba$kls$1@fred.mathworks.com>...
> 
> 
> "Paul Matthews" <paul.matthews@agcocorp.com> wrote in message 
> news:io21gc$isj$1@fred.mathworks.com...
> > Hi,
> >    I'm new to the Parallel Computing toolbox and am having trouble 
> > converting some of my code to be compliant to support variable 
> > classification within my 'parfor' loop. Could someone help explain what is 
> > wrong with this simplified example?
> >
> > Pop(40,10) = zeros;
> > [pop_size, pop_cols] = size(Pop);
> >
> > parfor a = 1 : pop_size
> >    Child = rand(1,pop_cols);
> >    Pop(a, :) = Child();
> >    Pop(a, pop_cols) = inf; %This line causes Pop not to be able to be 
> > classified
> 
> I think you're expecting this to be a sliced variable:
> 
> http://www.mathworks.com/help/toolbox/distcomp/brdqtjj-1.html#bq_tcng-1
> 
> but it fails the criterion listed in the second bullet "Fixed Index Listing" 
> because you're indexing into it with both (a, :) and (a, pop_cols) in the 
> loop.
> 
> Preallocate Pop to be an appropriately sized random matrix prior to entering 
> the PARFOR loop and fill in the particular element whose values you want to 
> replace by inf inside the loop, or preallocate the entire array to be Inf 
> and fill in all but the last column with random data, or just eliminate the 
> PARFOR loop entirely.
> 
> -- 
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on 
> http://www.mathworks.com 


Hi Steven,
   Thanks for the response. I've read the referenced content, but am still struggling to determine how to change the code to address the issue. I've enclosed the actual function in case you're able to offer any explicit suggestions(the error is at the end and there's a warning halfway down commented);

function [ Pop2 ] = NextGen( Pop )
    %for a given population take a weighted random selection from the 
    %current population (Pop) for two parents and cross them over to 
    %create a new child. Repeat this process for 3/4 of the population
    %creating a new population (Pop2) that still contains the best 1/4
    %from the initial population.

    [pop_size, pop_cols] = size(Pop);
    Pop2 = Pop; %Create new population (we'll keep the best)
   
    %Breed
    parfor a = pop_size / 4 + 1: pop_size %replace 3/4 of pop
        %Select Parent ID's
        p1 = floor(randw(pop_size, 1)) + 1;
        p2 = p1;
        while p1 == p2 %No mating with yourself
            p2 = floor(randw(pop_size, 1)) + 1;
        end
        
        %create child
        pp1 = Pop(p1,:); % <-- WARNING : Pop is indexed,but not sliced...
        pp2 = Pop(p2,:);
        Child = Crossover(pp1, pp2);
        while row_exists(Pop2, Child) == 1 %no identical twins
            %Select Parents
            p1 = floor(randw(pop_size, 1)) + 1;
            p2 = p1;
            while p1 == p2 %Make sure they're unique
                p2 = floor(randw(pop_size, 1)) + 1;
            end
            Child = Crossover(Pop2(p1,:), Pop2(p2,:));           
        end
        Child(pop_cols) = inf;  %reset performance field
        Pop2(a,:) = Child; % <-- ERROR can't classify Parfor
    end;
end

Thanks, Paul.
0
Reply paul.matthews (4) 4/12/2011 7:04:04 PM

Ok,
   I believe I have it now. I think it was the size of Child was changing each pass which then had the knock on of effecting Pop2. The solution was to define the size of Child outside of the parfor such that the parfor know that only the context are being updated and not the size, which then made Pop2 happy.

Paul.
0
Reply paul.matthews (4) 4/12/2011 7:40:21 PM

 Sir Are you finally classify the variable pop2 are not?
0
Reply p.priyanga19 (2) 6/28/2012 5:41:06 AM

"Paul Matthews" wrote in message <io21gc$isj$1@fred.mathworks.com>...
> Hi,
>     I'm new to the Parallel Computing toolbox and am having trouble converting some of my code to be compliant to support variable classification within my 'parfor' loop. Could someone help explain what is wrong with this simplified example?
> 
> Pop(40,10) = zeros;
> [pop_size, pop_cols] = size(Pop);
> 
> parfor a = 1 : pop_size
>     Child = rand(1,pop_cols);
>     Pop(a, :) = Child();
>     Pop(a, pop_cols) = inf; %This line causes Pop not to be able to be classified
> end
> 
> "??? Error: The variable Pop in a parfor cannot be classified.
>  See Parallel for Loops in MATLAB, "Overview".
> 
> Error in ==> test at 5
> parfor a = 1 : pop_size"
> 
> In my other program even the prior line with the Child assignment flags the same error. As I'm only effecting individual rows it should be able to be made parallel.
> 
> Thanks in advance, Paul.


How to classify that Pop2 variable in this program Finally sir?
0
Reply p.priyanga19 (2) 6/28/2012 5:45:07 AM


"PRIYANGA " <p.priyanga19@gmail.com> wrote in message 
news:jsgr13$981$1@newscl01ah.mathworks.com...
> "Paul Matthews" wrote in message <io21gc$isj$1@fred.mathworks.com>...
>> Hi,
>>     I'm new to the Parallel Computing toolbox and am having trouble 
>> converting some of my code to be compliant to support variable 
>> classification within my 'parfor' loop. Could someone help explain what 
>> is wrong with this simplified example?
>>
>> Pop(40,10) = zeros;
>> [pop_size, pop_cols] = size(Pop);
>>
>> parfor a = 1 : pop_size
>>     Child = rand(1,pop_cols);
>>     Pop(a, :) = Child();
>>     Pop(a, pop_cols) = inf; %This line causes Pop not to be able to be 
>> classified
>> end

http://www.mathworks.com/help/toolbox/distcomp/brdqtjj-1.html#bq_of7_-1

Is Pop the loop variable? No, that's a.
Is Pop a sliced variable? Does it satisfy the four requirements given on 
that page? It does not, so it is not a sliced variable. In particular it 
fails the "Fixed Index Listing" requirement; in one place you index into it 
with a and : and in another you index into it with a and pop_cols.
Similarly Pop is not a broadcast, reduction, or temporary variable.

You can do what you want by modifying the temporary Child variable 
(replacing its last element with Inf) and then deleting the third line 
inside the PARFOR. That should cause Pop to satisfy the four requirements 
for a sliced variable.

parfor a = 1 : pop_size
    Child = rand(1,pop_cols);
    Child(end) = Inf;
    Pop(a, :) = Child();
end

Note I haven't tested this, but I believe it should work.

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

0
Reply slord (13284) 6/28/2012 1:19:24 PM

7 Replies
27 Views

(page loaded in 0.082 seconds)


Reply: