NVCC compile to ptx using CMAKE cuda_compile_ptx

I have a simple kernel in the kernel.cu file

__global__ void add1( double * pi, double c ) 
{
    *pi += c;
}

      

and can easily compile it to ptx kernel.ptx file with:

nvcc -ptx kernel.cu

now I wanted to reproduce the same behavior using cmake with the following CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(cmake_ptx)
find_package(CUDA REQUIRED)
cuda_compile_ptx(
  test
  kernel.cu
)

      

but when i type

cmake. && do

no ptx file. How can I reproduce the behavior of the above nvcc command using cmake? Specifically how:

  • add separate compilation of ptx files to target (eg all) . I noticed that when there is another cuda_add_executable in the same CMakeFile, it also creates a ptx file, otherwise.

  • name the file like the original file , but with .ptx instead of .cu ending: related question: How to change the output file name cuda_compile_ptx in CMake?

+3


source to share


2 answers


cuda_compile_ptx

only creates rules for generating files, but does not add them to any target. You need to add a custom target that depends on the ptx files:

cmake_minimum_required(VERSION 2.8)
project(cmake_ptx)
find_package(CUDA REQUIRED)
cuda_compile_ptx(
  cuda_ptx_files
  kernel.cu
)
add_custom_target(ptx ALL
    DEPENDS ${cuda_ptx_files} kernel.cu
    SOURCES kernel.cu)

      



Now, if you run make

or make ptx

, it will generate ptx files.

+5


source


Just for reference, this is what worked for my project setup (I have src / and include / folder) using advice from the accepted answer:

cmake_minimum_required(VERSION 2.8)

project(cmake_ptx)

find_package(CUDA REQUIRED)
include_directories(include/)

cuda_compile_ptx(
  cuda_ptx_files
  src/common_kernels.cu
  OPTIONS -DCUDA_MATLAB
)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/common_kernels.ptx COMMAND ${CMAKE_COMMAND} -E rename ${cuda_ptx_files} ${CMAKE_BINARY_DIR}/common_kernels.ptx DEPENDS ${cuda_ptx_files}) 
add_custom_target(ptx ALL
    DEPENDS ${CMAKE_BINARY_DIR}/common_kernels.ptx src/common_kernels.cu
    SOURCES src/common_kernels.cu
)

      



This gave me exactly the same output as when called:

nvcc -ptx src / common_kernels.cu -I includes / -DCUDA_MATLAB

+1


source







All Articles