From b4bc7bfd8187382bff1903fb5c2bd08e352c1570 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 08:28:37 -0800 Subject: [PATCH 01/21] Compile for M1 --- CMakeLists.txt | 48 ++++++++----- pybind_interface/decide/CMakeLists.txt | 94 ++++++++++++++++---------- pybind_interface/decide/decide.cpp | 14 ++-- 3 files changed, 98 insertions(+), 58 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f902a421..d4574b73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,24 +1,38 @@ cmake_minimum_required(VERSION 3.11) -execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) -if(has_nvcc STREQUAL "") - execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc) - if(has_hipcc STREQUAL "") - project(qsim) - else() - project(qsim LANGUAGES CXX HIP) - ADD_SUBDIRECTORY(pybind_interface/hip) - endif() +# Check if we are on an Apple system and if the architecture is ARM +if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") + # Set a variable to indicate we are on Apple ARM + set(APPLE_ARM TRUE) else() - project(qsim LANGUAGES CXX CUDA) - ADD_SUBDIRECTORY(pybind_interface/cuda) - if(DEFINED ENV{CUQUANTUM_ROOT}) - ADD_SUBDIRECTORY(pybind_interface/custatevec) - endif() + set(APPLE_ARM FALSE) endif() -ADD_SUBDIRECTORY(pybind_interface/sse) -ADD_SUBDIRECTORY(pybind_interface/avx512) -ADD_SUBDIRECTORY(pybind_interface/avx2) ADD_SUBDIRECTORY(pybind_interface/basic) ADD_SUBDIRECTORY(pybind_interface/decide) + +# You can then use this variable to control conditional compilation +if(APPLE_ARM) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a") +else() + execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) + if(has_nvcc STREQUAL "") + execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc) + if(has_hipcc STREQUAL "") + project(qsim) + else() + project(qsim LANGUAGES CXX HIP) + ADD_SUBDIRECTORY(pybind_interface/hip) + endif() + else() + project(qsim LANGUAGES CXX CUDA) + ADD_SUBDIRECTORY(pybind_interface/cuda) + if(DEFINED ENV{CUQUANTUM_ROOT}) + ADD_SUBDIRECTORY(pybind_interface/custatevec) + endif() + endif() + + ADD_SUBDIRECTORY(pybind_interface/sse) + ADD_SUBDIRECTORY(pybind_interface/avx512) + ADD_SUBDIRECTORY(pybind_interface/avx2) +endif() diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt index b94b0ade..182e10f7 100644 --- a/pybind_interface/decide/CMakeLists.txt +++ b/pybind_interface/decide/CMakeLists.txt @@ -1,15 +1,28 @@ cmake_minimum_required(VERSION 3.11) -execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) -if(has_nvcc STREQUAL "") - execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc) - if(has_hipcc STREQUAL "") - project(qsim) - else() - project(qsim LANGUAGES CXX HIP) - endif() +# Check if we are on an Apple system and if the architecture is ARM +if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") + # Set a variable to indicate we are on Apple ARM + set(APPLE_ARM TRUE) else() - project(qsim LANGUAGES CXX CUDA) + set(APPLE_ARM FALSE) +endif() + +# Set the project based on the detected platform +if(APPLE_ARM) + project(qsim LANGUAGES CXX) +else() + execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) + if(has_nvcc STREQUAL "") + execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc) + if(has_hipcc STREQUAL "") + project(qsim) + else() + project(qsim LANGUAGES CXX HIP) + endif() + else() + project(qsim LANGUAGES CXX CUDA) + endif() endif() IF (WIN32) @@ -26,41 +39,48 @@ endif() INCLUDE(../GetPybind11.cmake) -if(has_nvcc STREQUAL "") - if(has_hipcc STREQUAL "") +# Configure based on the detected platform +if(APPLE_ARM) + # Configuration for Apple ARM + # Set additional flags if necessary + # Compile the module using standard C++ pybind11_add_module(qsim_decide decide.cpp) - else() - list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip") - find_package(HIP REQUIRED) - find_package(PythonLibs 3.7 REQUIRED) - include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) +elif(has_nvcc STREQUAL "") + if(has_hipcc STREQUAL "") + pybind11_add_module(qsim_decide decide.cpp) + else() + list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip") + find_package(HIP REQUIRED) + find_package(PythonLibs 3.7 REQUIRED) - hip_add_library(qsim_decide MODULE decide.cpp) + include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) - set_target_properties(qsim_decide PROPERTIES - PREFIX "${PYTHON_MODULE_PREFIX}" - SUFFIX "${PYTHON_MODULE_EXTENSION}" - ) - set_source_files_properties(decide.cpp PROPERTIES LANGUAGE HIP) - endif() + hip_add_library(qsim_decide MODULE decide.cpp) + + set_target_properties(qsim_decide PROPERTIES + PREFIX "${PYTHON_MODULE_PREFIX}" + SUFFIX "${PYTHON_MODULE_EXTENSION}" + ) + set_source_files_properties(decide.cpp PROPERTIES LANGUAGE HIP) + endif() else() - find_package(PythonLibs 3.7 REQUIRED) - find_package(CUDA REQUIRED) + find_package(PythonLibs 3.7 REQUIRED) + find_package(CUDA REQUIRED) - include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) + include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) - cuda_add_library(qsim_decide MODULE decide.cpp) + cuda_add_library(qsim_decide MODULE decide.cpp) - if(DEFINED ENV{CUQUANTUM_ROOT}) - target_compile_options(qsim_decide PRIVATE - $<$:-D__CUSTATEVEC__> - ) - endif() + if(DEFINED ENV{CUQUANTUM_ROOT}) + target_compile_options(qsim_decide PRIVATE + $<$:-D__CUSTATEVEC__> + ) + endif() - set_target_properties(qsim_decide PROPERTIES - PREFIX "${PYTHON_MODULE_PREFIX}" - SUFFIX "${PYTHON_MODULE_EXTENSION}" - ) - set_source_files_properties(decide.cpp PROPERTIES LANGUAGE CUDA) + set_target_properties(qsim_decide PROPERTIES + PREFIX "${PYTHON_MODULE_PREFIX}" + SUFFIX "${PYTHON_MODULE_EXTENSION}" + ) + set_source_files_properties(decide.cpp PROPERTIES LANGUAGE CUDA) endif() diff --git a/pybind_interface/decide/decide.cpp b/pybind_interface/decide/decide.cpp index 5e9a63f1..c611ac39 100644 --- a/pybind_interface/decide/decide.cpp +++ b/pybind_interface/decide/decide.cpp @@ -34,10 +34,15 @@ enum Instructions { AVX512F = 0, AVX2 = 1, SSE4_1 = 2, BASIC = 3}; int detect_instructions() { Instructions instr = BASIC; - int info[4]; + // Check for ARM architecture, specifically for Apple M1 or similar + #if defined(__aarch64__) && defined(__APPLE__) + // On Apple ARM systems, always use BASIC + instr = BASIC; + #else + // Existing x86/x86_64 specific instruction set detection logic + int info[4]; cpuid(info, 0); - int nIds = info[0]; if (nIds >= 1) { cpuid(info, 1); @@ -47,18 +52,19 @@ int detect_instructions() { } if (nIds >= 7) { cpuid(info, 7); - if ((info[1] & (1 << 5))!= 0) { + if ((info[1] & (1 << 5)) != 0) { instr = AVX2; } if ((info[1] & (1 << 16)) != 0) { instr = AVX512F; } - } + #endif return static_cast(instr); } + enum GPUCapabilities { CUDA = 0, CUSTATEVEC = 1, HIP = 2, NO_GPU = 10, NO_CUSTATEVEC = 11 }; From 7e2a8f9681dd94a3f209b23922f613f1a42f7598 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 09:53:10 -0800 Subject: [PATCH 02/21] Make qsimcirq Apple Silicon (M1, M2, ARM64) compatible --- CMakeLists.txt | 22 ++++++++++++++++------ Makefile | 4 ++-- pybind_interface/GetPybind11.cmake | 1 + pybind_interface/decide/CMakeLists.txt | 24 ++++++++++++++++-------- pybind_interface/decide/decide.cpp | 4 ++-- setup.py | 4 ++-- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4574b73..98aae939 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,25 +1,34 @@ +set(CMAKE_CXX_STANDARD 11) +set(OpenMP_INCLUDE_DIR "/Users/bas.nijholt/micromamba/lib/clang/17.0.4/include") +include_directories(${OpenMP_INCLUDE_DIR}) cmake_minimum_required(VERSION 3.11) -# Check if we are on an Apple system and if the architecture is ARM -if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") - # Set a variable to indicate we are on Apple ARM - set(APPLE_ARM TRUE) +if(APPLE) + # Returns x86_64 or arm64 + execute_process(COMMAND uname -m OUTPUT_VARIABLE OSX_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE) + if (OSX_ARCH STREQUAL "arm64") + set(APPLE_ARM TRUE) + else() + set(APPLE_ARM FALSE) + endif() else() set(APPLE_ARM FALSE) -endif() +endif(APPLE) +message(STATUS "APPLE_ARM: ${APPLE_ARM}") ADD_SUBDIRECTORY(pybind_interface/basic) ADD_SUBDIRECTORY(pybind_interface/decide) # You can then use this variable to control conditional compilation if(APPLE_ARM) + project(qsim LANGUAGES CXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a") else() execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) if(has_nvcc STREQUAL "") execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc) if(has_hipcc STREQUAL "") - project(qsim) + project(qsim LANGUAGES CXX) else() project(qsim LANGUAGES CXX HIP) ADD_SUBDIRECTORY(pybind_interface/hip) @@ -36,3 +45,4 @@ else() ADD_SUBDIRECTORY(pybind_interface/avx512) ADD_SUBDIRECTORY(pybind_interface/avx2) endif() +find_package(OpenMP REQUIRED) diff --git a/Makefile b/Makefile index 227ca603..7ebbaaba 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,11 @@ EIGEN_URL = "https://gitlab.com/libeigen/eigen/-/archive/" TARGETS = qsim TESTS = run-cxx-tests -CXX=g++ +CXX=x86_64-apple-darwin13.4.0-clang++ NVCC=nvcc HIPCC=hipcc -CXXFLAGS = -O3 -fopenmp +CXXFLAGS = -O3 -fopenmp -I ARCHFLAGS = -march=native NVCCFLAGS = -O3 HIPCCFLAGS = -O3 diff --git a/pybind_interface/GetPybind11.cmake b/pybind_interface/GetPybind11.cmake index e8516a0b..56223801 100644 --- a/pybind_interface/GetPybind11.cmake +++ b/pybind_interface/GetPybind11.cmake @@ -1,3 +1,4 @@ +add_compile_options(-I/Users/bas.nijholt/micromamba/envs/py311/include/python3.11 -I/Users/bas.nijholt/micromamba/envs/py311/lib/python3.11/site-packages/pybind11/include) include(FetchContent) set(MIN_PYBIND_VERSION "2.10.1") diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt index 182e10f7..b3b24ca4 100644 --- a/pybind_interface/decide/CMakeLists.txt +++ b/pybind_interface/decide/CMakeLists.txt @@ -1,12 +1,19 @@ cmake_minimum_required(VERSION 3.11) +set(CMAKE_SYSTEM_PROCESSOR arm64) # Check if we are on an Apple system and if the architecture is ARM -if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") - # Set a variable to indicate we are on Apple ARM - set(APPLE_ARM TRUE) +if(APPLE) + # Returns x86_64 or arm64 + execute_process(COMMAND uname -m OUTPUT_VARIABLE CMAKE_OSX_ARCHITECTURES OUTPUT_STRIP_TRAILING_WHITESPACE) + if (OSX_ARCH STREQUAL "arm64") + set(APPLE_ARM TRUE) + else() + set(APPLE_ARM FALSE) + endif() else() set(APPLE_ARM FALSE) -endif() +endif(APPLE) +message(STATUS "APPLE_ARM: ${APPLE_ARM}") # Set the project based on the detected platform if(APPLE_ARM) @@ -45,14 +52,15 @@ if(APPLE_ARM) # Set additional flags if necessary # Compile the module using standard C++ pybind11_add_module(qsim_decide decide.cpp) - -elif(has_nvcc STREQUAL "") + # Print a message to indicate the module is being compiled using standard C++ + message(STATUS "Compiling qsim_decide using standard C++ on Apple ARM") +elseif(has_nvcc STREQUAL "") if(has_hipcc STREQUAL "") pybind11_add_module(qsim_decide decide.cpp) else() list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip") find_package(HIP REQUIRED) - find_package(PythonLibs 3.7 REQUIRED) + find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter Development) include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) @@ -65,7 +73,7 @@ elif(has_nvcc STREQUAL "") set_source_files_properties(decide.cpp PROPERTIES LANGUAGE HIP) endif() else() - find_package(PythonLibs 3.7 REQUIRED) + find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter Development) find_package(CUDA REQUIRED) include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) diff --git a/pybind_interface/decide/decide.cpp b/pybind_interface/decide/decide.cpp index c611ac39..3641833c 100644 --- a/pybind_interface/decide/decide.cpp +++ b/pybind_interface/decide/decide.cpp @@ -21,8 +21,8 @@ namespace py = pybind11; #include #define cpuid(info, x) __cpuidex(info, x, 0) -#else -// GCC Intrinsics +#elif defined(__x86_64__) || defined(__i386__) +// GCC Intrinsics for x86/x86_64 #include void cpuid(int info[4], int infoType){ __cpuid_count(infoType, 0, info[0], info[1], info[2], info[3]); diff --git a/setup.py b/setup.py index 593620a6..06b7ba16 100644 --- a/setup.py +++ b/setup.py @@ -60,8 +60,8 @@ def build_extension(self, ext): if platform.system() == "Darwin": cmake_args += [ - "-DCMAKE_C_COMPILER=/usr/local/opt/llvm/bin/clang", - "-DCMAKE_CXX_COMPILER=/usr/local/opt/llvm/bin/clang++", + "-DCMAKE_C_COMPILER=/Users/bas.nijholt/micromamba/envs/py311/bin/clang", + "-DCMAKE_CXX_COMPILER=/Users/bas.nijholt/micromamba/envs/py311/bin/clang++", ] if shutil.which("hipcc") is not None: From 66949709c6879730481d50c19c7cb8c3d6a5aac5 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 10:01:37 -0800 Subject: [PATCH 03/21] make work --- Makefile | 4 ++-- pybind_interface/decide/CMakeLists.txt | 23 +---------------------- pybind_interface/decide/decide.cpp | 1 - 3 files changed, 3 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 7ebbaaba..227ca603 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,11 @@ EIGEN_URL = "https://gitlab.com/libeigen/eigen/-/archive/" TARGETS = qsim TESTS = run-cxx-tests -CXX=x86_64-apple-darwin13.4.0-clang++ +CXX=g++ NVCC=nvcc HIPCC=hipcc -CXXFLAGS = -O3 -fopenmp -I +CXXFLAGS = -O3 -fopenmp ARCHFLAGS = -march=native NVCCFLAGS = -O3 HIPCCFLAGS = -O3 diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt index b3b24ca4..7724a3e9 100644 --- a/pybind_interface/decide/CMakeLists.txt +++ b/pybind_interface/decide/CMakeLists.txt @@ -1,21 +1,5 @@ cmake_minimum_required(VERSION 3.11) -set(CMAKE_SYSTEM_PROCESSOR arm64) -# Check if we are on an Apple system and if the architecture is ARM -if(APPLE) - # Returns x86_64 or arm64 - execute_process(COMMAND uname -m OUTPUT_VARIABLE CMAKE_OSX_ARCHITECTURES OUTPUT_STRIP_TRAILING_WHITESPACE) - if (OSX_ARCH STREQUAL "arm64") - set(APPLE_ARM TRUE) - else() - set(APPLE_ARM FALSE) - endif() -else() - set(APPLE_ARM FALSE) -endif(APPLE) -message(STATUS "APPLE_ARM: ${APPLE_ARM}") - -# Set the project based on the detected platform if(APPLE_ARM) project(qsim LANGUAGES CXX) else() @@ -38,7 +22,7 @@ ELSE() set(CMAKE_CXX_FLAGS "-O3 -fopenmp") ENDIF() -if(APPLE) +if(APPLE AND NOT APPLE_ARM) set(CMAKE_CXX_STANDARD 14) include_directories("/usr/local/include" "/usr/local/opt/llvm/include") link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib") @@ -48,12 +32,7 @@ INCLUDE(../GetPybind11.cmake) # Configure based on the detected platform if(APPLE_ARM) - # Configuration for Apple ARM - # Set additional flags if necessary - # Compile the module using standard C++ pybind11_add_module(qsim_decide decide.cpp) - # Print a message to indicate the module is being compiled using standard C++ - message(STATUS "Compiling qsim_decide using standard C++ on Apple ARM") elseif(has_nvcc STREQUAL "") if(has_hipcc STREQUAL "") pybind11_add_module(qsim_decide decide.cpp) diff --git a/pybind_interface/decide/decide.cpp b/pybind_interface/decide/decide.cpp index 3641833c..0da7b0a8 100644 --- a/pybind_interface/decide/decide.cpp +++ b/pybind_interface/decide/decide.cpp @@ -64,7 +64,6 @@ int detect_instructions() { return static_cast(instr); } - enum GPUCapabilities { CUDA = 0, CUSTATEVEC = 1, HIP = 2, NO_GPU = 10, NO_CUSTATEVEC = 11 }; From 44ed30523d590a042131cc855581b3eb24d528c6 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 10:10:39 -0800 Subject: [PATCH 04/21] Support Apple x86 and ARM in setup.py with homebrew and current PATH --- setup.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 06b7ba16..f460b2b5 100644 --- a/setup.py +++ b/setup.py @@ -59,10 +59,23 @@ def build_extension(self, ext): build_args += ["--", "-j2"] if platform.system() == "Darwin": - cmake_args += [ - "-DCMAKE_C_COMPILER=/Users/bas.nijholt/micromamba/envs/py311/bin/clang", - "-DCMAKE_CXX_COMPILER=/Users/bas.nijholt/micromamba/envs/py311/bin/clang++", - ] + homebrew_x86 = "/usr/local/opt/llvm/bin" + homebrew_arm = "/opt/homebrew/opt/llvm/bin" + # Add clang + if shutil.which("clang") is not None: + cmake_args.append("-DCMAKE_C_COMPILER=clang") + elif os.path.exists(f"{homebrew_x86}/clang"): + cmake_args.append(f"-DCMAKE_C_COMPILER={homebrew_x86}/clang") + elif os.path.exists(f"{homebrew_arm}/clang"): + cmake_args.append(f"-DCMAKE_C_COMPILER={homebrew_arm}/clang") + + # Add clang++ + if shutil.which("clang++") is not None: + cmake_args.append("-DCMAKE_CXX_COMPILER=clang++") + elif os.path.exists(f"{homebrew_x86}/clang++"): + cmake_args.append(f"-DCMAKE_CXX_COMPILER={homebrew_x86}/clang++") + elif os.path.exists(f"{homebrew_arm}/clang++"): + cmake_args.append(f"-DCMAKE_CXX_COMPILER={homebrew_arm}/clang++") if shutil.which("hipcc") is not None: cmake_args += [ From 31536d81adfaee14279b136368042ad1acd5e565 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 10:22:23 -0800 Subject: [PATCH 05/21] Style --- CMakeLists.txt | 15 +++++++++++---- setup.py | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 98aae939..ff9148f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,12 +3,12 @@ set(OpenMP_INCLUDE_DIR "/Users/bas.nijholt/micromamba/lib/clang/17.0.4/include") include_directories(${OpenMP_INCLUDE_DIR}) cmake_minimum_required(VERSION 3.11) +# Set APPLE_ARM to TRUE if running on Apple Silicon if(APPLE) - # Returns x86_64 or arm64 execute_process(COMMAND uname -m OUTPUT_VARIABLE OSX_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE) if (OSX_ARCH STREQUAL "arm64") set(APPLE_ARM TRUE) - else() + else() # x86_64 set(APPLE_ARM FALSE) endif() else() @@ -16,10 +16,14 @@ else() endif(APPLE) message(STATUS "APPLE_ARM: ${APPLE_ARM}") +message(STATUS "CXX Flags: " ${CMAKE_CXX_FLAGS}) +message(STATUS "C Flags: " ${CMAKE_C_FLAGS}) + + +# basic and decide can be compiled with any compiler ADD_SUBDIRECTORY(pybind_interface/basic) ADD_SUBDIRECTORY(pybind_interface/decide) -# You can then use this variable to control conditional compilation if(APPLE_ARM) project(qsim LANGUAGES CXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a") @@ -45,4 +49,7 @@ else() ADD_SUBDIRECTORY(pybind_interface/avx512) ADD_SUBDIRECTORY(pybind_interface/avx2) endif() -find_package(OpenMP REQUIRED) + +if(APPLE) + find_package(OpenMP REQUIRED) # Automatically happens for Linux +endif() diff --git a/setup.py b/setup.py index f460b2b5..ed4720c3 100644 --- a/setup.py +++ b/setup.py @@ -62,7 +62,7 @@ def build_extension(self, ext): homebrew_x86 = "/usr/local/opt/llvm/bin" homebrew_arm = "/opt/homebrew/opt/llvm/bin" # Add clang - if shutil.which("clang") is not None: + if shutil.which("clang") is not None: # Always prefer from PATH cmake_args.append("-DCMAKE_C_COMPILER=clang") elif os.path.exists(f"{homebrew_x86}/clang"): cmake_args.append(f"-DCMAKE_C_COMPILER={homebrew_x86}/clang") @@ -70,7 +70,7 @@ def build_extension(self, ext): cmake_args.append(f"-DCMAKE_C_COMPILER={homebrew_arm}/clang") # Add clang++ - if shutil.which("clang++") is not None: + if shutil.which("clang++") is not None: # Always prefer from PATH cmake_args.append("-DCMAKE_CXX_COMPILER=clang++") elif os.path.exists(f"{homebrew_x86}/clang++"): cmake_args.append(f"-DCMAKE_CXX_COMPILER={homebrew_x86}/clang++") From a10c693031a91f4adebf829a16013bc85dfe5bb1 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 10:23:29 -0800 Subject: [PATCH 06/21] remove debug messages in cmake --- CMakeLists.txt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ff9148f1..bf87799c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,11 +14,6 @@ if(APPLE) else() set(APPLE_ARM FALSE) endif(APPLE) -message(STATUS "APPLE_ARM: ${APPLE_ARM}") - -message(STATUS "CXX Flags: " ${CMAKE_CXX_FLAGS}) -message(STATUS "C Flags: " ${CMAKE_C_FLAGS}) - # basic and decide can be compiled with any compiler ADD_SUBDIRECTORY(pybind_interface/basic) From 0fbbc89e0c1a61289f71e6637c1a53d65ccd18ed Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 10:55:01 -0800 Subject: [PATCH 07/21] Generalize pybind11 finding --- pybind_interface/GetPybind11.cmake | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pybind_interface/GetPybind11.cmake b/pybind_interface/GetPybind11.cmake index 56223801..9df238b9 100644 --- a/pybind_interface/GetPybind11.cmake +++ b/pybind_interface/GetPybind11.cmake @@ -1,4 +1,3 @@ -add_compile_options(-I/Users/bas.nijholt/micromamba/envs/py311/include/python3.11 -I/Users/bas.nijholt/micromamba/envs/py311/lib/python3.11/site-packages/pybind11/include) include(FetchContent) set(MIN_PYBIND_VERSION "2.10.1") @@ -9,6 +8,24 @@ FetchContent_Declare( ) FetchContent_GetProperties(pybind11) find_package(pybind11 "${MIN_PYBIND_VERSION}" CONFIG) + +if (pybind11_FOUND) + message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}") + # The pybind11_add_module doesn't correctly set the CXX_INCLUDES properly if a system pybind11 is found. + # Using `include_directories(...)` doesn't result in anything in + # CXX_INCLUDES. e.g., `pybind_interface/basic/CMakeFiles/qsim_basic.dir/flags.make` would only + # have `CXX_INCLUDES = -isystem $PREFIX/include/python3.11` and would miss `$PREFIX/include`. + # This problem would result in `fatal error: pybind11/complex.h: No such file or directory` + # This is a hack to get around that by passing `-I/path/to/include` to CXX_FLAGS + execute_process(COMMAND python3 -m pybind11 --includes OUTPUT_VARIABLE PYBIND11_INCLUDES OUTPUT_STRIP_TRAILING_WHITESPACE) + message(STATUS "pybind11 includes: ${PYBIND11_INCLUDES}") + # Separate the includes into a list and add them as individual compile options + separate_arguments(PYBIND11_INCLUDES_LIST UNIX_COMMAND "${PYBIND11_INCLUDES}") + foreach(INCLUDE_DIR ${PYBIND11_INCLUDES_LIST}) + add_compile_options("${INCLUDE_DIR}") + endforeach() +endif() + if((NOT pybind11_FOUND) AND (NOT pybind11_POPULATED)) # check first on system path, then attempt git fetch FetchContent_Populate(pybind11) add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR}) From 08ad3d74b22d3b758c1cc445b911e064c14ba615 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 10:55:28 -0800 Subject: [PATCH 08/21] Set project first --- CMakeLists.txt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf87799c..59c45c28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,13 +15,10 @@ else() set(APPLE_ARM FALSE) endif(APPLE) -# basic and decide can be compiled with any compiler -ADD_SUBDIRECTORY(pybind_interface/basic) -ADD_SUBDIRECTORY(pybind_interface/decide) - if(APPLE_ARM) project(qsim LANGUAGES CXX) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a") + ADD_SUBDIRECTORY(pybind_interface/basic) + ADD_SUBDIRECTORY(pybind_interface/decide) else() execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) if(has_nvcc STREQUAL "") @@ -43,6 +40,8 @@ else() ADD_SUBDIRECTORY(pybind_interface/sse) ADD_SUBDIRECTORY(pybind_interface/avx512) ADD_SUBDIRECTORY(pybind_interface/avx2) + ADD_SUBDIRECTORY(pybind_interface/basic) + ADD_SUBDIRECTORY(pybind_interface/decide) endif() if(APPLE) From e8b6ed6816e4a65f269524f1eec2206c14864b94 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 11:00:22 -0800 Subject: [PATCH 09/21] Simplify --- pybind_interface/GetPybind11.cmake | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pybind_interface/GetPybind11.cmake b/pybind_interface/GetPybind11.cmake index 9df238b9..9a55c904 100644 --- a/pybind_interface/GetPybind11.cmake +++ b/pybind_interface/GetPybind11.cmake @@ -12,17 +12,14 @@ find_package(pybind11 "${MIN_PYBIND_VERSION}" CONFIG) if (pybind11_FOUND) message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}") # The pybind11_add_module doesn't correctly set the CXX_INCLUDES properly if a system pybind11 is found. - # Using `include_directories(...)` doesn't result in anything in + # Using `include_directories(${pybind11_INCLUDE_DIRS})` doesn't result in anything in # CXX_INCLUDES. e.g., `pybind_interface/basic/CMakeFiles/qsim_basic.dir/flags.make` would only # have `CXX_INCLUDES = -isystem $PREFIX/include/python3.11` and would miss `$PREFIX/include`. # This problem would result in `fatal error: pybind11/complex.h: No such file or directory` # This is a hack to get around that by passing `-I/path/to/include` to CXX_FLAGS - execute_process(COMMAND python3 -m pybind11 --includes OUTPUT_VARIABLE PYBIND11_INCLUDES OUTPUT_STRIP_TRAILING_WHITESPACE) - message(STATUS "pybind11 includes: ${PYBIND11_INCLUDES}") - # Separate the includes into a list and add them as individual compile options - separate_arguments(PYBIND11_INCLUDES_LIST UNIX_COMMAND "${PYBIND11_INCLUDES}") - foreach(INCLUDE_DIR ${PYBIND11_INCLUDES_LIST}) - add_compile_options("${INCLUDE_DIR}") + # Iterate over each include directory and add it as a compile option + foreach(INCLUDE_DIR ${pybind11_INCLUDE_DIRS}) + add_compile_options("-I${INCLUDE_DIR}") endforeach() endif() From 423b8ed7d6b9f610b4a9217175c0263a46d395b5 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 11:10:40 -0800 Subject: [PATCH 10/21] Simplify logic in CMakeLists.txt --- CMakeLists.txt | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59c45c28..736cd4a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,4 @@ set(CMAKE_CXX_STANDARD 11) -set(OpenMP_INCLUDE_DIR "/Users/bas.nijholt/micromamba/lib/clang/17.0.4/include") -include_directories(${OpenMP_INCLUDE_DIR}) cmake_minimum_required(VERSION 3.11) # Set APPLE_ARM to TRUE if running on Apple Silicon @@ -15,10 +13,10 @@ else() set(APPLE_ARM FALSE) endif(APPLE) -if(APPLE_ARM) +# Set the project name and language +if(APPLE) project(qsim LANGUAGES CXX) - ADD_SUBDIRECTORY(pybind_interface/basic) - ADD_SUBDIRECTORY(pybind_interface/decide) + find_package(OpenMP REQUIRED) # Automatically happens for Linux else() execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) if(has_nvcc STREQUAL "") @@ -27,23 +25,26 @@ else() project(qsim LANGUAGES CXX) else() project(qsim LANGUAGES CXX HIP) - ADD_SUBDIRECTORY(pybind_interface/hip) endif() else() project(qsim LANGUAGES CXX CUDA) + endif() +endif() + +# Add subdirectories based on the architecture or available compilers +ADD_SUBDIRECTORY(pybind_interface/basic) +ADD_SUBDIRECTORY(pybind_interface/decide) +if(NOT APPLE_ARM) + if(NOT has_nvcc STREQUAL "") ADD_SUBDIRECTORY(pybind_interface/cuda) if(DEFINED ENV{CUQUANTUM_ROOT}) ADD_SUBDIRECTORY(pybind_interface/custatevec) endif() + elseif(NOT has_hipcc STREQUAL "") + ADD_SUBDIRECTORY(pybind_interface/hip) endif() ADD_SUBDIRECTORY(pybind_interface/sse) ADD_SUBDIRECTORY(pybind_interface/avx512) ADD_SUBDIRECTORY(pybind_interface/avx2) - ADD_SUBDIRECTORY(pybind_interface/basic) - ADD_SUBDIRECTORY(pybind_interface/decide) -endif() - -if(APPLE) - find_package(OpenMP REQUIRED) # Automatically happens for Linux endif() From e333c2ec39e56c7396b39d7590175be3fb0850a6 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 20 Nov 2023 11:46:21 -0800 Subject: [PATCH 11/21] Simplify CMakeLists.txt even more --- CMakeLists.txt | 20 ++++---- pybind_interface/decide/CMakeLists.txt | 71 ++++++++------------------ 2 files changed, 32 insertions(+), 59 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 736cd4a3..151ad6b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,16 +18,16 @@ if(APPLE) project(qsim LANGUAGES CXX) find_package(OpenMP REQUIRED) # Automatically happens for Linux else() - execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) - if(has_nvcc STREQUAL "") - execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc) - if(has_hipcc STREQUAL "") - project(qsim LANGUAGES CXX) - else() + execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc OUTPUT_STRIP_TRAILING_WHITESPACE) + if(has_nvcc) + project(qsim LANGUAGES CXX CUDA) + else() + execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc OUTPUT_STRIP_TRAILING_WHITESPACE) + if(has_hipcc) project(qsim LANGUAGES CXX HIP) + else() + project(qsim LANGUAGES CXX) endif() - else() - project(qsim LANGUAGES CXX CUDA) endif() endif() @@ -35,12 +35,12 @@ endif() ADD_SUBDIRECTORY(pybind_interface/basic) ADD_SUBDIRECTORY(pybind_interface/decide) if(NOT APPLE_ARM) - if(NOT has_nvcc STREQUAL "") + if(has_nvcc) ADD_SUBDIRECTORY(pybind_interface/cuda) if(DEFINED ENV{CUQUANTUM_ROOT}) ADD_SUBDIRECTORY(pybind_interface/custatevec) endif() - elseif(NOT has_hipcc STREQUAL "") + elseif(has_hipcc) ADD_SUBDIRECTORY(pybind_interface/hip) endif() diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt index 7724a3e9..159e8b1f 100644 --- a/pybind_interface/decide/CMakeLists.txt +++ b/pybind_interface/decide/CMakeLists.txt @@ -1,26 +1,10 @@ cmake_minimum_required(VERSION 3.11) -if(APPLE_ARM) - project(qsim LANGUAGES CXX) -else() - execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc) - if(has_nvcc STREQUAL "") - execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc) - if(has_hipcc STREQUAL "") - project(qsim) - else() - project(qsim LANGUAGES CXX HIP) - endif() - else() - project(qsim LANGUAGES CXX CUDA) - endif() -endif() - -IF (WIN32) +if(WIN32) set(CMAKE_CXX_FLAGS "/O2 /openmp") -ELSE() +else() set(CMAKE_CXX_FLAGS "-O3 -fopenmp") -ENDIF() +endif() if(APPLE AND NOT APPLE_ARM) set(CMAKE_CXX_STANDARD 14) @@ -28,46 +12,35 @@ if(APPLE AND NOT APPLE_ARM) link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib") endif() -INCLUDE(../GetPybind11.cmake) +include(../GetPybind11.cmake) # Configure based on the detected platform -if(APPLE_ARM) - pybind11_add_module(qsim_decide decide.cpp) -elseif(has_nvcc STREQUAL "") - if(has_hipcc STREQUAL "") - pybind11_add_module(qsim_decide decide.cpp) - else() - list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip") - find_package(HIP REQUIRED) - find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter Development) - - include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) - - hip_add_library(qsim_decide MODULE decide.cpp) - - set_target_properties(qsim_decide PROPERTIES - PREFIX "${PYTHON_MODULE_PREFIX}" - SUFFIX "${PYTHON_MODULE_EXTENSION}" - ) - set_source_files_properties(decide.cpp PROPERTIES LANGUAGE HIP) - endif() -else() - find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter Development) +if(has_nvcc) find_package(CUDA REQUIRED) - - include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) - cuda_add_library(qsim_decide MODULE decide.cpp) - if(DEFINED ENV{CUQUANTUM_ROOT}) target_compile_options(qsim_decide PRIVATE $<$:-D__CUSTATEVEC__> ) endif() - + find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter Development) + include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) set_target_properties(qsim_decide PROPERTIES - PREFIX "${PYTHON_MODULE_PREFIX}" - SUFFIX "${PYTHON_MODULE_EXTENSION}" + PREFIX "${PYTHON_MODULE_PREFIX}" + SUFFIX "${PYTHON_MODULE_EXTENSION}" ) set_source_files_properties(decide.cpp PROPERTIES LANGUAGE CUDA) +elseif(has_hipcc) + list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip") + find_package(HIP REQUIRED) + hip_add_library(qsim_decide MODULE decide.cpp) + set_source_files_properties(decide.cpp PROPERTIES LANGUAGE HIP) + find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter Development) + include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include) + set_target_properties(qsim_decide PROPERTIES + PREFIX "${PYTHON_MODULE_PREFIX}" + SUFFIX "${PYTHON_MODULE_EXTENSION}" + ) +else() + pybind11_add_module(qsim_decide decide.cpp) endif() From fff819ee24b4d234e83b8fee573cebe7838fb39a Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 09:19:28 -0800 Subject: [PATCH 12/21] Implement suggestion by @sergeisakov --- pybind_interface/decide/decide.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pybind_interface/decide/decide.cpp b/pybind_interface/decide/decide.cpp index 0da7b0a8..b40f6975 100644 --- a/pybind_interface/decide/decide.cpp +++ b/pybind_interface/decide/decide.cpp @@ -35,11 +35,7 @@ enum Instructions { AVX512F = 0, AVX2 = 1, SSE4_1 = 2, BASIC = 3}; int detect_instructions() { Instructions instr = BASIC; - // Check for ARM architecture, specifically for Apple M1 or similar - #if defined(__aarch64__) && defined(__APPLE__) - // On Apple ARM systems, always use BASIC - instr = BASIC; - #else + #if !defined(__aarch64__) || !defined(__APPLE__) // Existing x86/x86_64 specific instruction set detection logic int info[4]; cpuid(info, 0); From 1ca54779b37c8972fa42d9014c9fa64ee8c0879f Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 09:32:28 -0800 Subject: [PATCH 13/21] Set flags for OpenMP_CXX --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 151ad6b8..75b609cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ endif(APPLE) if(APPLE) project(qsim LANGUAGES CXX) find_package(OpenMP REQUIRED) # Automatically happens for Linux + set(OpenMP_CXX_FLAGS "-fopenmp=libomp") + set(OpenMP_CXX_LIB_NAMES "libomp") else() execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc OUTPUT_STRIP_TRAILING_WHITESPACE) if(has_nvcc) From 6f074e939bc8ea50fcc22ca88addffefd53d53c7 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 11:06:17 -0800 Subject: [PATCH 14/21] Link libomp with homebrew --- .github/workflows/release_wheels.yml | 2 +- .github/workflows/testing_wheels.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release_wheels.yml b/.github/workflows/release_wheels.yml index 292f4dc5..5b63bf29 100644 --- a/.github/workflows/release_wheels.yml +++ b/.github/workflows/release_wheels.yml @@ -32,7 +32,7 @@ jobs: CIBW_SKIP: "*musllinux*" CIBW_ARCHS: "${{ matrix.cibw.arch || 'auto' }}" CIBW_MANYLINUX_X86_64_IMAGE: "${{ matrix.cibw.manylinux_image }}" - CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm" + CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --force libomp" CIBW_REPAIR_WHEEL_COMMAND_MACOS: "delocate-listdeps {wheel} && delocate-wheel --verbose --require-archs {delocate_archs} -w {dest_dir} {wheel}" # to install latest delocate package CIBW_DEPENDENCY_VERSIONS: "latest" diff --git a/.github/workflows/testing_wheels.yml b/.github/workflows/testing_wheels.yml index 4d3c1ca3..80233abd 100644 --- a/.github/workflows/testing_wheels.yml +++ b/.github/workflows/testing_wheels.yml @@ -37,7 +37,7 @@ jobs: CIBW_SKIP: "*musllinux*" CIBW_ARCHS: "${{ matrix.cibw.arch || 'auto' }}" CIBW_MANYLINUX_X86_64_IMAGE: "${{ matrix.cibw.manylinux_image }}" - CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm" + CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --force libomp" CIBW_REPAIR_WHEEL_COMMAND_MACOS: "delocate-listdeps {wheel} && delocate-wheel --verbose --require-archs {delocate_archs} -w {dest_dir} {wheel}" # to install latest delocate package CIBW_DEPENDENCY_VERSIONS: "latest" From b605cad39fb05752930beac9fe108d53de4b7947 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 11:07:19 -0800 Subject: [PATCH 15/21] Remove setting of CXX flags and lib --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 75b609cb..151ad6b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,8 +17,6 @@ endif(APPLE) if(APPLE) project(qsim LANGUAGES CXX) find_package(OpenMP REQUIRED) # Automatically happens for Linux - set(OpenMP_CXX_FLAGS "-fopenmp=libomp") - set(OpenMP_CXX_LIB_NAMES "libomp") else() execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc OUTPUT_STRIP_TRAILING_WHITESPACE) if(has_nvcc) From bb92b2e64e32666429bbad2f5ee5ff0da35c8f80 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 11:18:59 -0800 Subject: [PATCH 16/21] Rely on 'find_package(OpenMP REQUIRED)' to set -fopenmp --- CMakeLists.txt | 3 ++- pybind_interface/avx2/CMakeLists.txt | 2 +- pybind_interface/avx512/CMakeLists.txt | 2 +- pybind_interface/basic/CMakeLists.txt | 8 ++++---- pybind_interface/cuda/CMakeLists.txt | 8 ++++---- pybind_interface/custatevec/CMakeLists.txt | 8 ++++---- pybind_interface/decide/CMakeLists.txt | 2 +- pybind_interface/hip/CMakeLists.txt | 8 ++++---- pybind_interface/sse/CMakeLists.txt | 2 +- 9 files changed, 22 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 151ad6b8..b98e1bbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,6 @@ endif(APPLE) # Set the project name and language if(APPLE) project(qsim LANGUAGES CXX) - find_package(OpenMP REQUIRED) # Automatically happens for Linux else() execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc OUTPUT_STRIP_TRAILING_WHITESPACE) if(has_nvcc) @@ -31,6 +30,8 @@ else() endif() endif() +find_package(OpenMP REQUIRED) + # Add subdirectories based on the architecture or available compilers ADD_SUBDIRECTORY(pybind_interface/basic) ADD_SUBDIRECTORY(pybind_interface/decide) diff --git a/pybind_interface/avx2/CMakeLists.txt b/pybind_interface/avx2/CMakeLists.txt index eebba584..18047a1e 100644 --- a/pybind_interface/avx2/CMakeLists.txt +++ b/pybind_interface/avx2/CMakeLists.txt @@ -4,7 +4,7 @@ project(qsim) IF (WIN32) set(CMAKE_CXX_FLAGS "/arch:AVX2 /O2 /openmp") ELSE() - set(CMAKE_CXX_FLAGS "-mavx2 -mfma -O3 -fopenmp") + set(CMAKE_CXX_FLAGS "-mavx2 -mfma -O3") ENDIF() if(APPLE) diff --git a/pybind_interface/avx512/CMakeLists.txt b/pybind_interface/avx512/CMakeLists.txt index 86cfdfa8..5875c08c 100644 --- a/pybind_interface/avx512/CMakeLists.txt +++ b/pybind_interface/avx512/CMakeLists.txt @@ -5,7 +5,7 @@ project(qsim) IF (WIN32) set(CMAKE_CXX_FLAGS "/arch:AVX512 /O2 /openmp") ELSE() - set(CMAKE_CXX_FLAGS "-mavx512f -mbmi2 -O3 -fopenmp") + set(CMAKE_CXX_FLAGS "-mavx512f -mbmi2 -O3") ENDIF() if(APPLE) diff --git a/pybind_interface/basic/CMakeLists.txt b/pybind_interface/basic/CMakeLists.txt index 35347211..36bdfc8d 100644 --- a/pybind_interface/basic/CMakeLists.txt +++ b/pybind_interface/basic/CMakeLists.txt @@ -1,11 +1,11 @@ cmake_minimum_required(VERSION 3.11) project(qsim) -IF (WIN32) +if(WIN32) set(CMAKE_CXX_FLAGS "/O2 /openmp") -ELSE() - set(CMAKE_CXX_FLAGS "-O3 -fopenmp") -ENDIF() +else() + set(CMAKE_CXX_FLAGS "-O3") +endif() if(APPLE) diff --git a/pybind_interface/cuda/CMakeLists.txt b/pybind_interface/cuda/CMakeLists.txt index 4e46eb32..29fd367c 100644 --- a/pybind_interface/cuda/CMakeLists.txt +++ b/pybind_interface/cuda/CMakeLists.txt @@ -1,11 +1,11 @@ cmake_minimum_required(VERSION 3.11) project(qsim LANGUAGES CXX CUDA) -IF (WIN32) +if(WIN32) set(CMAKE_CXX_FLAGS "/O2 /openmp") -ELSE() - set(CMAKE_CXX_FLAGS "-O3 -fopenmp") -ENDIF() +else() + set(CMAKE_CXX_FLAGS "-O3") +endif() if(APPLE) diff --git a/pybind_interface/custatevec/CMakeLists.txt b/pybind_interface/custatevec/CMakeLists.txt index 1ef63166..1c34a1d9 100644 --- a/pybind_interface/custatevec/CMakeLists.txt +++ b/pybind_interface/custatevec/CMakeLists.txt @@ -15,11 +15,11 @@ cmake_minimum_required(VERSION 3.11) project(qsim LANGUAGES CXX CUDA) -IF (WIN32) +if(WIN32) set(CMAKE_CXX_FLAGS "/O2 /openmp") -ELSE() - set(CMAKE_CXX_FLAGS "-O3 -fopenmp") -ENDIF() +else() + set(CMAKE_CXX_FLAGS "-O3") +endif() if(APPLE) set(CMAKE_CXX_STANDARD 14) diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt index 159e8b1f..b3dac71c 100644 --- a/pybind_interface/decide/CMakeLists.txt +++ b/pybind_interface/decide/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.11) if(WIN32) set(CMAKE_CXX_FLAGS "/O2 /openmp") else() - set(CMAKE_CXX_FLAGS "-O3 -fopenmp") + set(CMAKE_CXX_FLAGS "-O3") endif() if(APPLE AND NOT APPLE_ARM) diff --git a/pybind_interface/hip/CMakeLists.txt b/pybind_interface/hip/CMakeLists.txt index fe4b1c54..b56687ab 100644 --- a/pybind_interface/hip/CMakeLists.txt +++ b/pybind_interface/hip/CMakeLists.txt @@ -1,11 +1,11 @@ cmake_minimum_required(VERSION 3.18) project(qsim LANGUAGES CXX HIP) -IF (WIN32) +if(WIN32) set(CMAKE_CXX_FLAGS "/O2 /openmp") -ELSE() - set(CMAKE_CXX_FLAGS "-O3 -fopenmp") -ENDIF() +else() + set(CMAKE_CXX_FLAGS "-O3") +endif() INCLUDE(../GetPybind11.cmake) find_package(PythonLibs 3.7 REQUIRED) diff --git a/pybind_interface/sse/CMakeLists.txt b/pybind_interface/sse/CMakeLists.txt index fe9b218e..9be08400 100644 --- a/pybind_interface/sse/CMakeLists.txt +++ b/pybind_interface/sse/CMakeLists.txt @@ -4,7 +4,7 @@ project(qsim) IF (WIN32) set(CMAKE_CXX_FLAGS "/O2 /openmp") ELSE() - set(CMAKE_CXX_FLAGS "-msse4.1 -O3 -fopenmp") + set(CMAKE_CXX_FLAGS "-msse4.1 -O3") ENDIF() From 67273764b32ca8b43b403d6bfa633793ace36013 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 11:42:42 -0800 Subject: [PATCH 17/21] Build wheels for Apple Silicon --- .github/workflows/release_wheels.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release_wheels.yml b/.github/workflows/release_wheels.yml index 5b63bf29..7e1201b6 100644 --- a/.github/workflows/release_wheels.yml +++ b/.github/workflows/release_wheels.yml @@ -12,10 +12,14 @@ jobs: fail-fast: false matrix: include: - - os: macos-12 + - os: macos-12 # x86_64 name: mac cibw: build: "cp37* cp38* cp39* cp310* cp311*" + - os: macos-13-large # Apple Silicon + name: mac_arm64 + cibw: + build: "cp37* cp38* cp39* cp310* cp311*" - os: ubuntu-20.04 name: manylinux2014 cibw: From 0d324c0b8a14e8a6a75162f3e2c8081f4bf85624 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 11:56:02 -0800 Subject: [PATCH 18/21] Add --overwrite --- .github/workflows/release_wheels.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release_wheels.yml b/.github/workflows/release_wheels.yml index 7e1201b6..971bd286 100644 --- a/.github/workflows/release_wheels.yml +++ b/.github/workflows/release_wheels.yml @@ -36,7 +36,7 @@ jobs: CIBW_SKIP: "*musllinux*" CIBW_ARCHS: "${{ matrix.cibw.arch || 'auto' }}" CIBW_MANYLINUX_X86_64_IMAGE: "${{ matrix.cibw.manylinux_image }}" - CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --force libomp" + CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --force --overwrite libomp" CIBW_REPAIR_WHEEL_COMMAND_MACOS: "delocate-listdeps {wheel} && delocate-wheel --verbose --require-archs {delocate_archs} -w {dest_dir} {wheel}" # to install latest delocate package CIBW_DEPENDENCY_VERSIONS: "latest" From e7649ffb2af3d0d05f13972358065c37c4e845c4 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 12:03:41 -0800 Subject: [PATCH 19/21] Add macos-13-large to testing wheels too --- .github/workflows/testing_wheels.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/testing_wheels.yml b/.github/workflows/testing_wheels.yml index 80233abd..a73fa771 100644 --- a/.github/workflows/testing_wheels.yml +++ b/.github/workflows/testing_wheels.yml @@ -17,10 +17,14 @@ jobs: fail-fast: false matrix: include: - - os: macos-12 + - os: macos-12 # x86_64 name: mac cibw: build: "cp37* cp38* cp39* cp310* cp311*" + - os: macos-13-large # Apple Silicon + name: mac_arm64 + cibw: + build: "cp37* cp38* cp39* cp310* cp311*" - os: ubuntu-20.04 name: manylinux2014 cibw: @@ -37,7 +41,7 @@ jobs: CIBW_SKIP: "*musllinux*" CIBW_ARCHS: "${{ matrix.cibw.arch || 'auto' }}" CIBW_MANYLINUX_X86_64_IMAGE: "${{ matrix.cibw.manylinux_image }}" - CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --force libomp" + CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --force --overwrite libomp" CIBW_REPAIR_WHEEL_COMMAND_MACOS: "delocate-listdeps {wheel} && delocate-wheel --verbose --require-archs {delocate_archs} -w {dest_dir} {wheel}" # to install latest delocate package CIBW_DEPENDENCY_VERSIONS: "latest" From b06b395d31d2d3866341c484237a9227f4b4dee1 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 27 Nov 2023 12:11:02 -0800 Subject: [PATCH 20/21] overwrite Python --- .github/workflows/release_wheels.yml | 2 +- .github/workflows/testing_wheels.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release_wheels.yml b/.github/workflows/release_wheels.yml index 971bd286..d6161a47 100644 --- a/.github/workflows/release_wheels.yml +++ b/.github/workflows/release_wheels.yml @@ -36,7 +36,7 @@ jobs: CIBW_SKIP: "*musllinux*" CIBW_ARCHS: "${{ matrix.cibw.arch || 'auto' }}" CIBW_MANYLINUX_X86_64_IMAGE: "${{ matrix.cibw.manylinux_image }}" - CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --force --overwrite libomp" + CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --overwrite python@3.11 && brew link --force libomp" CIBW_REPAIR_WHEEL_COMMAND_MACOS: "delocate-listdeps {wheel} && delocate-wheel --verbose --require-archs {delocate_archs} -w {dest_dir} {wheel}" # to install latest delocate package CIBW_DEPENDENCY_VERSIONS: "latest" diff --git a/.github/workflows/testing_wheels.yml b/.github/workflows/testing_wheels.yml index a73fa771..f30921af 100644 --- a/.github/workflows/testing_wheels.yml +++ b/.github/workflows/testing_wheels.yml @@ -41,7 +41,7 @@ jobs: CIBW_SKIP: "*musllinux*" CIBW_ARCHS: "${{ matrix.cibw.arch || 'auto' }}" CIBW_MANYLINUX_X86_64_IMAGE: "${{ matrix.cibw.manylinux_image }}" - CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --force --overwrite libomp" + CIBW_BEFORE_BUILD_MACOS: "brew install libomp llvm && brew link --overwrite python@3.11 && brew link --force libomp" CIBW_REPAIR_WHEEL_COMMAND_MACOS: "delocate-listdeps {wheel} && delocate-wheel --verbose --require-archs {delocate_archs} -w {dest_dir} {wheel}" # to install latest delocate package CIBW_DEPENDENCY_VERSIONS: "latest" From 34ca2a5fe57b78b04dab38da95776285232ef07b Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 28 Nov 2023 12:55:43 -0500 Subject: [PATCH 21/21] Call target_link_libraries for OpenMP --- pybind_interface/avx2/CMakeLists.txt | 2 ++ pybind_interface/avx512/CMakeLists.txt | 2 ++ pybind_interface/basic/CMakeLists.txt | 2 ++ pybind_interface/cuda/CMakeLists.txt | 2 ++ pybind_interface/custatevec/CMakeLists.txt | 2 ++ pybind_interface/decide/CMakeLists.txt | 2 ++ pybind_interface/hip/CMakeLists.txt | 2 ++ pybind_interface/sse/CMakeLists.txt | 2 ++ 8 files changed, 16 insertions(+) diff --git a/pybind_interface/avx2/CMakeLists.txt b/pybind_interface/avx2/CMakeLists.txt index 18047a1e..ab7d6d83 100644 --- a/pybind_interface/avx2/CMakeLists.txt +++ b/pybind_interface/avx2/CMakeLists.txt @@ -14,3 +14,5 @@ if(APPLE) endif() INCLUDE(../GetPybind11.cmake) pybind11_add_module(qsim_avx2 pybind_main_avx2.cpp) + +target_link_libraries(qsim_avx2 PUBLIC OpenMP::OpenMP_CXX) diff --git a/pybind_interface/avx512/CMakeLists.txt b/pybind_interface/avx512/CMakeLists.txt index 5875c08c..9d635dd0 100644 --- a/pybind_interface/avx512/CMakeLists.txt +++ b/pybind_interface/avx512/CMakeLists.txt @@ -16,3 +16,5 @@ endif() INCLUDE(../GetPybind11.cmake) pybind11_add_module(qsim_avx512 pybind_main_avx512.cpp) + +target_link_libraries(qsim_avx512 PUBLIC OpenMP::OpenMP_CXX) diff --git a/pybind_interface/basic/CMakeLists.txt b/pybind_interface/basic/CMakeLists.txt index 36bdfc8d..ab9cd94b 100644 --- a/pybind_interface/basic/CMakeLists.txt +++ b/pybind_interface/basic/CMakeLists.txt @@ -16,3 +16,5 @@ endif() INCLUDE(../GetPybind11.cmake) pybind11_add_module(qsim_basic pybind_main_basic.cpp) + +target_link_libraries(qsim_basic PUBLIC OpenMP::OpenMP_CXX) diff --git a/pybind_interface/cuda/CMakeLists.txt b/pybind_interface/cuda/CMakeLists.txt index 29fd367c..89df1948 100644 --- a/pybind_interface/cuda/CMakeLists.txt +++ b/pybind_interface/cuda/CMakeLists.txt @@ -26,3 +26,5 @@ set_target_properties(qsim_cuda PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}" ) set_source_files_properties(pybind_main_cuda.cpp PROPERTIES LANGUAGE CUDA) + +target_link_libraries(qsim_cuda PUBLIC OpenMP::OpenMP_CXX) diff --git a/pybind_interface/custatevec/CMakeLists.txt b/pybind_interface/custatevec/CMakeLists.txt index 1c34a1d9..77fe6631 100644 --- a/pybind_interface/custatevec/CMakeLists.txt +++ b/pybind_interface/custatevec/CMakeLists.txt @@ -44,3 +44,5 @@ set_target_properties(qsim_custatevec PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}" ) set_source_files_properties(pybind_main_custatevec.cpp PROPERTIES LANGUAGE CUDA) + +target_link_libraries(qsim_custatevec PUBLIC OpenMP::OpenMP_CXX) diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt index b3dac71c..15d51707 100644 --- a/pybind_interface/decide/CMakeLists.txt +++ b/pybind_interface/decide/CMakeLists.txt @@ -44,3 +44,5 @@ elseif(has_hipcc) else() pybind11_add_module(qsim_decide decide.cpp) endif() + +target_link_libraries(qsim_decide PUBLIC OpenMP::OpenMP_CXX) diff --git a/pybind_interface/hip/CMakeLists.txt b/pybind_interface/hip/CMakeLists.txt index b56687ab..8d540c90 100644 --- a/pybind_interface/hip/CMakeLists.txt +++ b/pybind_interface/hip/CMakeLists.txt @@ -22,3 +22,5 @@ set_target_properties(qsim_hip PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}" ) set_source_files_properties(pybind_main_hip.cpp PROPERTIES LANGUAGE HIP) + +target_link_libraries(qsim_hip PUBLIC OpenMP::OpenMP_CXX) diff --git a/pybind_interface/sse/CMakeLists.txt b/pybind_interface/sse/CMakeLists.txt index 9be08400..a9123a5a 100644 --- a/pybind_interface/sse/CMakeLists.txt +++ b/pybind_interface/sse/CMakeLists.txt @@ -16,3 +16,5 @@ endif() INCLUDE(../GetPybind11.cmake) pybind11_add_module(qsim_sse pybind_main_sse.cpp) + +target_link_libraries(qsim_sse PUBLIC OpenMP::OpenMP_CXX)