How to iterate over parfor

Hi everyone.
I need to apply some heavy signal processing to a third-order tensor (typcal set of data coming from thermography). The data has the structure of pixel-pixel-time.
I wanto to use the parfor, but I am struggling in implementing a for loop.
I am aware that this syntax does not work:
%% mock data
input signal = zeros(1,3000);
output_raw_signal = zeros(512,640,3000);
AA_filtered = []; %% no need to prealloc I know
parfor i=1:512
for j=1:640
AA_filtered(i,j,:) = xcorr(squeeze(output_raw_signal(i,j,:)),.input_signal) %% cross correlation for each pixel in time
end
end
If I run this, I get the error "The variable in a parfor cannot be classified"
But what is a possible solution?
Greetings.
Luca

1 Comment

Hi. You are absolutely right about the third dimension of the output tensor. I have corrected it now.
why do you think the syntax does not work? what error are you getting?
I am getting "variable in a The variable in a parfor cannot be classified". Which should be expected since in a parfor with a nested for loop, the variables running one loop should not communicate with the other ones. They should be independent.

Sign in to comment.

 Accepted Answer

Ridwan Alam
Ridwan Alam on 10 Dec 2019
Edited: Ridwan Alam on 11 Dec 2019
I am not sure how much this would help, as I couldn't reproduce your error. Please let me know how it goes.
input_signal = zeros(1,3000);
output_raw_signal = zeros(512,640,3000);
AA_filtered = cell(512,1);
parfor i=1:512
tempvar = [];
for j=1:640
newvar = xcorr(squeeze(output_raw_signal(i,j,:)),input_signal);
newvar = reshape(newvar,1,[]);
tempvar = [tempvar;newvar];
end
AA_filtered{i} = tempvar;
end
even this ran fine:
input_signal = zeros(1,3000);
output_raw_signal = zeros(512,640,3000);
AA_filtered = [];
parfor i=1:512
tempvar = [];
for j=1:640
newvar = xcorr(squeeze(output_raw_signal(i,j,:)),input_signal);
newvar = reshape(newvar,1,[]);
tempvar = [tempvar;newvar];
end
AA_filtered(i,:,:) = tempvar;
end

7 Comments

Hi. The second one works! I am about to test the first option you have written
For some reason your solution works for the case before, but in the following situation I get an error.
In one .m code I am defining the following command:
hyper_cube_filtered_lowpass = lowPassHyperCube(,LP); %% LP is a filter object
%% Then I define the following code in another external function that should process each signal from each pixel in time. The function receives 2 inputs: inputdata which is a 3rd order tensor and filterVar which is filter object
function [outputData] = lowPassHyperCube(inputData,filterVar)
outputData = [];
parfor i=1:size(inputData,1)
tempvar = [];
for j=1:size(inputData,2)
newvar = filter(filterVar,squeeze(inputData(i,j,:))-mean(squeeze(inputData(i,j,:))))+mean(squeeze(inputData(i,j,:)));
newvar = reshape(newvar,1,[]);
tempvar = [tempvar;newvar];
end
outputData(i,:,:) = tempvar;
disp(['Iteration n.', num2str(i)])
end
end
But if I run it I get the following error:
Error using lowPassHyperCube>(parfor body) (line 9)
An UndefinedFunction error was thrown on the workers for 'filter'. This might be because the file containing 'filter' is not
accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation
for 'parallel.Pool/addAttachedFiles' for more details.
Error in lowPassHyperCube (line 6)
parfor i=1:size(inputData,1)
Caused by:
Undefined function 'filter' for input arguments of type 'struct'.
Any idea why?
Let me know you can try this out. Attached you have the mat file containing the filter object I have created but you shouldn't need any specific data, you can just try with fake ones.
Let me know
Where is B coming from?
sorry typo from another code. It's "filterVar" I have already corrected my comment.
Ridwan Alam
Ridwan Alam on 11 Dec 2019
Edited: Ridwan Alam on 11 Dec 2019
I don't think it's an issue of parfor, rather how you are calling filter(). The arguments are not compatible. Your LP is a filter object, as far as I understood, not a vector as required by filter(). As pointed out by Matlab:
Undefined function 'filter' for input arguments of type 'struct'.
Btw, you can open a new question for debugging that part, that way your question will get more visibility.
However: when there is an object transferred whose class is not known to the workers, it appears as a struct.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 11 Dec 2019
Edited: Walter Roberson on 11 Dec 2019
%% mock data
input_signal = zeros(1,3000);
output_raw_signal = zeros(512,640,3000);
AA_filtered = []; %% no need to prealloc I know
parfor i=1:512
output_raw_signal_i = output_raw_signal(i,:,:);
for j=1:640
AA_filtered_i(j,:) = xcorr(output_raw_signal_i(j,:).', input_signal) %% cross correlation for each pixel in time
end
AA_filtered(i, :, :) = AA_filtered_i;
end

2 Comments

Hi
This option gives me the following error in the workspace.
Error: The variable AA_filtered in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".
Try removing the initial assignment to AA_filtered

Sign in to comment.

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!