-3

I am currently working on a project, where I want to execute some code in Cuda, which should be called from the main c++ file. When I am compiling with Clang only the .cpp files are compiled and the compiler tells me "expected exprission" on the <<<>>> Kernel call notation. Any Idea how I can fix this?

I have a .cuh files with the definition which I am including and a .cu source file. I am using CMake to configure the project and building it with Ninja.

I am using ccached clang++ and supplying "--cuda-path=/usr/local/cuda-10.1 --cuda-gpu-arch=sm_61 -L/usr/local/cuda-10.1/lib64 -lcudart_static -ldl -lrt -pthread -std=c++17" to clang args.

When I add the -x cuda flag, the error does not appear, but instead it tells me that a library that I am linking against is not allowed to overwrite some host function, but I think this is because it wants to compile everything as cuda, which is not intended.

I am passing all files inside my source folder to add_executable in CMake via a GLOB ${APP_PATH}/src/*, which should add all files.

main.cpp

#include "ParticleEngine.cuh"

...

int main(){
 simulation_timestep(&this->particles[0], this->gravity, 1, delta_frame,
                      this->particles.size());
}

ParticleEngine.cuh

#pragma once
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

void simulation_timestep(Particle *particles, ci::vec3 gravity, double mass,
                         double time_delta, unsigned long long n_particles);

ParticleEngine.cu

#include "ParticleEngine.cuh"

__global__ void particle_kernel(Particle *particles, ci::vec3 *gravity,
                                double *mass, double *time_delta) {
...
}


void simulation_timestep(Particle *particles, ci::vec3 gravity, double mass,
                         double time_delta, unsigned long long n_particles) {

  ... //memcpy stuff

  particle_kernel<<<dimgrid, dimblock>>>(cuda_particles, cuda_gravity,
                                         cuda_mass, cuda_time_delta);

  ...
}

edit: Full error message:

[build] In file included from ../src/main.cpp:1:
[build] ../src/ParticleEngine.cu:43:20: error: expected expression
[build]   particle_kernel<<<dimgrid, dimblock>>>(cuda_particles, cuda_gravity,
[build]                    ^

edit: Error message when executing clang with -x cuda:

[build] /home/mebenstein/Cinder/include/glm/gtx/io.inl:97:32: error: __host__ __device__ function 'get_facet' cannot overload __host__ function 'get_facet'
[build]         GLM_FUNC_QUALIFIER FTy const& get_facet(std::basic_ios<CTy, CTr>& ios)
[build]                                       ^
[build] /home/mebenstein/Cinder/include/glm/gtx/io.hpp:145:14: note: previous declaration is here
[build]                 FTy const& get_facet(std::basic_ios<CTy,CTr>&);
[build]                            ^

I am including the c++ library cinder in main.cpp and this error appears.

  • 1
    Rename .cpp files with CUDA code to .cu? – tera May 22 '19 at 20:09
  • The file with the CUDA code is ParticleEngine.cu. The problem is that it is not compiled as CUDA, an makes the compilation fail because when main.cpp is compiled it is also treated as a c++ file. – Michael Ebenstein May 22 '19 at 22:25
  • 2
    If that is the case, we'll need more information, including the actual error messages. In fact, on StackOverflow you are supposed to provide a [mcve] for questions of the kind "Why isn't this code working?". – tera May 22 '19 at 23:10
  • The code above is all you need. I added the full error message to the question. – Michael Ebenstein May 23 '19 at 12:41
  • Don't `#include` the .cu file into a C++ file. You need to compile the .cu file as a separate compilation unit, or the compiler will try to interpret it as (pure) C++. – tera May 23 '19 at 15:18

1 Answers1

3

#include in C++ works by literally replacing that statement with the contents of the included file. As a consequence, the included file is also parsed as C++ code.

To compile a file as CUDA code, the file needs to be a separate compilation unit, i.e. given as an argument to the clang invocation. It also either needs to have a name ending in .cu, or the -x cuda flag needs to be given to clang.

Update after error messages have been included in the question:

It appears that Cinder does not support compilation of the CUDA part with clang++ because of a difference in how __host__/__device__ attributes are treated.

At this point your options are the following:

  1. You can modify Cinder to also support clang++, it's open source.

  2. You can ask the Cinder authors or third parties whether they are willing to make the necessary changes. A cash incentive may or may not increase willingness.

  3. You can use nvcc to compile the code.

tera
  • 7,080
  • 1
  • 21
  • 32
  • Thanks for the answer. As I already mentioned in the question I passed `-x cuda` to clang and got another error, since it tried to compile included third party libraries with it. – Michael Ebenstein May 23 '19 at 19:50
  • 2
    Error message, or it didn't happen. – tera May 23 '19 at 20:03
  • I added the error message to the question. Sorry that I did not do this earlier. – Michael Ebenstein May 23 '19 at 21:45
  • 1
    OK, that finally tells us something. Cinder is not up to compilation with clang++ because of a difference in how `__host__`/`__device` attributes are treated. Ask nicely if Cinder authors are willing to support clang++ for CUDA code, or use nvcc. – tera May 23 '19 at 22:00
  • 2
    Next time, please check the [ask] page and provide error messages straight away. Answering your question would have been a matter of minutes instead of hours. You were quite lucky I showed this much persistence this time. – tera May 23 '19 at 23:16