Slice Broadcast Variables in Parfoor Loop - Index Problems
显示 更早的评论
I tried to speed up the following code by using a parfor loop
parfor i = 1:Nr
[F_OIS] = mcSimNr(tg,dt,tgT,tr,q,maxTenor,H,F0_OIS,T);
for j = 1:size(MatTenor,1)
VswapMC(i,j) = Swap(M,tr,K(j),w,F_OIS(:,indextgT(j)),B0,indexs(j),indexe(j),maxTenor);
end
end
where K, indextgT, indexs and indexe have dimensions size(MatTenor,1)x1. In the nested forloop of a particular loop of the parfor loop, all the elements of those arrays are used.
I get a warning message in Matlab saying that those variables are broadcast variables and a suggestion that I should try to slice them.
However, is this necessary in this case since, in my opinion, the whole arrays have to be sent to each worker since in each iteration of the parfor loop they are used as a whole? If not, does anybody have any suggestions how I could slice them?
Any help is appreciated. Thanks in advance.
回答(1 个)
Walter Roberson
2016-7-2
I would expect that code to error. The adjusted code would be more like
MTLen = size(MatTenor,1);
VSclass = class(VswapMC);
parfor i = 1:Nr
[F_OIS] = mcSimNr(tg,dt,tgT,tr,q,maxTenor,H,F0_OIS,T);
Vswapj = zeros(1, MTLen, VSclass);
for j = 1 : MTLen
Vswapj(1,j) = Swap(M, tr, K(j), w, F_OIS(:,indextgT(j)), B0, indexs(j), indexe(j), maxTenor);
end
VswapMC(i, :) = Vswapj;
end
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!