开发者

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜