forked from NiuTrans/NiuTrans.NMT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
288 lines (270 loc) · 11.2 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# CMake minimum version
cmake_minimum_required(VERSION 2.8)
# Project's name
project(NiuTrans.NMT)
# The prefix of the generated executable file
set(NIUTRANS_EXE "NiuTrans.NMT")
set(NIUTRANS_DLL "${NIUTRANS_EXE}")
# Generated file path
set(EXECUTABLE_OUTPUT_PATH ../bin)
set(LIBRARY_OUTPUT_PATH ../lib)
# Use CMAKE_MACOSX_RPATH for MacOS
set(CMAKE_MACOSX_RPATH 1)
# Open floder manage
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
option(USE_CUDA "Use CUDA" OFF)
option(USE_MKL "Use MKL" OFF)
option(USE_OPENBLAS "Use OpenBLAS" OFF)
option(USE_FP16 "Use FP16" OFF)
option(GEN_DLL "Generate Dynamic Link Library" OFF)
if (USE_CUDA)
if(NOT DEFINED CUDA_TOOLKIT_ROOT_DIR)
if(WIN32)
message(STATUS "HERE cuda")
set(CUDA_TOOLKIT_ROOT_DIR "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1")
else()
set(CUDA_TOOLKIT_ROOT_DIR "/usr/cuda-9.0")
endif()
endif()
message(STATUS "CUDA_TOOLKIT_ROOT_DIR: ${CUDA_TOOLKIT_ROOT_DIR}")
endif()
if(USE_MKL)
if(NOT DEFINED INTEL_ROOT)
if(WIN32)
message(STATUS "HERE mkl")
set(INTEL_ROOT "C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2020.2.254/windows")
else()
set(INTEL_ROOT "/usr/intel/compilers_and_libraries_2020.2.254/linux")
endif()
endif()
message(STATUS "INTEL_ROOT: ${INTEL_ROOT}")
endif()
if(USE_OPENBLAS)
if(NOT DEFINED OPENBLAS_ROOT)
if(WIN32)
set(OPENBLAS_ROOT "D:/software/BaiduNetdiskDownload/thirdparty20170624/OpenBLAS")
else()
set(OPENBLAS_ROOT "/usr/OpenBLAS")
endif()
endif()
message(STATUS "OPENBLAS_ROOT: ${OPENBLAS_ROOT}")
endif()
# Find all the .cpp .h .cu .chu files in source folder
file(GLOB_RECURSE CPP_FILES source/*.cpp)
file(GLOB_RECURSE H_FILES source/*.h)
file(GLOB_RECURSE CU_FILES source/*.cu)
file(GLOB_RECURSE CUH_FILES source/*.cuh)
function(assign_source_group)
foreach(_source IN ITEMS ${ARGN})
if (IS_ABSOLUTE "${_source}")
file(RELATIVE_PATH _source_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${_source}")
else()
set(_source_rel "${_source}")
endif()
get_filename_component(_source_path "${_source_rel}" PATH)
string(REPLACE "/" "\\" _source_path_msvc "${_source_path}")
source_group("${_source_path_msvc}" FILES "${_source}")
endforeach()
endfunction(assign_source_group)
function(my_add_executable)
foreach(_source IN ITEMS ${ARGN})
assign_source_group(${_source})
endforeach()
if(USE_CUDA)
cuda_add_executable(${ARGV})
else()
add_executable(${ARGV})
endif()
endfunction(my_add_executable)
# Set libs and compiler options for CUDA
if(USE_CUDA)
add_definitions(-DUSE_CUDA)
if(USE_FP16)
add_definitions(-DHALF_PRECISION)
endif()
find_package(CUDA ${CUDA_VERSION} REQUIRED)
if(WIN32)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4819")
set(CMAKE_CUDA_FLAGS ${CMAKE_CUDA_FLAGS} "-maxrregcount=0 -m64 --disable-warnings -use_fast_math -DUSE_CUDA")
set(CMAKE_CUDA_FLAGS ${CMAKE_CUDA_FLAGS} -arch=sm_30
-gencode=arch=compute_30,code=sm_30
-gencode=arch=compute_50,code=sm_50
-gencode=arch=compute_52,code=sm_52
-gencode=arch=compute_60,code=sm_60
-gencode=arch=compute_61,code=sm_61
-gencode=arch=compute_62,code=sm_62
-gencode=arch=compute_70,code=sm_70
-gencode=arch=compute_70,code=compute_70
)
set(CMAKE_POLICY_DEFAULT_CMP0028 NEW)
link_directories("${CUDA_TOOLKIT_ROOT_DIR}/lib/x64")
include_directories("${CUDA_TOOLKIT_ROOT_DIR}/include")
set(CUDA_LIB_DIR "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64/")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}cublas.lib")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}npps.lib")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}nppc.lib")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}cudadevrt.lib")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}curand.lib")
else()
set(CMAKE_CXX_FLAGS "-fPIC -msse4.2 -w -march=native -Wno-enum-compare -Wno-sign-compare -Wno-format -Wno-dev -O3 -DNDEBUG -rdynamic")
if(USE_FP16)
set(CUDA_NVCC_FLAGS "-Xcompiler -fPIC -maxrregcount=0 --disable-warnings -use_fast_math -DUSE_CUDA -DHALF_PRECISION -Wno-deprecated-gpu-targets -std=c++11 ")
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -arch=sm_60
-gencode=arch=compute_60,code=sm_60
-gencode=arch=compute_61,code=sm_61
-gencode=arch=compute_62,code=sm_62
-gencode=arch=compute_70,code=sm_70
-gencode=arch=compute_70,code=compute_70
)
else()
set(CUDA_NVCC_FLAGS "-Xcompiler -fPIC -maxrregcount=0 --disable-warnings -use_fast_math -DUSE_CUDA -Wno-deprecated-gpu-targets -std=c++11 ")
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -arch=sm_30
-gencode=arch=compute_30,code=sm_30
-gencode=arch=compute_50,code=sm_50
-gencode=arch=compute_52,code=sm_52
-gencode=arch=compute_60,code=sm_60
-gencode=arch=compute_61,code=sm_61
-gencode=arch=compute_62,code=sm_62
-gencode=arch=compute_70,code=sm_70
-gencode=arch=compute_70,code=compute_70
)
endif()
link_directories(${CUDA_TOOLKIT_ROOT_DIR}/lib64)
include_directories(${CUDA_TOOLKIT_ROOT_DIR}/include)
set(CUDA_LIB_DIR "${CUDA_TOOLKIT_ROOT_DIR}/lib64/")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}libcublas_static.a")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}libculibos.a")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}libnpps_static.a")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}libnppc_static.a")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}libcudadevrt.a")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "${CUDA_LIB_DIR}libcurand_static.a")
set(CUDA_LIB_PATH ${CUDA_LIB_PATH} "/usr/lib64/libdl.so.2")
endif()
endif()
# Set libs and compiler options for MKL
if(USE_MKL)
add_definitions(-DMKL)
set(COMPILER_DIR "${INTEL_ROOT}/compiler")
set(MKL_DIR "${INTEL_ROOT}/mkl")
set(CPU_ARCH intel64)
if(WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -DMKL")
link_directories(${MKL_DIR}/lib/intel64/)
link_directories(${COMPILER_DIR}/lib/intel64)
include_directories(${MKL_DIR}/include)
set(COMPILER_LIB_DIR "${COMPILER_DIR}/lib/intel64/")
set(MKL_LIB_DIR "${MKL_DIR}/lib/intel64/")
set(MKL_LIB_PATH ${MKL_LIB_PATH} "${MKL_LIB_DIR}mkl_intel_lp64.lib")
set(MKL_LIB_PATH ${MKL_LIB_PATH} "${MKL_LIB_DIR}mkl_core.lib")
set(MKL_LIB_PATH ${MKL_LIB_PATH} "${MKL_LIB_DIR}mkl_intel_thread.lib")
set(MKL_LIB_PATH ${MKL_LIB_PATH} "${COMPILER_LIB_DIR}libiomp5md.lib")
else()
if(USE_CUDA)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-reorder -DMKL")
else()
set(CMAKE_CXX_FLAGS "-std=c++11 -msse4.2 -w -march=native -Wno-enum-compare -Wno-sign-compare -Wno-reorder -Wno-format -O3 -flto -DNDEBUG -rdynamic -fkeep-inline-functions -fPIC -DMKL")
endif(USE_CUDA)
link_directories(${MKL_DIR}/lib/intel64/)
link_directories(${COMPILER_DIR}/lib/intel64)
include_directories(${MKL_DIR}/include)
set(COMPILER_LIB_DIR "${COMPILER_DIR}/lib/intel64/")
set(MKL_LIB_DIR "${MKL_DIR}/lib/intel64/")
set(MKL_LIB_PATH ${MKL_LIB_PATH} "${MKL_LIB_DIR}libmkl_intel_lp64.a")
set(MKL_LIB_PATH ${MKL_LIB_PATH} "${MKL_LIB_DIR}libmkl_core.a")
set(MKL_LIB_PATH ${MKL_LIB_PATH} "${MKL_LIB_DIR}libmkl_intel_thread.a")
set(MKL_LIB_PATH ${MKL_LIB_PATH} "${COMPILER_LIB_DIR}libiomp5.a")
endif()
endif()
# Set libs and compiler options for OpenBLAS
if(USE_OPENBLAS)
add_definitions(-DUSE_BLAS -DMKL)
set(OPENBLAS_INCLUDE_DIR "${OPENBLAS_ROOT}/include")
set(OPENBLAS_LIB_DIR "${OPENBLAS_ROOT}/lib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_BLAS")
if(WIN32)
link_directories(${OPENBLAS_LIB_DIR})
include_directories(${OPENBLAS_INCLUDE_DIR})
set(OPENBLAS_LIB_PATH ${OPENBLAS_LIB_PATH} "${OPENBLAS_LIB_DIR}/libopenblas.lib")
else()
link_directories(${OPENBLAS_LIB_DIR})
include_directories(${OPENBLAS_INCLUDE_DIR})
set(OPENBLAS_LIB_PATH ${OPENBLAS_LIB_PATH} "${OPENBLAS_LIB_DIR}/libopenblas.a")
endif()
endif()
# Integrate all libs
set(CUDA_LIB ${CUDA_LIB_PATH})
set(MKL_LIB ${MKL_LIB_PATH})
set(OPENBLAS_LIB ${OPENBLAS_LIB_PATH})
# Add executable files to project
# Generate dynamic link library about project
if(USE_CUDA)
if(GEN_DLL)
cuda_add_library(${NIUTRANS_DLL} SHARED ${CPP_FILES} ${H_FILES} ${CU_FILES} ${CUH_FILES})
else()
my_add_executable(${NIUTRANS_EXE} ${CPP_FILES} ${H_FILES} ${CU_FILES} ${CUH_FILES})
endif()
else()
if(GEN_DLL)
add_library(${NIUTRANS_DLL} SHARED ${CPP_FILES} ${H_FILES})
else()
my_add_executable(${NIUTRANS_EXE} ${CPP_FILES} ${H_FILES})
endif()
endif()
# Link external libs to executable files
# Link external libs to dynamic link library
if(WIN32)
add_definitions(-DWIN32)
set(MESS ${MESS} "On Windows")
if(USE_CUDA)
set(MESS ${MESS} " Use CUDA")
set(ALL_LIB ${ALL_LIB} ${CUDA_LIB})
endif()
if(USE_MKL)
set(MESS ${MESS} " Use MKL")
set(ALL_LIB ${ALL_LIB} ${MKL_LIB})
elseif(USE_OPENBLAS)
set(MESS ${MESS} " Use OpenBLAS")
set(ALL_LIB ${ALL_LIB} ${OPENBLAS_LIB})
else()
endif()
if(GEN_DLL)
message(STATUS "Generate Dynamic Link Library")
message(STATUS "Name of Dynamic Link Library: " ${NIUTRANS_DLL})
target_link_libraries(${NIUTRANS_DLL} ${ALL_LIB})
else()
message(STATUS "Generate Makefile For Executable File")
message(STATUS "Name of Executable File :" ${NIUTRANS_EXE})
target_link_libraries(${NIUTRANS_EXE} ${ALL_LIB})
endif()
message(STATUS "${MESS}")
else()
add_definitions(-std=c++11)
set(MESS ${MESS} "On Linux")
if(USE_CUDA)
set(MESS ${MESS} " Use CUDA")
set(ALL_LIB ${ALL_LIB} ${CUDA_LIB})
set(FLAG ${FLAG} "-lpthread -lcudart -lnvidia-ml")
else()
set(FLAG ${FLAG} "-lpthread")
endif()
if(USE_MKL)
set(MESS ${MESS} " Use MKL")
set(ALL_LIB ${ALL_LIB} ${MKL_LIB})
set(FLAG ${FLAG} "-liomp5 -lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -ldl")
elseif(USE_OPENBLAS)
set(MESS ${MESS} " Use OpenBLAS")
set(ALL_LIB ${ALL_LIB} ${OPENBLAS_LIB})
set(FLAG ${FLAG} "-lopenblas")
else()
endif()
if(GEN_DLL)
message(STATUS "Generate Dynamic Link Library")
message(STATUS "Name of Dynamic Link Library: " ${NIUTRANS_DLL})
target_link_libraries(${NIUTRANS_DLL} ${ALL_LIB} ${FLAG})
else()
message(STATUS "Generate Makefile For Executable File")
message(STATUS "Name of Executable File: " ${NIUTRANS_EXE})
target_link_libraries(${NIUTRANS_EXE} ${ALL_LIB} ${FLAG})
endif()
message(STATUS "${MESS}")
endif()