CUDA调用设备功能从单独的文件(名称混淆?)

CUDA calling device functions from separate files (Name mangling?)

本文关键字:文件 调用 功能 单独 CUDA      更新时间:2023-10-16

我应该如何正确地做到这一点?下面是代码的简化:

//main.cu    
#include "math.cuh"
__global__ void test(float *x, unsigned numElements)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < numElements)
    {
        float array[5] = {1, 2, 3, 4, 5};
        copyArray(x + 5*i, array, 5);
    }
}
int main(int argc, char **argv)
{
    test<<<blocksPerGrid, threadsPerBlock>>>(d_A, numElements);
}
//math.cuh
__device__ void copyArray(float *dest, float *src, unsigned length);
//math.cu
#include "math.cuh"
__device__ void copyArray(float *dest, float *src, size_t length)
{
    for (int i = 0; i < length; i++) {
        dest[i] = src[i];
    }
}

用这个命令编译:

nvcc -rdc=true -arch=sm_20 -o cudaMain main.cu math.cu -Xlinker -framework,OpenGL,-framework,GLUT && ./cudaMain

并得到这个错误:

nvlink error   : Undefined reference to '_Z9copyArrayPfS_j' in '/tmp/tmpxft_00000265_00000000-21_main.o'

这显然看起来像一个名称混淆错误,但我试着把外部"C"在各种地方,它没有工作。

函数原型使用unsigned,而定义使用size_t。这是原因吗?