Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add more than one CUDA gencode using modern CMAKE (per target)?

I'm unable to get modern cmake (per target) to set more than one CUDA gencode flag.

CMake example:

target_compile_options(myHeaderLib INTERFACE                        
                        $<$<COMPILE_LANGUAGE:CUDA>:-gencode arch=compute_50,code=sm_50>
                        $<$<COMPILE_LANGUAGE:CUDA>:-gencode arch=compute_52,code=sm_52>
                        $<$<COMPILE_LANGUAGE:CUDA>:-gencode arch=compute_53,code=sm_53>
                        $<$<COMPILE_LANGUAGE:CUDA>:-gencode arch=compute_60,code=sm_60>
                        $<$<COMPILE_LANGUAGE:CUDA>:-gencode arch=compute_62,code=sm_62>
                        )

This will output:

... -gencode arch=compute_50,code=sm_50 arch=compute_52,code=sm_52 arch=compute_53,code=sm_53

But what is needed is:

... -gencode arch=compute_50,code=sm_50  -gencode arch=compute_52,code=sm_52  -gencode arch=compute_53,code=sm_53 ...

And NO just settings the ${CUDA_NVCC_FLAGS} is not a robust solution (that's not how modern CMAKE should be done).

like image 793
Jimmy Pettersson Avatar asked Feb 03 '19 15:02

Jimmy Pettersson


2 Answers

This is because of CMake "de-duplication" strategy for the compile options.

With target_compile_options you create correct option's list

-gencode arch=compute_50,code=sm_50  -gencode arch=compute_52,code=sm_52 ...

but CMake removes all -gencode except the first one:

-gencode arch=compute_50,code=sm_50 arch=compute_52,code=sm_52 ...

There is bugreport about needs to disable CMake "de-duplication" in some cases, and it has been resolved in 3.12 version by adding SHELL: construction.

Since CMake 3.12 this should work:

target_compile_options(myHeaderLib INTERFACE
   "SHELL:-gencode arch=compute_50,code=sm_50"
   "SHELL:-gencode arch=compute_52,code=sm_52"
)

How SHELL: works

First, placing combined options into double quotes prevents CMake to "de-duplicate" common options: CMake sees different strings

-gencode arch=compute_50,code=sm_50
-gencode arch=compute_52,code=sm_52

and doesn't think of them as a duplicated.

Then, a construction SHELL explains CMake that a string within given double quotes is needed to be splitted into separate command line options. But this splitting is performed after "de-duplication" stage, so repeated -gencode from different SHELL: strings are not removed.

like image 77
Tsyvarev Avatar answered Nov 19 '22 02:11

Tsyvarev


I know it's been a while. But recently I filed an issue and it turns out generators can also works perfectly. The trick to combine "SHELL:" with generators is to surround generators with double quotes.

Here's the link where the maintainer of cmake points out my mistake. Hope this helps others in the future.

target_compile_options(foo PRIVATE
  "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-gencode arch=compute_60,code=sm_60>"
  )
target_compile_options(foo PRIVATE
  "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-gencode arch=compute_52,code=sm_52 -gencode arch=compute_50,code=sm_50>"
  )
like image 1
TerryTsao Avatar answered Nov 19 '22 03:11

TerryTsao