0
votes

I'm trying to prove a code to run in parallel, code works perfectly except when I'm assiging data to a sliced variable, I believe I'm respecting Matlab's parfor restrictions, and in fact it runs smoothly for some parfor iterations but a certain point Matlab crashs asking to exiting.

The problem is in the last line when asigning values to "features" (I've tried also with cells but same problem):

if (matlabpool('size')==0) 
    matlabpool();
end


%Prepare iterations
numTrain = size(Xnorm(1000:1250,:),1);
numFeatures = size(Xnorm,2);
classData=Y(1000:1250,:);

%Linear Space for Grid
zz = linspace(0.1,20,10);
zsize=length(zz);

%Definitions to use inside parfor
features=zeros(numFeatures,1);
fmax=@(x) max(x);

parfor i = 1:numFeatures

    %Internal variables definition
    CsCross=zeros(zsize,1);
    SigmaCsMax=zeros(zsize,1);

    fprintf('\nFeature:%i...',i);

    for j=1:zsize

    %Precompute kernel

       K =  [ (1:numTrain)' , gpuKernel(Xnorm(1000:1250,i),zz(j)) ];

        for iter = 1:zsize

            options = sprintf('-c %d -t 4 -v 10 -q',zz(iter));
            model = svmtrain(classData, K, options);
            CsCross(iter)=model;

        end

        SigmaCsMax(j)=fmax(CsCross);
    end
   temp=fmax(SigmaCsMax);

   %%% HERE THE ERROR IS CAUSED. When removed code runs smoothly
   features(i)=temp;

end

Any suggestion about it? What am I doing wrong? (just to say code works without parfor)

Thanks in advance,

Sergi

1
What error message do you get? - Daniel
I assume there are some details on the matlab console? Could you add them as well? - Daniel
Unfortunately there is no error in console. It simply crashs. Actually in some occassions (very few) it works. - user3774343

1 Answers

0
votes

I found the way to make it work. I predefine "features" as features=[] (without any predefined size), then I let the parfor to build up the entire array. Surprisingly the result respect the order. Here the code (I've omitted the initial part): [.....]

%HERE I DEFINE AND EMPTY ARRAY
features=[];


parfor i = 1:numFeatures

    %Internal variables definition
    CsCross=zeros(zsize,1);
    SigmaCsMax=zeros(zsize,1);
    temp=0;

    fprintf('\nFeature:%i...',i);

    for j=1:zsize

    %Precompute kernel

       K =  [ (1:numTrain)' , gpuKernel(Xnorm(1000:1250,i),zz(j)) ];

        for iter = 1:zsize

            options = sprintf('-c %d -t 4 -v 10 -q',zz(iter));
            model = svmtrain(classData, K, options);
            CsCross(iter)=model;

        end

        SigmaCsMax(j)=fmax(CsCross);
    end
   temp=fmax(SigmaCsMax);

   %%% EVERY ITERATION ADD NEW RESULTS

   features=[features,temp];

end

Find here a more detailed explanation about why it works: http://www.mathworks.es/es/help/distcomp/getting-started-with-parfor.html#brdqn6p-1

Hope it may help.

Sergi