parfor in matlab. sliced variable and nested loop

邮差的信 提交于 2019-11-26 14:47:22

问题


I did my best to follow the documentation of the parallel toolbox, but still I could not avoid the problem of reusing array that was indexed in a nested loop. The problem is with variable node

parfor i=1:nX
   for j=1:nY


    [ind,dist]=findInCircle(node(i,j,:), part,r);

    UV=calcVelocity(part(ind,:), dist,node(i,j,:)) ;


    %here matlab complains that node is not indexed properly
    node(i,j,3)= UV(1);
    node(i,j,4)= UV(2);
    node(i,j,5)= UV(3);



   end


end

I do not use the array outside of the nested loop, the indexing is also according to the rule. Did I miss another parfor restriction?


回答1:


According to the documentation you can not use different indices like you did:

Within the first-level parenthesis or braces, the list of indices is the same for all occurrences of a given variable.

A simple workaround is possible:

parfor i=1:nX
   nodeSlice=node(i,:,:)

   for j=1:nY


    [ind,dist]=findInCircle(nodeSlice(j,:), part,r);

    UV=calcVelocity(part(ind,:), dist,nodeSlice(j,:)) ;


    %here matlab complains that node is not indexed properly
    nodeSlice(j,3)= UV(1);
    nodeSlice(j,4)= UV(2);
    nodeSlice(j,5)= UV(3);



   end
   node(i,:,:)=nodeSlice;

end

Get a slice from the matrix which contains all indices, work with it and then return it.



来源:https://stackoverflow.com/questions/25531559/parfor-in-matlab-sliced-variable-and-nested-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!