Parfor to speed up loops

6 次查看(过去 30 天)
Jw
Jw 2012-2-10
Hi, i have a dual core processor, i wish to make use of parfor to speed up my for loop processing how do i do that? Basically i understand the i have to initialise matlabpool(2) and parfor i=1:NPARTICLES and also matlabpool close.
How do i implement it in my coding? Thanks
if CDiff < ODiff
dt = CDiff;
% Predict step
for i=1:NPARTICLES
particles(i)= predict (particles(i), V,G,Qe, WHEELBASE,dt, SWITCH_PREDICT_NOISE);
end
cIndex = cIndex + 1;
else
dt = ODiff;
z1 = Ob(:,OIndex);
z = cell2mat (z1);
z(1) = z(1)/100;
% Predict step
for i=1:NPARTICLES
particles(i)= predict (particles(i), V,G,Qe, WHEELBASE,dt, SWITCH_PREDICT_NOISE);
end
% Perform update
for i=1:NPARTICLES
[zf,idf,zn]= data_associate(particles(i),z,R);
if ~isempty(zf) % observe map features
w= compute_weight(particles(i), zf,idf, R); % w = p(z_k | x_k)
if w <= 1e-5
w = 1e-5;
end
particles(i).w= particles(i).w * w;
particles(i)= feature_update(particles(i), zf, idf, R);
end
if ~isempty(zn) % observe new features, augment map
particles(i)= add_feature(particles(i), zn,R);
end
end
particles= resample_particles(particles, NEFFECTIVE, SWITCH_RESAMPLE);
dt2 = timestamp(cIndex+1)-Obser_time(OIndex);
for i=1:NPARTICLES
particles(i)= predict (particles(i), V,G,Qe, WHEELBASE,dt2, SWITCH_PREDICT_NOISE);
end

回答(1 个)

Ken Atwell
Ken Atwell 2012-2-10
You code looks good to go.
  1. Replace the "for" with "parfor"
  2. "matlabpool open" before starting
  3. "matlabpool close" when you're all done
But, before you do any of that, in deference to Amdahl's Law, I would be tempted to run this code through the profiler first to be certain that the bits to be parallelized make up a meaningful percentage of the program.

类别

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