Automatically detect cuda availability

This commit is contained in:
2024-09-05 17:38:49 +02:00
parent e7ec6c91f8
commit 9973ebc477

View File

@@ -4,27 +4,45 @@ project(CUDANet
LANGUAGES CXX CUDA LANGUAGES CXX CUDA
) )
find_package(CUDAToolkit REQUIRED) find_package(CUDAToolkit QUIET)
include_directories(${CUDAToolkit_INCLUDE_DIRS})
file(GLOB_RECURSE LIBRARY_SOURCES # Set USE_CUDA option based on whether CUDA was found
src/*.cu if(CUDAToolkit_FOUND)
src/utils/*.cu option(USE_CUDA "Use CUDA implementation" ON)
src/kernels/*.cu else()
src/layers/*.cu option(USE_CUDA "Use CUDA implementation" OFF)
message(STATUS "CUDA not found. Defaulting to CPU implementation.")
endif()
if(USE_CUDA)
enable_language(CUDA)
add_definitions(-DUSE_CUDA)
endif()
file(GLOB_RECURSE CPU_SOURCES
src/model/*.cpp src/model/*.cpp
) )
set(LIBRARY_SOURCES set(LIBRARY_SOURCES ${CPU_SOURCES})
${LIBRARY_SOURCES}
) if(USE_CUDA)
file(GLOB_RECURSE CUDA_SOURCES
src/*.cu
src/cuda/utils/*.cu
src/cuda/kernels/*.cu
src/cuda/layers/*.cu
)
set(LIBRARY_SOURCES ${LIBRARY_SOURCES} ${CUDA_SOURCES})
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Build static library # Build static library
add_library(${PROJECT_NAME} STATIC ${LIBRARY_SOURCES}) add_library(${PROJECT_NAME} STATIC ${LIBRARY_SOURCES})
target_link_libraries(${PROJECT_NAME} CUDA::cudart) if(USE_CUDA)
target_link_libraries(${PROJECT_NAME} CUDA::cudart)
endif()
# Set include directories for the library # Set include directories for the library
target_include_directories(${PROJECT_NAME} PUBLIC target_include_directories(${PROJECT_NAME} PUBLIC