14

I have a C project in Cmake in which I have embedded cuda kernel module.

I want to pass --ptxas-options=-v only to nvcc in-order to view Number of registers usage per thread and shared Memory usage per block.

By searching on howto pass flags to nvcc in Cmake, I came across a solution

add_compile_options(myprog
    PRIVATE
    $<$<COMPILE_LANGUAGE:C>:-Wall>
    $<$<COMPILE_LANGUAGE:CUDA>:-arch=sm_20 -ptxas-options=-v>
)

but this didn't show me the above properties. I think these flags aren't passed to nvcc properly.

How can I pass --ptxas-options=-v to my nvcc compiler ?

Nouman Tajik
  • 433
  • 1
  • 5
  • 18
  • 1
    I think you are looking for [target_compile_options](https://cmake.org/cmake/help/latest/command/target_compile_options.html), not [add_compile_options](https://cmake.org/cmake/help/latest/command/add_compile_options.html). – havogt Nov 12 '18 at 21:46
  • target_compile_options(myprog PUBLIC $<$:--ptxas-options=-v>) worked. Thank you. – Nouman Tajik Nov 13 '18 at 04:02

3 Answers3

23

The proper way to set CUDA flags only on a target is

target_compile_options(<my_target> PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:my_cuda_option>) 

This will set the option, via the generator expression, only for files which are compiled for the CUDA language.

Using CMAKE_CUDA_FLAGS as suggested by the other answer sets a global property for all targets, which might or might not be the right approach depending on the use-case.

havogt
  • 2,572
  • 1
  • 27
  • 37
15

The newer approach of cmake cuda sets some other variables. Check the docs here.

What we need is to set CMAKE_<LANG>_FLAGS, which actually CMAKE_CUDA_FLAGS here.

set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --ptxas-options=-v")
halfelf
  • 9,737
  • 13
  • 54
  • 63
1

How about?...

find_package( CUDA REQUIRED )
set( CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS}" "--ptxas-options=-v" )

include_directories( ${CUDA_INCLUDE_DIRS} )
cuda_add_library( kernel_lib ${sources} )    

You can check also the CMake CUDA documentation online... https://cmake.org/cmake/help/latest/module/FindCUDA.html

KlingonJoe
  • 815
  • 8
  • 17
  • Make sure you are compiling with the cuda__add ... target commands. – KlingonJoe Nov 12 '18 at 09:39
  • You should also think about breaking up your CMakeLists.txt file. You have a lot of stuff in there. Consider sending some stuff to /config directory, and adding subdirectory - separating the C and CUDA code into separate directories is a good idea here. – KlingonJoe Nov 12 '18 at 09:46