Parfor to speed up loops

1 visualizzazione (ultimi 30 giorni)
Jw
Jw il 10 Feb 2012
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

Risposte (1)

Ken Atwell
Ken Atwell il 10 Feb 2012
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.

Categorie

Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by