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.
0 件のコメント
回答 (1 件)
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.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!