2

I am trying to build an application that makes use of a Cuda kernel. For that, I am using the meson build system but without success. Basically what I am trying to do is the following:

//main.cpp

extern void kernel_function();

int main(int argc, char *argv[]){

  // some logic here...

  kernel_function();
  return 0;
}
//kernel.cu

__global__ void kernel(){
  // some code here...
}

void kernel_function(){

  dim3 threads( 2, 1 );
  dim3 blocks( 1, 1 );

  kernel<<< blocks, threads >>>();
}

I can compile the above code with the following commands:

g++ -c main.cpp
nvcc -c kernel.cu
nvcc -o main main.o kernel.o

How can I replicate this compilation process that I do on the terminal with Meson?

lumurillo
  • 31
  • 3

1 Answers1

2

In the simplest case it could be something like:

project('cuda dependency', 'cpp', 'cuda')
executable('main', 'main.cpp', 'kernel.cu')

And run with

$ CXX=g++ meson build
$ ninja -C build

(based on example).

But down the road you might need to add dependencies like

dep = dependency('cuda', version : '>=10', modules : ['cublas'])
executable(..., dependencies: dep)

or set some special compiler flags with the help of cuda module, check this reference on meson.

pmod
  • 10,450
  • 1
  • 37
  • 50
  • Does not work for me, I get the following error message: "relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE". – Alexander Feb 20 '21 at 21:34
  • @Alexander what if you configure project with b_pie, i.e. $ meson --reconfigure -Db_pie=true – pmod Feb 20 '21 at 22:06
  • With "-Db_pie=true" option I have the following: "ERROR: Language Cuda does not support position-independent executable". – Alexander Feb 22 '21 at 13:16
  • Just one more thing: the command "nvcc -o myexe myfile.cpp -lmylib" works, where 'mylib' was built with meson. Also, meson can build cuda-executable from a single source file: executable('main', 'kernel.cu') - works! What does not work, if I have two+ files. It looks like 'PIE' option needs to be passed only to the main compiler (I use gcc), but not to nvcc. – Alexander Feb 22 '21 at 13:27
  • With this config: "meson configure -Dcpp_args=-fPIE" I was able to compile. Now meson complains on undefined reference to `cublasCreate_v2'. It looks cublas library is not linked with the executable. – Alexander Feb 22 '21 at 18:47
  • Sorry, here I am out of... it requires some cuda knowledge I have never worked with, maybe you already found this https://stackoverflow.com/questions/64113574/undefined-reference-to-cublascreate-v2-in-tmp-tmpxft-0000120b-0000000-10-my – pmod Feb 22 '21 at 20:41
  • maybe this as well would be helpful https://github.com/mesonbuild/meson/issues/8337 where they discuss passing flags to gcc through nvcc using -Xcompiler, and this guy could provide more help in meson+cuda https://github.com/obilaniu – pmod Feb 22 '21 at 21:09