This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
167 lines (147 loc) · 4.42 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
cmake_minimum_required(VERSION 3.17)
cmake_policy(SET CMP0076 NEW)
# Add cmake/ as module path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# clangd setting
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Fortran is needed when using find_package(BLAS)
project(monolish CXX Fortran)
option(MONOLISH_USE_NVIDIA_GPU "build NVIDIA GPU variant" OFF)
option(MONOLISH_USE_MPI "build MPI variant" OFF)
option(MONOLISH_USE_AVX "Require AVX" ON)
option(MONOLISH_FOR_PACKAGING "build for packaging" OFF)
if(MONOLISH_USE_MPI)
if(MONOLISH_USE_NVIDIA_GPU)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if (MONOLISH_FOR_PACKAGING)
set(MONOLISH_TARGET monolish_gpu_mpi_${GPU_CC})
else()
set(MONOLISH_TARGET monolish_gpu_mpi)
endif()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(FATAL_ERROR "GPU mode cannot be enabled with gcc")
endif()
else()
set(MONOLISH_TARGET monolish_cpu_mpi)
endif()
else()
if(MONOLISH_USE_NVIDIA_GPU)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if (MONOLISH_FOR_PACKAGING)
set(MONOLISH_TARGET monolish_gpu_${GPU_CC})
else()
set(MONOLISH_TARGET monolish_gpu)
endif()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(FATAL_ERROR "GPU mode cannot be enabled with gcc")
endif()
else()
set(MONOLISH_TARGET monolish_cpu)
endif()
endif()
file(GLOB_RECURSE MONOLISH_SOURCE_FILES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
src/*.cpp
)
add_library(${MONOLISH_TARGET} SHARED "")
target_sources(${MONOLISH_TARGET}
PRIVATE
${MONOLISH_SOURCE_FILES}
)
set_property(TARGET ${MONOLISH_TARGET} PROPERTY CXX_STANDARD 17)
target_compile_options(${MONOLISH_TARGET} PRIVATE -g -O3 -Wall)
install(
TARGETS ${MONOLISH_TARGET}
LIBRARY
DESTINATION lib
)
# AVX
if(MONOLISH_USE_AVX)
target_compile_options(${MONOLISH_TARGET} PRIVATE "-mavx")
target_compile_definitions(${MONOLISH_TARGET} PRIVATE MONOLISH_USE_AVX)
endif()
# MPI
if(MONOLISH_USE_MPI)
target_compile_definitions(${MONOLISH_TARGET} PRIVATE MONOLISH_USE_MPI)
endif()
install(
TARGETS ${MONOLISH_TARGET}
LIBRARY
DESTINATION lib
)
# OpenMP
find_package(OpenMP REQUIRED)
if(MONOLISH_USE_NVIDIA_GPU)
# GPU offloading
if (NOT MONOLISH_FOR_PACKAGING)
# get CC
execute_process(
COMMAND allgebra_get_device_cc
OUTPUT_VARIABLE GPU_CC
ERROR_QUIET
)
string(REGEX REPLACE "\n$" "" GPU_CC "${GPU_CC}")
endif()
message(STATUS "GPU Compute Capability is ${GPU_CC}")
target_compile_options(${MONOLISH_TARGET} PRIVATE
-fopenmp
-fopenmp-targets=nvptx64
-Xopenmp-target -march=sm_${GPU_CC}
)
target_link_options(${MONOLISH_TARGET} PRIVATE
-fopenmp
-fopenmp-targets=nvptx64
-Xopenmp-target -march=sm_${GPU_CC}
)
else()
target_compile_options(${MONOLISH_TARGET} PRIVATE ${OpenMP_CXX_FLAGS})
target_link_options(${MONOLISH_TARGET} PRIVATE ${OpenMP_CXX_FLAGS})
target_link_libraries(${MONOLISH_TARGET} PRIVATE ${OpenMP_CXX_LIBRARIES})
endif()
# MKL
if(MONOLISH_USE_AVX)
set(MKL_VML_AVX TRUE)
endif()
find_package(MKL)
if(MKL_FOUND)
# Not use FindBLAS and FindLAPACK if MKL is already found
target_include_directories(${MONOLISH_TARGET} PRIVATE ${MKL_INCLUDE_PATH})
target_link_libraries(${MONOLISH_TARGET} PRIVATE ${MKL_LIBRARIES})
target_compile_definitions(${MONOLISH_TARGET} PRIVATE MONOLISH_USE_MKL)
else()
find_package(BLAS REQUIRED)
# BLAS_INCLUDE_DIRS is missing in FindBLAS.cmake.
# see https://gitlab.kitware.com/cmake/cmake/-/issues/20268
find_path(BLAS_INCLUDE_DIRS
NAMES cblas.h
HINTS
/usr/include
/usr/local/include
/usr/include/openblas
)
target_include_directories(${MONOLISH_TARGET} PRIVATE ${BLAS_INCLUDE_DIRS})
target_link_libraries(${MONOLISH_TARGET} PRIVATE ${BLAS_LIBRARIES})
find_package(LAPACK)
if(LAPACK_FOUND)
target_include_directories(${MONOLISH_TARGET} PRIVATE ${LAPACK_INCLUDE_DIRS})
target_link_libraries(${MONOLISH_TARGET} PRIVATE ${LAPACK_LIBRARIES})
target_compile_definitions(${MONOLISH_TARGET} PRIVATE MONOLISH_USE_LAPACK)
endif()
endif()
if(MONOLISH_USE_NVIDIA_GPU)
target_compile_definitions(${MONOLISH_TARGET} PRIVATE MONOLISH_USE_NVIDIA_GPU)
# Link CUDA libraries
find_package(CUDAToolkit REQUIRED)
target_link_libraries(${MONOLISH_TARGET}
PRIVATE
CUDA::cublas
CUDA::cusolver
CUDA::cusparse
CUDA::cudart
)
endif()
install(
DIRECTORY include/
DESTINATION include
FILES_MATCHING PATTERN "*.hpp"
)