Issues with parfor, strsplit and MATLAB path
이전 댓글 표시
Hi
(MATLAB 9.2.0.556344 (R2017a) on Windows 10)
I am testing out the parfor function, and I run into some unexpected behaviour:
1) It seems like the order in MATLAB path changes during parfor
2) The MATLAB function strsplit seems to not work as expected in parfor
(Using parfor in the examples below is of course meaningless. But the strsplit is only a small part of a larger code which is supposed to run in parallel).
Example of #1:
I use the function library of Peter J Acklam, which had a strsplit before MATLAB introduced this function. His function takes the separator as first argument and string as second (reversed compared to MATLAB's strsplit). I am using other of his functions, so I have him added to my MATLAB path, but with lower priority than MATLAB functions. When I run this code:
a = 'hello_world';
disp('Strsplit before parfor:');
disp(strsplit(a, '_'));
disp(' ');
disp('Strsplit during parfor:');
parfor (n = 1 : 1, 4)
disp(strsplit('_', a));
end
disp(' ');
disp('Strsplit after parfor:');
disp(strsplit(a, '_'));
I get the following output
Strsplit before parfor:
'hello' 'world'
Strsplit during parfor:
** Using strsplit to Peter J. Acklam **
'hello' 'world'
Strsplit after parfor:
'hello' 'world'
Note that I added a comment inside Peter's strsplit ( Using strsplit to Peter J. Acklam **), and I am also forced to change the order of input arguments to make the code run.
Example of #2:
To avoid #1 from happening for strsplit, I can just rename Peter's strsplit to e.g. xstrsplit. But when I do that and update my code, MATLAB's strsplit seems to fail in parfor. After renaming, I run this code:
a = 'hello_world';
disp('Strsplit before parfor:');
disp(strsplit(a, '_'));
disp(' ');
disp('Strsplit during parfor:');
parfor (n = 1 : 1, 4)
disp(strsplit(a, '_'));
end
disp(' ');
disp('Strsplit after parfor:');
disp(strsplit(a, '_'));
and I get this output
Strsplit before parfor:
'hello' 'world'
Strsplit during parfor:
'hello_world'
Strsplit after parfor:
'hello' 'world'
Any input on what I am doing wrong is welcome.
채택된 답변
추가 답변 (1개)
Edric Ellis
2017년 8월 17일
You can see which version of strsplit the workers are seeing by executing:
fetchOutputs(parfeval(@which, 1, 'strsplit'))
It seems as though the path synchronisation for parallel pools doesn't work out quite right if you have a function that is shadowed by a function that is part of MATLAB. One workaround is to add this to your path after opening the parallel pool (using addpath ... -end).
카테고리
도움말 센터 및 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!