Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The MATLAB interface does not pass the recursive test on macOS and Windows #83

Closed
zaikunzhang opened this issue Sep 20, 2023 · 2 comments
Labels
help wanted Extra attention is needed macOS Issues related to macOS matlab Issues related to the MATLAB interface or implementation tests Issues related to tests windows Issues related to Windows

Comments

@zaikunzhang
Copy link
Member

zaikunzhang commented Sep 20, 2023

The MATLAB interface does not pass the recursive test on macOS and Windows, even if the recursion depth is only one:

https://github.com/libprima/prima/blob/main/matlab/tests/recursive.m

The same test succeeds on Linux. See

https://github.com/sprimalib/prima/actions/runs/6247172429

Is this a limitation of MATLAB, OS, or PRIMA?

@zaikunzhang zaikunzhang added help wanted Extra attention is needed matlab Issues related to the MATLAB interface or implementation windows Issues related to Windows macOS Issues related to macOS labels Sep 20, 2023
@zaikunzhang
Copy link
Member Author

zaikunzhang commented Sep 20, 2023

Update: The Windows / macOS test does not always fail. This is even stranger.

https://github.com/sprimalib/prima/actions/runs/6247172429

@zaikunzhang zaikunzhang added the tests Issues related to tests label Sep 20, 2023
@zaikunzhang
Copy link
Member Author

Fixed by adding -assume recursion (macos) or /assume:recursion (windows) to the flags of ifort. See

% Modify the compiler options by revising FFLAGS or COMPFLAGS.
% See https://www.mathworks.com/help/matlab/ref/mex.html
% 1. We force the Fortran compiler to allocate arrays on the heap instead of the stack. Otherwise,
% the solvers will encounter stack overflow when the problem size is large. As of gfortran 12.0,
% `-fno-stack-arrays` is indeed the default, and we specify it for safety; as of Intel oneAPI
% 2023.1.0, `-no-heap-arrays` is the default, so we must specify `-heap-arrays`.
% N.B.: We assume that the function evaluation is much more expensive than the memory allocation,
% so the performance loss due to the heap allocation is negligible. This is true for derivative-free
% optimization, but may not be true for optimization with derivatives.
% 2. We require the Fortran compiler to compile the solvers so that they can be called recursively.
% Otherwise, the solvers will not work properly in recursive invocations. See
% https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety
compiler_configurations = mex.getCompilerConfigurations('fortran', 'selected');
if contains(compiler_configurations.Manufacturer, 'gnu', 'IgnoreCase', true) % gfortran
extra_compiler_options = '-fno-stack-arrays -frecursive';
elseif contains(compiler_configurations.Manufacturer, 'intel', 'IgnoreCase', true) % Intel compiler
if ispc
extra_compiler_options = '/heap-arrays /assume:recursion';
else
extra_compiler_options = '-heap-arrays -assume recursion';
end
else
warning('prima:UnrecognizedCompiler', 'Unrecognized compiler %s. The package may not work.', ...
compiler_configurations.Name);
extra_compiler_options = '';
end
if ispc % Windows
compiler_options = ['COMPFLAGS="$COMPFLAGS ', extra_compiler_options, '"'];
else
compiler_options = ['FFLAGS="$FFLAGS ', extra_compiler_options, '"'];
end

https://fortran-lang.discourse.group/t/frecursive-assume-recursion-and-recursion-thread-safety

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed macOS Issues related to macOS matlab Issues related to the MATLAB interface or implementation tests Issues related to tests windows Issues related to Windows
Projects
None yet
Development

No branches or pull requests

1 participant