Skip to content

Commit

Permalink
Improve GPU detection by doing less subsequent executions (#222)
Browse files Browse the repository at this point in the history
We now only write out the source file, and compile the executable when the executable doesn't already exist.

Authors:
  - Robert Maynard (https://github.com/robertmaynard)

Approvers:
  - Bradley Dice (https://github.com/bdice)

URL: #222
  • Loading branch information
robertmaynard authored Jul 29, 2022
1 parent ad9c114 commit 9c9f0dd
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions rapids-cmake/cuda/detail/detect_architectures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ function(rapids_cuda_detect_architectures possible_archs_var gpu_archs)

# Unset this first in case it's set to <empty_string> Which can happen inside rapids
set(CMAKE_CUDA_ARCHITECTURES OFF)
set(possible_archs ${${possible_archs_var}})
set(__gpu_archs ${${possible_archs_var}})

set(eval_file ${PROJECT_BINARY_DIR}/eval_gpu_archs.cu)
set(eval_exe ${PROJECT_BINARY_DIR}/eval_gpu_archs)
set(error_file ${PROJECT_BINARY_DIR}/eval_gpu_archs.stderr.log)
file(WRITE ${eval_file}
"

if(NOT DEFINED CMAKE_CUDA_COMPILER)
message(FATAL_ERROR "No CUDA compiler specified, unable to determine machine's GPUs.")
endif()

if(NOT EXISTS "${eval_exe}")
file(WRITE ${eval_file}
"
#include <cstdio>
#include <set>
#include <string>
Expand All @@ -46,7 +52,7 @@ int main(int argc, char** argv) {
}
}
if(archs.empty()) {
printf(\"${possible_archs}\");
printf(\"${__gpu_archs}\");
} else {
bool first = true;
for(const auto& arch : archs) {
Expand All @@ -56,15 +62,18 @@ int main(int argc, char** argv) {
}
printf(\"\\n\");
return 0;
}
")
}
")
execute_process(COMMAND ${CMAKE_CUDA_COMPILER} -std=c++11 -o "${eval_exe}" "${eval_file}"
ERROR_FILE "${error_file}")
endif()

set(__gpu_archs "${possible_archs}")
if(DEFINED CMAKE_CUDA_COMPILER)
execute_process(COMMAND ${CMAKE_CUDA_COMPILER} -std=c++11 -o ${eval_exe} --run ${eval_file}
OUTPUT_VARIABLE __gpu_archs OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_FILE ${error_file})
if(EXISTS "${eval_exe}")
execute_process(COMMAND "${eval_exe}" OUTPUT_VARIABLE __gpu_archs
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_FILE "${error_file}")
message(STATUS "Auto detection of gpu-archs: ${__gpu_archs}")
else()
message(STATUS "Failed auto detection of gpu-archs. Falling back to using ${__gpu_archs}.")
endif()

set(${gpu_archs} ${__gpu_archs} PARENT_SCOPE)
Expand Down

0 comments on commit 9c9f0dd

Please sign in to comment.