我正在试验MATLAB的SPDM。不过,我有以下问题要解决:
我将尽可能地描述我想要的内容:--我希望能够在循环的一组迭代中保存到目前为止的结果,并且希望通过worker.保存结果。
下面是一个最小(非)-Working示例。最后四行应该放在不同的.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发布于 2018-07-03 08:49:21
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);https://stackoverflow.com/questions/51138320
复制相似问题