Skip to content

Commit 6dd69e2

Browse files
committed
Add HIP to pybind11
1 parent caa5463 commit 6dd69e2

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

pybind_interface/decide/decide.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int detect_instructions() {
6060
}
6161

6262
enum GPUCapabilities {
63-
CUDA = 0, CUSTATEVEC = 1, NO_GPU = 10, NO_CUSTATEVEC = 11 };
63+
CUDA = 0, CUSTATEVEC = 1, HIP = 2, NO_GPU = 10, NO_CUSTATEVEC = 11 };
6464

6565
// For now, GPU detection is performed at compile time, as our wheels are
6666
// generated on Github Actions runners which do not have GPU support.
@@ -70,6 +70,8 @@ enum GPUCapabilities {
7070
int detect_gpu() {
7171
#ifdef __NVCC__
7272
GPUCapabilities gpu = CUDA;
73+
#elif __HIP__
74+
GPUCapabilities gpu = HIP;
7375
#else
7476
GPUCapabilities gpu = NO_GPU;
7577
#endif

pybind_interface/hip/CMakeLists.txt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
project(qsim LANGUAGES CXX HIP)
3+
4+
IF (WIN32)
5+
set(CMAKE_CXX_FLAGS "/O2 /openmp")
6+
ELSE()
7+
set(CMAKE_CXX_FLAGS "-O3 -fopenmp")
8+
ENDIF()
9+
10+
if(APPLE)
11+
set(CMAKE_CXX_STANDARD 14)
12+
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
13+
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
14+
endif()
15+
16+
INCLUDE(../GetPybind11.cmake)
17+
find_package(PythonLibs 3.7 REQUIRED)
18+
find_package(HIP REQUIRED)
19+
20+
include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include)
21+
22+
hip_add_library(qsim_hip MODULE pybind_main_hip.cpp)
23+
24+
set_target_properties(qsim_hip PROPERTIES
25+
PREFIX "${PYTHON_MODULE_PREFIX}"
26+
SUFFIX "${PYTHON_MODULE_EXTENSION}"
27+
)
28+
set_source_files_properties(pybind_main_hip.cpp PROPERTIES LANGUAGE HIP)
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019 Google LLC. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "pybind_main_hip.h"
16+
17+
#include "../../lib/simulator_cuda.h"
18+
19+
namespace qsim {
20+
using Simulator = SimulatorCUDA<float>;
21+
22+
struct Factory {
23+
using Simulator = qsim::Simulator;
24+
using StateSpace = Simulator::StateSpace;
25+
26+
Factory(
27+
unsigned num_sim_threads,
28+
unsigned num_state_threads,
29+
unsigned num_dblocks
30+
) : ss_params{num_state_threads, num_dblocks} {}
31+
32+
StateSpace CreateStateSpace() const {
33+
return StateSpace(ss_params);
34+
}
35+
36+
Simulator CreateSimulator() const {
37+
return Simulator();
38+
}
39+
40+
StateSpace::Parameter ss_params;
41+
};
42+
43+
inline void SetFlushToZeroAndDenormalsAreZeros() {}
44+
inline void ClearFlushToZeroAndDenormalsAreZeros() {}
45+
}
46+
47+
#include "../pybind_main.cpp"
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2019 Google LLC. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "../pybind_main.h"
16+
17+
PYBIND11_MODULE(qsim_hip, m) { GPU_MODULE_BINDINGS }

qsimcirq/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def _load_qsim_gpu():
3434
instr = qsim_decide.detect_gpu()
3535
if instr == 0:
3636
qsim_gpu = importlib.import_module("qsimcirq.qsim_cuda")
37+
elif instr == 2:
38+
qsim_gpu = importlib.import_module("qsimcirq.qsim_hip")
3739
else:
3840
qsim_gpu = None
3941
return qsim_gpu

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def build_extension(self, ext):
111111
CMakeExtension("qsimcirq/qsim_cuda"),
112112
CMakeExtension("qsimcirq/qsim_custatevec"),
113113
CMakeExtension("qsimcirq/qsim_decide"),
114+
CMakeExtension("qsimcirq/qsim_hip"),
114115
],
115116
cmdclass=dict(build_ext=CMakeBuild),
116117
zip_safe=False,

0 commit comments

Comments
 (0)