Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CUDA function call from anther cu file

Tags:

cuda

I have two cuda files say A and B. I need to call a function from A to B like..

__device__ int add(int a, int b) //this is a function in A
{
   return a+b;
}



__device__ void fun1(int a, int b) //this is a function in B
{
    int c = A.add(a,b);
}

How can I do this??

Can I use static keyword? Please give me an example..

like image 556
user570593 Avatar asked Oct 19 '25 01:10

user570593


1 Answers

The short answer is that you can't. CUDA only supports internal linkage, thus everything needed to compile a kernel must be defined within the same translation unit.

What you might be able to do is put the functions into a header file like this:

// Both functions in func.cuh
#pragma once
__device__ inline int add(int a, int b) 
{
   return a+b;
}

__device__ inline void fun1(int a, int b)
{
    int c = add(a,b);
}

and include that header file into each .cu file you need to use the functions. The CUDA built chain seems to honour the inline keyword and that sort of declaration won't generate duplicate symbols on any of the CUDA platforms I use (which doesn't include Windows). I am not sure whether it is intended to work or not, so cavaet emptor.

like image 180
talonmies Avatar answered Oct 22 '25 05:10

talonmies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!