Parfor sliced variable error

2 次查看(过去 30 天)
Dear Matlab,
I have an issue with sliced variable declaration in Matlab.
The below code works perfectly fine
Block - 1
x=zeros(2,2,10);
for i = 1:10
x(:,:,i)= [i,3*i;5*i,7*i];
end
parfor i = 1:10
x(:,:,i)= [i,2*i;4*i,6*i];
end
disp(x)
In my main code, original for loop implementation works as expected
Block - 2
%Pre initialising par for loop variables
modes_points=zeros(max_iter,length(data_sort(1,:)),length(data_sort(:,1)));
%convert mat 2 cell
modes_points = mat2cell(modes_points,max_iter,length(data_sort(1,:)),length(data_sort(:,1)));
for k = 1: n
%Pre initialising par for loop variables
mode=zeros(max_iter,length(data_sort(1,:)));
if(k*Range <= length(data_sort(1,:)))
%performing first iteration
mode = FindMode(data_sort,1+(k-1)*Range,R,max_iter);
modes_points(: ,:,1+(k-1)*Range) = zeros([size(mode)]);
However, if I implement the above with parfor loop, I get an error.
Block-3
%Pre initialising par for loop variables
modes_points=zeros(max_iter,length(data_sort(1,:)),length(data_sort(:,1)));
%convert mat 2 cell
modes_points = mat2cell(modes_points,max_iter,length(data_sort(1,:)),length(data_sort(:,1)));
parfor k = 1: n
%Pre initialising par for loop variables
mode=zeros(max_iter,length(data_sort(1,:)));
if(k*Range <= length(data_sort(1,:)))
%performing first iteration
mode = FindMode(data_sort,1+(k-1)*Range,R,max_iter);
modes_points(: ,:,1+(k-1)*Range) = zeros([size(mode)]);
Output of check code:
L 111 (C 1-6): The PARFOR loop cannot run due to the way variable 'modes_points' is used.
L 125 (C 9-20): Valid indices for 'modes_points' are restricted in PARFOR loops.
But, this code in parfor works
Block-4
%Pre initialising par for loop variables
modes_points=zeros(max_iter,length(data_sort(1,:)),length(data_sort(:,1)));
%convert mat 2 cell
modes_points = mat2cell(modes_points,max_iter,length(data_sort(1,:)),length(data_sort(:,1)));
parfor k = 1: n
%Pre initialising par for loop variables
mode=zeros(max_iter,length(data_sort(1,:)));
if(k*Range <= length(data_sort(1,:)))
%performing first iteration
mode = FindMode(data_sort,1+(k-1)*Range,R,max_iter);
modes_points = cat(3,modes_points,mode);
Runtime performance of block-2(for loop) & block-4(parfor loop with cat) is same.
Please clarify why Block-3 is causing parfor error, although it is similar to Block-1 in terms of sliced variables.

回答(1 个)

Walter Roberson
Walter Roberson 2017-3-6
In Block 4, MATLAB can tell that modes_points is a reduction variable and handles that specially.
In Block 3, with
parfor k = 1: n
modes_points(: ,:,1+(k-1)*Range) = zeros([size(mode)]);
then MATLAB might not be able to prove that 1+(k-1)*Range will definitely be unique or contained within
modes_points=zeros(max_iter,length(data_sort(1,:)),length(data_sort(:,1)));
I would suggest,
modes_points = zeros(max_iter, length(data_sort(1,:)), Range, n);
and in the loop,
modes_points(: ,:, 1, k) = zeros([size(mode)]);
as it is clear that 1+(k-1)*Range is emulating a Range by n matrix.
  1 个评论
Shawn Fernandes
Shawn Fernandes 2017-3-6
编辑:Shawn Fernandes 2017-3-6
Dear Walter Roberson,
The variable 'Range' is a constant, and equals length(data)/n.
In the expression 1+(k-1)*Range, only k=1,2, 3,4....n changes for the parfor loops, remaining all values remain constant.
Variable mode is a 2d matrix. mode_points is a 3D matrix. mode_points is an array of mode, which are 2D matrices.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by