An issue with parfor in MATLAB R2023a--R2025a
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Consider two MATLAB m files named profile.m and try_parfor.m as follows.
% profile.m
function profile()
olddir = pwd();
cd(tempdir);
fprintf('\nCurrent directory: %s\n', pwd());
func = @(x) fun(x, 0);
addpath(fullfile(fileparts(mfilename('fullpath')), 'try_parfor'));
try_parfor(func);
cd(olddir);
fprintf('\nCurrent directory: %s\n', pwd());
fprintf('\nSuccess!\n');
end
function y = fun(x, z)
y = x + z;
end
% try_parfor.m
function try_parfor(func)
fprintf('\nEntering try_parfor\n');
fprintf('\nCurrent directory: %s\n', pwd());
parfor i = 1:2
fprintf("\n%d\n", feval(func, i));
end
fprintf('\nExiting try_parfor\n');
end
If you have MATLAB R2023a--R2025a with the Parallel Computing Toolbox installed, try the following.
- Place profile.m in a directory named test.
- Place try_parfor.m in the try_parfor subdirectory of test.
- Open MATLAB and change the current directory to test.
- Run the following command in the MATLAB command window:
profile
It should fail with an error like the following.
Analyzing and transferring files to the workers ...done.
{Error using try_parfor (line 6)
The source code
(/home/runner/work/test_matlab/test_matlab/test_parfor/try_parfor/try_parfor.m)
for the parfor-loop that is trying to execute on the worker could not be found.
Error in profile (line 10)
try_parfor(func);
^^^^^^^^^^^^^^^^^
Caused by:
Unrecognized function or variable 'fun'.
Worker unable to find file.
Unrecognized function or variable 'fun'.
}
exit status 1
Question: Is this failure expected or a bug?
I understand that the failure must depend on the fact that MATLAB has a built-in function named "profile", which is shadowed by "profile.m". Needless to say, this is bad practice, but it is not clear to me why it should lead to such a failure.
As a comparison, rename profile.m to test.m and revise its function name accordingly as follows. Then the MATLAB command
test
will finish successfully.
% test.m
function test()
olddir = pwd();
cd(tempdir);
fprintf('\nCurrent directory: %s\n', pwd());
func = @(x) fun(x, 0);
addpath(fullfile(fileparts(mfilename('fullpath')), 'try_parfor'));
try_parfor(func);
cd(olddir);
fprintf('\nCurrent directory: %s\n', pwd());
fprintf('\nSuccess!\n');
end
function y = fun(x, z)
y = x + z;
end
See https://github.com/zequipe/test_matlab/tree/master/test_parfor for a reproducer of the issue described above, and https://github.com/zequipe/test_matlab/actions/workflows/test_parfor.yml for a GitHub Actions workflow reproducing it. See https://stackoverflow.com/questions/79768366/an-issue-with-parfor-in-matlab-r2023a-r2025a for the same question on StackOverflow.
0 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!