Skip to content

Commit a67d1b3

Browse files
authored
Detect availability of OpenMP in CMake and Make files (#1001)
All of the secondary cmakelists.txt files contained code equivalent to: ```cmake target_link_libraries(qsim_decide OpenMP::OpenMP_CXX) ``` This PR updates the form to use the recommended approach of configuring the use in the top-level `CMakeLists.txt` and then using `INTERFACE` in the calls to `target_link_libraries`. The code there and also in the updated `Makefile` checks that OpenMP is supported on the host before adding the link flag.
1 parent 6a10b95 commit a67d1b3

11 files changed

Lines changed: 32 additions & 17 deletions

File tree

CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ else()
5252
endif()
5353
endif()
5454

55+
# Use OpenMP if it can be found.
56+
find_package(OpenMP COMPONENTS CXX NO_POLICY_SCOPE)
57+
add_library(qsim_openmp_config INTERFACE)
58+
if(OpenMP_CXX_FOUND)
59+
target_link_libraries(qsim_openmp_config INTERFACE OpenMP::OpenMP_CXX)
60+
else()
61+
message(STATUS "${MSG_PREFIX} cannot use thread parallelization")
62+
endif()
63+
5564
# Add optimization flags to any configuration that is NOT Debug. Note: CMake
5665
# will automatically use the appropriate debug flags (e.g., /Od and /Zi for
5766
# MSVC, or -O0 -g for GCC/Clang), so there's no need to do that part.
@@ -61,8 +70,6 @@ else()
6170
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-O3>)
6271
endif()
6372

64-
find_package(OpenMP REQUIRED)
65-
6673
# Always build the basic part.
6774
add_subdirectory(pybind_interface/basic)
6875
add_subdirectory(pybind_interface/decide)
@@ -90,5 +97,4 @@ set(CMAKE_CXX_STANDARD 17)
9097
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
9198

9299
# Print additional useful info.
93-
message(STATUS "${MSG_PREFIX} OpenMP found = ${OPENMP_FOUND}")
94100
message(STATUS "${MSG_PREFIX} shell $PATH = $ENV{PATH}")

Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ CXX ?= g++
3838
NVCC ?= nvcc
3939
HIPCC ?= hipcc
4040

41-
BASE_CXXFLAGS := -std=c++17 -fopenmp
41+
BASE_CXXFLAGS := -std=c++17
4242
BASE_NVCCFLAGS := -std c++17 -Wno-deprecated-gpu-targets
4343
BASE_HIPCCFLAGS :=
4444

@@ -49,7 +49,15 @@ HIPCCFLAGS := $(BASE_HIPCCFLAGS) $(HIPCCFLAGS)
4949
LTO_FLAGS := -flto=auto
5050
USING_CLANG := $(shell $(CXX) --version | grep -isq clang && echo "true")
5151
ifeq ($(USING_CLANG),"true")
52-
LTO_FLAGS := -flto
52+
LTO_FLAGS := -flto
53+
endif
54+
55+
# Test if OpenMP header files are available and we can link with the library.
56+
OMP_CHECK_CMD := echo "int main() { return 0; }" | \
57+
$(CXX) -fopenmp -x c++ - -o /dev/null 2>/dev/null
58+
HAVE_OPENMP := $(shell $(OMP_CHECK_CMD) && echo "true")
59+
ifeq ($(HAVE_OPENMP),true)
60+
OPENMP_FLAGS := -fopenmp
5361
endif
5462

5563
ifdef DEBUG
@@ -58,7 +66,7 @@ ifdef DEBUG
5866
NVCCFLAGS += $(DEBUG_FLAGS)
5967
HIPCCFLAGS += $(DEBUG_FLAGS)
6068
else
61-
CXXFLAGS += -O3 $(LTO_FLAGS)
69+
CXXFLAGS += -O3 $(OPENMP_FLAGS) $(LTO_FLAGS)
6270
NVCCFLAGS += -O3
6371
HIPCCFLAGS += -O3
6472
endif

pybind_interface/avx2/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ endif()
4646
include(../GetPybind11.cmake)
4747
pybind11_add_module(qsim_avx2 pybind_main_avx2.cpp)
4848

49-
target_link_libraries(qsim_avx2 PUBLIC OpenMP::OpenMP_CXX)
49+
target_link_libraries(qsim_avx2 PRIVATE qsim_openmp_config)

pybind_interface/avx512/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ endif()
3939
include(../GetPybind11.cmake)
4040
pybind11_add_module(qsim_avx512 pybind_main_avx512.cpp)
4141

42-
target_link_libraries(qsim_avx512 PUBLIC OpenMP::OpenMP_CXX)
42+
target_link_libraries(qsim_avx512 PRIVATE qsim_openmp_config)

pybind_interface/basic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ endif()
3939
include(../GetPybind11.cmake)
4040
pybind11_add_module(qsim_basic pybind_main_basic.cpp)
4141

42-
target_link_libraries(qsim_basic PUBLIC OpenMP::OpenMP_CXX)
42+
target_link_libraries(qsim_basic PRIVATE qsim_openmp_config)

pybind_interface/cuda/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ set_target_properties(qsim_cuda PROPERTIES
5757
)
5858
set_source_files_properties(pybind_main_cuda.cpp PROPERTIES LANGUAGE CUDA)
5959

60-
target_link_libraries(qsim_cuda OpenMP::OpenMP_CXX)
60+
target_link_libraries(qsim_cuda PRIVATE qsim_openmp_config)

pybind_interface/custatevec/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ set_target_properties(qsim_custatevec PROPERTIES
5353
)
5454
set_source_files_properties(pybind_main_custatevec.cpp PROPERTIES LANGUAGE CUDA)
5555

56-
target_link_libraries(qsim_custatevec OpenMP::OpenMP_CXX)
56+
target_link_libraries(qsim_custatevec PRIVATE qsim_openmp_config)

pybind_interface/custatevecex/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ set_target_properties(qsim_custatevecex PROPERTIES
5353
)
5454
set_source_files_properties(pybind_main_custatevecex.cpp PROPERTIES LANGUAGE CUDA)
5555

56-
target_link_libraries(qsim_custatevecex OpenMP::OpenMP_CXX)
56+
target_link_libraries(qsim_custatevecex PRIVATE qsim_openmp_config)

pybind_interface/decide/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ if(CMAKE_CUDA_COMPILER)
6161
SUFFIX "${PYTHON_MODULE_EXTENSION}"
6262
)
6363
set_source_files_properties(decide.cpp PROPERTIES LANGUAGE CUDA)
64-
target_link_libraries(qsim_decide OpenMP::OpenMP_CXX)
64+
6565
elseif(has_hipcc)
6666
list(APPEND CMAKE_MODULE_PATH "/opt/rocm/lib/cmake/hip")
6767
find_package(HIP REQUIRED)
@@ -73,8 +73,9 @@ elseif(has_hipcc)
7373
PREFIX "${PYTHON_MODULE_PREFIX}"
7474
SUFFIX "${PYTHON_MODULE_EXTENSION}"
7575
)
76-
target_link_libraries(qsim_decide PUBLIC OpenMP::OpenMP_CXX)
76+
7777
else()
7878
pybind11_add_module(qsim_decide decide.cpp)
79-
target_link_libraries(qsim_decide PUBLIC OpenMP::OpenMP_CXX)
8079
endif()
80+
81+
target_link_libraries(qsim_decide PRIVATE qsim_openmp_config)

pybind_interface/hip/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ set_target_properties(qsim_hip PROPERTIES
3737
)
3838
set_source_files_properties(pybind_main_hip.cpp PROPERTIES LANGUAGE HIP)
3939

40-
target_link_libraries(qsim_hip PUBLIC OpenMP::OpenMP_CXX)
40+
target_link_libraries(qsim_hip PRIVATE qsim_openmp_config)

0 commit comments

Comments
 (0)