Skip to content

Adjust OpenMP flags for macOS, weil_polynomials OpenMP support via meson#41626

Open
dimpase wants to merge 21 commits into
sagemath:developfrom
dimpase:dimpase-patch-7
Open

Adjust OpenMP flags for macOS, weil_polynomials OpenMP support via meson#41626
dimpase wants to merge 21 commits into
sagemath:developfrom
dimpase:dimpase-patch-7

Conversation

@dimpase
Copy link
Copy Markdown
Member

@dimpase dimpase commented Feb 11, 2026

Should fix a problem reported on #40898 - OpenMP on macOS needs different flags

Also, correctly support OpenMP for weil_polynomials with Meson

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 11, 2026

Documentation preview for this PR (built with commit a338058; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@dimpase dimpase changed the title Adjust OpenMP flags for macOS Adjust OpenMP flags for macOS, weil_polynomials OpenMP support via meson Feb 11, 2026
@dimpase dimpase requested review from davidlowryduda and removed request for davidlowryduda February 11, 2026 22:42
Copy link
Copy Markdown
Contributor

@tobiasdiez tobiasdiez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from trivial whitespace issues, the changes to the meson files look good to me. I cannot test if they do the job though as I don't have a mac.

Comment thread src/sage/meson.build Outdated
Comment thread src/sage/meson.build
Comment thread src/sage/meson.build
@dcoudert
Copy link
Copy Markdown
Collaborator

even with this PR, compilation fails for me on macOS 26.3

[fflas_ffpack-2.5.0+sage-2024-05-18b] [spkg-install] /opt/homebrew/bin/bash ../../../libtool  --tag=CXX   --mode=compile g++ -std=gnu++11 -DHAVE_CONFIG_H -I. -I../../..  -I../../..  -O2 -march=native -Wall -DNDEBUG -UDEBUG  -I/opt/homebrew/Cellar/openblas/0.3.31_1/include -I/opt/homebrew/opt/libomp/include -Xpreprocessor -fopenmp  -g -O2 -c -o fflas_L1_inst.lo fflas_L1_inst.C
[fflas_ffpack-2.5.0+sage-2024-05-18b] [spkg-install] libtool: compile:  g++ -std=gnu++11 -DHAVE_CONFIG_H -I. -I../../.. -I../../.. -O2 -march=native -Wall -DNDEBUG -UDEBUG -I/opt/homebrew/Cellar/openblas/0.3.31_1/include -I/opt/homebrew/opt/libomp/include -Xpreprocessor -fopenmp -g -O2 -c fflas_L1_inst.C  -fno-common -DPIC -o .libs/fflas_L1_inst.o
[fflas_ffpack-2.5.0+sage-2024-05-18b] [spkg-install] In file included from fflas_L1_inst.C:26:
[fflas_ffpack-2.5.0+sage-2024-05-18b] [spkg-install] ../../../fflas-ffpack/fflas-ffpack-config.h:102:2: warning: "openmp was not detected correctly at configure time, please report this bug" [-W#warnings]
[fflas_ffpack-2.5.0+sage-2024-05-18b] [spkg-install]   102 | #warning "openmp was not detected correctly at configure time, please report this bug"
...
...
[fflas_ffpack-2.5.0+sage-2024-05-18b] [spkg-install] clang++: error: unsupported option '-fopenmp'

A failing doctest indicates that some commits are missing https://github.com/sagemath/sage/actions/runs/21930838760/job/63334515946?pr=41626

@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 15, 2026

even with this PR, compilation fails for me on macOS 26.3

it would have worked if installed fflas-ffpack etc from Homebree Macaulay2 tap...

@dcoudert
Copy link
Copy Markdown
Collaborator

I did brew install Macaulay2/homebrew-tap/gfan Macaulay2/homebrew-tap/linbox and fflas-ffpack 2.5.0 is installed but configure says it's not suitable.

I first see

Checking whether SageMath should install SPKG fflas_ffpack...
checking whether any of givaro gmp openblas is installed as or will be installed as SPKG... no
checking for fflas-ffpack >= 2.5.0... yes
real_configure: will use system package and not install SPKG fflas_ffpack

and then

Checking whether SageMath should install SPKG linbox...
checking whether any of fflas_ffpack flint fplll givaro gmp iml m4ri m4rie mpfr ntl is installed as or will be installed as SPKG... yes; install linbox as well
real_configure: no suitable system package found for SPKG linbox

and finally in the summary

fflas_ffpack:                   no suitable system package; standard, SPKG version 2.5.0+sage-2024-05-18b will be installed

@tobiasdiez
Copy link
Copy Markdown
Contributor

CI still fails due to problems with the formatting of the meson build. Could you please call the update-meson script to correct this?

@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 15, 2026

I did brew install Macaulay2/homebrew-tap/gfan Macaulay2/homebrew-tap/linbox and fflas-ffpack 2.5.0 is installed but configure says it's not suitable.

I first see

Checking whether SageMath should install SPKG fflas_ffpack...
checking whether any of givaro gmp openblas is installed as or will be installed as SPKG... no
checking for fflas-ffpack >= 2.5.0... yes
real_configure: will use system package and not install SPKG fflas_ffpack

and then

Checking whether SageMath should install SPKG linbox...
checking whether any of fflas_ffpack flint fplll givaro gmp iml m4ri m4rie mpfr ntl is installed as or will be installed as SPKG... yes; install linbox as well
real_configure: no suitable system package found for SPKG linbox

and finally in the summary

fflas_ffpack:                   no suitable system package; standard, SPKG version 2.5.0+sage-2024-05-18b will be installed

oops, this needs #40397 - otherwise openblas, linked on Homebrew with GNU OpenMP, conflicts with the "normal" OpenMP...

@dimpase dimpase marked this pull request as draft February 16, 2026 00:12
@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 16, 2026

need to modify the line ax_openmp_flags="-fopenmp -openmp -qopenmp -mp -xopenmp -omp -qsmp=omp none" in m4/ax_openmp.m4 to include -Xpreprocessor -fopenmp.

And need to figure out how to build openblas on macOS with libomp rather than libgomp.

@dimpase dimpase marked this pull request as ready for review February 16, 2026 19:24
@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 16, 2026

This should work better, but still there are caveats on macOS - a possibility that one of the libraries uses OpenBLAS linked to GNU openMP (libgomp), but also llvm's OpenMP (libomp). The result is a bit problem...


Actually the latest openblas ftom Homebrew links to openmp, not gopenmp.
This is good news

@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 16, 2026

CI still fails due to problems with the formatting of the meson build. Could you please call the update-meson script to correct this?

@tobiasdiez I've tried it, but it produces a huge diff for some reason.
I imagine after a small change to a couple of meson.build files, it should not be huge...

meson-update.diff.txt

@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 17, 2026

@tobiasdiez - the latest commit illustrates that something is broken in the update-meson script. It has added a non-existent file to src/doc/en/reference/spkg/meson.build, namely, index_standard.rst

The latter is a generated file, it absolutely should not be in doc_sources

@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 17, 2026

on conda macos CI we see

symbol not found in flat namespace '___kmpc_barrier'

or, in full:

2026-02-17T02:42:38.7201850Z src/bin/sage -t --warn-long 5.0 --random-seed=330025257595958169682966971689567088936 src/sage/env.py
2026-02-17T02:42:38.7309430Z **********************************************************************
2026-02-17T02:42:38.7446250Z ##[error]Failed example:: Exception raised:
Traceback (most recent call last):
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/doctest/forker.py", line 734, in _run
    self.compile_and_execute(example, compiler, test.globs)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/doctest/forker.py", line 1158, in compile_and_execute
    exec(compiled, globs)
    ~~~~^^^^^^^^^^^^^^^^^
  File "<doctest sage.env.cython_aliases[5]>", line 1, in <module>
    cython(                                               # optional - sage.misc.cython
    ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    '''
    ^^^
    ...<11 lines>...
    print(sum)
    ^^^^^^^^^^
    ''')
    ^^^^
  File "sage/misc/lazy_import.pyx", line 413, in sage.misc.lazy_import.LazyImport.__call__
    return self.get_object()(*args, **kwds)
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/misc/cython.py", line 730, in cython_compile
    return cython_import_all(tmpfile, get_globals(), **kwds)
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/misc/cython.py", line 616, in cython_import_all
    m = cython_import(filename, **kwds)
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/misc/cython.py", line 589, in cython_import
    return builtins.__import__(name)
           ~~~~~~~~~~~~~~~~~~~^^^^^^
ImportError: dlopen(/var/folders/w0/cd8qgn052r16zsblrrxl1gxw0000gn/T/sage_gy70yvzg/spyx_p7dlwqrq/_var_folders_w0_cd8qgn052r16zsblrrxl1gxw0000gn_T_sage_gy70yvzg_tmp_weikmcta_pyx/_var_folders_w0_cd8qgn052r16zsblrrxl1gxw0000gn_T_sage_gy70yvzg_tmp_weikmcta_pyx_0.cpython-313-darwin.so, 0x0002): symbol not found in flat namespace '___kmpc_barrier'

2026-02-17T02:42:38.7555770Z     cython(                                               # optional - sage.misc.cython
2026-02-17T02:42:38.7656970Z     '''
2026-02-17T02:42:38.7758330Z     #distutils: extra_compile_args = OPENMP_CFLAGS
2026-02-17T02:42:38.7800810Z     #distutils: extra_link_args = OPENMP_CFLAGS
2026-02-17T02:42:38.7809570Z     from cython.parallel import prange
2026-02-17T02:42:38.7835280Z 
2026-02-17T02:42:38.7858930Z     cdef int i
2026-02-17T02:42:38.7881220Z     cdef int n = 30
2026-02-17T02:42:38.7906240Z     cdef int sum = 0
2026-02-17T02:42:38.7914440Z 
2026-02-17T02:42:38.7939110Z     for i in prange(n, num_threads=4, nogil=True):
2026-02-17T02:42:38.7943080Z         sum += i
2026-02-17T02:42:38.7963940Z 
2026-02-17T02:42:38.7965990Z     print(sum)
2026-02-17T02:42:38.7983780Z     ''')
2026-02-17T02:42:38.7992510Z Exception raised:
2026-02-17T02:42:38.8016250Z     Traceback (most recent call last):
2026-02-17T02:42:38.8039210Z       File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/doctest/forker.py", line 734, in _run
2026-02-17T02:42:38.8044270Z         self.compile_and_execute(example, compiler, test.globs)
2026-02-17T02:42:38.8063640Z         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-02-17T02:42:38.8076890Z       File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/doctest/forker.py", line 1158, in compile_and_execute
2026-02-17T02:42:38.8081980Z         exec(compiled, globs)
2026-02-17T02:42:38.8083100Z         ~~~~^^^^^^^^^^^^^^^^^
2026-02-17T02:42:38.8085010Z       File "<doctest sage.env.cython_aliases[5]>", line 1, in <module>
2026-02-17T02:42:38.8186240Z         cython(                                               # optional - sage.misc.cython
2026-02-17T02:42:38.8186810Z         ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-02-17T02:42:38.8190180Z         '''
2026-02-17T02:42:38.8234480Z         ^^^
2026-02-17T02:42:38.8239640Z         ...<11 lines>...
2026-02-17T02:42:38.8340730Z         print(sum)
2026-02-17T02:42:38.8441970Z         ^^^^^^^^^^
2026-02-17T02:42:38.8451770Z         ''')
2026-02-17T02:42:38.8519150Z         ^^^^
2026-02-17T02:42:38.8524970Z       File "sage/misc/lazy_import.pyx", line 413, in sage.misc.lazy_import.LazyImport.__call__
2026-02-17T02:42:38.8548850Z         return self.get_object()(*args, **kwds)
2026-02-17T02:42:38.8570850Z       File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/misc/cython.py", line 730, in cython_compile
2026-02-17T02:42:38.8574190Z         return cython_import_all(tmpfile, get_globals(), **kwds)
2026-02-17T02:42:38.8579510Z       File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/misc/cython.py", line 616, in cython_import_all
2026-02-17T02:42:38.8680670Z         m = cython_import(filename, **kwds)
2026-02-17T02:42:38.8683410Z       File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.13/site-packages/sage/misc/cython.py", line 589, in cython_import
2026-02-17T02:42:38.8685100Z         return builtins.__import__(name)
2026-02-17T02:42:38.8785990Z                ~~~~~~~~~~~~~~~~~~~^^^^^^
2026-02-17T02:42:38.8911440Z     ImportError: dlopen(/var/folders/w0/cd8qgn052r16zsblrrxl1gxw0000gn/T/sage_gy70yvzg/spyx_p7dlwqrq/_var_folders_w0_cd8qgn052r16zsblrrxl1gxw0000gn_T_sage_gy70yvzg_tmp_weikmcta_pyx/_var_folders_w0_cd8qgn052r16zsblrrxl1gxw0000gn_T_sage_gy70yvzg_tmp_weikmcta_pyx_0.cpython-313-darwin.so, 0x0002): symbol not found in flat namespace '___kmpc_barrier'
2026-02-17T02:42:38.9016460Z **********************************************************************
2026-02-17T02:42:38.9117450Z 1 item had failures:
2026-02-17T02:42:38.9226610Z    1 of   7 in sage.env.cython_aliases
2026-02-17T02:42:38.9465100Z     [39 tests, 1 failure, 11.07s wall]

not sure if this is Conda-only.

Maybe it comes from

 #distutils: extra_link_args = OPENMP_CFLAGS

in src/sage/env.py not containing everything it should, e.g. no -lomp

@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 17, 2026

@tobiasdiez we need to have a Homebrew-based CI run, as far as I understand. Conda uses its own clang, etc.

I asked here: conda-forge/openblas-feedstock#182

@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 17, 2026

Anyhow, I do not know now what OpenMP flags are needed for Conda on macOS. With or without -Xpreprocessor ?

@dimpase
Copy link
Copy Markdown
Member Author

dimpase commented Feb 17, 2026

Well, actually, with corrected autoconf macro, we don't need to hardcode OpenMP flags in meson.build.
(although perhaps it's easier to add a test for Conda/nonConda, instead of doing the dance of passing parameters to meson.build

Comment thread src/sage/meson.build
@@ -31,8 +31,14 @@ conf_data.set('SAGE_ARCHFLAGS', 'unset')
conf_data.set('SAGE_PKG_CONFIG_PATH', '')
openmp = dependency('openmp', required: false, disabler: true)
if openmp.found()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhpalmieri - can you try removing the whole if block here and see if the build works?

That's cause Meson probably is doing the right thing here without us messing up with flags.

@tobiasdiez

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhpalmieri - can you try removing the whole if block here and see if the build works?

That's cause Meson probably is doing the right thing here without us messing up with flags.

@tobiasdiez

I tried this:

diff --git a/src/sage/meson.build b/src/sage/meson.build
index d2edf7b687..4018fbf914 100644
--- a/src/sage/meson.build
+++ b/src/sage/meson.build
@@ -30,16 +30,16 @@ conf_data.set('SAGE_ARCHFLAGS', 'unset')
 # not needed when using conda, as we then don't build any pc files
 conf_data.set('SAGE_PKG_CONFIG_PATH', '')
 openmp = dependency('openmp', required: false, disabler: true)
-if openmp.found()
-  if host_machine.system() == 'darwin'
-    conf_data.set('OPENMP_CFLAGS', '-Xpreprocessor -fopenmp')
-    conf_data.set('OPENMP_CXXFLAGS', '-Xpreprocessor -fopenmp')
-    conf_data.set('OPENMP_CLIB', '-lomp')
-  else
-    conf_data.set('OPENMP_CFLAGS', '-fopenmp')
-    conf_data.set('OPENMP_CXXFLAGS', '-fopenmp')
-  endif
-endif
+# if openmp.found()
+#   if host_machine.system() == 'darwin'
+#     conf_data.set('OPENMP_CFLAGS', '-Xpreprocessor -fopenmp')
+#     conf_data.set('OPENMP_CXXFLAGS', '-Xpreprocessor -fopenmp')
+#     conf_data.set('OPENMP_CLIB', '-lomp')
+#   else
+#     conf_data.set('OPENMP_CFLAGS', '-fopenmp')
+#     conf_data.set('OPENMP_CXXFLAGS', '-fopenmp')
+#   endif
+# endif
 gap_exe = find_program('gap', required: not is_windows, disabler: true)
 if gap_exe.found()
   gaprun = run_command(

and it made no difference.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Looks like that we either are converging on a Meson bug, or on a fix :-)
Could you attach here build/sage-distro/meson-info/intro-dependencies.json from the latest run?

On top of the latter diff (in #41626 (comment)), could you please try applying

--- a/build/pkgs/sagelib/spkg-install.in
+++ b/build/pkgs/sagelib/spkg-install.in
@@ -31,6 +31,9 @@ export SAGE_SRC_ROOT=/doesnotexist
 export SAGE_DOC_SRC=/doesnotexist
 export SAGE_DOCS=/doesnotexist
 
+unset OPENMP_CFLAGS
+unset OPENMP_CXXFLAGS
+
 # We also poison all directories below SAGE_LOCAL
 # except for SAGE_SPKG_SCRIPTS, which is needed by sage-dist-helpers
 export SAGE_PKGCONFIG=/doesnotexist

and rebuilding? The latter should be removing any remaining interference we might have over Meson's dealing with OpenMP.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made no difference: I still see "error: unknown argument: '-MD'".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so this seems to be a bug in Meson dealing with OpenMP: flags containing -Xpreprocessor are received from pkg-config files, and not dealt with properly

But we need a small reproducer, to provide a good bug report

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhpalmieri - could you please post intro-dependencies.json for the --with-darwin-accerate=yes case? I really would like to see how -framework Accelerate appear there.

Here it is.

intro-dependencies.json

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Here is the bug report: mesonbuild/meson#15764

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work if you use -Xpreprocessor=-fopenmp instead? (from the linked meson issue)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meson upstream acknowledged this as a regression, and aims to fix it in the next release, 1.11.2. Which should be in a couple of weeks, so I'd wait for it before proceeding here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work if you use -Xpreprocessor=-fopenmp instead? (from the linked meson issue)

it's not documented (perhaps they talked about using the = padding in their code, not as input)?
There is a rough equivalent, -Wp,-fopenmp - but I'm not sure whether it would be understood by our build system (probably not out of the box)

dimpase and others added 21 commits May 18, 2026 18:48
Removed comments regarding OpenMP support.
Co-authored-by: Tobias Diez <code@tobiasdiez.com>

Update src/sage/meson.build

Co-authored-by: Tobias Diez <code@tobiasdiez.com>

Update src/sage/meson.build

Co-authored-by: Tobias Diez <code@tobiasdiez.com>
Note that we disabled checking for "-openmp" flag, as it leads to
creation of an executable called "penmp" during the ./configure run
WIP patch for libtool to allow "-Xpreprocessor -fopenmp"
same treatment for linbox as for fflas
WIP: same libtool patch as for fflas
for an unclear so far reason, this needs to be explicit
Link libomp after installation in CI workflow.
@dimpase dimpase force-pushed the dimpase-patch-7 branch from e1724f1 to a338058 Compare May 18, 2026 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants