Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code parsing not working with CUDA, Clion and CMake

Tags:

I have a project divided in modules, here is a dummy example:

  • root
    • CMakeLists.txt
    • modules
      • utils
        • CMakeLists.txt
        • src
          • util_file.cpp
      • cuda
        • CMakeLists.txt
        • src
          • cuda_file.cu

If I edit the cuda_file.cu with CLion, all the symbols are unresolved (even the includes from standard library) by CLion. All the code completion/creation features are then of course gone (among other things). The problem seems to be that whenever you create a library or an executable with only CUDA files, Clion becomes stupid and doesn't parse or resolve anything anymore.

There is two workarounds I've found but they are not friendly or "clean" to use :

  • add an empty .cpp file to the directory and add it to the add_library() CMake line.
  • switch to another library or executable target that has .cpp files (like utils in my dummy example). But then when you want to compile or execute you have to switch again to cuda target (or some subtarget like test_cuda for test units) and then switch back again to continue coding or debugging, etc...

Here is the CMakeLists.txt from the cuda module with the workaround:

cmake_minimum_required(VERSION 3.5)
message(STATUS "Configuring module cuda")

# Build module static library
FILE(GLOB CUDA_SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
FILE(GLOB CUDA_CU_SRCS
     ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cu)
FILE(GLOB CUDA_CU_HDRS
    ${CMAKE_CURRENT_SOURCE_DIR}/include/*.cuh)

cuda_compile(cuda_objs ${CUDA_CU_SRCS} ${CUDA_CU_HDRS})
add_library(cuda STATIC ${CUDA_SRCS} ${cuda_objs})
# because only .cu files, help cmake detect C++ language
set_target_properties(cuda PROPERTIES LINKER_LANGUAGE CXX)

Is there a way to avoid CLion derping when resolving links to other headers and libraries ?

I've already added .cu and .cuh files as C/C++ code in CLion options and tried using JETBRAINS_IDE define option as explained in another similar post, but those two problems are not the same.

like image 948
Kriegalex Avatar asked Aug 02 '17 13:08

Kriegalex


1 Answers

It seems like without the intervention of Jetbrains to add official CUDA support, the most I could get out of the combo CLion + CMake + CUDA was achieved by:

  • adding .cu and .cuh as C++ files in CLion. This allows Clion to recognize cuda code as C++ code and color it correctly.
  • adding an empty dummy .cpp file to the cuda source directory if it is only filled by .cu files (one of my "dirty" hacks from my question). I could not find better. This allows Clion to not completely derp. A simple thing like recognizing cstdio doesn't work without this "hack" and CLion is basically an enhanced notepad.
  • using when possible CMake 3.8+ that officially supports CUDA as a language and use the new "cuda aware" add_library() instead of the old macro defined cuda_add_library(). This can avoid problems in the future in case of deprecation.
  • in the CMakeLists of the cuda module (or main CMakeLists if only one), include the path to the include directory of cuda to allow Clion to "see" the cuda headers. CLion can then propose to you to include them so that CLion resolves correctly CUDA API calls like cudaMalloc() or cudaFree(). This is only needed for CLion as the CUDA compiler doesn't need this includes to compile properly (cuda.h, cuda_runtime.h, ...).
  • use this answer to create a "clion helper" header file, so that it doesn't derp on symbols like __device__ or __global__.

I think that if Jetbrains starts adding support for CUDA, not also will it remove the need to add this dummy file, but it will probably also resolve all other things listed.

Here is the link to the nvidia blog with examples about the official cuda language support in CMake and new "cuda aware" add_library() : https://devblogs.nvidia.com/parallelforall/building-cuda-applications-cmake/

like image 74
Kriegalex Avatar answered Oct 11 '22 12:10

Kriegalex