Help with using parfor (possible indexing problem)
4 ビュー (過去 30 日間)
古いコメントを表示
I've created a program which essentially is a block processing version of the xcorr command enabling you to use the command without getting out of memory errors when using large data.
My data (the input and template file) comes from two mat files on desktop. Each block (of a 1D array) of the input is read in, processed (Applying xcorr command to produce 1D array of length 2M-1), and the output (this 2M-1 length array) is saved back into another mat file via block processing
(What makes this trickier is that due to the concept of how xcorr works, to get the correct output, all the the blocks need to overlap by ~50% where all the elements from the second block are added on to the elements they overlap from the first, and so on).
My run time was approximately 15 hours however and I really want to try and shorten this down (ironically, my computer can handle using xcorr without block processing and it takes several minutes to complete).
I understand that for parfor to work, I need each iteration to be independent of each other (because they are not processed in order). I also need to make sure my indexing is simplistic (which is where my problem lies). Below is a slightly simplified version of my code which processes all the blocks apart from the first and last (which are done separately without a loop).
inputData=matfile('DataToInput.mat'); %file to test for correlation
template=matfile('template.mat'); %the template used for correlation
r=0; %initialise the saving
save('output.mat','r','=v7.3') %save new mat file for output of program
m=matfile('output.mat','Writable',true);
x=template.x;
len1=length(inputData.y);
len2=length(template.x);
for N=2:floor(len1/len2)
y=inputData.y((N-1)*len2+1:N*len2,1);
r=xcorr(y,x);
m.r=[m.r; zeros(len2,1)];
m.r(end-(2*len2-2):end,1)=m.r(end-(2*len2-2):end,1) +r;
end
if I replace "for" with "parfor" I get the following issues:
I get an orange warning for y=inputData.y((N-1)*len2+1:N*len2,1); stating: the entire array structure 'inputData' is a broadcast variable.This might result in unnecessary communication overhead.
and I get a red warning for m.r=[m.r; zeros(len2,1)]; stating: Valid indices for 'm' are restricted in PARFOR loops
I've tried preallocating m (I know the final length of m) outside the loop (which required changing the indexing of the last line in the loop, because of the "end" bit) but I'll still get a red warning for that last line.
Any help?
0 件のコメント
回答 (1 件)
Edric Ellis
2015 年 4 月 20 日
編集済み: Edric Ellis
2015 年 4 月 24 日
I would suggest making a few changes to your code that should eliminate all the code analyzer warnings / variable classification errors. Firstly, parfor unfortunately isn't always great at understanding indexing expressions into fields of structs, so I would avoid using them for input and output. Secondly, your parfor loop appears to be accessing inputData.y in a conceptually "sliced" manner, but unfortunately your indexing expressions are too complicate for parfor to understand. Your output m.r is a problem because you're updating elements as you go along. You need to convert that to a "reduction" output. Something like this:
num_iter = floor(len1/len2) - 1;
inputData_y = reshape(inputData.y, len2, []);
m_r = zeros(len2 * (1+num_iter), 1);
parfor N = 2:floor(len1/len2)
y = inputData_y(:, N);
tmp = zeros(len2 * (1+num_iter),1);
tmp(((N-2)*len2 + 2):N*len2) = xcorr(y, x);
m_r = m_r + tmp;
end
3 件のコメント
Edric Ellis
2015 年 4 月 24 日
As I said in my original answer, unfortunately parfor isn't great at understanding indexing expressions into struct types. So, you should try using simple variables instead of structs for inputData and m.
参考
カテゴリ
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!