Sean
Sean

Reputation: 149

Calling __global__ CUDA functions from regular C++ code

I am adding a library using CUDA to a C++ project. As of now what I'm doing is to import a .cuh (or .h) header from a .cpp file, and a .cu file implements the functions in this header. But this header contains the declaration of the methods, which have the __global__ modifier that the regular C++ compiler complains about.

So I am wondering, what is the correct way of using a static CUDA library from C++ code?

Upvotes: 0

Views: 628

Answers (1)

Sean
Sean

Reputation: 149

I solved by creating a header with wrapper functions that are then implemented in a .cu file like this:

__global__
void real_foo(int number, int *out) { 
    *out = number * 2; 
}

inline int foo(int number) { 
    int* x;
    
    cudaMallocManaged(&x, sizeof(int));
    real_foo<<<1,1>>>(number, x); 
    cudaDeviceSynchronize();
    int y = *x;
    cudaFree(x);
    return y;
} 

This way the externally exposed function has nothing CUDA-specific in its signature.

Upvotes: 1

Related Questions