[CMake] FindCUDA - creating .ptx and .cubin files as final build target
James Bigler
jamesbigler at gmail.com
Fri Sep 10 17:04:48 EDT 2010
On Fri, Sep 10, 2010 at 1:06 PM, Alex G <mr.nuke.me at gmail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I have a project that I'm converting from plain ole' makefile to cmake.
> I need to have the build result be .ptx and .cubin (both, as the app
> attempts to read the .cubin first, and if it's incompatible with the GPU
> architecture, assemble the .ptx file).
>
> The way I did it with the ole' makefile approach was to invoke nvcc
> - --ptx on File1.cu and File2.cu, and then invoke nvcc --cubin on the
> resulting File1.ptx, and File2.ptx. The latter step created File1.cubin,
> and File2.cubin.
>
> I've looked over the documentation of FindCUDA, and it seemed that
> CUDA_COMPILE and CUDA_COMPILE_PTX might be my best bets; however, I
> can't figure out how to set targets to be .ptx, or .cubin files.
> CUDA_ADD_LIBRARY wants to generate an (empty) object library, which only
> adds headaches, as the project compilation fails when gcc is invoked to
> create these libs. This is both redundant and annoying.
>
> I only need the .ptx and .cubin files. I would greatly appreciate a hint
> or pointers.
>
> Alex
Alex, thanks for your interest. There is an option called CUDA_BUILD_CUBIN,
which builds the cubin along with the OBJ file, but it appears to be
disabled for PTX compilation. I'm not exactly sure why, and I even wrote
it!
I'm in the process of adding a new target type to the CUDA_WRAP_SRCS macro
which currently only supports OBJ and PTX to support CUBINs. In the mean
time, you have two options:
If you want to modify FindCUDA.cmake, you can edit the following lines
(around line 1049 - depending on your version):
set(build_cubin OFF)
if ( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
# comment out this line
# if ( NOT compile_to_ptx )
set ( build_cubin ON )
# and this line
# endif( NOT compile_to_ptx )
endif( NOT CUDA_BUILD_EMULATION AND CUDA_BUILD_CUBIN )
If you can't edit your FindCUDA.cmake script, you can add a new compile step
that generates the CUBIN from your PTX.
# Compile the CUDA code to PTX. <my_target> is just a string used to set
either the shared library flag <my_target>_EXPORTS and the generated file
names' prefixes.
CUDA_WRAP_SRCS(my_target PTX generated_ptx_files myfile.cu myfile2.cu
myfile3.cu)
# FindCUDA doesn't look for ptxas, but you can do it yourself:
find_program(CUDA_PTXAS_EXECUTABLE NAMES ptxas PATHS
"${CUDA_TOOLKIT_ROOT_DIR}")
# Now set up the build rules to compile the PTX to CUBINs.
set(generated_cubin_files)
foreach(ptx_file ${generated_ptx_files})
# You can get creative and use things like get_filename_component() to
strip off the ptx from the filename.
set(generated_file "${ptx_file}.cubin")
add_custom_command(
OUTPUT ${generated_file}
# These output files depend on the source_file and the contents of
cmake_dependency_file
MAIN_DEPENDENCY "${ptx_file}"
# Here's the ptxas command
COMMAND ${ptxas} "${ptx_file}" -o "${generated_file}"
COMMENT "Generating ${generated_file}"
)
list(APPEND generated_cubin_files "${generated_file}")
endforeach()
Then make sure that you add your CUDA files, ptx files, and
generated_cubin_files to your library or executable target.
If you have the PTX, why do you need to generate CUBINs at all? You should
be able to let the driver compile the CUBIN and be ready to go no matter
what device you have created.
James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20100910/1f3214ea/attachment-0001.htm>
More information about the CMake
mailing list