Converting for loop to parfor loop
Show older comments
Hi..i am new to matlab and i am having trouble to convert the two 'for' loop below into parfor loop so it takes shorter time to execute.
I tried by just replacing the 'for' to 'parfor' in for the first 'for' loop which i know is not correct and i get a longer execution time? Can anyone explain to me too?
For the second 'for loop' i had an
Error using ==> parallel_function at 598 Error in ==> parallel_function>make_general_channel/channel_general at 894 Undefined function or variable 'irange'.
for i=2:1:nc
sum1 = 0.0;
sum2 = 0.0;
for j=1:1:nc+1
if i==j % skipped
continue;
end
dxx = x(1,j) - x(1,i);
rr=abs(dxx);
if rr < re
sum1 = sum1 + kernel(rr,re,ktype)*rr^nor;
sum2 = sum2 + kernel(rr,re,ktype)*rr^(nor-2.0);
end
end
dtt = 0.5* cd * sum1/sum2;
if dtt < dt
dt = dtt;
end
end
matlabpool open local 2
tic;
% time marching nt=1; time(1,1) = 0.0; error(1,1) = 1.0e-10;
for t=dt:dt:0.1
nt=nt+1;
time(1,nt) = t;
disp(t);
i_range=2:1:nc;
j_range=1:1:nc+1;
parfor loop=1:(numel(i_range)*numel(j_range))
i=i_range(floor(loop/length(irange))+1);
j=j_range((floor(mod(loop,length(irange))))+1);
end
% get the laplacian for all internal grids
for i=2:1:nc
sum = 0.0;
zeta(1,i) = 0.0;
for j=1:1:nc+1
if i==j % skipped
continue;
end
dxx = x(1,j) - x(1,i);
rr=abs(dxx);
if rr < re
sum = sum + (p(nt-1,j)-p(nt-1,i))*kernel(rr,re,ktype)*rr^(nor-2.0);
zeta(1,i) = zeta(1,i) + kernel(rr,re,ktype)*rr^nor;
%sumw(1,i) = sumw(1,i) + kernel(rr,re,1);
end
end
lapp(1,i) = 2.0*dim*sum/zeta(1,i);
p(nt,i) = p(nt-1,i) + dt*lapp(1,i);
end
% update boundary value
p(nt,1) = function_phi(0,t);
p(nt,nc+1) = function_phi(1,t);
% calculate L2norm error
sumerr = 0.0;
for i=1:1:nc+1
ptheo = function_phi(x(1,i),t);
err = (ptheo - p(nt,i));
sumerr = sumerr + err*err;
end
sumerr = sqrt(sumerr);
disp(sumerr);
error(1,nt) = sumerr;
end
u=toc;
disp(['Time for parallel calculation: ', num2str(u), ' seconds.'])
Answers (0)
Categories
Find more on Big Data Processing 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!