-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
218 lines (189 loc) · 7.04 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
cmake_minimum_required(VERSION 3.19)
project(gpu-kernel-runner
DESCRIPTION "Dynamically-compiled GPU kernel runner"
VERSION 0.1.0
HOMEPAGE_URL "https://github.com/eyalroz/gpu-kernel-runner"
LANGUAGES CXX)
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
endif()
###############
## Modules ##
###############
# Standard CMake modules
# (none)
# Custom modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
include(CompileWithWarnings)
############################
## Package dependencies ##
############################
# There seem to be some cl.hpp changes in some CUDA 11 versions;
# need to test with that before supporting it. 10.2 has not been tested but should hopefully be fine
find_package(CUDAToolkit 10.1 REQUIRED)
find_package(spdlog 1.14.0 REQUIRED)
find_package(cuda-api-wrappers 0.7.1 REQUIRED)
find_package(Python REQUIRED)
###############
## OPTIONS ##
###############
set(EXTRA_ADAPTER_SOURCE_DIRS CACHE STRING "A semicolon-separated list of directories of additional self-registering kernel adapter .cpp files")
#message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
###############
## Targets ##
###############
add_executable(kernel-runner
src/kernel-runner.cpp
src/buffer_ops.cpp
src/util/buffer_io.cpp
src/util/cxxopts-extra.hpp
src/util/optional_and_any.hpp
src/nvrtc-related/execution.hpp
src/nvrtc-related/build.hpp
src/nvrtc-related/standard_header_substitutes.hpp
src/opencl-related/build.hpp
src/opencl-related/execution.hpp
src/opencl-related/miscellany.hpp
src/opencl-related/types.hpp
src/opencl-related/ugly_error_handling.hpp
src/util/cxxopts-extra.hpp
src/util/factory_producible.hpp
src/util/filesystem.hpp
src/util/functional.hpp
src/util/miscellany.hpp
src/util/optional_and_any.hpp
src/util/spdlog-extra.hpp
src/util/static_block.hpp
src/parsers.hpp)
add_library(kernel-adapters OBJECT)
target_link_libraries(kernel-runner PRIVATE kernel-adapters)
set(adapter-definition-root-dirs src/kernel_adapters)
list(APPEND adapter-definition-root-dirs ${EXTRA_ADAPTER_SOURCE_DIRS})
foreach(adapter-definitions-root-dir ${adapter-definition-root-dirs})
message(STATUS "Using adapters defined under ${adapter-definitions-root-dir}")
file(GLOB_RECURSE adapter-definitions "${adapter-definitions-root-dir}/*.hpp")
file(GLOB_RECURSE adapter-sources "${adapter-definitions-root-dir}/*.cpp")
foreach(adapter-definition ${adapter-definitions})
target_sources(kernel-adapters PRIVATE "${adapter-definition}")
endforeach()
foreach(adapter-source ${adapter-sources})
target_sources(kernel-adapters PRIVATE "${adapter-source}")
endforeach()
list(LENGTH adapter-sources num-sources-in-dir)
math(EXPR num-adapters-with-source "${num-adapters-with-source} + ${num-sources-in-dir}" OUTPUT_FORMAT DECIMAL)
set(adapter-definitions-with-sources ${adapter-sources})
list(TRANSFORM adapter-definitions-with-sources REPLACE "\\.cpp\$" ".hpp")
list(REMOVE_ITEM adapter-definitions ${adapter-definitions-with-sources})
list(LENGTH adapter-definitions num-generated-in-dir)
math(EXPR num-generated-adapters "${num-generated-adapters} + ${num-generated-in-dir}" OUTPUT_FORMAT DECIMAL)
foreach(adapter-definition ${adapter-definitions})
file(READ ${adapter-definition} adapter-definition-contents)
if(NOT "${adapter-definition-contents}" MATCHES "[ \t]*(struct|class)[ \t]+.*kernel_adapter.*{")
math(EXPR num-generated-adapters "${num-generated-adapters} - 1" OUTPUT_FORMAT DECIMAL)
continue()
endif()
get_filename_component(adapter-name ${adapter-definition} NAME_WE)
get_filename_component(adapter-definition-dir ${adapter-definition} DIRECTORY)
set(adapter-source "src/${adapter-definition-dir}/${adapter-name}.cpp") # Note this will be generated within the build directory
if (Python_FOUND)
add_custom_command(
OUTPUT ${adapter-source}
MAIN_DEPENDENCY ${adapter-definition}
DEPENDS ${PROJECT_SOURCE_DIR}/scripts/generate-adapter-cpp.py
COMMAND Python::Interpreter ${PROJECT_SOURCE_DIR}/scripts/generate-adapter-cpp.py "${adapter-definition}" "${adapter-source}"
COMMENT "Generating source file for adapter ${adapter-name}"
VERBATIM
)
else()
add_custom_command(
OUTPUT ${adapter-source}
MAIN_DEPENDENCY ${adapter-definition}
DEPENDS ${PROJECT_SOURCE_DIR}/scripts/generate-adapter-cpp.sh
COMMAND ${PROJECT_SOURCE_DIR}/scripts/generate-adapter-cpp.sh "${adapter-definition}" "${adapter-source}"
COMMENT "Generating source file for adapter ${adapter-name}"
VERBATIM
)
endif()
target_sources(kernel-adapters PRIVATE "${adapter-source}")
endforeach()
endforeach()
math(EXPR num-adapters "${num-adapters-with-source} + ${num-generated-adapters}")
if (num-adapters EQUAL 0)
message(FATAL_ERROR "No kernel adapters found")
endif()
message(STATUS "Adapter instantiation sources: ${num-adapters-with-source} pre-existing, ${num-generated-adapters} to be auto-generated")
# Note: There's a bit of redundancy in the following loop;
# but it makes more sense if you want to separate the kernel
# adapters into a library and have it depend on less stuff.
set(compiled-targets kernel-adapters kernel-runner)
foreach(tgt ${compiled-targets})
set_target_properties(
${tgt}
PROPERTIES
CXX_STANDARD 14
# Not C++17, because we want to be SLES 15-compatible :-(
# ... but note that our CUDA kernel _can_ theoretically
# be C++17 (we'll just have to be careful when passing
# instances of C++17-modified classes to kernels.
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
CUDA_STANDARD 14
CUDA_STANDARD_REQUIRED O
)
# Give some special treatment to libraries we didn't write
target_include_directories(${tgt} SYSTEM PRIVATE third-party)
target_include_directories(${tgt} PRIVATE src src/cuda-samples-helpers)
target_warning_options(${tgt})
target_compile_definitions(${tgt} PRIVATE CUDA_INCLUDE_DIR="${CUDAToolkit_INCLUDE_DIRS}")
target_link_libraries(
${tgt}
PRIVATE
spdlog::spdlog
cuda-api-wrappers::runtime-and-driver
CUDA::OpenCL
)
endforeach(tgt)
target_link_libraries(
kernel-runner
PRIVATE
spdlog::spdlog
stdc++fs # For std::filesystem / std::experimental::filesystem
CUDA::cudart
CUDA::cuda_driver
CUDA::nvrtc
cuda-api-wrappers::runtime-and-driver
CUDA::OpenCL
)
####################
## Installation ##
####################
# (not supported for now)
#
#include(GNUInstallDirs)
#install(
# TARGETS kernel-runner
# EXPORT kernel-runner_export
# INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
#)
#install(
# DIRECTORY src/kat
# DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
# FILES_MATCHING REGEX "\\.(h|hpp|cuh)$"
#)
#install(
# EXPORT kernel-runner_export
# DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/kernel-runner"
# NAMESPACE "kernel-runner::"
# FILE kernel-runner-config.cmake
#)
#include(CMakePackageConfigHelpers)
#write_basic_package_version_file(
# "kernel-runner-config-version.cmake"
# VERSION ${PROJECT_VERSION}
# COMPATIBILITY SameMinorVersion
#)
#install(
# FILES "${CMAKE_CURRENT_BINARY_DIR}/kernel-runner-config-version.cmake"
# DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/kernel-runner"
#)