Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouCompleteMe and CUDA source files

Tags:

vim

plugins

I am trying to make YCM plugin of vim to work for CUDA source files. Since CUDA is basically C++ syntax with some extensions, I thought that editing the standard '.ycm_extra_conf.py' file would be sufficient. I changed the line

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm']

to

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm', '.cu' ]

and the line

return extension in [ '.h', '.hxx', '.hpp', '.hh']

to

return extension in [ '.h', '.hxx', '.hpp', '.hh', '.cuh' ]

But YCM does not work, it does not even ask me to use the config file as it should in the beginning. In normal C/C++ source files YCM works correct.

Any ideas what is missing?

like image 351
labotsirc Avatar asked Nov 18 '14 18:11

labotsirc


Video Answer


1 Answers

I got this working by the following steps:

First remap .cu files to cpp in your .vimrc

" Map cuda files to c++ so that Ycm can parse
autocmd BufNewFile,BufRead *.cu set filetype=cpp

Next update .ycm_extra_conf.py with flags for Clang CUDA support.

import os
import ycm_core

includes = ['-I/opt/cudatoolkit/6.5/include', '-I/your/includes/here']

common = ['-std=c++11',
          '-DUSE_CLANG_COMPLETER',
          '-I/usr/local/include',
          '-I/usr/include/clang/3.5/include',
          '-I/usr/include/x86_64-linux-gnu',
          '-I/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/include',
          '-I/usr/include',
          '-I/usr/include/c++/4.9']

cpp_flags = ['-x', 'c++',]

# http://llvm.org/docs/CompileCudaWithLLVM.html
cuda_flags = ['-x', 'cuda', '--cuda-gpu-arch=sm_35']

def FlagsForFile( filename ):

  compile_flags = cpp_flags
  if filename.endswith('.cu'):
    compile_flags = cuda_flags
  compile_flags.extend(common)
  compile_flags.extend(includes)

  return {
    'flags': compile_flags,
    'do_cache': True
  }

Finally you need to add in a header file to your .cu file so Ycm can parse the CUDA builtins. This file, cuda_builtin_vars.h was in my local Clang build.

#ifdef __clang__
#include <cuda_builtin_vars.h>
#endif

Even with all this, the Clang parser still doesn't seem to accept that my __global__ functions are actually __global__ (even though it can handle the kernel call syntax with any problems), so I usually wrap them with #ifndef __clang__

Sources:

  • https://github.com/Valloric/YouCompleteMe/issues/1766
  • http://llvm.org/docs/CompileCudaWithLLVM.html
  • https://github.com/Microsoft/clang-1/blob/master/test/SemaCUDA/kernel-call.cu
like image 59
Gearoid Murphy Avatar answered Oct 11 '22 12:10

Gearoid Murphy