How to save inside parfor loops - Save in external function doesn not work.
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jon
2015-7-29
编辑:Jon
2015-7-29
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.
回答(1 个)
Edric Ellis
2015-7-29
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!