首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MATLAB中保存SPMD过程中的中间迭代?

如何在MATLAB中保存SPMD过程中的中间迭代?
EN

Stack Overflow用户
提问于 2018-07-02 14:20:32
回答 1查看 269关注 0票数 0

我正在试验MATLAB的SPDM。不过,我有以下问题要解决:

  • 我正在运行一个相当长的算法,我想保存沿途的进展,以防电源被切断,有人拔出电源插头或内存错误。
  • 循环有144个迭代,每个迭代大约需要30分钟才能完成=> 72h。在这段时间内可能会出现很多问题。当然,我的机器上有一个分布式计算工具箱。这台计算机有4个物理核心。我运行MATLAB R2016a。
  • 我真的不想使用parfor循环,因为我连接结果,并且跨迭代具有依赖性。我认为SPMD是我想做的最好的选择。

我将尽可能地描述我想要的内容:--我希望能够在循环的一组迭代中保存到目前为止的结果,并且希望通过worker.保存结果。

下面是一个最小(非)-Working示例。最后四行应该放在不同的.m文件中。这个函数在parfor循环中调用,允许保存中间迭代。它在我使用的其他例程中正常工作。该错误位于第45行(output_save)。不知何故,我想“将”复合对象“拉”成“常规”对象(单元格/结构)。

我的预感是,我不太明白复合对象是如何工作的,尤其是它们如何被保存到“常规”对象(单元格、结构等)中。

代码语言:javascript
复制
% SPMD MWE

% Clear necessary things
clear output output2 output_temp iter kk


% Useful thing that will be used later on
Rorder=perms(1:4);

% Stem of the file to save the data to
stem='MWE_MATLAB_spmd';

% Create empty cells where the results of the kk loop will be stored
output1{1,1}=[];
output2{1,2}=[];

% Start the parpool
poolobj=gcp;

% Define which worker/lab will do which iteration
iterperworker=ceil(size(Rorder,1)/poolobj.NumWorkers);
for i=1:poolobj.NumWorkers
    if i<poolobj.NumWorkers
        itertodo{1,i}=1+(iterperworker)*(i-1):iterperworker*i;
    else
        itertodo{1,i}=1+(iterperworker)*(i-1):size(Rorder,1);
    end
end

%Start the spmd
% try
    spmd
        iter=1;
        for kk=itertodo{1,labindex}
            % Print which iteration is done at the moment
            fprintf('\n');
            fprintf('Ordering %d/%d \r',kk,size(Rorder,1));

            for j=1:size(Rorder,2)
            output_temp(1,j)=Rorder(kk,j).^j; % just to populate a structure
            end
            output.output1{1,1}=cat(2,output.output1{1,1},output_temp);  % Concatenate the results
            output.output2{1,2}=cat(2,output.output1{1,2},0.5*output_temp);  % Concatenate the results

            labindex_save=labindex;

            if mod(iter,2)==0
                output2.output=output; % manually put output in a structure
                dosave(stem,labindex_save,output2); % Calls the function that allows me to save in parallel computing
                end
                iter=iter+1;
            end
        end
    % catch me
    % end


    % Function to paste in another m-file
    % function dosave(stem,i,vars)
    %     save(sprintf([stem '%d.mat'],i),'-struct','vars')
    % end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-03 08:49:21

Composite只在spmd块之外创建。特别是,您在spmd块中定义的变量作为该块之外的Composite存在。当在spmd块中使用相同的变量时,它将被转换回原始值。就像这样:

代码语言:javascript
复制
spmd
    x = labindex;
end
isa(x, 'Composite') % true
spmd
    isa(x, 'Composite') % false
    isequal(x, labindex) % true
end

因此,您不应该使用output索引来转换{:} --它不是Composite。我觉得你应该能用

代码语言:javascript
复制
dosave(stem, labindex, output);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51138320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档