Skip to content

Commit 929f498

Browse files
committed
build: refactor CUDA macros, adjust CUDA_TARGET_ARCH by CUDA version
Shuffle some options and macros among cmake files. Ensure that most "find dependencies" logic is in externalpackages.cmake, and move more options and logic related to "how do we compile to ptx and link against cuda/optix" into cuda_macros. The CUDA target architecture had been sm_60 for a long time, but CUDA Toolkit 13.0+ doesn't target that old an architecture. So make the architecture dependent on CUDA vrsion -- 13+ gets sm_75 as its default now (still overrideable by setting build option CUDA_TARGET_ARCH_NUMBER, but if CUDA <= 12.x is found, it gets a default target of sm_60 in order to allow a wider range of target cards. Some other miscellaneous cleanup here and there. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent b1bf9fe commit 929f498

6 files changed

Lines changed: 42 additions & 34 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ option (OSL_BUILD_PLUGINS "Bool OSL plugins, for example OIIO plugin" ON)
127127
option (OSL_BUILD_SHADERS "Build shaders" ON)
128128
option (OSL_USE_OPTIX "Include OptiX support" OFF)
129129
message(STATUS "OSL_USE_OPTIX: ${OSL_USE_OPTIX}")
130-
set (OPTIX_EXTRA_LIBS CACHE STRING "Extra lib targets needed for OptiX")
131-
set (CUDA_EXTRA_LIBS CACHE STRING "Extra lib targets needed for CUDA")
132-
set (CUDA_TARGET_ARCH "sm_60" CACHE STRING "CUDA GPU architecture (e.g. sm_50)")
133130
set (OSL_SHADER_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/shaders"
134131
CACHE STRING "Directory where shaders will be installed")
135132
set (OSL_PTX_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/ptx"
@@ -210,8 +207,8 @@ include (pythonutils)
210207
# Dependency finding utilities and all dependency-related options
211208
include (externalpackages)
212209
include (flexbison)
213-
include (cuda_macros)
214210
include (llvm_macros)
211+
include (cuda_macros)
215212

216213
# Include all our testing apparatus and utils, but not if it's a subproject
217214
if (${PROJECT_NAME}_BUILD_TESTS AND PROJECT_IS_TOP_LEVEL)

src/cmake/cuda_macros.cmake

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33
# https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
44

5-
set ($OSL_EXTRA_NVCC_ARGS "" CACHE STRING "Custom args passed to nvcc when compiling CUDA code")
5+
# NOTE: externalpackages.cmake is included before this file
6+
7+
if (CUDA_VERSION VERSION_GREATER 13.0)
8+
# CUDA 13+ requires sm_75 at least, so this is the minimum
9+
set (CUDA_TARGET_ARCH_NUMBER "75" CACHE STRING "CUDA GPU architecture numeric code (e.g. 60)")
10+
else ()
11+
# CUDA <= 12.x supports old GPUs, so conservatively target sm_60 to give
12+
# the widest possible range of GPUs that OSL shaders will run on.
13+
set (CUDA_TARGET_ARCH_NUMBER "60" CACHE STRING "CUDA GPU architecture numeric code (e.g. 60)")
14+
endif ()
15+
set (CUDA_TARGET_ARCH "sm_${CUDA_TARGET_ARCH_NUMBER}" CACHE STRING "CUDA GPU architecture (e.g. 60)")
16+
set (OSL_EXTRA_NVCC_ARGS "" CACHE STRING "Custom args passed to nvcc when compiling CUDA code")
617
set (CUDA_OPT_FLAG_NVCC "-O3" CACHE STRING "The optimization level to use when compiling CUDA/C++ files with nvcc")
718
set (CUDA_OPT_FLAG_CLANG "-O3" CACHE STRING "The optimization level to use when compiling CUDA/C++ files with clang")
19+
set (CUDA_EXTRA_LIBS CACHE STRING "Extra lib targets needed for CUDA")
20+
set (OPTIX_EXTRA_LIBS CACHE STRING "Extra lib targets needed for OptiX")
21+
set (CUDA_LIB_FLAGS "--cuda-path=${CUDA_TOOLKIT_ROOT_DIR}")
822

923
# FTZ is enabled by default since it offers the best performance,
1024
# but it may be disabled to debug numerical issues, to better match
@@ -14,6 +28,15 @@ if (CUDA_NO_FTZ)
1428
add_compile_definitions (DOSL_CUDA_NO_FTZ=1)
1529
endif ()
1630

31+
32+
function (osl_optix_target TARGET)
33+
target_include_directories (${TARGET} BEFORE PRIVATE ${CUDA_INCLUDES} ${OPTIX_INCLUDES})
34+
## XXX: Should -DPTX_PATH point to (or include) CMAKE_CURRENT_BINARY_DIR so tests can run before installation ?
35+
target_compile_definitions (${TARGET} PRIVATE PTX_PATH="${OSL_PTX_INSTALL_DIR}")
36+
target_link_libraries (${TARGET} PRIVATE ${CUDA_LIBRARIES} ${CUDA_EXTRA_LIBS} ${OPTIX_LIBRARIES} ${OPTIX_EXTRA_LIBS})
37+
endfunction()
38+
39+
1740
# Compile a CUDA file to PTX using NVCC
1841
function ( NVCC_COMPILE cuda_src extra_headers ptx_generated extra_nvcc_args extra_libs)
1942
get_filename_component ( cuda_src_we ${cuda_src} NAME_WE )
@@ -167,7 +190,7 @@ function ( MAKE_CUDA_BITCODE src suffix generated_bc extra_clang_args )
167190
${ALL_OpenImageIO_INCLUDES}
168191
${ALL_IMATH_INCLUDES}
169192
${ALL_OPENEXR_INCLUDES}
170-
${LLVM_COMPILE_FLAGS} ${CUDA_LIB_FLAGS} ${CLANG_MSVC_FIX} ${CUDA_TEXREF_FIX}
193+
${LLVM_COMPILE_FLAGS} ${CUDA_LIB_FLAGS} ${CLANG_MSVC_FIX} ${CUDA_TEXREF_FIX}
171194
-D__CUDACC__ -DOSL_COMPILING_TO_BITCODE=1 -DNDEBUG -DOIIO_NO_SSE -D__CUDADEVRT_INTERNAL__
172195
--language=cuda --cuda-device-only --cuda-gpu-arch=${CUDA_TARGET_ARCH}
173196
-Wno-deprecated-register -Wno-format-security

src/cmake/externalpackages.cmake

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ if (OSL_USE_OPTIX)
118118
message (STATUS "CUDA_TOOLKIT_ROOT_DIR = ${CUDA_TOOLKIT_ROOT_DIR}")
119119
endif ()
120120

121-
checked_find_package (CUDA REQUIRED
121+
checked_find_package(CUDA REQUIRED
122122
VERSION_MIN 9.0
123123
RECOMMEND_MIN 11.0
124124
RECOMMEND_MIN_REASON
@@ -142,17 +142,16 @@ if (OSL_USE_OPTIX)
142142
"first shipped in LLVM 22.1.2. Either upgrade LLVM or downgrade CUDA.${ColorReset}")
143143
endif ()
144144

145-
set (CUDA_LIB_FLAGS "--cuda-path=${CUDA_TOOLKIT_ROOT_DIR}")
146-
147-
# If the user wants, try to use static libs here to putting static lib
148-
# suffixes earlier in the suffix list. Don't forget to restore after
149-
# so that this only applies to these library searches right here.
150-
set (save_lib_path ${CMAKE_FIND_LIBRARY_SUFFIXES})
151145
if (CUDA_PREFER_STATIC_LIBS)
146+
# If the user wants, try to use static libs here by putting static
147+
# lib suffixes earlier in the suffix list.
148+
set (save_lib_path ${CMAKE_FIND_LIBRARY_SUFFIXES})
152149
set (CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
153150
find_library(cudart_lib REQUIRED
154151
NAMES cudart_static cudart
155152
PATHS "${CUDA_TOOLKIT_ROOT_DIR}/lib64" "${CUDA_TOOLKIT_ROOT_DIR}/x64" "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64")
153+
set (CMAKE_FIND_LIBRARY_SUFFIXES ${save_lib_path})
154+
unset (save_lib_path)
156155
else ()
157156
find_library(cudart_lib REQUIRED
158157
NAMES cudart
@@ -161,8 +160,6 @@ if (OSL_USE_OPTIX)
161160
# Is it really a good idea to completely reset CUDA_LIBRARIES here?
162161
set(CUDA_LIBRARIES ${cudart_lib})
163162
set(CUDA_EXTRA_LIBS ${CUDA_EXTRA_LIBS} dl rt)
164-
set (CMAKE_FIND_LIBRARY_SUFFIXES ${save_lib_path})
165-
unset (save_lib_path)
166163
endif()
167164

168165
# OptiX setup
@@ -174,17 +171,6 @@ if (OSL_USE_OPTIX)
174171
message (FATAL_ERROR "Enabling OptiX requires USE_LLVM_BITCODE=1 and USE_FAST_MATH=1")
175172
endif ()
176173
endif ()
177-
178-
function (osl_optix_target TARGET)
179-
target_include_directories (${TARGET} BEFORE PRIVATE ${CUDA_INCLUDES} ${OPTIX_INCLUDES})
180-
## XXX: Should -DPTX_PATH point to (or include) CMAKE_CURRENT_BINARY_DIR so tests can run before installation ?
181-
target_compile_definitions (${TARGET} PRIVATE PTX_PATH="${OSL_PTX_INSTALL_DIR}")
182-
target_link_libraries (${TARGET} PRIVATE ${CUDA_LIBRARIES} ${CUDA_EXTRA_LIBS} ${OPTIX_LIBRARIES} ${OPTIX_EXTRA_LIBS})
183-
endfunction()
184-
else ()
185-
message(STATUS "CUDA/OptiX support disabled")
186-
function (osl_optix_target TARGET)
187-
endfunction()
188174
endif ()
189175

190176

src/testrender/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ target_link_libraries (testrender
8585
pugixml::pugixml
8686
Threads::Threads)
8787

88-
osl_optix_target (testrender)
88+
if (OSL_USE_OPTIX)
89+
osl_optix_target (testrender)
90+
endif ()
8991

9092
install ( TARGETS testrender RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )

src/testshade/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ endif ()
8888

8989
install (TARGETS testshade RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
9090

91-
osl_optix_target(testshade)
92-
91+
if (OSL_USE_OPTIX)
92+
osl_optix_target(testshade)
93+
endif ()
9394

9495
# As explained in PR #39, problems were reported with calling OSL from a
9596
# Houdini plugin. So this section sets up a version of testshade as a DSO, to

testsuite/example-cuda/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33
# https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
44

5-
cmake_minimum_required(VERSION 3.15)
5+
cmake_minimum_required(VERSION 3.19)
66
project(examplecuda LANGUAGES CXX)
77

88
if (NOT CMAKE_BUILD_TYPE)
@@ -20,9 +20,9 @@ include(checked_find_package)
2020

2121
find_package(OSL REQUIRED)
2222
find_package(CUDA REQUIRED)
23-
checked_find_package(LLVM 7.0 REQUIRED)
23+
checked_find_package(LLVM 14.0 REQUIRED)
2424
checked_find_package(Imath 3.1 REQUIRED)
25-
checked_find_package(OpenImageIO 2.4 REQUIRED)
25+
checked_find_package(OpenImageIO 3.0 REQUIRED)
2626
checked_find_package(OptiX REQUIRED)
2727

2828

@@ -35,8 +35,7 @@ set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
3535
find_library(CUDA_nvrtc_LIBRARY nvrtc HINTS ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64)
3636
find_library(CUDA_cuda_LIBRARY cuda HINTS ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib/stubs lib64/stubs)
3737

38-
# TODO: move to sm_60?
39-
set(CUDA_TARGET_ARCH sm_35)
38+
set(CUDA_TARGET_ARCH sm_75)
4039

4140
set (CMAKE_CXX_STANDARD 17 CACHE STRING
4241
"C++ standard to build with (17, 20, etc.)")

0 commit comments

Comments
 (0)