0
votes

I want to paralyze my forloop in Matlab.I use parfor function for that, but I get error because the way I used variable inside of the loop. would someone help me to fix that. I'm new in matlab. Here is part of my try.

Here is part of problematic part:

CV_err=zeros(length(gamma), (Num_Tasks + 1));

parfor k=1:length(gamma)

#block of code

#
CV_err(k,1:Num_Tasks)= sum(In_Fold_Error)./size(In_Fold_Error,1);
CV_err(k,Lambda_location)= Lambda;
CV_err(k,(Num_Tasks +2))= sum(CV_err(k,1:Num_Tasks))/Num_Tasks;
end

Error: parfor loop can not run due to way CV_err is used. CV_err is indexed in different ways, potentially causing dependencies

Seems that valid indices are restricted in parfor .

2
Can you post the actual error message please? One sure thing already is that you try to write something in CV_err(k,(Num_Tasks +2)) although you only defined it's 2nd dimension to be of lengthNum_tasks+1 - BillBokeey
you for loop is currently paralyzed - GameOfThrows
I know, but doesn't work. - user51661

2 Answers

2
votes

While your variable is sliced, you only access the k-th row in the k-th iteration, the code analyser does not understand it. Give a little help to matlab, first put all data into a vector and then write all at once to the sliced variable.

CV_err=zeros(length(gamma), (Num_Tasks + 2));
parfor k=1:length(gamma)

%block of code

%
  temp=zeros(1,(Num_Tasks + 2));
  temp(1,1:Num_Tasks)= sum(In_Fold_Error)./size(In_Fold_Error,1);
  temp(1,Lambda_location)= Lambda;
  temp(1,(Num_Tasks +2))= sum(temp(1,1:Num_Tasks))/Num_Tasks;
  CV_err(k,:)=temp;
end

The limitation is explained in the documentation:

Form of Indexing. Within the list of indices for a sliced variable, one of these indices is of the form i, i+k, i-k, k+i, or k-i, where i is the loop variable and k is a constant or a simple (nonindexed) broadcast variable; and every other index is a scalar constant, a simple broadcast variable, a nested for-loop index, colon, or end.

Source

1
votes

To fix pre-allocation, don't pre-allocate. You're just telling to MATLAB how it should split the work among workers; parfor doesn't like that.

The answer is: don't make loops change common variables, write your results separately, grow cell arrays instead of matrices, i.e.

    clear CV_err;

    parfor k=1:length(gamma)
            %// here your other code
            this_CV_err                  = zeros(Num_Tasks+2,1);
            this_CV_err(1:Num_Tasks)     = sum(In_Fold_Error)./size(In_Fold_Error,1);
            this_CV_err(Lambda_location) = Lambda;
            this_CV_err(Num_Tasks+2)     = mean(this_CV_err(1:Num_Tasks));
            CV_err{k} = this_CV_err;
    end;