parfor循环不适用于MATLAB中的IF语句 [英] parfor loop won't work with IF statement in MATLAB

查看:408
本文介绍了parfor循环不适用于MATLAB中的IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此代码试图最大化三个变量EEG_tempEEG_ampfMRI_amp,以确定哪种组合产生最高的重叠.因为我有一个可以为任务分配16个核心的集群,所以我认为parfor可以帮助加快分析速度,因为只有十个甚至数十万个组合.

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.

我遇到的问题是highest_overlap变量.如果我在parfor循环外定义它,MATLAB甚至不会让我开始运行分析,因为它是在parfor循环外定义的,但是,如果我不在parfor循环外定义它,则MATLAB崩溃时会崩溃. parfor循环,因为未定义.

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.

任何人都可以提出建议来解决我遇到的问题吗?我认为IF语句可能与它有关,我必须按区别方式定义highest_overlap,因为如果我只是highest_overlap==overlap的话,它就告诉我我误用了highest_overlap多变的.因此,我将采用任何解决方案来使您可以使用此代码.只要是使用它,就可以改变使用最高重叠率的方式,也可以改变整个代码结构.

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.

推荐答案

查看

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.

在您的情况下,当在循环外未定义maximum_overlap时,它是一个临时变量,因此不会为循环的每次迭代保存,这对您的问题不起作用.鉴于MATLAB的逻辑,它必须是一个临时变量,因为您为其分配了变量,即

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;

表示maximum_overlap是一个临时变量.然后,在parfor循环之外定义它时,它会看到临时变量已定义,并且会引发错误.

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.

那么您如何解决呢?最简单的解决方案是使用切片变量.您可以预先分配一个向量,并将Overlap的值保存到该向量,然后进行缩减以实际解决parfor循环之外的maximum_overlap(不是差分).由于大部分计算时间可能花费在其他函数调用上,因此这仍然可以提供良好的加速效果.我不完全明白为什么在那里需要if语句,但是要使用切片变量(如我提到的那样),您需要保存所有EEG_amp_value等内容以对变量进行切片,以恢复解决方案.

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.

由于MATLAB parfor的工作方式,许多解决方案需要做一些事,这些事需要更多的内存来换取加速.我建议的方式将是这样.但是,如果您真的很小心,则可以将maximum_overlap归类为归约变量,然后它会起作用,但是我认为,因为它在if语句中,所以不可能.

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.

这篇关于parfor循环不适用于MATLAB中的IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆