필터 지우기
필터 지우기

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