我一直在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个迭代如何保存out1
和out2
?
(每100次迭代节省工作空间也可以)
提前感谢!
嗯,我不知道这是否应该存在。您将不得不从所有工作人员那里收集数据。如果您只是在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
我的建议是使用parfeval
重写代码,然后可以调用fetchNext
获取输出。请记住,fetchNext返回一个索引,因为结果可能未按顺序排列。因此,基本上调用fetchNext 100次,将结果拼接在一起,保存。