forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgpu_param_header_generator.cmake
More file actions
121 lines (114 loc) · 5.88 KB
/
gpu_param_header_generator.cmake
File metadata and controls
121 lines (114 loc) · 5.88 KB
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
# Copyright 2019-2020 CERN and copyright holders of ALICE O2.
# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
# All rights not expressly granted are reserved.
#
# This software is distributed under the terms of the GNU General Public
# License v3 (GPL Version 3), copied verbatim in the file "COPYING".
#
# In applying this license CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.
# file gpu_param_header_generator.cmake
# author Gabriele Cimador
function(generate_macros json_content output types arch_list arch_list_output)
foreach(arch IN LISTS arch_list)
set(OUTPUT_TMP_${arch} "")
endforeach()
set(arch_list_output_tmp)
list(FIND arch_list "ALL" do_all_architectures)
foreach(TYPE IN LISTS types)
string(JSON n_params LENGTH "${json_content}" "${TYPE}")
math(EXPR last "${n_params} - 1")
foreach(i RANGE 0 ${last})
string(JSON param_name MEMBER "${json_content}" "${TYPE}" "${i}")
string(JSON n_archs LENGTH "${JSON_CONTENT}" "${TYPE}" "${param_name}")
math(EXPR last_arch "${n_archs} - 1")
foreach(iArch RANGE 0 ${last_arch})
string(JSON arch MEMBER "${JSON_CONTENT}" "${TYPE}" "${param_name}" "${iArch}")
if(arch STREQUAL "default_cpu" AND NOT TYPE STREQUAL "PAR")
message(FATAL_ERROR "Bogus entry ${param_name} for ${arch}")
endif()
if(do_all_architectures GREATER -1)
if(arch_list_output AND NOT arch MATCHES ^default)
list(APPEND arch_list_output_tmp "${arch}")
endif()
set(list_idx 0)
else()
list(FIND arch_list "${arch}" list_idx)
endif()
if(list_idx GREATER -1)
string(JSON param_values GET "${JSON_CONTENT}" "${TYPE}" "${param_name}" "${arch}")
if(TYPE STREQUAL "LB")
set(MACRO_NAME "GPUCA_LB_${param_name}")
elseif(TYPE STREQUAL "PAR")
set(MACRO_NAME "GPUCA_PAR_${param_name}")
else()
set(MACRO_NAME "GPUCA_${param_name}")
endif()
set(vals "${param_values}")
string(REGEX REPLACE "^\\[ *" "" vals "${vals}")
string(REGEX REPLACE " *\\]$" "" vals "${vals}")
string(REGEX REPLACE "\"" "" vals "${vals}")
set(MACRO_DEFINITION "#define ${MACRO_NAME} ${vals}")
if(arch MATCHES ^default)
# fallback defaults are wrapped in #ifndef
string(APPEND OUTPUT_TMP_${arch} "#ifndef ${MACRO_NAME}\n ${MACRO_DEFINITION}\n#endif\n\n")
else()
string(APPEND OUTPUT_TMP_${arch} "${MACRO_DEFINITION}\n")
endif()
endif()
endforeach()
endforeach()
endforeach()
foreach(arch IN LISTS arch_list)
set(${output}_${arch} "${OUTPUT_TMP_${arch}}" PARENT_SCOPE)
endforeach()
if(arch_list_output)
list(REMOVE_DUPLICATES arch_list_output_tmp)
list(SORT arch_list_output_tmp)
set(${arch_list_output} "${arch_list_output_tmp}" PARENT_SCOPE)
endif()
endfunction()
function(generate_gpu_param_header GPU_ARCH OUT_HEADER)
set(TARGET_ARCH "UNKNOWN")
if(GPU_ARCH STREQUAL "AUTO")
detect_gpu_arch("ALL")
else()
set(TARGET_ARCH ${GPU_ARCH})
endif()
file(READ "${GPU_PARAM_JSON}" JSON_CONTENT)
set(TMP_HEADER "#ifndef GPUDEFPARAMETERSDEFAULTS_H\n#define GPUDEFPARAMETERSDEFAULTS_H\n\n")
string(APPEND TMP_HEADER "// This file is auto-generated from gpu_params.json. Do not edit directly.\n")
string(REPLACE "," ";" ARCH_LIST "${TARGET_ARCH}")
string(APPEND TMP_HEADER "// Architectures: ${TARGET_ARCH}\n\n")
string(APPEND TMP_HEADER "#if defined(GPUCA_GPUCODE) && !defined(GPUCA_GPUCODE_GENRTC) && !defined(GPUCA_GPUCODE_NO_LAUNCH_BOUNDS) // Avoid including for RTC generation besides normal include protection.\n\n")
# Types
set(TYPES CORE LB PAR)
# Per architecture definitions
generate_macros("${JSON_CONTENT}" TMP_OUTPUT "${TYPES}" "${ARCH_LIST};default;default_cpu" "JSON_ARCHITECTURES")
list(FIND ARCH_LIST "ALL" do_all_architectures)
if(ARGC GREATER 2)
set(${ARGV2} "${JSON_ARCHITECTURES}" PARENT_SCOPE)
endif()
if(do_all_architectures GREATER -1)
set(ARCH_LIST ${JSON_ARCHITECTURES})
endif()
string(APPEND TMP_HEADER "#if 0\n")
foreach(ARCH IN LISTS ARCH_LIST)
string(APPEND TMP_HEADER "\n#elif defined(GPUCA_GPUTYPE_${ARCH})\n")
string(APPEND TMP_HEADER ${TMP_OUTPUT_${ARCH}})
endforeach()
string(APPEND TMP_HEADER "#else\n#error GPU TYPE NOT SET\n#endif\n")
# Default parameters
string(APPEND TMP_HEADER "\n// Default parameters if not defined for the target architecture\n\n")
string(APPEND TMP_HEADER ${TMP_OUTPUT_default})
string(APPEND TMP_HEADER "#endif // defined(GPUCA_GPUCODE) && !defined(GPUCA_GPUCODE_GENRTC) && !defined(GPUCA_GPUCODE_NO_LAUNCH_BOUNDS)\n\n")
# CPU fallback
string(APPEND TMP_HEADER "#ifndef GPUCA_GPUCODE_GENRTC // Defaults for non-LB parameters also for CPU fallback\n\n")
string(APPEND TMP_HEADER ${TMP_OUTPUT_default_cpu})
string(APPEND TMP_HEADER "\n#endif // GPUCA_GPUCODE_GENRTC\n")
string(APPEND TMP_HEADER "\n#endif // GPUDEFPARAMETERSDEFAULTS_H\n")
file(GENERATE OUTPUT "${OUT_HEADER}" CONTENT "${TMP_HEADER}")
message(STATUS "Generated ${OUT_HEADER}")
add_custom_target(GPU_PARAM_HEADER_${GPU_ARCH}_ALL ALL DEPENDS ${OUT_HEADER} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gpu_param_header_generator.cmake ${GPU_PARAM_JSON})
endfunction()