程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 parfor 和选项来并行化代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 parfor 和选项来并行化代码?

开发过程中遇到使用 parfor 和选项来并行化代码的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 parfor 和选项来并行化代码的解决方法建议,希望对你解决使用 parfor 和选项来并行化代码有所启发或帮助;

我有以下 MATLAB 代码:

n_part = 5; %count the participants
options.params.cutoff=1;
options.params.lags=0;% use zero,to set n=0 as in eq 4 in the paper,otherwise you will include historical values in the model (one connectivity matrix per historical value) 
options.params.autocorrelation=0; % to exclude self dependancIEs,since we have already take care of this. This makes the diagonal values equal to zero in the model
options.params.concurrent=1; % must be one to include the concurrent information
options.params.pass_th=0;
options.perc=[0.7 0.3]; % First argument is the partition size to calculate SV,second argument is the size of the test sample
options.rep_svd=10; % how many times to repeat the svd decomposition to maximize out of sample data preDictions
options.rep_model=1; % if number of frames not empty,how many times recalculate the model based on the minimum required frames

parfor i=1:n_part
    display(['Running participant ' num2str(i)])
    options.min_frames=size(signal,1); %how many frames to include in the model
    options.SV=SV; %assign SV to options
end

我想优化 for 循环,因为每次迭代都是独立的。所以,首先,我尝试了这个:

parfor i=1:n_part
    .
    .
    .
end

这给了我这个错误:

Error: The variable options in a parfor cAnnot be classifIEd.

错误出现在这一行:

parfor i=1:n_part

然后我尝试了这个:

parpool(2);
parfor i=1:n_part
    display(['Running participant ' num2str(i)])
    y=TC{i};
    TC_no_AC_LS{i}=remove_autocorrelation(y,n_ar);
    
    frameSV(i).signal=TC_no_AC_LS{i}; % pick the signal with no autocorrelation
    [SV,R]=model_tsvd(signal,options); %calculate the SVD
    
    frameSV(i).min_frames = size(frameSV(i).signal,1);
    frameSV(i).SV = SV;
end

for i = 1 : n_part
    options.min_frames= frameSV(i).min_frames; %how many frames to include in the model
    options.SV = frameSV(i).SV; %assign SV to options
    signal = frameSV(i).signal;
    m(:,:,i) = make_model_tsvd(signal,options); % calculate the model
end

但是得到这个错误:

StarTing parallel pool (parpool) using the 'local' profile ...
Connected to the parallel pool (number of workers: 2).
Error using connectotyPing_example2>(parfor supply)
Undefined function 'frameSV' for input arguments of type 'double'.

Error in connectotyPing_example2 (line 27)
parfor i=1:n_part

有没有办法解决这个问题,让循环的迭代可以并行运行?

解决方法

@H_450_44@

问题是以下两行:

options.min_frames=size(signal,1); %how many frames to include in the model
options.SV=SV; %assign SV to options

Parfor 循环必须确保每次循环迭代都独立于下一次。在上面的代码中,看起来 options 在每次迭代中都会更新,然后在下一次迭代中使用更新后的结构。这不能使用 parfor 来完成,因为技术上不能保证迭代 ii+1 将在同一台机器上完成。

如果 @H_4_5@min_frames 和 SV 不需要成为选项结构的一部分,您可以在每次迭代中以独立于数据的方式存储它们的值:

parfor i=1:n_part
    % ...
    frameSV(i).min_frames = size(signal,1);
    frameSV(i).SV = SV;
end
% Do some calculation with frameSV to get the data you need,after the loop

这将创建一个 1 x n_part 结构数组,其中每个循环迭代的 @H_4_5@min_frames 和 SV 的值是独立存储的。

同样,这都是假设 options 的值不需要在一次迭代中更新并​​在下一次使用。

大佬总结

以上是大佬教程为你收集整理的使用 parfor 和选项来并行化代码全部内容,希望文章能够帮你解决使用 parfor 和选项来并行化代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:使用