Parrallel run using parfor and institute server?

조회 수: 2(최근 30일)
Sharad
Sharad 2022년 10월 10일
답변: Edric Ellis 2022년 10월 10일
I need to speed up my matlab code, for this How can I use parfor nested loop? Here, I am able to use iner parfor loop but it takes time almost equal to normal nested for loop. And outer parfor loop give error. I am trying to run the for loop code as.
function output=f
t=[0.2 0.5];
na=(0:10:20).*10.^11 ;
output=zeros(length(t),length(na));
for i=1:length(t)
for j=1:length(na)
KFa=sqrt(pi.*na(j));
mu=log(exp(1./t(i))-1);
output(i,j)=KFa.*.*t(i)./mu
end
end
end
Along with this, I also want to run this code using a server facility which contain multiple CPU (as worker) and cores. How would I run it using the server.

답변(1개)

Edric Ellis
Edric Ellis 2022년 10월 10일
The problem with converting the outer loop to parfor is described in the error message that you get when you try:
Error: When indexing the sliced variable 'output', the range of the for-loop variable 'j' must be a row vector of positive constant numbers or variables.
The fix is to calculate the length of na outside the parfor loop. (I know this seems unncessary, but parfor is very fussy about this sort of thing)
t=[0.2 0.5];
na=(0:10:20).*10.^11;
na_len = length(na);
output=zeros(length(t),na_len);
for i=1:length(t)
for j=1:na_len
KFa=sqrt(pi.*na(j));
mu=log(exp(1./t(i))-1);
output(i,j)=KFa.*t(i)./mu
end
end
The speed-up you get here may well be only small using local workers.
To use more resources, you need to get MATLAB Parallel Server set up on your server facility. Once that is available, you can run your parfor loop across multiple machines.

범주

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by