为什么matlab不会并行运行我的简单嵌套for循环?

时间:2021-09-22 20:34:22

I unfortunately have to run a simple, but enormous and 3-times-nested for-loop. The loops populate a 3-dimensional matrix representing the intensity of a function in a 3-space coordinate. (It is necessary to populate the entire matrix because I need it to draw a 3d intensity cloud plot...)

遗憾的是,我必须运行一个简单但巨大的3次嵌套for循环。循环填充表示3空间坐标中的函数强度的3维矩阵。 (有必要填充整个矩阵,因为我需要它来绘制一个3d强度云图......)

I would like to run it in parallel since with my laptop it will take weeks to run sequentially:

我想并行运行它,因为我的笔记本电脑需要数周才能顺序运行:

 % Initialize vars
 c0values = 400:1:600;
 n0values = 1:1:10000;
 phivalues = 0:0.1:2*pi;
 mtxHeight = zeros(numel(c0values),numel(n0values),numel(phivalues));
 % Run over c0
 parfor c0ctr=1:numel(c0values)
     % Run over n0
     for n0ctr=1:numel(n0values)
         % Run over phi
         for phictr=1:numel(phivalues)
             % Sum over ell
             dHeight = 0;
             for ell=1:10
                  dHeight = dHeight + fnToMaximize(mtxObs(ell,:), ...
                      c0values(c0ctr), ...
                      n0values(n0ctr), ...
                      phivalues(phictr));
             end     
             mtxHeight(c0ctr,n0ctr,phictr) = dHeight;
         end
     end
 end

For some reason, Matlab complains that mtxHeight cannot be classified and refuses to run the code in a parallelized for.

出于某种原因,Matlab抱怨mtxHeight无法归类并拒绝在并行化的情况下运行代码。

What am I doing that is not allowed?

我在做什么是不允许的?

1 个解决方案

#1


MATLAB can't handle 3 nested loops with parfor I think. I suggest that you update mtxHeight outside the inner for loop.
Something like this:

我认为MATLAB不能用parfor处理3个嵌套循环。我建议你在内部for循环之外更新mtxHeight。像这样的东西:

 parfor c0ctr=1:numel(c0values)
     % Run over n0
     for n0ctr=1:numel(n0values)
         % Run over phi
         dHeight = zeros(1,numel(phivalues));
         for phictr=1:numel(phivalues)
             % Sum over ell
             for ell=1:10
                  dHeight(phictr) = dHeight(phictr) + fnToMaximize(mtxObs(ell,:), ...
                      c0values(c0ctr), ...
                      n0values(n0ctr), ...
                      phivalues(phictr));
             end     
        end
       mtxHeight(c0ctr,n0ctr,:) = dHeight;      
     end
 end

#1


MATLAB can't handle 3 nested loops with parfor I think. I suggest that you update mtxHeight outside the inner for loop.
Something like this:

我认为MATLAB不能用parfor处理3个嵌套循环。我建议你在内部for循环之外更新mtxHeight。像这样的东西:

 parfor c0ctr=1:numel(c0values)
     % Run over n0
     for n0ctr=1:numel(n0values)
         % Run over phi
         dHeight = zeros(1,numel(phivalues));
         for phictr=1:numel(phivalues)
             % Sum over ell
             for ell=1:10
                  dHeight(phictr) = dHeight(phictr) + fnToMaximize(mtxObs(ell,:), ...
                      c0values(c0ctr), ...
                      n0values(n0ctr), ...
                      phivalues(phictr));
             end     
        end
       mtxHeight(c0ctr,n0ctr,:) = dHeight;      
     end
 end