MATLAB: Problem with cell array indexing when using parfor

cell arraysMATLABParallel Computing Toolboxparforvariable

Dear all,
I'm try to do certain calculation that automatically generates a certain (large) matrix for each iteration and calculates the Eigenvalue for it. To speed up the lengthy calculation, I am using parfor. Unfortunately, the code gives me "The variable IM in a parfor cannot be classified" error. It seems like looking at M-lint tells me that IM, B1, B2, and B3 are bad variables. These are all cell arrays that were created so that different cell can be assigned independently by each iteration, preventing interaction between other iterations.
I do not understand the problem here. Could someone help me? Thank you.
The code is shown below. IM, IIM, B1, B2, and B3 are already defined elsewhere as empty cell arrays. variables l and n depends on the input of the user but is defined prior to the code.
parfor cycle = 1:n
IM{cycle, 1} = IIM
B1{cycle, 1} = setdiff(1:(2*l+1)^3, ((2*l+1)^3+1)/2).'
if Impurity == 1
o(cycle) = randi([0, 1], 1);
if o(cycle) == 0
B2{cycle, 1} = 0;
else
p(cycle) = randi(length(B1{cycle, 1}),1);
IM{cycle, 1}(:, B1{cycle, 1}(p(cycle), 1)) = 0;
IM{cycle, 1}(B1{cycle, 1}(p(cycle), 1), :) = IM{B1{p(cycle),1}, :}*1;
IM{cycle, 1}(B1{cycle, 1}(p(cycle), 1), B1{cycle, 1}(p(cycle), 1)) = -str2double(answer3(2,1));
B2{cycle, 1} = B1{cycle, 1}(p(cycle), 1);
end
end
B3{cycle, 1} = zeros(m,1);
q(cycle) = 1;
while length(B1{cycle, 1}) > o(cycle) + str2double(answer2(1,1)) - 1
r(cycle) = randi(length(B1{cycle, 1}),1);
if B1{r(cycle), 1} ~= B2{cycle, 1}
B3{cycle, 1}(q(cycle),1) = B1{cycle, 1}(r(cycle), 1);
B1{r(cycle), 1} = [];
IM{:, B3{q(cycle), 1}} = 0;
IM{B3{q(cycle), 1}, :} = 0;
q(cycle) = q(cycle) + 1;
end
end
B1{cycle, 1}(B1{cycle, 1}==B2{cycle, 1}) = [];
for q = 1:length(B1{cycle, 1})
IM{B1{cycle, 1}(q(cycle), 1), B1{cycle, 1}(q(cycle), 1)} = 0;
IM{B1{cycle, 1}(q(cycle), 1), B1{cycle, 1}(q(cycle), 1)} = -(str2double(answer3(1,1)) + str2double(answer3(2,1)) + sum(IM{cycle, 1}(:, B1{q(cycle),1}))) + str2double(answer3(6,1))*sum(IM{cycle, 1}(:, B1{q(cycle),1}));
end
IM{cycle, 1}(((2*l+1)^3+1)/2, ((2*l+1)^3+1)/2) = 0;
IM{cycle, 1}(((2*l+1)^3+1)/2, ((2*l+1)^3+1)/2) = -(str2double(answer3(1,1)) + str2double(answer3(2,1)) + sum(IM{cycle, 1}(:, ((2*l+1)^3+1)/2)) + str2double(answer3(6,1))*sum(IM{cycle, 1}(:, ((2*l+1)^3+1)/2)))
I = eye((2*l+1)^3);
C = zeros((2*l+1)^3, 1);
C(((2*l+1)^3+1)/2) = 1;
[P{cycle,1},D{cycle,1}] = eig(IM{cycle,1})
V{cycle,1} = (P{cycle,1}*expm((D{cycle,1})*t)*(P{cycle,1})^-1)*C
S1(cycle) = sum(V{cycle,1}(B1{q(cycle),1},1)) + V{cycle,1}(((2*l+1)^3+1)/2,1);
end

Best Answer

On first glance, here are some issues. Rewrite the code to prevent accessing cells in dependent or ambiguous manners.
Example 1) Use the same indexing style for all variables linked to the parfor index. For instance:
IM = cell(1, 100);
parfor j = 1:numel(IM)
IM{j} = zeros(1, 10);%You're access IM via IM{j}. Must be same in code.
for k = 1:10
IM{j}(k) = k; %OKAY, same indexing style as IM{j}
IM{1, j}(k) = k; %NOT OKAY. Different indexing IM{1, j} even
%though it's correct without parfor.
end
end
Example 2) Don't modify a sliced variable with different index as the parfor counter, since MATLAB cannot confirm if these are truly independent.
B1 = cell(1, 100);
parfor j = 1:numel(B1)
r(j) = randi(numel(B1), 1);
B1{r(j)} = j; %NOT OKAY. Index to sliced variable B1 differs from
%parfor loop counter variable j. Conflict arises when
%multiple jobs tries to write to same cell in B1.
end