Effacer les filtres
Effacer les filtres

How to save inside parfor loops - Save in external function doesn not work.

5 vues (au cours des 30 derniers jours)
GiulioSt
GiulioSt le 28 Juil 2015
Hi all, I'm new in Matlab and I would like to ask you help about a problem that I'm facing. The problem is how to save variables inside a parfor loop. I have looked up on internet and, as suggested in many forums, I have already tried to save them inside an external function (parsave) called inside parfor loops, but it doesn't work. This is my code:
for Es_N0_index=1:length(Es_N0_vec)
hyp1_count_var = 0;
hyp0_count_var = 0;
Detected_frame_var = 0;
FA_frame_var = 0;
parfor noise_iter = 1:NOISE_ITER
hypothesis_used=randi(2)-1;
if(hypothesis_used)
hyp1_count_var = hyp1_count_var + 1;
end
if(not(hypothesis_used))
hyp0_count_var = hyp0_count_var + 1;
end
........
if( (strcmp(Demod_State,'Frame_Sync')) && CorrectTimeSync && hypothesis_used)
Detected_frame_var=Detected_frame_var + 1;
elseif( strcmp(Demod_State,'Frame_Sync') && (~hypothesis_used)) %GS
FA_frame_var=FA_frame_var + 1;
else
disp('Final Case')
end
if (rem(noise_iter,10)==0)
disp('saving state..')
parsave(sprintf('output%d.mat', Es_N0_index),Detected_frame_var, FA_frame_var, hyp1_count_var, hyp0_count_var);
end
end
...........
end
and the external function is:
function parsave(fname,Detected_frame_var, FA_frame_var, hyp1_count_var, hyp0_count_var)
parsave(sprintf('output%d.mat', Es_N0_index),Detected_frame_var, FA_frame_var,hyp1_count_var,hyp0_count_var);
end
What makes me crazy is that, if I comment the "if condition" where the parsave function is called (if(rem...)==...), everything works fine. Instead, if I uncomment these 3 lines,I get an error:
"Error using FS_Stand_Alone (line 62) An UndefinedFunction error was thrown on the workers for 'hyp1_count_var'. This might be because the file containing 'hyp1_count_var' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details.
Caused by: Undefined function or variable 'hyp1_count_var'. "
Do you know what I can do to fix it? Thanks
  1 commentaire
Jon
Jon le 29 Juil 2015
Modifié(e) : Jon le 29 Juil 2015
I am just learning about parfor myself, but I've used it extensively without problems by putting everything into an inner function, e.g.
parfor i = 1:nits
[output1 output2 etc] = innerfunction(input1, input2, etc)
end
where "innerfunction" is everything you have in the parfor loop. While this isn't elegant and basically bypasses understanding of parfor, it also has the advantage of easily adding new variables or changing your code without worrying about variable classification problems. You can change the parsave to a regular save (I believe) and avoid your problem.

Connectez-vous pour commenter.

Réponses (1)

Edric Ellis
Edric Ellis le 29 Juil 2015
I think the problem here is that because of the way you're using hyp1_count_var, parfor is treating it as a temporary loop variable (rather than a reduction loop variable which I think is what you're after).
In any case, you cannot access partially evaluated reduction variables inside the parfor loop - the value would depend on the order of execution, and parfor forbids any access to things that are order-dependent. (Likewise you cannot access partially evaluated sliced variables).
In your case, unfortunately all the variables that you are trying to save fall into the same category.

Catégories

En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by