Error: The variable in a parfor cannot be classified.

1 view (last 30 days)
In the code below I get an error: The variable temp_act in a parfor cannot be classified.See Parallel for Loops in MATLAB, "Overview".
parfor i=1:size(path_table,1)
[temp_path_table(i,:),node_act_1,node_act_2]=path_automatron(path_table(i,:),node_table{path_table{i,2}(1),path_table{i,2}(2),7},node_table{path_table{i,3}(1),path_table{i,3}(2),7});
temp_act{temp_path{i,2}(1),temp_path{i,2}(2)}=node_act_1;
temp_act{temp_path{i,3}(1),temp_path{i,3}(2)}=node_act_2;
end

Accepted Answer

Walter Roberson
Walter Roberson on 22 Aug 2018
A) Roughly speaking: you can only have one assignment statement for each output variable that is indexed by the parfor loop counter. The work-around to this is generally:
  1. extract all of the entries to be modified into temporary variables
  2. do any modifications to the temporary variables
  3. use a signal assignment statement with a range to write the temporary variables into the output variable
For example, instead of
t(i, 1) = t(i,1) * 2;
t(i, 2) = t(i,2) - 1;
You could use
t(i, 1:2) = [t(i,1) *2, t(i,2) - 1];
B) The output location must be calculated by simple arithmetic based upon the parfor index, so that the code should be able to prove that each output location is unique.
In your code, MATLAB has no reason to know that (temp_path{i,2}(1),temp_path{i,2}(2)) is not the same location as (temp_path{i,3}(1),temp_path{i,3}(2)), and no reason to know that (temp_path{i,2}(1),temp_path{i,2}(2)) will definitely be unique over all i values.
Cases like yours are difficult to handle in parallel. Usually about the best you can do is write to an output variable in order, like
temp_act_t(i, 1:2) = {node_act_1, node_act_2};
and then outside the parfor have a serial loop that drops them into the appropriate location in temp_act . The person coding the serial loop would have to worry about what to do if there are duplicate output locations, but could implement whatever strategy was appropriate (e.g., take first, take last, take max, raise an error.)

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!