Skip to content

Commit bc87faa

Browse files
campospintoyguclu
andauthored
Fix CPU flags in psydac/api/settings.py (#569)
Clean up platform-specific flags for GCC compilers. In particular, do not use the plausible but inexistent flag `-mcpu=apple-m4` on Apple M4 computers (which use ARM chips). Instead, always use `-march=native`, and additionally `-mavx` on `x86_64` architectures. Further, do not use `-mtune=native` as this is not always supported. These changes fix #568 --------- Co-authored-by: Yaman Güçlü <yaman.guclu@gmail.com>
1 parent 4f78a33 commit bc87faa

1 file changed

Lines changed: 30 additions & 28 deletions

File tree

psydac/api/settings.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# LICENSE file or go to https://github.com/pyccel/psydac/blob/devel/LICENSE #
44
# for full license details. #
55
#---------------------------------------------------------------------------#
6-
import subprocess # nosec B404
6+
# import subprocess # nosec B404
77
import platform
8-
import re
9-
from packaging.version import Version
8+
# import re
9+
# from packaging.version import Version
1010

1111

1212
__all__ = ('PSYDAC_DEFAULT_FOLDER', 'PSYDAC_BACKENDS')
@@ -20,11 +20,14 @@
2020

2121
PSYDAC_BACKEND_GPYCCEL = {'name': 'pyccel',
2222
'compiler_family': 'GNU',
23-
'flags' : '-O3 -ffast-math',
23+
'flags' : '-O3 -ffast-math -march=native',
2424
'folder' : '__gpyccel__',
2525
'tag' : 'gpyccel',
2626
'openmp' : False}
2727

28+
if platform.machine() == 'x86_64':
29+
PSYDAC_BACKEND_GPYCCEL['flags'] += ' -mavx'
30+
2831
PSYDAC_BACKEND_IPYCCEL = {'name': 'pyccel',
2932
'compiler_family': 'intel',
3033
'flags' : '-O3',
@@ -47,32 +50,31 @@
4750
'openmp' : False}
4851
# ...
4952

50-
# Get gfortran version
51-
gfortran_version_output = subprocess.check_output(['gfortran', '--version']).decode('utf-8') # nosec B603, B607
52-
gfortran_version_string = re.search(r"(\d+\.\d+\.\d+)", gfortran_version_output).group()
53-
gfortran_version = Version(gfortran_version_string)
54-
55-
# Platform-dependent flags
56-
if platform.system() == "Darwin" and platform.machine() == 'arm64' and gfortran_version >= Version("14"):
53+
# Get gfortran version [MCP 19.02.2026: commented since currently not needed]
54+
# gfortran_version_output = subprocess.check_output(['gfortran', '--version']).decode('utf-8') # nosec B603, B607
55+
# gfortran_version_string = re.search(r"(\d+\.\d+\.\d+)", gfortran_version_output).group()
56+
# gfortran_version = Version(gfortran_version_string)
5757

58-
# Apple silicon requires architecture-specific flags (see https://github.com/pyccel/psydac/pull/411)
59-
# which are only available on GCC version >= 14
60-
cpu_brand = subprocess.check_output(['sysctl','-n','machdep.cpu.brand_string']).decode('utf-8').strip() # nosec B603, B607
61-
if cpu_brand.startswith("Apple M"):
62-
# Example: "Apple M3 Pro (virtual)" --> " -mcpu=apple-m3"
63-
cpu_flag = '-'.join(cpu_brand.lower().split()[:2])
64-
PSYDAC_BACKEND_GPYCCEL['flags'] += f' -mcpu={cpu_flag}'
65-
else:
66-
# TODO: Support later Apple CPU models. Perhaps the CPU naming scheme could be easily guessed
67-
# based on the output of 'sysctl -n machdep.cpu.brand_string', but I wouldn't rely on this
68-
# guess unless it has been manually verified. Loud errors are better than silent failures!
69-
raise SystemError(f"Unsupported Apple CPU '{cpu_brand}'.")
58+
# if platform.system() == "Darwin" and platform.machine() == 'arm64' and gfortran_version >= Version("14"):
59+
#
60+
# # Apple silicon requires architecture-specific flags (see https://github.com/pyccel/psydac/pull/411)
61+
# # which are only available on GCC version >= 14
62+
# cpu_brand = subprocess.check_output(['sysctl','-n','machdep.cpu.brand_string']).decode('utf-8').strip() # nosec B603, B607
63+
# if cpu_brand.startswith("Apple M"):
64+
# # Example: "Apple M3 Pro (virtual)" --> " -mcpu=apple-m3"
65+
# cpu_flag = '-'.join(cpu_brand.lower().split()[:2])
66+
# PSYDAC_BACKEND_GPYCCEL['flags'] += f' -mcpu={cpu_flag}'
67+
# else:
68+
# # TODO: Support later Apple CPU models. Perhaps the CPU naming scheme could be easily guessed
69+
# # based on the output of 'sysctl -n machdep.cpu.brand_string', but I wouldn't rely on this
70+
# # guess unless it has been manually verified. Loud errors are better than silent failures!
71+
# raise SystemError(f"Unsupported Apple CPU '{cpu_brand}'.")
7072

71-
else:
72-
# Default architecture flags
73-
PSYDAC_BACKEND_GPYCCEL['flags'] += ' -march=native -mtune=native'
74-
if platform.machine() == 'x86_64':
75-
PSYDAC_BACKEND_GPYCCEL['flags'] += ' -mavx'
73+
# else:
74+
# # Default architecture flags
75+
# PSYDAC_BACKEND_GPYCCEL['flags'] += ' -march=native -mtune=native'
76+
# if platform.machine() == 'x86_64':
77+
# PSYDAC_BACKEND_GPYCCEL['flags'] += ' -mavx'
7678

7779
#==============================================================================
7880

0 commit comments

Comments
 (0)