【问题标题】:How to save intermediate iterations during SPMD in MATLAB?如何在 MATLAB 中的 SPMD 期间保存中间迭代?
【发布时间】:2018-07-02 14:20:32
【问题描述】:

我正在试验 MATLAB SPDM。但是,我有以下问题需要解决:

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

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

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

我的直觉是我不太了解复合对象的工作原理,尤其是如何将它们保存到“常规”对象(单元格、结构等)中。

% 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

【问题讨论】:

    标签: matlab parallel-processing spmd


    【解决方案1】:

    Composite 仅在 spmd 块之外创建。特别是,您在 spmd 块内定义的变量在该块外以 Composite 的形式存在。当在spmd 块内使用相同的变量时,它会被转换回原始值。像这样:

    spmd
        x = labindex;
    end
    isa(x, 'Composite') % true
    spmd
        isa(x, 'Composite') % false
        isequal(x, labindex) % true
    end
    

    因此,您不应该使用{:} 索引转换output - 它不是Composite。我认为你应该能够简单地使用

    dosave(stem, labindex, output);
    

    【讨论】:

    • 谢谢。我将output 转换为structure,因为函数dosave 不允许保存Composite。因此,选择是 (i) 将 Composite 转换为 structure 或 (ii) 修改 dosave,以便我可以保存 Composite。无论如何,我将不得不将Composite 转换为structure,因为我稍后会使用它。
    • 如果我尝试直接保存输出,我有以下错误:The argument to -STRUCT must be the name of a scalar structure variable.
    • 我已经解决了这个问题,感谢您的意见。我已编辑问题并接受了您的回答。
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 2013-04-15
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多