Using parfor loop with fetchNext

조회 수: 5 (최근 30일)
Poul-Erik Hansen
Poul-Erik Hansen 2025년 8월 15일
댓글: Poul-Erik Hansen 2025년 8월 18일
Hi
I have a parallel calculations of eigenvalues using parfeval and fetching of the eigenvalues using fetchNext. The code runs fine as long as I have a serial readout of the eigenvalus and eigenvectors stored in f. However, this is rater slow and I want to make a parallel readout using a parfor loop instead of the for loop in the code below.However, I get the erro unable to classify eigMtr.
L=20;
NumOfSlabs=50;
eigMtr=zeros(2*L,2*NumOfSlabs*L,'single');
W=eigMtr;
W_1=zeros(L,2*NumOfSlabs*L,'single');W_2=W_1;
f(1:NumOfSlabs) = parallel.FevalFuture;
for idx = 1:NumOfSlabs
[CompletedIdx, Wt,Dt] = fetchNext(f);
start=1+(CompletedIdx-1)*2*L;stop=2*CompletedIdx*L;
eigMtr(1:2*L,start:stop)=sqrt(Dt);
W1=Wt(1:L,:);
W2=Wt(L+1:2*L,:);
W_1(:,start:stop)=W1;
W_2(:,start:stop)=W2;
W(1:L,start:stop)=W2;
W(L+1:2*L,start:stop)=W1;
clear Wt Dt W1 W2
end
Thanks
Poul-Erik

답변 (1개)

Walter Roberson
Walter Roberson 2025년 8월 15일
For all variables output from parfor, the indices must be a mix of constants and expressions involving constants defined simply before the parfor starts, together with simple expressions involving the parfor index variable.
Your code does not index by the parfor index variable; instead it tries to index by the computed start and stop which are not constants.
There is no way for parfor to prove that each portion of the matrix will be written to at most once, so it bans the operation.
You might (somehow) know that the returned CompletedIdx are unique positive integers, but there is no way for parfor to know it.
  댓글 수: 1
Poul-Erik Hansen
Poul-Erik Hansen 2025년 8월 18일
I thought that I had a workaround with the following code, that is accepted by parfor. However I get this error message >>fetchNext unable to operate on Futures in state 'unavailable'<<.
This error is strange since f(1:NumOfSlabs) = parallel.FevalFuture; is avaiable. The values returned in f is found in a previous parfeval loop.
eigM=zeros(NumOfSlabs,2*L,2*L,'single');
W1=zeros(NumOfSlabs,L,2*L,'single');W2=W1;
IdxLoop=zeros(NumOfSlabs,1,'single');
RealLoopIdx=IdxLoop;
W=zeros(2*L,2*NumOfSlabs*L,'single');
parfor idx = 1:NumOfSlabs
[CompletedIdx, Wt,Dt] = fetchNext(f);
eigM(idx,:,:)=sqrt(Dt);
IdxLoop(idx)=idx;
RealLoopIdx(idx)=CompletedIdx;
W1(idx,:,:)=Wt(1:L,:);
W2(idx,:,:)=Wt(L+1:2*L,:);
end

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by