1
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.

5
  • Hi and welcome to SO! I just edited your question to improve the formatting – please do this yourself in future questions. Legible text makes it easier to answer. :-) Commented Jan 23, 2015 at 16:05
  • I don't have a solution for your problem, but I would strongly suggest not to use a grid search (with or without parfor) but an optimization function. Have a look at fminsearch and friends. Commented Jan 23, 2015 at 16:06
  • 1
    It is not possible because you have a dependency between the iterations. The second iteration of the parfor requires the result of the first one to evaluate highest_overlap. Where are you changing EEG_amp in your code, I don't really get how it works. Commented Jan 23, 2015 at 16:31
  • the EEG_amp is it's own for loop which everything I have above is nested within. But if MATLAB would let me have highest_overlap defined going into the parfor loop there would not be a dependancy, since for all iterations there would be a value defined, and at which point it doesn't matter how many iterations have been run, so long as the current one is larger than the previous one. Commented Jan 23, 2015 at 19:07
  • @ a Donda I can't use fminsearch, because fminsearch doesn't allow intergers to be used. As a result it sits there making changes to the temporal threshold on the oreder of .01 or .001, which for this dataset doesn't make sense, and as a result the optimization ends up being whatever the initial conditions are +- some very very small changes. Commented Jan 23, 2015 at 19:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.