Skip to content

Commit 5edafd6

Browse files
committed
Add HIP to make and cmake files
1 parent 6dd69e2 commit 5edafd6

File tree

8 files changed

+121
-25
lines changed

8 files changed

+121
-25
lines changed

CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ cmake_minimum_required(VERSION 3.11)
22

33
execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc)
44
if(has_nvcc STREQUAL "")
5-
project(qsim)
5+
execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc)
6+
if(has_hipcc STREQUAL "")
7+
project(qsim)
8+
else()
9+
project(qsim LANGUAGES CXX HIP)
10+
ADD_SUBDIRECTORY(pybind_interface/hip)
11+
endif()
612
else()
713
project(qsim LANGUAGES CXX CUDA)
814
ADD_SUBDIRECTORY(pybind_interface/cuda)

Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ TESTS = run-cxx-tests
66

77
CXX=g++
88
NVCC=nvcc
9+
HIPCC=hipcc
910

1011
CXXFLAGS = -O3 -fopenmp
1112
ARCHFLAGS = -march=native
1213
NVCCFLAGS = -O3
14+
HIPCCFLAGS = -O3
1315

1416
# CUQUANTUM_ROOT should be set.
1517
CUSTATEVECFLAGS = -I$(CUQUANTUM_ROOT)/include -L${CUQUANTUM_ROOT}/lib -L$(CUQUANTUM_ROOT)/lib64 -lcustatevec -lcublas
@@ -22,6 +24,8 @@ export ARCHFLAGS
2224
export NVCC
2325
export NVCCFLAGS
2426
export CUSTATEVECFLAGS
27+
export HIPCC
28+
export HIPCCFLAGS
2529

2630
ifeq ($(PYBIND11), true)
2731
TARGETS += pybind
@@ -43,6 +47,10 @@ qsim-cuda:
4347
qsim-custatevec:
4448
$(MAKE) -C apps/ qsim-custatevec
4549

50+
.PHONY: qsim-hip
51+
qsim-hip:
52+
$(MAKE) -C apps/ qsim-hip
53+
4654
.PHONY: pybind
4755
pybind:
4856
$(MAKE) -C pybind_interface/ pybind
@@ -59,6 +67,10 @@ cuda-tests:
5967
custatevec-tests:
6068
$(MAKE) -C tests/ custatevec-tests
6169

70+
.PHONY: hip-tests
71+
hip-tests:
72+
$(MAKE) -C tests/ hip-tests
73+
6274
.PHONY: run-cxx-tests
6375
run-cxx-tests: cxx-tests
6476
$(MAKE) -C tests/ run-cxx-tests
@@ -71,6 +83,10 @@ run-cuda-tests: cuda-tests
7183
run-custatevec-tests: custatevec-tests
7284
$(MAKE) -C tests/ run-custatevec-tests
7385

86+
.PHONY: run-hip-tests
87+
run-hip-tests: hip-tests
88+
$(MAKE) -C tests/ run-hip-tests
89+
7490
PYTESTS = $(shell find qsimcirq_tests/ -name '*_test.py')
7591

7692
.PHONY: run-py-tests

apps/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ CUDA_TARGETS := $(CUDA_TARGETS:%cuda.cu=%cuda.x)
77
CUSTATEVEC_TARGETS = $(shell find . -maxdepth 1 -name "*custatevec.cu")
88
CUSTATEVEC_TARGETS := $(CUSTATEVEC_TARGETS:%custatevec.cu=%custatevec.x)
99

10+
HIP_TARGETS = $(shell find . -maxdepth 1 -name '*cuda.cu')
11+
HIP_TARGETS := $(HIP_TARGETS:%cuda.cu=%hip.x)
12+
1013
.PHONY: qsim
1114
qsim: $(CXX_TARGETS)
1215

@@ -16,6 +19,9 @@ qsim-cuda: $(CUDA_TARGETS)
1619
.PHONY: qsim-custatevec
1720
qsim-custatevec: $(CUSTATEVEC_TARGETS)
1821

22+
.PHONY: qsim-hip
23+
qsim-hip: $(HIP_TARGETS)
24+
1925
%.x: %.cc
2026
$(CXX) -o ./$@ $< $(CXXFLAGS) $(ARCHFLAGS)
2127

@@ -25,6 +31,9 @@ qsim-custatevec: $(CUSTATEVEC_TARGETS)
2531
%custatevec.x: %custatevec.cu
2632
$(NVCC) -o ./$@ $< $(NVCCFLAGS) $(CUSTATEVECFLAGS)
2733

34+
%hip.x: %cuda.cu
35+
$(HIPCC) -o ./$@ $< $(HIPCCFLAGS)
36+
2837
.PHONY: clean
2938
clean:
3039
-rm -f ./*.x ./*.a ./*.so ./*.mod

apps/make.sh

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ g++ -O3 -march=native -fopenmp -o qsim_amplitudes.x qsim_amplitudes.cc
2323
g++ -O3 -march=native -fopenmp -o qsimh_base.x qsimh_base.cc
2424
g++ -O3 -march=native -fopenmp -o qsimh_amplitudes.x qsimh_amplitudes.cc
2525

26-
nvcc -O3 -o qsim_base_cuda.x qsim_base_cuda.cu
27-
nvcc -O3 -o qsim_qtrajectory_cuda.x qsim_qtrajectory_cuda.cu
26+
if command -v nvcc &>/dev/null; then
27+
nvcc -O3 -o qsim_base_cuda.x qsim_base_cuda.cu
28+
nvcc -O3 -o qsim_qtrajectory_cuda.x qsim_qtrajectory_cuda.cu
2829

29-
# CUQUANTUM_ROOT should be set.
30-
CUSTATEVECFLAGS="-I${CUQUANTUM_ROOT}/include -L${CUQUANTUM_ROOT}/lib -L${CUQUANTUM_ROOT}/lib64 -lcustatevec -lcublas"
31-
nvcc -O3 $CUSTATEVECFLAGS -o qsim_base_custatevec.x qsim_base_custatevec.cu
30+
if [ -n "$CUQUANTUM_ROOT" ]; then
31+
CUSTATEVECFLAGS="-I${CUQUANTUM_ROOT}/include -L${CUQUANTUM_ROOT}/lib -L${CUQUANTUM_ROOT}/lib64 -lcustatevec -lcublas"
32+
nvcc -O3 $CUSTATEVECFLAGS -o qsim_base_custatevec.x qsim_base_custatevec.cu
33+
fi
34+
elif command -v hipcc &>/dev/null; then
35+
hipcc -O3 -o qsim_base_hip.x qsim_base_cuda.cu
36+
hipcc -O3 -o qsim_qtrajectory_hip.x qsim_qtrajectory_cuda.cu
37+
fi

pybind_interface/Makefile

+25-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ QSIMLIB_AVX2 = ../qsimcirq/qsim_avx2`python3-config --extension-suffix`
55
QSIMLIB_AVX512 = ../qsimcirq/qsim_avx512`python3-config --extension-suffix`
66
QSIMLIB_CUDA = ../qsimcirq/qsim_cuda`python3-config --extension-suffix`
77
QSIMLIB_CUSTATEVEC = ../qsimcirq/qsim_custatevec`python3-config --extension-suffix`
8+
QSIMLIB_HIP = ../qsimcirq/qsim_hip`python3-config --extension-suffix`
89
QSIMLIB_DECIDE = ../qsimcirq/qsim_decide`python3-config --extension-suffix`
910

1011
# The flags for the compilation of the simd-specific Pybind11 interfaces
@@ -13,21 +14,29 @@ PYBINDFLAGS_SSE = -msse4.1 -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 -
1314
PYBINDFLAGS_AVX2 = -mavx2 -mfma -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
1415
PYBINDFLAGS_AVX512 = -mavx512f -mbmi2 -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
1516

16-
# The flags for the compilation of GPU-specific Pybind11 interfaces
17+
# The flags for the compilation of CUDA-specific Pybind11 interfaces
1718
PYBINDFLAGS_CUDA = -std=c++17 -x cu -Xcompiler "-Wall -shared -fPIC `python3 -m pybind11 --includes`"
1819

1920
# The flags for the compilation of cuStateVec-specific Pybind11 interfaces
2021
PYBINDFLAGS_CUSTATEVEC = $(CUSTATEVECFLAGS) $(PYBINDFLAGS_CUDA)
2122

23+
# The flags for the compilation of HIP-specific Pybind11 interfaces
24+
PYBINDFLAGS_HIP = -std=c++17 -Wall -shared -fPIC `python3 -m pybind11 --includes`
25+
2226
# Check for nvcc to decide compilation mode.
2327
ifeq ($(shell which $(NVCC)),)
28+
# Check for hipcc to decide compilation mode.
29+
ifeq ($(shell which $(HIPCC)),)
2430
pybind: pybind-cpu decide-cpu
2531
else
32+
pybind: pybind-cpu pybind-hip decide-hip
33+
endif
34+
else
2635
# Check for the cuStateVec library.
2736
ifeq ($(CUQUANTUM_ROOT),)
28-
pybind: pybind-cpu pybind-gpu decide-gpu
37+
pybind: pybind-cpu pybind-cuda decide-cuda
2938
else
30-
pybind: pybind-cpu pybind-gpu pybind-custatevec decide-custatevec
39+
pybind: pybind-cpu pybind-cuda pybind-custatevec decide-custatevec
3140
endif
3241
endif
3342

@@ -40,14 +49,15 @@ pybind-cpu:
4049

4150
.PHONY: decide-cpu
4251
decide-cpu:
52+
echo "building decide-cpu"
4353
$(CXX) decide/decide.cpp -o $(QSIMLIB_DECIDE) $(CXXFLAGS) $(PYBINDFLAGS_BASIC)
4454

45-
.PHONY: pybind-gpu
46-
pybind-gpu:
55+
.PHONY: pybind-cuda
56+
pybind-cuda:
4757
$(NVCC) cuda/pybind_main_cuda.cpp -o $(QSIMLIB_CUDA) $(NVCCFLAGS) $(PYBINDFLAGS_CUDA)
4858

49-
.PHONY: decide-gpu
50-
decide-gpu:
59+
.PHONY: decide-cuda
60+
decide-cuda:
5161
$(NVCC) decide/decide.cpp -o $(QSIMLIB_DECIDE) $(NVCCFLAGS) $(PYBINDFLAGS_CUDA)
5262

5363
.PHONY: pybind-custatevec
@@ -58,6 +68,14 @@ pybind-custatevec:
5868
decide-custatevec:
5969
$(NVCC) decide/decide.cpp -D__CUSTATEVEC__ -o $(QSIMLIB_DECIDE) $(NVCCFLAGS) $(PYBINDFLAGS_CUDA)
6070

71+
.PHONY: pybind-hip
72+
pybind-hip:
73+
$(HIPCC) hip/pybind_main_hip.cpp -o $(QSIMLIB_HIP) $(HIPCCFLAGS) $(PYBINDFLAGS_HIP)
74+
75+
.PHONY: decide-hip
76+
decide-hip:
77+
$(HIPCC) decide/decide.cpp -o $(QSIMLIB_DECIDE) $(HIPCCFLAGS) $(PYBINDFLAGS_HIP)
78+
6179
.PHONY: clean
6280
clean:
6381
-rm -f ./basic/*.x ./basic/*.a ./basic/*.so ./basic/*.mod $(QSIMLIB_BASIC)

pybind_interface/decide/CMakeLists.txt

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ cmake_minimum_required(VERSION 3.11)
22

33
execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc)
44
if(has_nvcc STREQUAL "")
5-
project(qsim)
5+
execute_process(COMMAND which hipcc OUTPUT_VARIABLE has_hipcc)
6+
if(has_hipcc STREQUAL "")
7+
project(qsim)
8+
else()
9+
project(qsim LANGUAGES CXX HIP)
10+
endif()
611
else()
712
project(qsim LANGUAGES CXX CUDA)
813
endif()
@@ -22,7 +27,22 @@ endif()
2227
INCLUDE(../GetPybind11.cmake)
2328

2429
if(has_nvcc STREQUAL "")
25-
pybind11_add_module(qsim_decide decide.cpp)
30+
if(has_hipcc STREQUAL "")
31+
pybind11_add_module(qsim_decide decide.cpp)
32+
else()
33+
find_package(HIP REQUIRED)
34+
find_package(PythonLibs 3.7 REQUIRED)
35+
36+
include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include)
37+
38+
hip_add_library(qsim_decide MODULE decide.cpp)
39+
40+
set_target_properties(qsim_decide PROPERTIES
41+
PREFIX "${PYTHON_MODULE_PREFIX}"
42+
SUFFIX "${PYTHON_MODULE_EXTENSION}"
43+
)
44+
set_source_files_properties(decide.cpp PROPERTIES LANGUAGE HIP)
45+
endif()
2646
else()
2747
find_package(PythonLibs 3.7 REQUIRED)
2848
find_package(CUDA REQUIRED)

tests/Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ CUDA_TARGETS := $(CUDA_TARGETS:%cuda_test.cu=%cuda_test.x)
1414
CUSTATEVEC_TARGETS = $(shell find . -maxdepth 1 -name "*custatevec_test.cu")
1515
CUSTATEVEC_TARGETS := $(CUSTATEVEC_TARGETS:%custatevec_test.cu=%custatevec_test.x)
1616

17+
HIP_TARGETS = $(shell find . -maxdepth 1 -name "*cuda_test.cu")
18+
HIP_TARGETS := $(HIP_TARGETS:%cuda_test.cu=%hip_test.x)
19+
1720
GTEST_DIR = $(CURDIR)/googletest/googletest
1821
GMOCK_DIR = $(CURDIR)/googletest/googlemock
1922

@@ -30,6 +33,9 @@ cuda-tests: $(CUDA_TARGETS)
3033
.PHONY: custatevec-tests
3134
custatevec-tests: $(CUSTATEVEC_TARGETS)
3235

36+
.PHONY: hip-tests
37+
hip-tests: $(HIP_TARGETS)
38+
3339
.PHONY: run-cxx-tests
3440
run-cxx-tests: cxx-tests
3541
for exe in $(CXX_TARGETS); do if ! ./$$exe; then exit 1; fi; done
@@ -42,6 +48,10 @@ run-cuda-tests: cuda-tests
4248
run-custatevec-tests: custatevec-tests
4349
for exe in $(CUSTATEVEC_TARGETS); do if ! ./$$exe; then exit 1; fi; done
4450

51+
.PHONY: run-hip-tests
52+
run-hip-tests: hip-tests
53+
for exe in $(HIP_TARGETS); do if ! ./$$exe; then exit 1; fi; done
54+
4555
$(GTEST_DIR)/make:
4656
-git submodule update --init --recursive googletest
4757
mkdir -p $(GTEST_DIR)/make
@@ -56,6 +66,9 @@ $(GTEST_DIR)/make:
5666
%custatevec_test.x: %custatevec_test.cu $(GTEST_DIR)/make
5767
$(NVCC) -o ./$@ $< $(TESTFLAGS) $(NVCCFLAGS) $(CUSTATEVECFLAGS)
5868

69+
%hip_test.x: %cuda_test.cu $(GTEST_DIR)/make
70+
$(HIPCC) -o ./$@ $< $(TESTFLAGS) $(HIPCCFLAGS)
71+
5972
.PHONY: clean
6073
clean:
6174
-rm -f ./*.x ./*.a ./*.so ./*.mod

tests/make.sh

+18-10
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,22 @@ g++ -O3 -I$path_to_include -L$path_to_lib -fopenmp -o unitaryspace_basic_test.x
5555
g++ -O3 -I$path_to_include -L$path_to_lib -msse4 -fopenmp -o unitaryspace_sse_test.x unitaryspace_sse_test.cc -lgtest -lpthread
5656
g++ -O3 -I$path_to_include -L$path_to_lib -o vectorspace_test.x vectorspace_test.cc -lgtest -lpthread
5757

58-
nvcc -O3 -I$path_to_include -L$path_to_lib -o hybrid_cuda_test.x hybrid_cuda_test.cu -lgtest -lpthread
59-
nvcc -O3 -I$path_to_include -L$path_to_lib -o qtrajectory_cuda_test.x qtrajectory_cuda_test.cu -lgtest -lpthread
60-
nvcc -O3 -I$path_to_include -L$path_to_lib -o simulator_cuda_test.x simulator_cuda_test.cu -lgtest -lpthread
61-
nvcc -O3 -I$path_to_include -L$path_to_lib -o statespace_cuda_test.x statespace_cuda_test.cu -lgtest -lpthread
58+
if command -v nvcc &>/dev/null; then
59+
nvcc -O3 -I$path_to_include -L$path_to_lib -o hybrid_cuda_test.x hybrid_cuda_test.cu -lgtest -lpthread
60+
nvcc -O3 -I$path_to_include -L$path_to_lib -o qtrajectory_cuda_test.x qtrajectory_cuda_test.cu -lgtest -lpthread
61+
nvcc -O3 -I$path_to_include -L$path_to_lib -o simulator_cuda_test.x simulator_cuda_test.cu -lgtest -lpthread
62+
nvcc -O3 -I$path_to_include -L$path_to_lib -o statespace_cuda_test.x statespace_cuda_test.cu -lgtest -lpthread
6263

63-
# CUQUANTUM_ROOT should be set.
64-
CUSTATEVECFLAGS="-I${CUQUANTUM_ROOT}/include -L${CUQUANTUM_ROOT}/lib -L${CUQUANTUM_ROOT}/lib64 -lcustatevec -lcublas"
65-
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o hybrid_custatevec_test.x hybrid_custatevec_test.cu -lgtest -lpthread
66-
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o qtrajectory_custatevec_test.x qtrajectory_custatevec_test.cu -lgtest -lpthread
67-
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o simulator_custatevec_test.x simulator_custatevec_test.cu -lgtest -lpthread
68-
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o statespace_custatevec_test.x statespace_custatevec_test.cu -lgtest -lpthread
64+
if [ -n "$CUQUANTUM_ROOT" ]; then
65+
CUSTATEVECFLAGS="-I${CUQUANTUM_ROOT}/include -L${CUQUANTUM_ROOT}/lib -L${CUQUANTUM_ROOT}/lib64 -lcustatevec -lcublas"
66+
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o hybrid_custatevec_test.x hybrid_custatevec_test.cu -lgtest -lpthread
67+
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o qtrajectory_custatevec_test.x qtrajectory_custatevec_test.cu -lgtest -lpthread
68+
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o simulator_custatevec_test.x simulator_custatevec_test.cu -lgtest -lpthread
69+
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o statespace_custatevec_test.x statespace_custatevec_test.cu -lgtest -lpthread
70+
fi
71+
elif command -v hipcc &>/dev/null; then
72+
hipcc -O3 -I$path_to_include -L$path_to_lib -o hybrid_hip_test.x hybrid_cuda_test.cu -lgtest -lpthread
73+
hipcc -O3 -I$path_to_include -L$path_to_lib -o qtrajectory_hip_test.x qtrajectory_cuda_test.cu -lgtest -lpthread
74+
hipcc -O3 -I$path_to_include -L$path_to_lib -o simulator_hip_test.x simulator_cuda_test.cu -lgtest -lpthread
75+
hipcc -O3 -I$path_to_include -L$path_to_lib -o statespace_hip_test.x statespace_cuda_test.cu -lgtest -lpthread
76+
fi

0 commit comments

Comments
 (0)