【问题标题】:Parfor inside for loop循环内的 Parfor
【发布时间】:2015-05-28 18:58:10
【问题描述】:

我有以下简短的 Matlab 代码:

res = cell(10*100,1);
for i = 1:10
    parfor j = 1:100
        idx = ((i-1) * 100) + j;
        res(idx) = 5; 
    end
end

res(idx) = 5; 出现错误。如果我不在 parfor 循环中使用变量 i 它可以工作,但我必须跟踪 i。

我该怎么做?

编辑:我已经解决了。

res = zeros(10*100,1);
for i = 1:10
    temp = zeros(100,1);
    parfor j = 1:100
        a = i;
        temp(j) = data((i-1) * 100) + j);
    end
    res((i-1)*100+1:i*100) = temp;
end

【问题讨论】:

  • 为什么不使用来自1:(10*100) 的单个parfor 循环呢?
  • 这是可能的,但我会在集群上运行它,而且我很可能不允许分配这么多并行循环。有没有可能在 parfor 循环中获取变量 i 的值?
  • 你读过this吗?

标签: matlab parfor


【解决方案1】:

spmd
res = zeros(10*100,1);
for i = 1:10
    for j = 1:100
        idx = ((i-1) * 100) + j;
        res(idx) = 5; 
    end
end
end

解决您的问题?

【讨论】:

  • 如果你愿意,当然可以将 spmd 移动到一个循环内。
猜你喜欢
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2014-06-10
  • 1970-01-01
  • 2017-08-27
  • 2016-09-12
  • 1970-01-01
相关资源
最近更新 更多