Skip to content

Instantly share code, notes, and snippets.

@daidedou
Created June 5, 2020 18:43
Show Gist options
  • Save daidedou/700cd87d6e24fbff1e2190fad1224c08 to your computer and use it in GitHub Desktop.
Save daidedou/700cd87d6e24fbff1e2190fad1224c08 to your computer and use it in GitHub Desktop.
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -ccbin=cuda-g++")
cmake_minimum_required(VERSION 3.7)
project(myproject)
#enable_language("CUDA")
find_package(CUDA QUIET)
Set(USE_CUDA ${CUDA_FOUND})
# second : we detect Gpu properties with a home-made script
if(USE_CUDA)
# getting some properties of GPUs to pass them as "-D..." options at compilation (adapted from caffe git repo).
function(caffe_detect_installed_gpus out_variable)
if(NOT CUDA_gpu_detect_props)
set(__cufile ${PROJECT_BINARY_DIR}/detect_cuda_props.cu)
file(WRITE ${__cufile} ""
"#include <cstdio>\n"
"int main()\n"
"{\n"
" int count = 0;\n"
" if (cudaSuccess != cudaGetDeviceCount(&count)) return -1;\n"
" if (count == 0) return -1;\n"
" std::printf(\"-DMAXIDGPU=%d;\",count-1);\n"
" for (int device = 0; device < count; ++device)\n"
" {\n"
" cudaDeviceProp prop;\n"
" if (cudaSuccess == cudaGetDeviceProperties(&prop, device))\n"
" std::printf(\"-DMAXTHREADSPERBLOCK%d=%d;-DSHAREDMEMPERBLOCK%d=%d;\", device, (int)prop.maxThreadsPerBlock, device, (int)prop.sharedMemPerBlock);\n"
" }\n"
" return 0;\n"
"}\n")
execute_process(COMMAND "${CUDA_NVCC_EXECUTABLE}" "--run" "${__cufile}" "-ccbin=cuda-g++"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/CMakeFiles/"
RESULT_VARIABLE __nvcc_res OUTPUT_VARIABLE __nvcc_out
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if(__nvcc_res EQUAL 0)
set(CUDA_gpu_detect_props ${__nvcc_out} CACHE INTERNAL "Returned GPU architetures from caffe_detect_gpus tool" FORCE)
endif()
endif()
if(NOT CUDA_gpu_detect_props)
set(${out_variable} FALSE PARENT_SCOPE)
else()
set(${out_variable} ${CUDA_gpu_detect_props} PARENT_SCOPE)
endif()
endfunction()
# run the detection
if(NOT gpu_compute_props)
caffe_detect_installed_gpus(gpu_compute_props)
if(NOT gpu_compute_props)
set(USE_CUDA FALSE)
message(STATUS "No GPU detected. USE_CUDA set to FALSE.")
else()
message(STATUS "Compute properties automatically set to: ${gpu_compute_props}")
add_definitions(${gpu_compute_props})
endif()
else()
message(STATUS "Compute properties manually set to ${gpu_compute_props}")
add_definitions(${gpu_compute_props})
endif()
endif()
# Check for CUDA ENV vars
IF(NOT DEFINED ENV{CUDA_PATH})
MESSAGE(FATAL_ERROR "CUDA_PATH Environment variable is not set.")
ENDIF(NOT DEFINED ENV{CUDA_PATH})
# Set the toolkit path
FILE(TO_CMAKE_PATH "$ENV{CUDA_PATH}" CUDA_TOOLKIT_ROOT_DIR)
SET(CUDA_TOOLKIT_ROOT_DIR ${CUDA_TOOLKIT_ROOT_DIR} CACHE STRING "Root directory of the Cuda Library" FORCE)
# Find the package
find_package(CUDA REQUIRED)
# Create and interface library as a link target (Requires CMake 3.7.0+)
add_library(cuda INTERFACE)
set_target_properties(cuda PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CUDA_INCLUDE_DIRS}
INTERFACE_LINK_LIBRARIES "${CUDA_LIBRARIES};${CUDA_CUFFT_LIBRARIES};${CUDA_CUBLAS_LIBRARIES}"
)
SET(CUDA_HOST_COMPILATION_CPP ON)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment