如何在PARFOR循环中定期保存矩阵?

问题描述 投票:1回答:2

我一直在Matlab中使用并行循环(Parfor),这是我的代码的一部分。 (应该运行几天,所以我想定期保存输出。

  parfor i = 1:N_boot

                      ...

           out1(i,:) = result1;
           out2(i,:) = result2;

       if mod(i,100) == 0
        % Here, I want to save out1 and out2 (export with .mat file) 

       end


  end

每100个迭代如何保存out1out2

(每100次迭代节省工作空间也可以)

提前感谢!

matlab parfor
2个回答
0
投票

嗯,我不知道这是否应该存在。您将不得不从所有工作人员那里收集数据。如果您只是在for周围包裹一个普通的parfor循环,那么一种快速的解决方法是:

stp = 100;
for j = 1:stp:N_boost/stp
   parfor i = j:stp

                      ...

           out1(i,:) = result1;
           out2(i,:) = result2;

       if mod(i,100) == 0
        % Here, I want to save out1 and out2 (export with .mat file) 

       end
  end
end

0
投票

我的建议是使用parfeval重写代码,然后可以调用fetchNext获取输出。请记住,fetchNext返回一个索引,因为结果可能未按顺序排列。因此,基本上调用fetchNext 100次,将结果拼接在一起,保存。

© www.soinside.com 2019 - 2024. All rights reserved.