An issue with parfor in MATLAB R2023a--R2025a

11 views (last 30 days)
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.
  1. Place profile.m in a directory named test.
  2. Place try_parfor.m in the try_parfor subdirectory of test.
  3. Open MATLAB and change the current directory to test.
  4. 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

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!