Skip to content

Commit

Permalink
Add HIP to make and cmake files
Browse files Browse the repository at this point in the history
  • Loading branch information
jakurzak committed Aug 16, 2023
1 parent 6dd69e2 commit 5edafd6
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 25 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ cmake_minimum_required(VERSION 3.11)

execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc)
if(has_nvcc STREQUAL "")
project(qsim)
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)
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ TESTS = run-cxx-tests

CXX=g++
NVCC=nvcc
HIPCC=hipcc

CXXFLAGS = -O3 -fopenmp
ARCHFLAGS = -march=native
NVCCFLAGS = -O3
HIPCCFLAGS = -O3

# CUQUANTUM_ROOT should be set.
CUSTATEVECFLAGS = -I$(CUQUANTUM_ROOT)/include -L${CUQUANTUM_ROOT}/lib -L$(CUQUANTUM_ROOT)/lib64 -lcustatevec -lcublas
Expand All @@ -22,6 +24,8 @@ export ARCHFLAGS
export NVCC
export NVCCFLAGS
export CUSTATEVECFLAGS
export HIPCC
export HIPCCFLAGS

ifeq ($(PYBIND11), true)
TARGETS += pybind
Expand All @@ -43,6 +47,10 @@ qsim-cuda:
qsim-custatevec:
$(MAKE) -C apps/ qsim-custatevec

.PHONY: qsim-hip
qsim-hip:
$(MAKE) -C apps/ qsim-hip

.PHONY: pybind
pybind:
$(MAKE) -C pybind_interface/ pybind
Expand All @@ -59,6 +67,10 @@ cuda-tests:
custatevec-tests:
$(MAKE) -C tests/ custatevec-tests

.PHONY: hip-tests
hip-tests:
$(MAKE) -C tests/ hip-tests

.PHONY: run-cxx-tests
run-cxx-tests: cxx-tests
$(MAKE) -C tests/ run-cxx-tests
Expand All @@ -71,6 +83,10 @@ run-cuda-tests: cuda-tests
run-custatevec-tests: custatevec-tests
$(MAKE) -C tests/ run-custatevec-tests

.PHONY: run-hip-tests
run-hip-tests: hip-tests
$(MAKE) -C tests/ run-hip-tests

PYTESTS = $(shell find qsimcirq_tests/ -name '*_test.py')

.PHONY: run-py-tests
Expand Down
9 changes: 9 additions & 0 deletions apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CUDA_TARGETS := $(CUDA_TARGETS:%cuda.cu=%cuda.x)
CUSTATEVEC_TARGETS = $(shell find . -maxdepth 1 -name "*custatevec.cu")
CUSTATEVEC_TARGETS := $(CUSTATEVEC_TARGETS:%custatevec.cu=%custatevec.x)

HIP_TARGETS = $(shell find . -maxdepth 1 -name '*cuda.cu')
HIP_TARGETS := $(HIP_TARGETS:%cuda.cu=%hip.x)

.PHONY: qsim
qsim: $(CXX_TARGETS)

Expand All @@ -16,6 +19,9 @@ qsim-cuda: $(CUDA_TARGETS)
.PHONY: qsim-custatevec
qsim-custatevec: $(CUSTATEVEC_TARGETS)

.PHONY: qsim-hip
qsim-hip: $(HIP_TARGETS)

%.x: %.cc
$(CXX) -o ./$@ $< $(CXXFLAGS) $(ARCHFLAGS)

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

%hip.x: %cuda.cu
$(HIPCC) -o ./$@ $< $(HIPCCFLAGS)

.PHONY: clean
clean:
-rm -f ./*.x ./*.a ./*.so ./*.mod
16 changes: 11 additions & 5 deletions apps/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ g++ -O3 -march=native -fopenmp -o qsim_amplitudes.x qsim_amplitudes.cc
g++ -O3 -march=native -fopenmp -o qsimh_base.x qsimh_base.cc
g++ -O3 -march=native -fopenmp -o qsimh_amplitudes.x qsimh_amplitudes.cc

nvcc -O3 -o qsim_base_cuda.x qsim_base_cuda.cu
nvcc -O3 -o qsim_qtrajectory_cuda.x qsim_qtrajectory_cuda.cu
if command -v nvcc &>/dev/null; then
nvcc -O3 -o qsim_base_cuda.x qsim_base_cuda.cu
nvcc -O3 -o qsim_qtrajectory_cuda.x qsim_qtrajectory_cuda.cu

# CUQUANTUM_ROOT should be set.
CUSTATEVECFLAGS="-I${CUQUANTUM_ROOT}/include -L${CUQUANTUM_ROOT}/lib -L${CUQUANTUM_ROOT}/lib64 -lcustatevec -lcublas"
nvcc -O3 $CUSTATEVECFLAGS -o qsim_base_custatevec.x qsim_base_custatevec.cu
if [ -n "$CUQUANTUM_ROOT" ]; then
CUSTATEVECFLAGS="-I${CUQUANTUM_ROOT}/include -L${CUQUANTUM_ROOT}/lib -L${CUQUANTUM_ROOT}/lib64 -lcustatevec -lcublas"
nvcc -O3 $CUSTATEVECFLAGS -o qsim_base_custatevec.x qsim_base_custatevec.cu
fi
elif command -v hipcc &>/dev/null; then
hipcc -O3 -o qsim_base_hip.x qsim_base_cuda.cu
hipcc -O3 -o qsim_qtrajectory_hip.x qsim_qtrajectory_cuda.cu
fi
32 changes: 25 additions & 7 deletions pybind_interface/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ QSIMLIB_AVX2 = ../qsimcirq/qsim_avx2`python3-config --extension-suffix`
QSIMLIB_AVX512 = ../qsimcirq/qsim_avx512`python3-config --extension-suffix`
QSIMLIB_CUDA = ../qsimcirq/qsim_cuda`python3-config --extension-suffix`
QSIMLIB_CUSTATEVEC = ../qsimcirq/qsim_custatevec`python3-config --extension-suffix`
QSIMLIB_HIP = ../qsimcirq/qsim_hip`python3-config --extension-suffix`
QSIMLIB_DECIDE = ../qsimcirq/qsim_decide`python3-config --extension-suffix`

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

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

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

# The flags for the compilation of HIP-specific Pybind11 interfaces
PYBINDFLAGS_HIP = -std=c++17 -Wall -shared -fPIC `python3 -m pybind11 --includes`

# Check for nvcc to decide compilation mode.
ifeq ($(shell which $(NVCC)),)
# Check for hipcc to decide compilation mode.
ifeq ($(shell which $(HIPCC)),)
pybind: pybind-cpu decide-cpu
else
pybind: pybind-cpu pybind-hip decide-hip
endif
else
# Check for the cuStateVec library.
ifeq ($(CUQUANTUM_ROOT),)
pybind: pybind-cpu pybind-gpu decide-gpu
pybind: pybind-cpu pybind-cuda decide-cuda
else
pybind: pybind-cpu pybind-gpu pybind-custatevec decide-custatevec
pybind: pybind-cpu pybind-cuda pybind-custatevec decide-custatevec
endif
endif

Expand All @@ -40,14 +49,15 @@ pybind-cpu:

.PHONY: decide-cpu
decide-cpu:
echo "building decide-cpu"
$(CXX) decide/decide.cpp -o $(QSIMLIB_DECIDE) $(CXXFLAGS) $(PYBINDFLAGS_BASIC)

.PHONY: pybind-gpu
pybind-gpu:
.PHONY: pybind-cuda
pybind-cuda:
$(NVCC) cuda/pybind_main_cuda.cpp -o $(QSIMLIB_CUDA) $(NVCCFLAGS) $(PYBINDFLAGS_CUDA)

.PHONY: decide-gpu
decide-gpu:
.PHONY: decide-cuda
decide-cuda:
$(NVCC) decide/decide.cpp -o $(QSIMLIB_DECIDE) $(NVCCFLAGS) $(PYBINDFLAGS_CUDA)

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

.PHONY: pybind-hip
pybind-hip:
$(HIPCC) hip/pybind_main_hip.cpp -o $(QSIMLIB_HIP) $(HIPCCFLAGS) $(PYBINDFLAGS_HIP)

.PHONY: decide-hip
decide-hip:
$(HIPCC) decide/decide.cpp -o $(QSIMLIB_DECIDE) $(HIPCCFLAGS) $(PYBINDFLAGS_HIP)

.PHONY: clean
clean:
-rm -f ./basic/*.x ./basic/*.a ./basic/*.so ./basic/*.mod $(QSIMLIB_BASIC)
Expand Down
24 changes: 22 additions & 2 deletions pybind_interface/decide/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ cmake_minimum_required(VERSION 3.11)

execute_process(COMMAND which nvcc OUTPUT_VARIABLE has_nvcc)
if(has_nvcc STREQUAL "")
project(qsim)
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()
Expand All @@ -22,7 +27,22 @@ endif()
INCLUDE(../GetPybind11.cmake)

if(has_nvcc STREQUAL "")
pybind11_add_module(qsim_decide decide.cpp)
if(has_hipcc STREQUAL "")
pybind11_add_module(qsim_decide decide.cpp)
else()
find_package(HIP REQUIRED)
find_package(PythonLibs 3.7 REQUIRED)

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(PythonLibs 3.7 REQUIRED)
find_package(CUDA REQUIRED)
Expand Down
13 changes: 13 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ CUDA_TARGETS := $(CUDA_TARGETS:%cuda_test.cu=%cuda_test.x)
CUSTATEVEC_TARGETS = $(shell find . -maxdepth 1 -name "*custatevec_test.cu")
CUSTATEVEC_TARGETS := $(CUSTATEVEC_TARGETS:%custatevec_test.cu=%custatevec_test.x)

HIP_TARGETS = $(shell find . -maxdepth 1 -name "*cuda_test.cu")
HIP_TARGETS := $(HIP_TARGETS:%cuda_test.cu=%hip_test.x)

GTEST_DIR = $(CURDIR)/googletest/googletest
GMOCK_DIR = $(CURDIR)/googletest/googlemock

Expand All @@ -30,6 +33,9 @@ cuda-tests: $(CUDA_TARGETS)
.PHONY: custatevec-tests
custatevec-tests: $(CUSTATEVEC_TARGETS)

.PHONY: hip-tests
hip-tests: $(HIP_TARGETS)

.PHONY: run-cxx-tests
run-cxx-tests: cxx-tests
for exe in $(CXX_TARGETS); do if ! ./$$exe; then exit 1; fi; done
Expand All @@ -42,6 +48,10 @@ run-cuda-tests: cuda-tests
run-custatevec-tests: custatevec-tests
for exe in $(CUSTATEVEC_TARGETS); do if ! ./$$exe; then exit 1; fi; done

.PHONY: run-hip-tests
run-hip-tests: hip-tests
for exe in $(HIP_TARGETS); do if ! ./$$exe; then exit 1; fi; done

$(GTEST_DIR)/make:
-git submodule update --init --recursive googletest
mkdir -p $(GTEST_DIR)/make
Expand All @@ -56,6 +66,9 @@ $(GTEST_DIR)/make:
%custatevec_test.x: %custatevec_test.cu $(GTEST_DIR)/make
$(NVCC) -o ./$@ $< $(TESTFLAGS) $(NVCCFLAGS) $(CUSTATEVECFLAGS)

%hip_test.x: %cuda_test.cu $(GTEST_DIR)/make
$(HIPCC) -o ./$@ $< $(TESTFLAGS) $(HIPCCFLAGS)

.PHONY: clean
clean:
-rm -f ./*.x ./*.a ./*.so ./*.mod
Expand Down
28 changes: 18 additions & 10 deletions tests/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,22 @@ g++ -O3 -I$path_to_include -L$path_to_lib -fopenmp -o unitaryspace_basic_test.x
g++ -O3 -I$path_to_include -L$path_to_lib -msse4 -fopenmp -o unitaryspace_sse_test.x unitaryspace_sse_test.cc -lgtest -lpthread
g++ -O3 -I$path_to_include -L$path_to_lib -o vectorspace_test.x vectorspace_test.cc -lgtest -lpthread

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

# CUQUANTUM_ROOT should be set.
CUSTATEVECFLAGS="-I${CUQUANTUM_ROOT}/include -L${CUQUANTUM_ROOT}/lib -L${CUQUANTUM_ROOT}/lib64 -lcustatevec -lcublas"
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o hybrid_custatevec_test.x hybrid_custatevec_test.cu -lgtest -lpthread
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o qtrajectory_custatevec_test.x qtrajectory_custatevec_test.cu -lgtest -lpthread
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o simulator_custatevec_test.x simulator_custatevec_test.cu -lgtest -lpthread
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o statespace_custatevec_test.x statespace_custatevec_test.cu -lgtest -lpthread
if [ -n "$CUQUANTUM_ROOT" ]; then
CUSTATEVECFLAGS="-I${CUQUANTUM_ROOT}/include -L${CUQUANTUM_ROOT}/lib -L${CUQUANTUM_ROOT}/lib64 -lcustatevec -lcublas"
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o hybrid_custatevec_test.x hybrid_custatevec_test.cu -lgtest -lpthread
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o qtrajectory_custatevec_test.x qtrajectory_custatevec_test.cu -lgtest -lpthread
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o simulator_custatevec_test.x simulator_custatevec_test.cu -lgtest -lpthread
nvcc -O3 $CUSTATEVECFLAGS -I$path_to_include -L$path_to_lib -o statespace_custatevec_test.x statespace_custatevec_test.cu -lgtest -lpthread
fi
elif command -v hipcc &>/dev/null; then
hipcc -O3 -I$path_to_include -L$path_to_lib -o hybrid_hip_test.x hybrid_cuda_test.cu -lgtest -lpthread
hipcc -O3 -I$path_to_include -L$path_to_lib -o qtrajectory_hip_test.x qtrajectory_cuda_test.cu -lgtest -lpthread
hipcc -O3 -I$path_to_include -L$path_to_lib -o simulator_hip_test.x simulator_cuda_test.cu -lgtest -lpthread
hipcc -O3 -I$path_to_include -L$path_to_lib -o statespace_hip_test.x statespace_cuda_test.cu -lgtest -lpthread
fi

0 comments on commit 5edafd6

Please sign in to comment.