Array indexing in parfor loops, why does this simple code not work?

Hi everybody,
I have recently been looking into parfor loops, mostly out of curiosity.
I have lots of loops which follow this syntax:
A = {};
B = [1 3 6];
parfor i = 1:3
A{B(i)} = i;
end
A
I don't understand why this code produces an error, I know that the order in which the calculations may be carried out are random but both A and B are defined outside the loop, the index into both is given by the loop number 'i' and the results can be entered into A randomly as it's an array. So why does this code fail?
Any help would be greatly appreciated,
Rod.
EDIT
My problem is basically that I have input matrices or vectors and for loops which I want to swap to parfor, but often I only want to access certain parts of each input matrix or vector. parfor won't accept a non continuous index like parfor i = [1 3 6], but you also can't use [1 3 6] as an index inside the loop because values might be duplicates.
The solution I've found is the inclusion of an if statement:
A = {};
parfor i = 1:6
if i == 1||i == 3||i == 6
A{i} = i;
end
end
A
Serious coders here will probably laugh at me, but it does what I want.

 Accepted Answer

You're getting the cell index for A out of vector B, however B could be defined with multiple elements having the same value (e.g. B=[1 1 6];), if this would occur multiple parallel instances would be attempting to access the same cell of A at the same time

5 Comments

I don't understand this, unless I'm mistaken i is only ever going to be one number so B(i) should only be one number so a process should only be accessing 'its' cell of A.
I understand that different processes may be trying to access A at the same time, if that is the problem then I can accept that (although it seems silly) but surely the different processes should be accessing different cells of A?
EDIT
I think I see what your saying actually, if B had multiple elements that were the same then the parfor loop would have problems because multiple processes would be accessing the same cell because multiple processes would have the same B(i) value. I still don't see why the above code crashes though because this problem doesn't occur, surely the code should fail if there is a problem, not if there is the possibility of a problem?
If you want to achieve what I think you were going for there though you can work around it like this
B = [1 3 6];
temp={};
parfor i = 1:3
temp{i}=i;
end
A(B)=temp;
which results in
A =
[1] [] [2] [] [] [3]
Thanks for your help, your comment actually helped me figure out a workaround, at least for my code.
Just had a look there, cause I wanted to understand the reason properly too. You wanted to use A as a sliced variable, but it can't be a sliced variable because it doesn't satisfy the characteristics criterion for the form of indexing which states that (from the parfor help file) "Within the list of indices for the variable, exactly one index involves the loop variable" and "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 constant, a simple broadcast variable, colon, or end"
Yea... I can't make heads or tails of the matlab help for parfor, I really need to experiment in Matlab or have people tell me what is wrong with specific examples, in this case I worked out why it wasn't working (with your help) and now I understand parfor a bit better.
Incidentally, I have managed to parfor every possible loop in my code now and shaved off 2-3 seconds per iteration, of which there are typically 80, so that's great, thanks.
My main problem was wanting my parfor loop to run non-continuously i.e. parfor i = [1 4 7 9] this is so that the data remain arranged in a constant manner when I index into an array using 'i' and I can find it later without a problem, parfor can't do this but I can do parfor i = 1:10 with the inclusion of an if statement, of course this means some of the 'workers' will get loops which don't actually require any work, but I'm prepared to make that sacrifice.
I've also found concatenating data inside the loop and then 'unpacking' it when the loop completes helps with a lot of problems.
Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!