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

Check OpenMP at configuration #31499

Closed
kliem opened this issue Mar 15, 2021 · 30 comments
Closed

Check OpenMP at configuration #31499

kliem opened this issue Mar 15, 2021 · 30 comments

Comments

@kliem
Copy link
Contributor

kliem commented Mar 15, 2021

We define cython aliases OPENMP_CFLAGS and OPENMP_CXXFLAGS.

They must be used as extra_link_args and extra_compile_args to get cython.parallel to work in parallel.

If they are empty, cython.parallel.prange will still compile and work (but not in parallel). This is useful in case that OpenMP isn't supported, see e.g. https://github.com/kliem/sage/pull/40/checks?check_run_id=2076988665).

With this ticket we can just run

sage: cython(''' 
....: #distutils: extra_compile_args = OPENMP_CFLAGS 
....: #distutils: extra_link_args = OPENMP_CFLAGS 
....: from cython.parallel import prange 
....:  
....: cdef int i 
....: cdef int n = 30 
....: cdef int sum = 0 
....:  
....: for i in prange(n, num_threads=4, nogil=True): 
....:     sum += i 
....:      
....: print(sum) 
....: ''')

regardless of whether the current system actually supports OpenMP.

Depends on #31384

CC: @tscrim @mkoeppe @kiwifb @kliem @w-bruns

Component: cython

Keywords: openmp, parallel

Author: Jonathan Kliem

Branch/Commit: c3cc31e

Reviewer: Matthias Koeppe

Issue created by migration from https://trac.sagemath.org/ticket/31499

@kliem kliem added this to the sage-9.3 milestone Mar 15, 2021
@kiwifb
Copy link
Member

kiwifb commented Mar 15, 2021

comment:3

Seems fair. Why is the extra test in python3/spkg-configure needed? Is it to deal with case where the compiler used to build python is not the same as the rest of sage?

@mkoeppe
Copy link
Member

mkoeppe commented Mar 15, 2021

comment:4
+    # OpenMP
+    aliases["OPENMP_CFLAGS"] = [SAGE_OPENMP_CFLAGS] if SAGE_OPENMP_CFLAGS else []
+    aliases["OPENMP_CXXFLAGS"] = [SAGE_OPENMP_CXXFLAGS] if SAGE_OPENMP_CXXFLAGS else []
+

As SAGE_OPENMP_CFLAGS could probably consist of several arguments separated by space, perhaps this should be aliases["OPENMP_CFLAGS"] = SAGE_OPENMP_CFLAGS.split()?

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 16, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

a9d3dfesplit to account for multiple arguments

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 16, 2021

Changed commit from b76dc3c to a9d3dfe

@kliem
Copy link
Contributor Author

kliem commented Mar 16, 2021

comment:6

Replying to @mkoeppe:

+    # OpenMP
+    aliases["OPENMP_CFLAGS"] = [SAGE_OPENMP_CFLAGS] if SAGE_OPENMP_CFLAGS else []
+    aliases["OPENMP_CXXFLAGS"] = [SAGE_OPENMP_CXXFLAGS] if SAGE_OPENMP_CXXFLAGS else []
+

As SAGE_OPENMP_CFLAGS could probably consist of several arguments separated by space, perhaps this should be aliases["OPENMP_CFLAGS"] = SAGE_OPENMP_CFLAGS.split()?

Good point.

@kliem
Copy link
Contributor Author

kliem commented Mar 16, 2021

comment:7

Replying to @kiwifb:

Seems fair. Why is the extra test in python3/spkg-configure needed? Is it to deal with case where the compiler used to build python is not the same as the rest of sage?

Exactly. In #27122 I didn't consider this and then we had to solve #30725. This was the solution we came up with (just reject OpenMP alltogether, if we cannot find consistent arguments to make it work.)

We don't try as hard as we could (if sage uses gcc, but for cython clang, it still might be possible to enable OpenMP for cython with different arguments). But this solution here should enable OpenMP in cython on many platforms and still work on the others.

@kiwifb
Copy link
Member

kiwifb commented Mar 16, 2021

comment:8

Argh!!! I was going to answer that - hours ago - but I got interrupted by my day work (which is currently horrible, 2021 so far is worse for me than the whole of 2020).

ax_cv_[]_AC_LANG_ABBREV[]_openmp=unknown
# Flags to try:  -fopenmp (gcc), -mp (SGI & PGI),
#                -qopenmp (icc>=15), -openmp (icc),
#                -xopenmp (Sun), -omp (Tru64),
#                -qsmp=omp (AIX),
#                none

As you can see there is only a single switch in each case which tells the compiler what headers and macro to enable at compile time and what library to use at link time. The greatest danger usually is that people, or build systems, forget you should also include the CFLAGS at linking time. Very often -lgomp or similar is missing because of that oversight.

I guess that proof things in case things change in the future (most likely several years).

@kliem
Copy link
Contributor Author

kliem commented Mar 23, 2021

@mkoeppe
Copy link
Member

mkoeppe commented Mar 24, 2021

comment:10

It would be better to set the new variables in sage_conf.py.in, not sage-env-config.in, because they are not used as environment variables. See comments at the top of these files.

And I think you can get rid of SAGE_HAVE_OPENMP completely - in build/pkgs/python3/spkg-configure.m4 just check whether the flags are nonempty.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 24, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

3e415ceMerge branch 'u/gh-kliem/check_openmp_at_configuration' of git://trac.sagemath.org/sage into u/gh-kliem/check_openmp_at_configuration_reb
499af5dremove SAGE_HAVE_OPENMP
7b3da21move to sage_conf.py.in

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 24, 2021

Changed commit from a9d3dfe to 7b3da21

@mkoeppe
Copy link
Member

mkoeppe commented Mar 24, 2021

comment:12

I guess that should be AS_IF([test -n "$OPENMP_CFLAGS$OPENMP_CXXFLAGS"] (test both)

@mkoeppe
Copy link
Member

mkoeppe commented Mar 24, 2021

comment:13

And SAGE_OPENMP_CFLAGS.split() if SAGE_OPENMP_CFLAGS else [] can be simplified to just SAGE_OPENMP_CFLAGS.split()

@mkoeppe
Copy link
Member

mkoeppe commented Mar 24, 2021

comment:14

Replying to @mkoeppe:

And SAGE_OPENMP_CFLAGS.split() if SAGE_OPENMP_CFLAGS else [] can be simplified to just SAGE_OPENMP_CFLAGS.split()

... if you set a default of "" in env.py

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 24, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

927291fsimplifications
d71c54aset default
7d3f230more solid check

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 24, 2021

Changed commit from 7b3da21 to 7d3f230

@mkoeppe
Copy link
Member

mkoeppe commented Mar 24, 2021

comment:16

On macOS (using /usr/bin/gcc) I am getting

checking whether C++ compiler accepts "-march=native"... yes
checking for OpenMP flag of C compiler... unknown
checking for OpenMP flag of C++ compiler... unknown

is this expected?

@mkoeppe
Copy link
Member

mkoeppe commented Mar 24, 2021

comment:17

Also, best to merge #31384 which touches the same function and will cause a merge conflict

@kliem
Copy link
Contributor Author

kliem commented Mar 24, 2021

comment:18

Replying to @mkoeppe:

On macOS (using /usr/bin/gcc) I am getting

checking whether C++ compiler accepts "-march=native"... yes
checking for OpenMP flag of C compiler... unknown
checking for OpenMP flag of C++ compiler... unknown

is this expected?

macOS might be a bit more complicated. https://stackoverflow.com/questions/66040039/openmp-support-for-mac-using-clang-or-gcc

I'm happy to add whatever to the list to make it work. Maybe -Xclang -fopenmp will work for you?

@kliem
Copy link
Contributor Author

kliem commented Mar 24, 2021

comment:19

I usually take the example from src/sage/env.py to check whether it works.

@kliem
Copy link
Contributor Author

kliem commented Mar 24, 2021

comment:21

How about -fopenmp=libgomp? Found this is in normaliz (but it is commented out).

@mkoeppe
Copy link
Member

mkoeppe commented Mar 25, 2021

comment:22

Let's just take this to mean that Xcode's gcc can't do OpenMP.
Good enough for this ticket.

@mkoeppe
Copy link
Member

mkoeppe commented Mar 25, 2021

Changed reviewer from https://github.com/kliem/sage/pull/43/checks to Matthias Koeppe

@kliem
Copy link
Contributor Author

kliem commented Mar 25, 2021

comment:23

Thank you.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 25, 2021

Branch pushed to git repo; I updated commit sha1 and set ticket back to needs_review. New commits:

4f42440sage.env.cython_aliases: Do not fail if one of the listed libraries is not known to pkgconfig
3cb8f90sage.env.cython_aliases: Make module lapack optional; generalize the interface
c3cc31emerge in 31384

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 25, 2021

Changed commit from 7d3f230 to c3cc31e

@kliem
Copy link
Contributor Author

kliem commented Mar 25, 2021

comment:25

Merged in #31384 as requested.

@kliem
Copy link
Contributor Author

kliem commented Mar 25, 2021

Dependencies: #31384

@mkoeppe
Copy link
Member

mkoeppe commented Apr 7, 2021

comment:26

Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Apr 7, 2021
@vbraun
Copy link
Member

vbraun commented May 27, 2021

Changed branch from u/gh-kliem/check_openmp_at_configuration to c3cc31e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants