Skip to content

Commit

Permalink
COMP: clean and modernize CMake handling of CUDA compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Rit committed Jul 16, 2021
1 parent f66e98a commit 642ad25
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 185 deletions.
22 changes: 3 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
endif()
endif()

#=========================================================
# Allow for multiple CL.EXE to write to the same .PDB file
if(RTK_USE_CUDA)
if(WIN32)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /FS")
endif()
endif()

include(cmake/rtkCompilerFlags.cmake)

# --------------------------------------------------------
Expand Down Expand Up @@ -212,11 +204,7 @@ set(RTK_VERSION_PATCH ${RTK_VERSION_PATCH})
# Whether the compiled version of RTK uses CUDA
set(RTK_USE_CUDA ${RTK_USE_CUDA})
if(${RTK_USE_CUDA})
list(APPEND CMAKE_MODULE_PATH ${RTK_MODULE_PATH_CONFIG})
# if we are using CUDA, make sure CUDA libraries are available
find_package(CUDA_wrap REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
if(RTK_USE_CUDA)
set(RTK_CUDA_PROJECTIONS_SLAB_SIZE \"16\" CACHE STRING \"Number of projections processed simultaneously in CUDA forward and back projections\")
endif()
")
Expand All @@ -230,9 +218,7 @@ set(RTK_VERSION_PATCH ${RTK_VERSION_PATCH})
# Whether the compiled version of RTK uses CUDA
set(RTK_USE_CUDA ${RTK_USE_CUDA})
if(${RTK_USE_CUDA})
# if we are using CUDA, make sure CUDA libraries are available
find_package(CUDA_wrap REQUIRED)
if(RTK_USE_CUDA)
set(RTK_CUDA_PROJECTIONS_SLAB_SIZE \"16\" CACHE STRING \"Number of projections processed simultaneously in CUDA forward and back projections\")
endif()
")
Expand All @@ -253,11 +239,9 @@ endif()

# Install configuration file
install(FILES ${RTK_BINARY_DIR}/rtkConfiguration.h DESTINATION ${RTK_INSTALL_INCLUDE_DIR})
install(FILES ${RTK_SOURCE_DIR}/cmake/FindCUDA_wrap.cmake
${RTK_SOURCE_DIR}/cmake/FindGengetopt.cmake
install(FILES ${RTK_SOURCE_DIR}/cmake/FindGengetopt.cmake
${RTK_SOURCE_DIR}/cmake/GetGitRevisionDescription.cmake
${RTK_SOURCE_DIR}/cmake/GetGitRevisionDescription.cmake.in
${RTK_SOURCE_DIR}/cmake/has_cuda_gpu.cxx
DESTINATION ${ITK_INSTALL_PACKAGE_DIR})
if(NOT ITK_SOURCE_DIR)
install(CODE "MESSAGE(FATAL_ERROR \"Cannot install, RTK is compiled separately from ITK. Installation is only functional if RTK is compiled within ITK.\")")
Expand Down
2 changes: 1 addition & 1 deletion applications/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ if(BUILD_TESTING)
set_tests_properties(rtkappprojectshepploganphantomtest PROPERTIES DEPENDS rtkappsimulatedgeometrytest)

# FDK test
if(RTK_USE_CUDA AND CUDA_HAVE_GPU)
if(RTK_USE_CUDA)
add_test(rtkappfdktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkfdk -g geo -p . -r sheppy.mha -o fdk.mha --hardware cuda --dimension 21)
else()
add_test(rtkappfdktest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rtkfdk -g geo -p . -r sheppy.mha -o fdk.mha --dimension 21)
Expand Down
77 changes: 0 additions & 77 deletions cmake/FindCUDA_wrap.cmake

This file was deleted.

28 changes: 0 additions & 28 deletions cmake/has_cuda_gpu.cxx

This file was deleted.

31 changes: 20 additions & 11 deletions itk-module-init.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@
#
list(APPEND CMAKE_MODULE_PATH ${RTK_SOURCE_DIR}/cmake)

find_package(CUDA_wrap QUIET)
if(CUDA_FOUND)
if(${CUDA_VERSION} VERSION_LESS 8.0)
message(WARNING "CUDA version ${CUDA_VERSION} is not supported by RTK.")
set(RTK_USE_CUDA_DEFAULT OFF)
include(CheckLanguage)
check_language(CUDA)

# Determine default value for RTK_USE_CUDA
set(RTK_USE_CUDA_DEFAULT OFF)
if (CMAKE_CUDA_COMPILER)
find_package(CUDAToolkit)
if(NOT CUDAToolkit_FOUND)
message(WARNING "CUDAToolkit not found (available since CMake v3.17). RTK_USE_CUDA set to OFF.")
elseif(DEFINED CUDAToolkit_VERSION AND "${CUDAToolkit_VERSION}" VERSION_LESS 8.0)
message(WARNING "CUDA version ${CUDAToolkit_VERSION} is not supported by RTK, version 8 is required. RTK_USE_CUDA set to OFF.")
else()
set(RTK_USE_CUDA_DEFAULT ON)
endif()
else()
set(RTK_USE_CUDA_DEFAULT OFF)
endif()
option(RTK_USE_CUDA "Use CUDA for RTK" ${RTK_USE_CUDA_DEFAULT})

# Configure CUDA compilation options
if(RTK_USE_CUDA)
if(NOT CUDA_FOUND)
find_package(CUDA_wrap REQUIRED)
endif()
enable_language(CUDA)
set(CMAKE_CUDA_RUNTIME_LIBRARY Static)

include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
find_package(CUDAToolkit REQUIRED 8.0)

set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-declarations")
set(RTK_CUDA_PROJECTIONS_SLAB_SIZE "16" CACHE STRING "Number of projections processed simultaneously in CUDA forward and back projections")
endif()

3 changes: 0 additions & 3 deletions rtkConfiguration.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ namespace rtk
using ThreadIdType = itk::ThreadIdType;
}

#ifndef CUDA_FOUND
# cmakedefine CUDA_FOUND
#endif
#cmakedefine RTK_PROBE_EACH_FILTER
#cmakedefine RTK_USE_CUDA
#cmakedefine RTK_BUILD_SHARED_LIBS 1
Expand Down
62 changes: 29 additions & 33 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,32 @@ set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "${defComSansVTK}")
# CUDA library
#=========================================================
if (RTK_USE_CUDA)
set(RTK_CUDA_FILES
set(RTK_SRCS ${RTK_SRCS}
rtkCudaAverageOutOfROIImageFilter.cxx
rtkCudaConstantVolumeSeriesSource.cxx
rtkCudaConstantVolumeSource.cxx
rtkCudaCropImageFilter.cxx
rtkCudaCyclicDeformationImageFilter.cxx
rtkCudaDisplacedDetectorImageFilter.cxx
rtkCudaFDKBackProjectionImageFilter.cxx
rtkCudaFDKConeBeamReconstructionFilter.cxx
rtkCudaFDKWeightProjectionFilter.cxx
rtkCudaForwardWarpImageFilter.cxx
rtkCudaInterpolateImageFilter.cxx
rtkCudaIterativeFDKConeBeamReconstructionFilter.cxx
rtkCudaLagCorrectionImageFilter.cxx
rtkCudaLaplacianImageFilter.cxx
rtkCudaLastDimensionTVDenoisingImageFilter.cxx
rtkCudaParkerShortScanImageFilter.cxx
rtkCudaPolynomialGainCorrectionImageFilter.cxx
rtkCudaRayCastBackProjectionImageFilter.cxx
rtkCudaSplatImageFilter.cxx
rtkCudaTotalVariationDenoisingBPDQImageFilter.cxx
rtkCudaWarpBackProjectionImageFilter.cxx
rtkCudaWarpForwardProjectionImageFilter.cxx
rtkCudaWarpImageFilter.cxx)

set(RTK_SRCS ${RTK_SRCS}
rtkCudaAverageOutOfROIImageFilter.cu
rtkCudaBackProjectionImageFilter.cu
rtkCudaConjugateGradientImageFilter.cu
Expand Down Expand Up @@ -99,44 +124,15 @@ if (RTK_USE_CUDA)
rtkCudaWarpImageFilter.cu
rtkCudaWeidingerForwardModelImageFilter.cu
)

set(CUDA_PROPAGATE_HOST_FLAGS ON)
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)#WARNING: mandatory for defining runtime type \MT vs \MD
CUDA_COMPILE(RTK_CUDA_WRAPPERS ${RTK_CUDA_FILES})

set(RTK_SRCS ${RTK_SRCS}
rtkCudaAverageOutOfROIImageFilter.cxx
rtkCudaConstantVolumeSeriesSource.cxx
rtkCudaConstantVolumeSource.cxx
rtkCudaCropImageFilter.cxx
rtkCudaCyclicDeformationImageFilter.cxx
rtkCudaDisplacedDetectorImageFilter.cxx
rtkCudaFDKBackProjectionImageFilter.cxx
rtkCudaFDKConeBeamReconstructionFilter.cxx
rtkCudaFDKWeightProjectionFilter.cxx
rtkCudaForwardWarpImageFilter.cxx
rtkCudaInterpolateImageFilter.cxx
rtkCudaIterativeFDKConeBeamReconstructionFilter.cxx
rtkCudaLagCorrectionImageFilter.cxx
rtkCudaLaplacianImageFilter.cxx
rtkCudaLastDimensionTVDenoisingImageFilter.cxx
rtkCudaParkerShortScanImageFilter.cxx
rtkCudaPolynomialGainCorrectionImageFilter.cxx
rtkCudaRayCastBackProjectionImageFilter.cxx
rtkCudaSplatImageFilter.cxx
rtkCudaTotalVariationDenoisingBPDQImageFilter.cxx
rtkCudaWarpBackProjectionImageFilter.cxx
rtkCudaWarpForwardProjectionImageFilter.cxx
rtkCudaWarpImageFilter.cxx
${RTK_CUDA_WRAPPERS}
${RTK_CUDA_FILES})
endif()
#=========================================================

itk_module_add_library(RTK ${RTK_SRCS})
target_link_libraries(RTK LINK_PUBLIC lpsolve55)

if (RTK_USE_CUDA)
target_link_libraries( RTK LINK_PUBLIC ITKCudaCommon ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY} ${CUDA_cufft_LIBRARY} ${CUDA_CUBLAS_LIBRARIES} )
target_link_libraries(RTK LINK_PUBLIC CUDA::cuda_driver)
target_link_libraries(RTK LINK_PUBLIC CUDA::cufft)
target_link_libraries(RTK LINK_PUBLIC CUDA::cublas)
set_property(TARGET RTK PROPERTY CUDA_STANDARD ${CMAKE_CXX_STANDARD})
endif ()
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ endfunction()
# rtk_add_cuda_test(testname testfile [DATA{}])
# Add test depending on CUDA
function(rtk_add_cuda_test testname testfile)
if(RTK_USE_CUDA AND CUDA_HAVE_GPU)
if(RTK_USE_CUDA)
add_executable(${testname} ${testfile})
target_link_libraries( ${testname} ${RTK_LIBRARIES} ${ITK_LIBRARIES})
itk_add_test(NAME ${testname}
Expand Down
9 changes: 4 additions & 5 deletions utilities/ITKCudaCommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ project(ITKCudaCommon)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/CMake)

find_package(CUDA REQUIRED)
if(NOT ${CUDA_FOUND})
message(FATAL "Could not find CUDA")
endif()
enable_language(CUDA)
find_package(CUDAToolkit REQUIRED)
include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
set(CMAKE_CUDA_RUNTIME_LIBRARY Static)

set(ITKCudaCommon_LIBRARIES ITKCudaCommon)
set(ITKCudaCommon_SYSTEM_INCLUDE_DIRS ${CUDA_INCLUDE_DIRS})
set(ITKCudaCommon_SYSTEM_LIBRARY_DIRS ${CUDA_LIBRARIES})

set(ITKCudaCommon_INCLUDE_DIRS ${ITKCudaCommon_SYSTEM_INCLUDE_DIRS}
${ITKCudaCommon_SOURCE_DIR}/include PARENT_SCOPE)

Expand Down
8 changes: 1 addition & 7 deletions utilities/ITKCudaCommon/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ set(ITKCudaCommon_SRC
itkCudaMemoryProbe.cxx
)

include_directories(${CUDA_INCLUDE_DIRS})
include_directories(${ITKCudaCommon_SOURCE_DIR}/include)
include_directories(${ITK_INCLUDE_DIRS})
add_library(ITKCudaCommon ${ITK_LIBRARY_BUILD_TYPE} ${ITKCudaCommon_SRC})
target_link_libraries(ITKCudaCommon LINK_PUBLIC ${ITKCommon_LIBRARIES} ${CUDA_CUDA_LIBRARY} ${CUDA_LIBRARIES})

# if(ITK_SOURCE_DIR)
# itk_module_link_dependencies()
# itk_module_target(ITKCudaCommon)
# endif()
target_link_libraries(ITKCudaCommon LINK_PUBLIC ${ITKCommon_LIBRARIES} CUDA::cudart CUDA::cuda_driver)

if(NOT RTK_INSTALL_NO_LIBRARIES)
target_include_directories(ITKCudaCommon PUBLIC $<INSTALL_INTERFACE:${ITKCudaCommon_INSTALL_INCLUDE_DIR}>)
Expand Down

0 comments on commit 642ad25

Please sign in to comment.