I have MATLAB code which I'm trying to parallelize with a simple change from "for" to "parfor." I'm unable to do so because of an error I'm receiving on the variable "votes" which states:
Valid indices for 'votes' are restricted in PARFOR loops.
Explanation:
For MATLAB to execute parfor loops efficiently, the amount of data sent to the MATLAB workers must be minimal. One of the ways MATLAB achieves this is by restricting the way variables can be indexed in parfor iterations. The indicated variable is indexed in a way that is incompatible with parfor. Suggested Action: Fix the indexing. For a description of the indexing restrictions, see “Sliced Variables” in the Parallel Computing Toolbox documentation.
Below is my code:
votes = zeros(num_layers, size(spikes, 1), size(SVMs_layer1, 1));
predDir = zeros(size(spikes, 1), 1);
chronProb = zeros([num_layers, size(chronDists)]);
for i = 1:num_layers
switch i
case 1
B = B1;
k_elem_temp = k_elem1;
rest_elem_temp = rest_elem1;
case 2
B = B2;
k_elem_temp = k_elem2;
rest_elem_temp = rest_elem2;
case 3
B = B3;
k_elem_temp = k_elem3;
rest_elem_temp = rest_elem3;
end
for j = 1:length(chronPred)
if chronDists(i, j, :) ~= 0
parfor k = 1:8
chronProb(i, j, k) = logistic(B{k}(1) + chronDists(i, j, k).*(B{k}(2)));
votes(i, j, k_elem_temp(k, :)) = votes(i, j, k_elem_temp(k, :)) + chronProb(i, j, k)/num_k(i)/num_layers;
votes(i, j, rest_elem_temp(k, :)) = votes(i, j, rest_elem_temp(k, :)) + (1 - chronProb(i, j, k))/num_rest(i)/num_layers;
end
end
end
end
Do you have any suggestions as to how I could adjust my code so that it runs in parallel?
Thank you!