Parallel loop variable query

2 views (last 30 days)
jnaumann
jnaumann on 14 Nov 2013
Commented: jnaumann on 14 Nov 2013
Good morning,
I am trying to run a genetic algorithm to find the best parameters for a denoising a signal using ALE. I have the code running fine but takes a long time so I am trying to run a part of it using parallel loops this is my code currently.
parfor i=1:pop_size
child=gen(i,:);
for m=1:9
ch3=ch_all(m,:);
ntr = child(3)*floor((length(ch3)-child(1))/child(3));
x = ch3(1:ntr);
d = ch3(1+child(1):ntr+child(1));
leak = 1; % No leakage
h = adaptfilt.blms(child(2),child(4),leak,child(3));
[y,e] = filter(h,x,d);
[ps p f]=best_processing(e, Fs, 14.9,child(5));
percent_inc(m)=ps;
end
percent_ave=median(percent_inc);
percent_diff(i)=sum(percent_ave)
end
I get the following warning - Parfor will not run due to the way the variable 'percent_inc' is used. Can anyone tell me why this is or what must be done to amend the code? As far as I can see the code isn't dependent on anything outside the loop and i have preallocated the variable before the loop. Any help will be greatly appreciated,
Thanks
Jack

Accepted Answer

Edric Ellis
Edric Ellis on 14 Nov 2013
PARFOR currently thinks you're re-using values in percent_inc from one iteration of the PARFOR loop to the next. You can fix this simply be pre-allocating percent_inc (a good idea anyway) like so:
parfor i=1:pop_size
...
percent_inc = zeros(1, 9);
for m = 1:9
...
percent_inc(m) = ...;
end
...
end
  1 Comment
jnaumann
jnaumann on 14 Nov 2013
Thanks. I had preallocated the array but had daftly put it before the PARFOR.
Thanks again

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!