Skip to content

Commit

Permalink
Adding support to not native x86 systems for external libraries (#732)
Browse files Browse the repository at this point in the history
- Filtering of x86 specific directive at both `Makefile` and `setup.py`
level. Former needed for libraries that integrate Stim at the binary
level (e.g. PyMatching).
- Replacing `sys` with `platform` to gain access to additional
information on the OS and CPU architecture.
- Wrapped up with a function the extensions that shall be built for a
specific architecture.
  • Loading branch information
mghibaudi authored Apr 12, 2024
1 parent 3a430f2 commit f535af2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
21 changes: 13 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY out)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY out)

# Convert desired SIMD_WIDTH into machine architecture flags.
if(NOT(SIMD_WIDTH))
set(MACHINE_FLAG "-march=native")
elseif(SIMD_WIDTH EQUAL 256)
set(MACHINE_FLAG "-mavx2" "-msse2")
elseif(SIMD_WIDTH EQUAL 128)
set(MACHINE_FLAG "-mno-avx2" "-msse2")
elseif(SIMD_WIDTH EQUAL 64)
set(MACHINE_FLAG "-mno-avx2" "-mno-sse2")

if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|I386|ARM64)$")
if(NOT(SIMD_WIDTH))
set(MACHINE_FLAG "-march=native")
elseif(SIMD_WIDTH EQUAL 256)
set(MACHINE_FLAG "-mavx2" "-msse2")
elseif(SIMD_WIDTH EQUAL 128)
set(MACHINE_FLAG "-mno-avx2" "-msse2")
elseif(SIMD_WIDTH EQUAL 64)
set(MACHINE_FLAG "-mno-avx2" "-mno-sse2")
endif()
else ()
set(MACHINE_FLAG "")
endif()

# make changes to file_lists trigger a reconfigure
Expand Down
23 changes: 14 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import platform

from setuptools import setup, Extension
import glob
Expand All @@ -27,7 +27,7 @@

__version__ = '1.14.dev0'

if sys.platform.startswith('win'):
if platform.system().startswith('Win'):
common_compile_args = [
'/std:c++20',
'/O2',
Expand Down Expand Up @@ -97,6 +97,17 @@
with open('glue/python/README.md', encoding='UTF-8') as f:
long_description = f.read()

def _get_extensions():
archs=["x86", "i686", "i386", "amd64"]
if any(_ext in platform.processor().lower() for _ext in archs):
# NOTE: disabled until https://github.com/quantumlib/Stim/issues/432 is fixed
# stim_avx2,
return [stim_detect_machine_architecture, stim_polyfill,
# stim_avx2,
stim_sse2]
else:
return [stim_detect_machine_architecture, stim_polyfill]

setup(
name='stim',
version=__version__,
Expand All @@ -107,13 +118,7 @@
description='A fast library for analyzing with quantum stabilizer circuits.',
long_description=long_description,
long_description_content_type='text/markdown',
ext_modules=[
stim_detect_machine_architecture,
stim_polyfill,
stim_sse2,
# NOTE: disabled until https://github.com/quantumlib/Stim/issues/432 is fixed
# stim_avx2,
],
ext_modules=_get_extensions(),
python_requires='>=3.6.0',
packages=['stim'],
package_dir={'stim': 'glue/python/src/stim'},
Expand Down

0 comments on commit f535af2

Please sign in to comment.