我理解模板函数
通常在头文件中声明和定义。
我遇到的问题是我的模板函数
调用了其他函数。这些其他函数的原型(prototype)在模板函数本身之前的同一头文件中。
那部分代码:
//header.h
template <int ignoreAdetection>
__global__ void MCMLKernel(SimState d_state, GPUThreadStates tstates)
{
// photon structure stored in registers
PhotonStructGPU photon;
// random number seeds
UINT64 rnd_x;
UINT32 rnd_a;
// Flag to indicate if this thread is active
UINT32 is_active;
// Restore the thread state from global memory.
RestoreThreadState(&d_state, &tstates, &photon, &rnd_x, &rnd_a, &is_active);
...
...
}
函数 RestoreThreadState
是从此模板函数调用的几个函数中的第一个。其他的在 for 循环中调用。
我不确定这个模板函数是否应该在头文件中。如果应该在头文件中,我该如何调用其他函数?
我在实例化 MCMLKernel 期间从编译器得到的错误:
- 错误:缺少显式类型(假定为“int”)
- 错误:变量“RestoreThreadState”可能未初始化
- 错误:“SimState *”类型的值不能用于初始化“int”类型的实体
- 错误:应为“)”
- 警告:声明与之前的“RestoreThreadState”不兼容
其他详细信息。所有这些函数都是 CUDA 内核函数。 MCMLKernel
是一个__global__
内核,它调用的其余函数是__device__
内核。我正在使用 Nsight Eclipse Edition 和计算能力 1.3 GPU(四张 Tesla C1060 卡)。
最佳答案
__device__ void RestoreThreadState(...);
是一个前向声明,意味着正确的定义跟在同一个翻译单元中(它甚至可能在函数调用下面)。
__device__ void RestoreThreadState(...) {}
是空操作函数的定义。
__device__ int HitBoundary(PhotonStructGPU *photon);
是前向声明。您提供定义吗?
更新
Here's the Gist基于您提供的代码片段。我将内核、设备函数和类型声明保存在 3 个单独的 header 中。我可以编译它(虽然显然它不能运行)
关于c++ - 模板函数调用其他函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170056/