Skip to content

Commit

Permalink
Enable CUDA builds and apply patches from https://github.com/quantuml…
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Dec 7, 2023
1 parent 6424440 commit 9f8f29e
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 1 deletion.
14 changes: 13 additions & 1 deletion recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,33 @@ package:
source:
url: https://github.com/quantumlib/qsim/archive/refs/tags/v{{ version }}.tar.gz
sha256: 81101fe9e6356e54125f703956e787aa366717c1e6e77aea794879b53c84ff60
patches:
- recipe/patches/0001-Fix-no-instance-of-constructor-qsim-StateSpaceCUDA-F.patch
- recipe/patches/0002-Remove-PUBLIC-from-target_link_libraries-with-OpenMP.patch
- recipe/patches/0003-Set-pybind11_INCLUDE_DIRS-correctly-for-CUDA.patch
- recipe/patches/0004-Set-PYTHON_INCLUDE_DIR-in-setup.py.patch
- recipe/patches/0005-Allow-installing-for-Python-3.12.patch
- recipe/patches/0006-Set-CUDA_ARCHITECTURES-all-cmake-policy-CMP0104.patch

build:
skip: true # [win]
number: 0
number: 1
script: {{ PYTHON }} -m pip install . -vvv --no-deps --no-build-isolation
script_env:
- CUQUANTUM_ROOT=$PREFIX # [cuda_compiler_version != "None"]

requirements:
build:
- {{ compiler('c') }}
- {{ compiler('cxx') }}
- {{ compiler('cuda') }} # [cuda_compiler_version != "None"]
- pybind11-abi
- llvm-openmp # [osx]
- libgomp # [linux]
- openmpi
- cmake >=3.27
- make
- cuquantum-python # [cuda_compiler_version != "None"]
host:
- python
- pybind11
Expand All @@ -38,6 +49,7 @@ requirements:
- pybind11
- typing_extensions
- {{ pin_compatible('numpy') }}
- cuquantum-python # [cuda_compiler_version != "None"]

test:
imports:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
From b804625db8bb78f35260e7e794f6b92a8ce037db Mon Sep 17 00:00:00 2001
From: Bas Nijholt <[email protected]>
Date: Wed, 29 Nov 2023 10:16:09 -0800
Subject: [PATCH 1/3] Fix 'no instance of constructor
"qsim::StateSpaceCUDA<FP>::Parameter::Parameter [with FP=float]" matches the
argument list'

Full error:
./pybind_interface/cuda/pybind_main_cuda.cpp(30): error: no instance of constructor "qsim::StateSpaceCUDA<FP>::Parameter::Parameter [with FP=float]" matches the argument list
---
pybind_interface/cuda/pybind_main_cuda.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/pybind_interface/cuda/pybind_main_cuda.cpp b/pybind_interface/cuda/pybind_main_cuda.cpp
index 57b0ba8..7480140 100644
--- a/pybind_interface/cuda/pybind_main_cuda.cpp
+++ b/pybind_interface/cuda/pybind_main_cuda.cpp
@@ -27,8 +27,10 @@ namespace qsim {
unsigned num_sim_threads,
unsigned num_state_threads,
unsigned num_dblocks
- ) : ss_params{num_state_threads, num_dblocks} {}
-
+ ) {
+ ss_params.num_threads = num_state_threads;
+ ss_params.num_dblocks = num_dblocks;
+ }
StateSpace CreateStateSpace() const {
return StateSpace(ss_params);
}
--
2.39.2
---
pybind_interface/cuda/pybind_main_cuda.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/pybind_interface/cuda/pybind_main_cuda.cpp b/pybind_interface/cuda/pybind_main_cuda.cpp
index 57b0ba8..7480140 100644
--- a/pybind_interface/cuda/pybind_main_cuda.cpp
+++ b/pybind_interface/cuda/pybind_main_cuda.cpp
@@ -27,8 +27,10 @@ namespace qsim {
unsigned num_sim_threads,
unsigned num_state_threads,
unsigned num_dblocks
- ) : ss_params{num_state_threads, num_dblocks} {}
-
+ ) {
+ ss_params.num_threads = num_state_threads;
+ ss_params.num_dblocks = num_dblocks;
+ }
StateSpace CreateStateSpace() const {
return StateSpace(ss_params);
}
--
2.43.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
From d0fa7a99e5111ccf072e6ffbff149db72d2ba06d Mon Sep 17 00:00:00 2001
From: Bas Nijholt <[email protected]>
Date: Mon, 4 Dec 2023 11:41:19 -0800
Subject: [PATCH] Remove PUBLIC from target_link_libraries with
OpenMP::OpenMP_CXX

Otherwise this could lead to the following problem when building with CUDA:
```
CMake Error at pybind_interface/cuda/CMakeLists.txt:30 (target_link_libraries):
The plain signature for target_link_libraries has already been used with
the target "qsim_cuda". All uses of target_link_libraries with a target
must be either all-keyword or all-plain.

The uses of the plain signature are here:

* /usr/share/cmake-3.16/Modules/FindCUDA.cmake:1836 (target_link_libraries)
```
---
pybind_interface/cuda/CMakeLists.txt | 2 +-
pybind_interface/custatevec/CMakeLists.txt | 2 +-
pybind_interface/decide/CMakeLists.txt | 4 +++-
3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/pybind_interface/cuda/CMakeLists.txt b/pybind_interface/cuda/CMakeLists.txt
index 89df194..e5644d4 100644
--- a/pybind_interface/cuda/CMakeLists.txt
+++ b/pybind_interface/cuda/CMakeLists.txt
@@ -27,4 +27,4 @@ set_target_properties(qsim_cuda PROPERTIES
)
set_source_files_properties(pybind_main_cuda.cpp PROPERTIES LANGUAGE CUDA)

-target_link_libraries(qsim_cuda PUBLIC OpenMP::OpenMP_CXX)
+target_link_libraries(qsim_cuda OpenMP::OpenMP_CXX)
diff --git a/pybind_interface/custatevec/CMakeLists.txt b/pybind_interface/custatevec/CMakeLists.txt
index 77fe663..8c1d387 100644
--- a/pybind_interface/custatevec/CMakeLists.txt
+++ b/pybind_interface/custatevec/CMakeLists.txt
@@ -45,4 +45,4 @@ set_target_properties(qsim_custatevec PROPERTIES
)
set_source_files_properties(pybind_main_custatevec.cpp PROPERTIES LANGUAGE CUDA)

-target_link_libraries(qsim_custatevec PUBLIC OpenMP::OpenMP_CXX)
+target_link_libraries(qsim_custatevec OpenMP::OpenMP_CXX)
diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt
index 15d5170..6b36282 100644
--- a/pybind_interface/decide/CMakeLists.txt
+++ b/pybind_interface/decide/CMakeLists.txt
@@ -30,6 +30,7 @@ if(has_nvcc)
SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
set_source_files_properties(decide.cpp PROPERTIES LANGUAGE CUDA)
+ target_link_libraries(qsim_decide OpenMP::OpenMP_CXX)
elseif(has_hipcc)
list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip")
find_package(HIP REQUIRED)
@@ -41,8 +42,9 @@ elseif(has_hipcc)
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
+ target_link_libraries(qsim_decide PUBLIC OpenMP::OpenMP_CXX)
else()
pybind11_add_module(qsim_decide decide.cpp)
+ target_link_libraries(qsim_decide PUBLIC OpenMP::OpenMP_CXX)
endif()

-target_link_libraries(qsim_decide PUBLIC OpenMP::OpenMP_CXX)
--
2.43.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
From 19a59821e9d8802a7977b931e4de04d1aaab5035 Mon Sep 17 00:00:00 2001
From: Bas Nijholt <[email protected]>
Date: Wed, 29 Nov 2023 15:10:09 -0500
Subject: [PATCH 3/3] Set pybind11_INCLUDE_DIRS correctly for CUDA

---
pybind_interface/cuda/CMakeLists.txt | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/pybind_interface/cuda/CMakeLists.txt b/pybind_interface/cuda/CMakeLists.txt
index 9bd369a..e8c0e70 100644
--- a/pybind_interface/cuda/CMakeLists.txt
+++ b/pybind_interface/cuda/CMakeLists.txt
@@ -18,13 +18,18 @@ INCLUDE(../GetPybind11.cmake)
find_package(PythonLibs 3.7 REQUIRED)
find_package(CUDA REQUIRED)

-include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include)
+include_directories(${PYTHON_INCLUDE_DIRS})
+if(pybind11_FOUND)
+ include_directories(${pybind11_INCLUDE_DIRS})
+else() # means pybind11 has been fetched in GetPybind11.cmake
+ include_directories(${pybind11_SOURCE_DIR}/include)
+endif()

cuda_add_library(qsim_cuda MODULE pybind_main_cuda.cpp)
set_target_properties(qsim_cuda PROPERTIES
- CUDA_ARCHITECTURES "all"
- PREFIX "${PYTHON_MODULE_PREFIX}"
- SUFFIX "${PYTHON_MODULE_EXTENSION}"
+ CUDA_ARCHITECTURES "all"
+ PREFIX "${PYTHON_MODULE_PREFIX}"
+ SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
set_source_files_properties(pybind_main_cuda.cpp PROPERTIES LANGUAGE CUDA)

--
2.43.0

47 changes: 47 additions & 0 deletions recipe/patches/0004-Set-PYTHON_INCLUDE_DIR-in-setup.py.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
From 34cab62e7d4b76a7282fe79993c3ce74354cf40d Mon Sep 17 00:00:00 2001
From: Bas Nijholt <[email protected]>
Date: Tue, 5 Dec 2023 13:14:23 -0800
Subject: [PATCH] Set PYTHON_INCLUDE_DIR in setup.py

Prevents
```
CMake Error at /home/bas.nijholt/micromamba/envs/qsim/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find PythonLibs (missing: PYTHON_INCLUDE_DIRS) (Required is at
least version "3.7")
Call Stack (most recent call first):
/home/bas.nijholt/micromamba/envs/qsim/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
/home/bas.nijholt/micromamba/envs/qsim/share/cmake-3.27/Modules/FindPythonLibs.cmake:323 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
pybind_interface/cuda/CMakeLists.txt:18 (find_package)
```
---
setup.py | 3 +++
1 file changed, 3 insertions(+)

diff --git a/setup.py b/setup.py
index ed4720c..1cb2ec5 100644
--- a/setup.py
+++ b/setup.py
@@ -4,6 +4,7 @@ import sys
import shutil
import platform
import subprocess
+import sysconfig

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
@@ -38,10 +39,12 @@ class CMakeBuild(build_ext):

def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
+ python_include_dir = sysconfig.get_path("include")
cmake_args = [
"-DCMAKE_CUDA_COMPILER=nvcc",
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
"-DPYTHON_EXECUTABLE=" + sys.executable,
+ "-DPYTHON_INCLUDE_DIR=" + python_include_dir,
]

cfg = "Debug" if self.debug else "Release"
--
2.43.0

26 changes: 26 additions & 0 deletions recipe/patches/0005-Allow-installing-for-Python-3.12.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
From dd26233a6b5cdfe36d855c7127cfad9bcdfc9e68 Mon Sep 17 00:00:00 2001
From: Bas Nijholt <[email protected]>
Date: Thu, 7 Dec 2023 08:54:39 -0800
Subject: [PATCH] Allow installing for Python 3.12

I have tested qsim on Python 3.12 and everything works just fine.
---
setup.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/setup.py b/setup.py
index ed4720c..70ebca4 100644
--- a/setup.py
+++ b/setup.py
@@ -114,7 +114,7 @@ setup(
url="https://github.com/quantumlib/qsim",
author="Vamsi Krishna Devabathini",
author_email="[email protected]",
- python_requires=">=3.7.0,<3.12.0",
+ python_requires=">=3.7.0",
install_requires=requirements,
extras_require={
"dev": dev_requirements,
--
2.43.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
From dff3a3726270a90c3654e53c7f623f4ae63e9703 Mon Sep 17 00:00:00 2001
From: Bas Nijholt <[email protected]>
Date: Wed, 29 Nov 2023 15:07:28 -0500
Subject: [PATCH 2/3] Set CUDA_ARCHITECTURES "all" (cmake-policy CMP0104)

---
pybind_interface/cuda/CMakeLists.txt | 3 ++-
pybind_interface/custatevec/CMakeLists.txt | 4 ++--
pybind_interface/decide/CMakeLists.txt | 11 ++++++-----
3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/pybind_interface/cuda/CMakeLists.txt b/pybind_interface/cuda/CMakeLists.txt
index 89df194..9bd369a 100644
--- a/pybind_interface/cuda/CMakeLists.txt
+++ b/pybind_interface/cuda/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 3.11)
+cmake_minimum_required(VERSION 3.18)
project(qsim LANGUAGES CXX CUDA)

if(WIN32)
@@ -22,6 +22,7 @@ include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include)

cuda_add_library(qsim_cuda MODULE pybind_main_cuda.cpp)
set_target_properties(qsim_cuda PROPERTIES
+ CUDA_ARCHITECTURES "all"
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
diff --git a/pybind_interface/custatevec/CMakeLists.txt b/pybind_interface/custatevec/CMakeLists.txt
index 77fe663..456f5bd 100644
--- a/pybind_interface/custatevec/CMakeLists.txt
+++ b/pybind_interface/custatevec/CMakeLists.txt
@@ -40,8 +40,8 @@ cuda_add_library(qsim_custatevec MODULE pybind_main_custatevec.cpp)
target_link_libraries(qsim_custatevec -lcustatevec -lcublas)

set_target_properties(qsim_custatevec PROPERTIES
- PREFIX "${PYTHON_MODULE_PREFIX}"
- SUFFIX "${PYTHON_MODULE_EXTENSION}"
+ PREFIX "${PYTHON_MODULE_PREFIX}"
+ SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
set_source_files_properties(pybind_main_custatevec.cpp PROPERTIES LANGUAGE CUDA)

diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt
index 15d5170..0f92919 100644
--- a/pybind_interface/decide/CMakeLists.txt
+++ b/pybind_interface/decide/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 3.11)
+cmake_minimum_required(VERSION 3.18)

if(WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
@@ -26,8 +26,9 @@ if(has_nvcc)
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}"
+ CUDA_ARCHITECTURES "all"
+ PREFIX "${PYTHON_MODULE_PREFIX}"
+ SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
set_source_files_properties(decide.cpp PROPERTIES LANGUAGE CUDA)
elseif(has_hipcc)
@@ -38,8 +39,8 @@ elseif(has_hipcc)
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}"
)
else()
pybind11_add_module(qsim_decide decide.cpp)
--
2.43.0

0 comments on commit 9f8f29e

Please sign in to comment.