Hi
I am using parfeval to calculate the eigenvalues of a matrix M with typical dimension of 1500x1500x100. The parfeval evaluations is slow since I read the output just after the evaluation of each matrix M(:,:,k) k=1,..,100. I want to store all the eigenvalues and eigenvectores in the variabel f and read the output after finishing the parfeval evaluation.
Current code:
eigMtr=complex(zeros(2*L,2*NumOfSlabs*L));
W=eigMtr;
W_1=complex(zeros(L,2*NumOfSlabs*L));W_2=W_1;
for k=1:NumOfSlabs
f=parfeval(@eig,3,M(:,:,k));
[Wt,Dt] = fetchOutputs(f);
start=1+(k-1)*2*L;stop=2*k*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;
end
I want to do something like this
for k=1:NumOfSlabs
f=parfeval(@eig,3,M(:,:,k));
end
for k=1:NumOfSlabs
[idx,Wt,Dt] = fetchNext(f)
start=1+(k-1)*2*L;stop=2*k*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;
end
Thanks for your help
Poul-Erik

 Réponse acceptée

Walter Roberson
Walter Roberson le 11 Avr 2025

0 votes

Note the correction to the number of output arguements parameter in the parfeval() call
f = cell(NumOfSlabs, 1);
for k=1:NumOfSlabs
f{k} = parfeval(@eig,2,M(:,:,k));
end
wait(f{1});
for k=1:NumOfSlabs
[Wt,Dt] = fetchOutputs(f{k});
start=1+(k-1)*2*L; stop=2*k*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;
end

8 commentaires

Poul-Erik Hansen
Poul-Erik Hansen le 12 Avr 2025
Thanks for the answer. How can you be sure that fetchOutputs preserve the calculation order (k=1,..., NumOfSlabs)?. Parfeval performes the calculations asynchronous. Isn't it better to uses fetchNext?
FethNext returns the evaluation index, idx, that make it possible to write
start=1+(idx-1)*2*L
stop=2*idx*L
....
Each parfeval() generates a singular future. We write those futures in cell arrays, so when we reference the cell array at a particular location, it is the location of a single future.
I did things this way to duck the question of whether futures could be stored using standard array indexing, which was something I could not find readily documented. However, looking at https://www.mathworks.com/help/matlab/ref/parallel.future.aftereach.html#mw_2abc4dc5-73a6-4a2b-8d9f-c6f09b39f6d5 I see that you can use regular indexing to create an array of futures. So perhaps the flow could be
for k=1:NumOfSlabs
f(k) = parfeval(@eig,3,M(:,:,k));
end
for k=1:NumOfSlabs
[idx,Wt,Dt] = fetchNext(f);
start = 1+(idx-1)*2*L; stop=2*idx*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;
end
Poul-Erik Hansen
Poul-Erik Hansen le 13 Avr 2025
Exactely. Thank you :-)
Edric Ellis
Edric Ellis le 15 Avr 2025
@Walter Roberson It is definitely expected that you can create arrays of parallel.Future - both fetchNext and fetchOutputs accept arrays of futures (in particular, fetchNext is only really useful with an array of futures).
Walter Roberson
Walter Roberson le 15 Avr 2025
The documentation for parfeval() does not show any arrays of futures, so I was under the impression that arrays of futures were potentially only generatable by parfevalOnAll
Edric Ellis
Edric Ellis le 15 Avr 2025
@Walter Roberson thanks for that - I'll see if we can get an example showing the expected usage on the parfeval page itself. Your comment above has exactly the code I would expect to see.
(A single call to parfevalOnAll returns a scalar parallel.FevalOnAllFuture - and while you can make arrays of these, it isn't as useful, and you can't use those with fetchNext)
Edric Ellis
Edric Ellis le 15 Avr 2025
Ah, actually, the Parallel Computing Toolbox page for parfeval already does have examples showing arrays https://uk.mathworks.com/help/parallel-computing/parallel.pool.parfeval.html . @Walter Roberson were you looking at the page in the MATLAB doc? https://uk.mathworks.com/help/matlab/ref/parfeval.html .
Walter Roberson
Walter Roberson le 15 Avr 2025

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 11 Avr 2025

0 votes

Have you compared the time of your parfeval call to the time required for a call to the pageeig function?

1 commentaire

Poul-Erik Hansen
Poul-Erik Hansen le 12 Avr 2025
Yes, the two functions have similar speed but different cpuload.
pageeig cpu load 50%
parfeval cpuload 100 %

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by