필터 지우기
필터 지우기

Just a Beginner's 'parfor' confusion

조회 수: 2 (최근 30일)
Amnah
Amnah 2014년 2월 8일
답변: Arslan Ahmad 2014년 2월 9일
I'm trying to execute this code and constantly getting the message that 'variable jVals is indexed but not sliced'. Can anyone kindly help
iVal=5:0.2:5.4;
jVal=2:0.5:2.5;
iLen=length(iVal)
jLen=length(jVal);
matrix=zeros(iLen,jLen);
parfor i=1:iLen
dummy=zeros(1,jLen);
for j=1:jLen
dummy(j)=jVal(j); %/// This line is in ERROR
end
matrix(i,:)=dummy;
end

채택된 답변

Matt J
Matt J 2014년 2월 8일
편집: Matt J 2014년 2월 8일
In your case, it will go away if you modify the loop as
parfor i=1:iLen
matrix(i,:)=jVal;
end
It's not an error, but rather a warning that you might not have efficient code. MATLAB sees that you are indexing a matrix variable inside the loop using another variable j. This usually means the matrix variable is a large array. If it were a small array, you would usually just index it with known, fixed constants, like jVal(1). If it is a large array, you usually want to find a way to have parfor send to the worker only a slice of the array that the worker will use, instead of broadcasting the whole array to every worker.
  댓글 수: 8
Amnah
Amnah 2014년 2월 9일
Now it is not saying that rvals is not sliced.. but it is still taking a lot of time in parallel processing as compared to sequential processing.. probably my code is inefficient or I don't know.. Thanks anyway Matt :)
Matt J
Matt J 2014년 2월 9일
Are you running inside a script or a function? Try both.

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

추가 답변 (1개)

Arslan Ahmad
Arslan Ahmad 2014년 2월 9일
It is better not to use another index inside the "parfor" use only the index of parfor in your case it's better to use i instead of j for more information I would like to refer you to MATLAB parfor documentation for more information and so for your can try this code for solving your problem. I modified it for you, it tooks only 0.194221 seconds on two matlab workers. If you find it useful than please accept the answer.
tic
iVal=5:0.2:5.4;
jVal=2:0.5:2.5;
iLen=length(iVal)
jLen=length(jVal);
matrix=zeros(iLen,jLen);
parfor i=1:iLen
jVal=2:0.5:2.5;
jLen=length(jVal);
dummy=zeros(1,jLen);
j=1:jLen
dummy(j)=jVal(j); %/// This line is in ERROR
matrix(i,:)=dummy;
end

카테고리

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