-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCMakeLists.txt
225 lines (190 loc) · 8.22 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
cmake_minimum_required(VERSION 3.24)
project(mcss LANGUAGES CXX C)
option(SINGLE "enable single precision" ON)
option(USE_SHARED_MEMORY "enable explicit shared memory use" OFF)
option(GPU "enable GPU Acceleration" OFF)
option(FPGA_BUILD "enable FPGA Acceleration" OFF)
option(TESTS "enable tests" OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(DCMAKE_RULE_MESSAGES OFF)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
Include(FetchContent)
include(CheckLanguage)
message(STATUS "DETECTED C++ COMPILER:${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
if ("cxx_std_23" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
message(STATUS "Using C++23 standard")
set(CMAKE_CXX_STANDARD 23)
elseif ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
message(STATUS "Using C++20 standard")
set(CMAKE_CXX_STANDARD 20)
elseif ("cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
set(CMAKE_CXX_STANDARD 17)
message(STATUS "Detected support for C++17 standard")
elseif ("cxx_std_14" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
set(CMAKE_CXX_STANDARD 14)
message(STATUS "Detected support for C++14 standard")
else ()
message(FATAL_ERROR "Cannot detect CXX_STANDARD of C++14 or newer.")
endif ()
if (GPU)
check_language(CUDA)
if (NOT CMAKE_CUDA_COMPILER)
message(FATAL_ERROR "Requested CUDA support, but no CMAKE_CUDA_COMPILER available")
endif ()
enable_language(CUDA)
if ("cuda_std_23" IN_LIST CMAKE_CUDA_COMPILE_FEATURES)
set(CMAKE_CUDA_STANDARD 23)
message(STATUS "Detected support for CUDA23 standard")
elseif ("cuda_std_20" IN_LIST CMAKE_CUDA_COMPILE_FEATURES)
set(CMAKE_CUDA_STANDARD 20)
message(STATUS "Detected support for CUDA20 standard")
elseif ("cuda_std_17" IN_LIST CMAKE_CUDA_COMPILE_FEATURES)
set(CMAKE_CUDA_STANDARD 17)
message(STATUS "Detected support for CUDA17 standard")
elseif ("cuda_std_14" IN_LIST CMAKE_CUDA_COMPILE_FEATURES)
set(CMAKE_CUDA_STANDARD 14)
message(STATUS "Detected support for CUDA14 standard")
else ()
message(FATAL_ERROR "Cannot detect CUDA_STANDARD of C++14 or newer.")
endif ()
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
endif ()
function(add_compilation_flags variableName)
set(old_flags ${${variableName}})
message(STATUS "COMPILATION FLAGS ${old_flags}")
if (GPU)
if (NOT "${old_flags}" STREQUAL "")
string(REPLACE ";" "," CUDA_flags "${old_flags}")
string(REPLACE "-pedantic" "-Wno-pedantic" CUDA_flags "${CUDA_flags}")
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${old_flags}>$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=${CUDA_flags}>")
endif ()
else ()
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${old_flags}>")
endif ()
endfunction()
if (CMAKE_BUILD_TYPE MATCHES Debug)
set(FLAGS -O0 -fstack-protector-all -Wall -Wextra -pedantic)
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:-G>")
elseif (CMAKE_BUILD_TYPE MATCHES Release)
set(FLAGS -fcx-limited-range -ffp-contract=fast -freciprocal-math
-fmerge-all-constants -fno-math-errno -march=native -funroll-loops
-ftree-vectorize)
elseif (CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
set(FLAGS -g -fno-omit-frame-pointer -fcx-limited-range
-ffp-contract=fast -freciprocal-math -fmerge-all-constants
-fno-math-errno -march=native -funroll-loops -ftree-vectorize)
endif ()
if (SINGLE)
add_definitions(-DSINGLE)
endif ()
if (USE_SHARED_MEMORY)
add_definitions(-DUSE_SHARED_MEMORY)
endif ()
add_compilation_flags(FLAGS)
find_package(OpenMP REQUIRED)
set(LIBRARIES OpenMP::OpenMP_CXX)
include_directories(
"${mcss_SOURCE_DIR}/software-model/include"
"${mcss_SOURCE_DIR}/common/include"
"${mcss_SOURCE_DIR}/reference/include"
)
file(GLOB SOURCES
"${mcss_SOURCE_DIR}/reference/src/*.cpp"
"${mcss_SOURCE_DIR}/software-model/src/*.cpp"
)
if (FPGA_BUILD)
add_definitions(-DFPGA_BUILD=1)
include_directories(FPGA/include)
set(MAXFILES_DIR "FPGA/CPU/max")
# Directory containing the C/C++ sources
set(CFILES_DIR "FPGA/CPU/src")
# Directory containing the C/C++ headers
set(INCLUDE_DIR "FPGA/CPU/inc")
include_directories(${INCLUDE_DIR})
# Recursively find all the files in the specified directories
file(GLOB_RECURSE MAXFILES ${MAXFILES_DIR}/*.max)
file(GLOB_RECURSE CFILES ${CFILES_DIR}/*.c*)
message(STATUS "MAXFILES found ${MAXFILES}")
message(STATUS "CFILES found ${CFILES}")
message(STATUS "INCLUDE dirs ${INCLUDE_DIR}")
# Using slic-config to specify the correct flags and libraries
FIND_PROGRAM(SLIC_CONFIG slic-config)
if (SLIC_CONFIG)
# ---- Get the release name ----
EXECUTE_PROCESS(COMMAND ${SLIC_CONFIG} --cflags OUTPUT_VARIABLE SLIC_CFLAGS_QUOTED)
EXECUTE_PROCESS(COMMAND ${SLIC_CONFIG} --libs OUTPUT_VARIABLE SLIC_LDFLAGS_QUOTED)
# Output of slic-config starts with space -- cmake considers this as error
string(STRIP "${SLIC_LDFLAGS_QUOTED}" SLIC_LDFLAGS)
list(APPEND LIBRARIES ${SLIC_LDFLAGS})
include_directories(
$ENV{MAXCOMPILERDIR}/include/slic
$ENV{MAXPOWERDIR}/src/maxpower/kernel/random/runtime
)
list(APPEND SOURCES $ENV{MAXPOWERDIR}/src/maxpower/kernel/random/runtime/random_mt.c)
else ()
message(FATAL_ERROR "slic config not found")
endif ()
# Directory for the compiled maxfiles
set(MAXOBJS_DIR "max")
# List of the compiled object files used in the dependency tree
set(MAXOBJS "")
# Using sliccompile to compile maxfiles
FIND_PROGRAM(SLIC_COMPILE sliccompile)
if (SLIC_COMPILE)
# Creates a directory for the compiled maxfiles
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${MAXOBJS_DIR}")
# Iterate over each maxfile
foreach (MAXFILE ${MAXFILES})
# Generate a name for the compiled maxfiles
get_filename_component(MAXFILE_NAME ${MAXFILE} NAME)
string(REPLACE ".max" ".o" MAXOBJ_NAME ${MAXFILE_NAME})
# Compile the maxfiles
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${MAXOBJS_DIR}/${MAXOBJ_NAME}"
COMMAND ${SLIC_COMPILE} ${MAXFILE} ${MAXOBJ_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${MAXOBJS_DIR}
DEPENDS ${MAXFILE} COMMENT "compiling maxfile ${MAXFILE}")
# Add the object to the list
list(APPEND MAXOBJS "${CMAKE_CURRENT_BINARY_DIR}/${MAXOBJS_DIR}/${MAXOBJ_NAME}")
endforeach ()
else ()
message(FATAL_ERROR "sliccompile not found")
endif ()
add_custom_target(MAX_OBJS_LIB SOURCES ${MAXOBJS})
message(STATUS "max objects generated ${MAXOBJS}")
list(APPEND LIBRARIES ${SLIC_LDFLAGS} ${MAXOBJS})
list(APPEND SOURCES ${CFILES})
endif ()
if (GPU)
add_definitions(-DGPU=1)
set_property(GLOBAL PROPERTY CUDA_ARCHITECTURES native)
list(APPEND GPU_FLAGS -Wno-deprecated-gpu-targets --default-stream per-thread
--use_fast_math --extra-device-vectorization --expt-relaxed-constexpr
--extended-lambda --restrict --ptxas-options=-v)
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:${GPU_FLAGS}>")
find_package(CUDAToolkit REQUIRED)
message("CUDA INCLUDE ${CUDA_INCLUDE_DIRS}")
message(STATUS "CUDA Version: ${CUDAToolkit_VERSION_MAJOR}")
message(STATUS "CUDA Path: ${CMAKE_CUDA_COMPILER}")
message(STATUS "CUDA CUDAToolkit include: ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}")
list(APPEND LIBRARIES CUDA::cudart_static CUDA::curand_static)
message("GPU LIBRARIES: ${LIBRARIES}")
include_directories("${mcss_SOURCE_DIR}/GPU/include")
file(GLOB CUFILES "${mcss_SOURCE_DIR}/GPU/src/*.cu")
list(APPEND SOURCES ${CUFILES})
endif ()
if (TESTS)
add_subdirectory(test)
endif ()
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach (dir ${dirs})
message(STATUS "dir='${dir}'")
endforeach ()
message(SOURCES = ${SOURCES})
message(LIBRARIES = ${LIBRARIES})
add_executable(mcss mcss.cpp ${SOURCES})
target_link_libraries(mcss PUBLIC ${LIBRARIES})
if (FPGA_BUILD)
add_dependencies(mcss MAX_OBJS_LIB)
target_compile_definitions(mcss PUBLIC FPGA_BUILD)
endif ()