Reputation: 1234
I'm trying to parallelize a loop in MATLAB and am getting an error which states, "Valid indices for 'X_train' are restricted in PARFOR loops." My code is below:
parfor c = 1:num_channels
% sum_n_train calculated here
for n = 1:sum_n_train
bin_n = bin(n);
Xmj = X_train(bin_n, :);
% some calculations happen in between
X_train(bin_n,:) = Xmj;
X_train(bin_n, p) = X_train(bin_n, p) + 1;
z_train(n)= zind;
end
z_train_cell{c} = z_train;
end
X_train is an n by p matrix and Xmj is a 1 by p vector. The error is for X_train. From reading the documentation, I see that the indexing for every variable must be fixed within the parfor loop. Even when I comment out the line X_train(bin_n, p) = X_train(bin_n, p) + 1;
(which has a different indexing of X_train than the other two lines), however, I still get the error. Could someone please explain why and how I can work around it?
Upvotes: 0
Views: 460
Reputation: 19132
I gave an answer to a similar problem over here. MATLAB doesn't know what to do with
X_train(bin_n,:)
X_train(bin_n, p) = X_train(bin_n, p) + 1;
since it needs to split up X_train to the right processors before the loop, but it doesn't know how to split until you calculate the indices bin_n. To put it succinctly, you can't save to variable indices.
If the bins do not overlap, then you need to hard code those bin indices with constant values. If the bins do overlap, then it loops like the solutions depend on the order that it is run (i.e. you update X_train at the bin_n locations, but use it via Xmj to calculate other things in the part left out) which means the problem in its current form cannot be solved via parfor.
Upvotes: 0
Reputation: 21563
I can't try it myself, but it seems that you are trying to write to the same thing in several instances of the parfor
loop.
It seems like
X_train(bin_n,:)=Xmx
Will occur each time the parfor is entered, and specifically: X_train(1,:)
gets overwritten a lot of times as @Daniel mentioned.
Here is what you can do:
Example:
`X_train(c,bin_n,:)`
Afterwards uncomment again and run the code! If the problem still occurs you may need to add a dimension to more variables.
Upvotes: 1