How can I classify a variable that takes a path and inputs it in a function in parfor

1 view (last 30 days)
Hey guys,
I am really new to matlab and I am currently trying to run my code in parallel. The error that it generates is
Error: The variable file in a parfor cannot be classified.
and my code is the following:
names=importdata('/home/tkhaleeq/Downloads/Hermes/Hermes/names_for_infile_andscore.txt');
path.i='/home/tkhaleeq/Downloads/Hermes/Hermes/infiles/';
path.s='/home/tkhaleeq/Downloads/Hermes/Hermes/scores/';
path.o='/home/tkhaleeq/Linktoworkserver/Cupid-v1.0/output/';
parfor i=1:length(names)
%disp(length(names));
bet=names(i);
file.i=strcat(path.i, bet);
file.s=strcat(path.s, bet);
file.o=strcat(path.o, bet);
f.i = char(file.i);
f.s = char(file.s);
f.o = char(file.o);
cupidcerna(f.i,f.s,f.o);
end
I basically have to run cupidcerna which is a function and for that it takes three arguments. The first 2 arg take two inputs and the 3rd arg saves the output in the desired location. All of the args are char that retreieves (in case of the first two arguments) files and stores them at a location. variable names contains the file names. For loop works just fine. But when using parfor it gives me an error
Error: The variable file in a parfor cannot be classified.
It points at parfor being a problem but it doesnt make any sense. I tried looking at the overview but that doesnt help. It says everything should be interative: I am assuming I am doing that correctly but other than that I have no clue.
Any suggestions?

Accepted Answer

Jon
Jon on 23 Jul 2015
Every time I use a parfor loop, I create an "inner function" to avoid variable classification problems. Something like
parfor i = 1:100
[outputvar1, outputvar2, ... outputvarN] = function(inputvar1, inputvar2, ... inputvarN);
end
where the function contains all the actual calculations.
In your case, why not just first create the structure variables in a separate loop:
path.i='/home/tkhaleeq/Downloads/Hermes/Hermes/infiles/';
path.s='/home/tkhaleeq/Downloads/Hermes/Hermes/scores/';
path.o='/home/tkhaleeq/Linktoworkserver/Cupid-v1.0/output/';
for i=1:length(names)
%disp(length(names));
f(i).i = char(strcat(path.i, names(i)););
f(i).s = char(strcat(path.s, names(i)));
f(i).o = char(strcat(path.o, names(i)));
end
then call them in the parfor loop:
parfor i = 1:length(names)
cupidcerna(f(i).i,f(i).s,f(i).o);
end
Or you could read all about parfor variable classification and try to work around it...

More Answers (1)

Edric Ellis
Edric Ellis on 24 Jul 2015
@Jon already gave you an answer that should work, but for the record - the reason parfor is upset about file is that it appears to it as though you're using it in an order-dependent way. You and I can see that you are not, but parfor is stricter than that. The problem is that you are assigning to only part of the variable file in each statement - i.e. you might be relying on the value of file.s when you assign into file.i. The way to convince parfor that file is a temporary variable is to assign to the whole variable in one statement. Two ways of doing this are:
parfor ...
% Assign to the whole variable "file" - make it empty
file = [];
file.i = ...;
...
end
or
parfor ...
% Assign to the whole variable "file" - build the struct
% all in one statement
file = struct('i', strcat(...), ...);
...
end

Categories

Find more on Parallel for-Loops (parfor) 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!