user570593
user570593

Reputation: 3520

CUDA function call from anther cu file

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..

Upvotes: 4

Views: 3390

Answers (2)

Nils D.
Nils D.

Reputation: 21

I think meanwhile there is a possibilty to solve it: CUDA external class linkage and unresolved extern function in ptxas file

You can enable "Generate Relocateable Device Code" in VS Project Properies->CUDA C/C++->Common or use compiler parameter -rdc=true.

Upvotes: 1

talonmies
talonmies

Reputation: 72348

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.

Upvotes: 4

Related Questions