The parfor
statement allows only a range of increasing sequential numbers with a step value
of 1.
Adjust your code, as follows:
If your code contains a statement such as parfor 1:2:10
,
rewrite your code so that step intervals of 1 return the expected results.
If your code contains a statement such as parfor i = V
,
ensure that V
resolves to a contiguous series of values in a
vector array, and the step between values is 1.
For example, assume your code is similar to the following, where
<myArray>
is an actual array, such as [2, 6,
7]:
vectorValues = [1, 4, 9, 16, 25, 36, 49];
vectorSequence = <myArray>;
parfor ii = vectorSequence
val = vectorValues(ii);
sprintf('index = %d value = %d\n', ii, val)
end
Change your code as follows, depending on whether the
vectorSequence
array is sparse. Keep in mind that MATLAB
transfers data to workers once per iteration.
If the vectorSequence
array is sparse, use this code, which
results in fewer data transfers than the original code, but incurs the cost of
copying the data into vectorValueSubset
.
vectorValues = [1, 4, 9, 16, 25, 36, 49];
vectorSequence = [2, 6, 7];
vectorValueSubset = vectorValues(vectorSequence);
parfor jj = 1:length(vectorSequence)
val = vectorValueSubset(jj);
sprintf('index = %d value = %d\n', vectorSequence(jj), val)
end
If the vectorSequence
array is not
sparse, use this code, which results in more data transfers than the original
code, but does not incur the cost of copying data. The extra transfers are
computationally lightweight.
vectorValues = [1, 4, 9, 16, 25, 36, 49];
vectorSequence = [2:3, 5:7];
parfor kk = min(vectorSequence):max(vectorSequence)
if (find( vectorSequence == kk))
val = vectorValues(kk);
sprintf('index = %d value = %d\n', kk, val)
end
end