Skip to content

Commit

Permalink
new embed ptx function
Browse files Browse the repository at this point in the history
  • Loading branch information
gralkapk committed Jul 5, 2024
1 parent e7df26c commit 7790963
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugins/optix_hpg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ if (optix_hpg_PLUGIN_ENABLED)
set(CMAKE_CUDA_ARCHITECTURES 80)
endif ()

include(configure_optix)
find_package(CUDAToolkit)
include(configure_optix)

get_target_property(GLM_INCLUDES glm::glm INTERFACE_INCLUDE_DIRECTORIES)

Expand Down
36 changes: 35 additions & 1 deletion plugins/optix_hpg/cmake/configure_optix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# ${CMAKE_MODULE_PATH}
# )

include(configure_cuda)
#include(configure_cuda)
#find_package(CUDA REQUIRED)
find_package(OptiX REQUIRED VERSION 7)

Expand All @@ -34,6 +34,40 @@ endif()
find_program(BIN2C bin2c
DOC "Path to the cuda-sdk bin2c executable.")

# adapted from https://github.com/owl-project/owl/blob/master/owl/cmake/embed_ptx.cmake
## Copyright 2021 Jefferson Amstutz
## SPDX-License-Identifier: Apache-2.0
function(embed_ptx)
set(oneArgs OUTPUT_TARGET)
set(multiArgs PTX_LINK_LIBRARIES SOURCES)
cmake_parse_arguments(EMBED_PTX "" "${oneArgs}" "${multiArgs}" ${ARGN})

if (NOT ${NUM_SOURCES} EQUAL 1)
message(FATAL_ERROR
"embed_ptx() can only compile and embed one file at a time.")
endif()

set(PTX_TARGET ${EMBED_PTX_OUTPUT_TARGET}_ptx)

add_library(${PTX_TARGET} OBJECT)
target_sources(${PTX_TARGET} PRIVATE ${EMBED_PTX_SOURCES})
target_link_libraries(${PTX_TARGET} PRIVATE ${EMBED_PTX_PTX_LINK_LIBRARIES})
set_property(TARGET ${PTX_TARGET} PROPERTY CUDA_PTX_COMPILATION ON)
set_property(TARGET ${PTX_TARGET} PROPERTY CUDA_ARCHITECTURES OFF)

get_filename_component(OUTPUT_FILE_NAME ${EMBED_PTX_C_FILE} NAME)
add_custom_command(
OUTPUT ${EMBED_PTX_C_FILE}
COMMAND ${BIN2C} -c --padd 0 --type char --name ${EMBED_PTX_OUTPUT_TARGET} $<TARGET_OBJECTS:${PTX_TARGET}> > ${EMBED_PTX_C_FILE}
VERBATIM
DEPENDS $<TARGET_OBJECTS:${PTX_TARGET}> ${PTX_TARGET}
COMMENT "Generating embedded PTX file: ${OUTPUT_FILE_NAME}"
)

add_library(${EMBED_PTX_OUTPUT_TARGET} OBJECT)
target_sources(${EMBED_PTX_OUTPUT_TARGET} PRIVATE ${EMBED_PTX_C_FILE})
endfunction()

# this macro defines cmake rules that execute the following four steps:
# 1) compile the given cuda file ${cuda_file} to an intermediary PTX file
# 2) use the 'bin2c' tool (that comes with CUDA) to
Expand Down

0 comments on commit 7790963

Please sign in to comment.