problem in using parralel computing toolbox

조회 수: 1 (최근 30일)
Abhinav
Abhinav 2017년 11월 6일
댓글: OCDER 2017년 11월 6일
I want to speed up my computations using parfor. I am using the code written below,
l=31:1:1000; % length-scale
sig2=6.5:0.1:10; % noise-variance
sigf2=1.3:0.1:10; % signal-variance
L=zeros(length(l),length(sig2),length(sigf2));
par=zeros(length(l),length(sig2),length(sigf2));
lsig2=length(sig2);
lsigf2=length(sigf2);
% cross-validation
parfor i=1:length(l)
for j=1:lsig2
for k=1:lsigf2
par(i,j,k)=[l(i),sig2(j),sigf2(k)];
L(i,j,k)=kcrossval(Xtrain,ytrain,M,l(i),sig2(j),sigf2(k));
end
end
end
but it is not working. I get error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts.
When I remove the code
par(i,j,k)=[l(i),sig2(j),sigf2(k)];
it works but gives the following warning:
the entire array or struct 'sig2' is a broadcast variable. This might result in unnecessary communication overhead.
I have read the documentation but not able to find a solution. Please suggest the solutions.

채택된 답변

OCDER
OCDER 2017년 11월 6일
편집: OCDER 2017년 11월 6일
You are assigning 3 values, l(i),sig2(j),sigf2(k), into one value at par(i, j, k), which is not possible.
par(i,j,k)=[l(i),sig2(j),sigf2(k)];
But you don't really need this, since you know what values were used. If you want this, change par into a cell array instead, and do:
par{i,j,k} = ...;
The broadcast variable warning is simply warning you that a constant vector is being passed to every worker every time, hence could be slow (especially if they're large matrices). In your case, sig2 and sigf2 are pretty small, so don't worry about this too much. This warning appears when you are accessing a small segment of a vector per loop. Notice that Xtrain and ytrain are not give you warning, because parfor knows these are broadcast variables (you aren't accessing " Xtrain(j)", but rather the full Xtrain vector).
To suppress these warnings, you could explicitly tell Matlab these are broadcast variables via another temporary variable like this:
parfor i=1:length(l)
sig2Temp = sig2;
sigf2Temp = sigf2;
for j=1:lsig2
for k=1:lsigf2
L(i,j,k)=kcrossval(Xtrain,ytrain,M,l(i),sig2Temp(j),sigf2Temp(k));
end
end
end
  댓글 수: 2
Abhinav
Abhinav 2017년 11월 6일
편집: Abhinav 2017년 11월 6일
Thanks a lot for a nice explanation!
OCDER
OCDER 2017년 11월 6일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by