CUDA.cmake 10 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# CUDA Module
if(USE_CUDA)
  find_cuda(${USE_CUDA} REQUIRED)
else(USE_CUDA)
  return()
endif()

###### Borrowed from MSHADOW project

include(CheckCXXCompilerFlag)
11
check_cxx_compiler_flag("-std=c++17"   SUPPORT_CXX17)
12

13
14
set(dgl_known_gpu_archs "35" "50" "60" "70")
set(dgl_cuda_arch_ptx "70")
15
if (CUDA_VERSION_MAJOR GREATER_EQUAL "11")
16
17
18
19
20
21
22
  list(APPEND dgl_known_gpu_archs "80")
  set(dgl_cuda_arch_ptx "80")
endif()
# CMake 3.5 doesn't support VERSION_GREATER_EQUAL
if (NOT CUDA_VERSION VERSION_LESS "11.8")
  list(APPEND dgl_known_gpu_archs "90")
  set(dgl_cuda_arch_ptx "90")
23
endif()
Xin Yao's avatar
Xin Yao committed
24
25
26
if (NOT CUDA_VERSION VERSION_LESS "12.0")
  list(REMOVE_ITEM dgl_known_gpu_archs "35")
endif()
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
################################################################################################
# A function for automatic detection of GPUs installed  (if autodetection is enabled)
# Usage:
#   dgl_detect_installed_gpus(out_variable)
function(dgl_detect_installed_gpus out_variable)
set(CUDA_gpu_detect_output "")
  if(NOT CUDA_gpu_detect_output)
    message(STATUS "Running GPU architecture autodetection")
    set(__cufile ${PROJECT_BINARY_DIR}/detect_cuda_archs.cu)

    file(WRITE ${__cufile} ""
      "#include <cstdio>\n"
      "#include <iostream>\n"
      "using namespace std;\n"
      "int main()\n"
      "{\n"
      "  int count = 0;\n"
      "  if (cudaSuccess != cudaGetDeviceCount(&count)) { return -1; }\n"
      "  if (count == 0) { cerr << \"No cuda devices detected\" << endl; return -1; }\n"
      "  for (int device = 0; device < count; ++device)\n"
      "  {\n"
      "    cudaDeviceProp prop;\n"
      "    if (cudaSuccess == cudaGetDeviceProperties(&prop, device))\n"
      "      std::printf(\"%d.%d \", prop.major, prop.minor);\n"
      "  }\n"
      "  return 0;\n"
      "}\n")
    if(MSVC)
      #find vcvarsall.bat and run it building msvc environment
      get_filename_component(MY_COMPILER_DIR ${CMAKE_CXX_COMPILER} DIRECTORY)
      find_file(MY_VCVARSALL_BAT vcvarsall.bat "${MY_COMPILER_DIR}/.." "${MY_COMPILER_DIR}/../..")
Xin Yao's avatar
Xin Yao committed
59
      execute_process(COMMAND ${MY_VCVARSALL_BAT} && ${CUDA_NVCC_EXECUTABLE} -arch native --run  ${__cufile}
60
61
62
63
64
65
66
                      WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/CMakeFiles/"
                      RESULT_VARIABLE __nvcc_res OUTPUT_VARIABLE __nvcc_out
                      OUTPUT_STRIP_TRAILING_WHITESPACE)
    else()
      if(CUDA_LIBRARY_PATH)
        set(CUDA_LINK_LIBRARY_PATH "-L${CUDA_LIBRARY_PATH}")
      endif()
Xin Yao's avatar
Xin Yao committed
67
      execute_process(COMMAND ${CUDA_NVCC_EXECUTABLE} -arch native --run ${__cufile} ${CUDA_LINK_LIBRARY_PATH}
68
69
70
71
72
73
74
75
                      WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/CMakeFiles/"
                      RESULT_VARIABLE __nvcc_res OUTPUT_VARIABLE __nvcc_out
                      OUTPUT_STRIP_TRAILING_WHITESPACE)
    endif()
    if(__nvcc_res EQUAL 0)
      # nvcc outputs text containing line breaks when building with MSVC.
      # The line below prevents CMake from inserting a variable with line
      # breaks in the cache
76
      message(STATUS "Found GPU arch ${__nvcc_out}")
77
      string(REGEX MATCH "([1-9].[0-9])" __nvcc_out "${__nvcc_out}")
78
79
80
81
82
83
      if(__nvcc_out VERSION_LESS "3.5")
        # drop support for cc < 3.5 and build for all known archs.
        message(WARNING "GPU arch less than 3.5 is not supported.")
      else()
        set(CUDA_gpu_detect_output ${__nvcc_out} CACHE INTERNAL "Returned GPU architetures from mshadow_detect_gpus tool" FORCE)
      endif()
84
85
86
87
88
89
    else()
      message(WARNING "Running GPU detection script with nvcc failed: ${__nvcc_out}")
    endif()
  endif()

  if(NOT CUDA_gpu_detect_output)
90
91
    message(WARNING "Automatic GPU detection failed. Building for all known architectures (${dgl_known_gpu_archs}).")
    set(${out_variable} ${dgl_known_gpu_archs} PARENT_SCOPE)
92
93
94
95
96
97
98
99
100
101
102
  else()
    set(${out_variable} ${CUDA_gpu_detect_output} PARENT_SCOPE)
  endif()
endfunction()


################################################################################################
# Function for selecting GPU arch flags for nvcc based on CUDA_ARCH_NAME
# Usage:
#   dgl_select_nvcc_arch_flags(out_variable)
function(dgl_select_nvcc_arch_flags out_variable)
103
104
  # List of arch names. Turing and Ada don't have a new major version, so they are not added to default build.
  set(__archs_names "Kepler" "Maxwell" "Pascal" "Volta" "Turing" "Ampere" "Ada" "Hopper" "All" "Manual")
Xin Yao's avatar
Xin Yao committed
105
106
107
  if (NOT CUDA_VERSION VERSION_LESS "12.0")
    list(REMOVE_ITEM __archs_names "Kepler")
  endif()
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  set(__archs_name_default "All")
  if(NOT CMAKE_CROSSCOMPILING)
    list(APPEND __archs_names "Auto")
    set(__archs_name_default "Auto")
  endif()

  # set CUDA_ARCH_NAME strings (so it will be seen as dropbox in CMake-Gui)
  set(CUDA_ARCH_NAME ${__archs_name_default} CACHE STRING "Select target NVIDIA GPU achitecture.")
  set_property( CACHE CUDA_ARCH_NAME PROPERTY STRINGS "" ${__archs_names} )
  mark_as_advanced(CUDA_ARCH_NAME)

  # verify CUDA_ARCH_NAME value
  if(NOT ";${__archs_names};" MATCHES ";${CUDA_ARCH_NAME};")
    string(REPLACE ";" ", " __archs_names "${__archs_names}")
    message(FATAL_ERROR "Only ${__archs_names} architeture names are supported.")
  endif()

  if(${CUDA_ARCH_NAME} STREQUAL "Manual")
126
    set(CUDA_ARCH_BIN ${dgl_known_gpu_archs} CACHE STRING "Specify 'real' GPU architectures to build binaries for, BIN(PTX) format is supported")
127
    set(CUDA_ARCH_PTX ${dgl_cuda_arch_ptx} CACHE STRING "Specify 'virtual' PTX architectures to build PTX intermediate code for")
128
129
130
131
132
133
    mark_as_advanced(CUDA_ARCH_BIN CUDA_ARCH_PTX)
  else()
    unset(CUDA_ARCH_BIN CACHE)
    unset(CUDA_ARCH_PTX CACHE)
  endif()

134
135
136
  if(${CUDA_ARCH_NAME} STREQUAL "Kepler")
    set(__cuda_arch_bin "35")
    set(__cuda_arch_ptx "35")
137
138
  elseif(${CUDA_ARCH_NAME} STREQUAL "Maxwell")
    set(__cuda_arch_bin "50")
139
    set(__cuda_arch_ptx "50")
140
  elseif(${CUDA_ARCH_NAME} STREQUAL "Pascal")
141
142
    set(__cuda_arch_bin "60")
    set(__cuda_arch_ptx "60")
143
144
  elseif(${CUDA_ARCH_NAME} STREQUAL "Volta")
    set(__cuda_arch_bin "70")
145
146
147
148
    set(__cuda_arch_ptx "70")
  elseif(${CUDA_ARCH_NAME} STREQUAL "Turing")
    set(__cuda_arch_bin "75")
    set(__cuda_arch_ptx "75")
149
150
  elseif(${CUDA_ARCH_NAME} STREQUAL "Ampere")
    set(__cuda_arch_bin "80")
151
152
153
154
155
156
157
    set(__cuda_arch_ptx "80")
  elseif(${CUDA_ARCH_NAME} STREQUAL "Ada")
    set(__cuda_arch_bin "89")
    set(__cuda_arch_ptx "89")
  elseif(${CUDA_ARCH_NAME} STREQUAL "Hopper")
    set(__cuda_arch_bin "90")
    set(__cuda_arch_ptx "90")
158
  elseif(${CUDA_ARCH_NAME} STREQUAL "All")
159
    set(__cuda_arch_bin ${dgl_known_gpu_archs})
160
    set(__cuda_arch_ptx ${dgl_cuda_arch_ptx})
161
162
  elseif(${CUDA_ARCH_NAME} STREQUAL "Auto")
    dgl_detect_installed_gpus(__cuda_arch_bin)
163
164
165
    # if detect successes, __cuda_arch_ptx = __cuda_arch_bin
    # if detect fails, __cuda_arch_ptx is the latest arch in __cuda_arch_bin
    list(GET __cuda_arch_bin -1 __cuda_arch_ptx)
166
167
  else()  # (${CUDA_ARCH_NAME} STREQUAL "Manual")
    set(__cuda_arch_bin ${CUDA_ARCH_BIN})
168
    set(__cuda_arch_ptx ${CUDA_ARCH_PTX})
169
170
171
172
  endif()

  # remove dots and convert to lists
  string(REGEX REPLACE "\\." "" __cuda_arch_bin "${__cuda_arch_bin}")
173
  string(REGEX REPLACE "\\." "" __cuda_arch_ptx "${__cuda_arch_ptx}")
174
175
176
177
  string(REGEX MATCHALL "[0-9()]+" __cuda_arch_bin "${__cuda_arch_bin}")
  string(REGEX MATCHALL "[0-9]+"   __cuda_arch_ptx "${__cuda_arch_ptx}")
  mshadow_list_unique(__cuda_arch_bin __cuda_arch_ptx)

178
  set(__nvcc_flags "--expt-relaxed-constexpr")
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
  set(__nvcc_archs_readable "")

  # Tell NVCC to add binaries for the specified GPUs
  foreach(__arch ${__cuda_arch_bin})
    if(__arch MATCHES "([0-9]+)\\(([0-9]+)\\)")
      # User explicitly specified PTX for the concrete BIN
      list(APPEND __nvcc_flags -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1})
      list(APPEND __nvcc_archs_readable sm_${CMAKE_MATCH_1})
    else()
      # User didn't explicitly specify PTX for the concrete BIN, we assume PTX=BIN
      list(APPEND __nvcc_flags -gencode arch=compute_${__arch},code=sm_${__arch})
      list(APPEND __nvcc_archs_readable sm_${__arch})
    endif()
  endforeach()

  # Tell NVCC to add PTX intermediate code for the specified architectures
  foreach(__arch ${__cuda_arch_ptx})
    list(APPEND __nvcc_flags -gencode arch=compute_${__arch},code=compute_${__arch})
    list(APPEND __nvcc_archs_readable compute_${__arch})
  endforeach()

  string(REPLACE ";" " " __nvcc_archs_readable "${__nvcc_archs_readable}")
  set(${out_variable}          ${__nvcc_flags}          PARENT_SCOPE)
  set(${out_variable}_readable ${__nvcc_archs_readable} PARENT_SCOPE)
endfunction()

################################################################################################
# Config cuda compilation.
# Usage:
#   dgl_config_cuda(<dgl_cuda_src>)
macro(dgl_config_cuda out_variable)
  if(NOT CUDA_FOUND)
    message(FATAL_ERROR "Cannot find CUDA.")
  endif()
  # always set the includedir when cuda is available
  # avoid global retrigger of cmake
	include_directories(${CUDA_INCLUDE_DIRS})

  add_definitions(-DDGL_USE_CUDA)

  file(GLOB_RECURSE DGL_CUDA_SRC
220
221
    src/array/cuda/*.cc
    src/array/cuda/*.cu
222
223
    src/array/cuda/uvm/*.cc
    src/array/cuda/uvm/*.cu
224
225
    src/kernel/cuda/*.cc
    src/kernel/cuda/*.cu
226
    src/partition/cuda/*.cu
227
    src/runtime/cuda/*.cc
228
    src/runtime/cuda/*.cu
229
    src/geometry/cuda/*.cu
230
    src/graph/transform/cuda/*.cu
231
    src/graph/sampling/randomwalks/*.cu
232
233
  )

234
235
236
237
238
  # NVCC flags
  # Manually set everything
  set(CUDA_PROPAGATE_HOST_FLAGS OFF)

  # 0. Add host flags
239
  message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
240
  string(REGEX REPLACE "[ \t\n\r]" "," CXX_HOST_FLAGS "${CMAKE_CXX_FLAGS}")
241
242
243
  if(MSVC AND NOT USE_MSVC_MT)
    string(CONCAT CXX_HOST_FLAGS ${CXX_HOST_FLAGS} ",/MD")
  endif()
czkkkkkk's avatar
czkkkkkk committed
244
  list(APPEND CUDA_NVCC_FLAGS "-Xcompiler" "${CXX_HOST_FLAGS}")
245
246

  # 1. Add arch flags
247
  dgl_select_nvcc_arch_flags(NVCC_FLAGS_ARCH)
248
249
250
  list(APPEND CUDA_NVCC_FLAGS ${NVCC_FLAGS_ARCH})

  # 2. flags in third_party/moderngpu
251
  list(APPEND CUDA_NVCC_FLAGS "--expt-extended-lambda;-Wno-deprecated-declarations;-std=c++17")
252

253
  message(STATUS "CUDA_NVCC_FLAGS: ${CUDA_NVCC_FLAGS}")
254
255

  list(APPEND DGL_LINKER_LIBS
256
257
    ${CUDA_CUDART_LIBRARY}
    ${CUDA_CUBLAS_LIBRARIES}
258
    ${CUDA_cusparse_LIBRARY})
259
260
261

  set(${out_variable} ${DGL_CUDA_SRC})
endmacro()