问题
parfor EEG_temp=10:100;
EEG_temp_filter=filter(ones(1,EEG_temp),1,EEG_amp_vals(eeg_temp_subset,:),[],2);
EEG_vertices=eeg_temp_subset((max(EEG_temp_filter,[],2)==EEG_temp)>0);
connected_EEG_vertices=EEG_vertices((sum(surface.VertConn(EEG_vertices,EEG_vertices))>=2)>0);
if length(connected_EEG_vertices)<5000 && length(connected_EEG_vertices)>500
for fMRI_index=1:length(fMRI_thresholds);
signal_union=union(connected_EEG_vertices,unique(fMRI_Vertices(fMRI_index,:)));
signal_intersection=intersect(connected_EEG_vertices,unique(fMRI_Vertices(fMRI_index,:)));
Overlap=length(signal_intersection)/length(signal_union)*100;
highest_overlap=max(highest_overlap,Overlap)-Overlap;
if highest_overlap==0;
EEG_amp_value=[EEG_amp_value,EEG_amp];
EEG_temp_value=[EEG_temp_value,EEG_temp];
fMRI_amp_value=[fMRI_amp_value,fMRI_thresholds(fMRI_index)/100];
highest_overlap=max(highest_overlap,Overlap);
end
end
end % end of if
% eeg_temp_subset=EEG_vertices;
end %end of EEG_temp
This code is trying to maximize three variables, EEG_temp
, EEG_amp
, and fMRI_amp
to determine which combination produces the highest overlap. Since there is 10s if not hundreds of thousands of combinations I thought parfor would help in speeding the analysis, since I have a cluster that can devote 16 cores to the task.
The problem I am having is with the highest_overlap
variable. If I define it outside of the parfor loop, MATLAB won't even let me start running the analysis because it is defined outside the parfor loop, however, if I don't define it outside the parfor loop MATLAB crashes when it gets to the parfor loop because it isn't defined.
Can anyone offer a suggestion to fix the problem I have? I think the IF statement may have something to do with it, I had to define the highest_overlap
the way it is where it is a differential because if I just did if highest_overlap==overlap
, it told me I was misusing the highest_overlap
variable. So I will take any solutions to get this code to work that you may have. Whether it is a change to the way highest overlap is used or to the entire code structure so long as it runs.
回答1:
Check out MATLAB's classification of parfor variables. Parfor is dumb and will scream if it's not clear what type of variable each one is.
In your case, when highest_overlap is not defined outside of the loop, it's a temporary variable and thus not saved for every iteration of the loop which won't work for your problem. Given MATLAB's logic, it must be a temporary variable because you assign to it, that is
highest_overlap=max(highest_overlap,Overlap)-Overlap;
means highest_overlap is a temporary variable. When you then define it outside of the parfor loop, it sees that the temporary variable is already defined and will throw an error.
So how do you get around it? The easiest solution is to use sliced variables. You can preallocate a vector and save the values of Overlap to the vector and then do the reduction to actually solve for highest_overlap (not as a differential) outside of the parfor loop. Since most of the computing time is probably spent on the other function calls, this should still give a good speedup. I don't exactly see why you need that if statement in there but to use the slice variables as I mentioned you would need to save out all of the EEG_amp_value etc. to slice variables as well to recover the solution.
Because of the way MATLAB's parfor works, many solutions require doing something that requires more memory using in return for the speedup. The way I suggested will be like that. However, if you are really careful, you may be able to get highest_overlap classified as a reduction variable and then it would work, but I think because it's in the if statement it cannot be.
来源:https://stackoverflow.com/questions/28113253/parfor-loop-wont-work-with-if-statement-in-matlab