问题描述
我理解模板函数
通常要在头文件中声明和定义。
I understand the template functions
usually are to be declared and defined in header files.
我的是模板函数
调用其他函数。
The problem I am having is that my template function
makes calls to other functions. The prototypes of those other functions are in the same header file before the template function itself.
这部分代码
//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循环内调用。
The function RestoreThreadState
is the first of several functions called from this template function. The others are called within a for loop.
我不知道这个模板函数应该或不应该在头文件。
I am not sure if this template function should or should not be in the header file. If should be in a header file, how do I call those other functions?
在MCMLKernel的实例化过程中,我从编译器得到的错误:
The errors I am getting from the compiler during the instantiation of MCMLKernel:
- 错误:缺少显式类型(假设为int)
- 错误:变量RestoreThreadState
- 错误:类型SimState *的值不能用于初始化类型为int的实体
- 错误:expected a
- 警告:声明与以前的RestoreThreadState不兼容
- error: explicit type is missing ("int" assumed)
- error: variable "RestoreThreadState" may not be initialized
- error: a value of type "SimState *" cannot be used to initialize an entity of type "int"
- error: expected a ")"
- warning: declaration is incompatible with previous "RestoreThreadState"
所有这些功能都是CUDA内核函数。 MCMLKernel
是一个 __ global __
内核,其调用的其余函数是 __ device __
内核。我使用Nsight Eclipse版和计算能力1.3 GPU(四个特斯拉C1060卡)。
Additional details. All this functions are CUDA kernel functions. MCMLKernel
is a __global__
kernel and the rest of the functions it calls are __device__
kernels. I am using Nsight Eclipse Edition and compute capability 1.3 GPUs (four Tesla C1060 cards).
推荐答案
@Eugene提供了答案问题。
@Eugene provided the answer to this question.
我创建了调用函数的原型,如下所示:
I created the prototype of the called-to function like this
__device__ void RestoreThreadState(SimState *d_state, GPUThreadStates *tstates,
PhotonStructGPU *photon,
UINT64 *rnd_x, UINT32 *rnd_a,
UINT32 *is_active);
但是,用Eugene给出的例子应该是这样的(不是PROTOTYPE! )
But, with an example given by Eugene it should be like this (NOT A PROTOTYPE! See other answer)
__device__ void RestoreThreadState(SimState *d_state, GPUThreadStates *tstates,
PhotonStructGPU *photon,
UINT64 *rnd_x, UINT32 *rnd_a,
UINT32 *is_active){}
在代码中我调用函数实际返回像这样
Next in the code I call to functions which actually return values like this
__device__ int HitBoundary(PhotonStructGPU *photon);
从相同的 MCMLKernel
模板函数调用。它给我一个警告:
Called from the same MCMLKernel
template function. It is giving me a warning:
- 警告:在非空函数HitBoundary结束时缺少返回语句
UPDATE:在另一个源文件 kernel.cu 中,我有以下声明和定义(为什么我有多个定义问题)
UPDATE: In another source file kernel.cu I have the following declaration and definition (reason why I have multiple definition problems):
__device__ void RestoreThreadState(SimState *d_state, GPUThreadStates *tstates,
PhotonStructGPU *photon,
UINT64 *rnd_x, UINT32 *rnd_a,
UINT32 *is_active)
{
UINT32 tid = blockIdx.x * NUM_THREADS_PER_BLOCK + threadIdx.x;
*rnd_x = d_state->x[tid];
*rnd_a = d_state->a[tid];
photon->x = tstates->photon_x[tid];
photon->y = tstates->photon_y[tid];
photon->z = tstates->photon_z[tid];
photon->ux = tstates->photon_ux[tid];
photon->uy = tstates->photon_uy[tid];
photon->uz = tstates->photon_uz[tid];
photon->w = tstates->photon_w[tid];
photon->sleft = tstates->photon_sleft[tid];
photon->layer = tstates->photon_layer[tid];
*is_active = tstates->is_active[tid];
}
IN总结:我有四个源文件
IN SUMMARY: I have four source files
- main.cu
- kernel.cu b $ b
- rng.cu
- mem.cu
- main.cu
- kernel.cu
- rng.cu
- mem.cu
除 main.cu 之外的每个源文件都有一个相关的标题
Each source file, except main.cu has an associated header
- kernel.cuh
- rng.cuh / strong>
- kernel.cuh
- rng.cuh
- mem.cuh
其中,我要转发声明要在 main.cu 中使用的函数。
where I want to forward-declare functions to use in main.cu.
一切正常,直到我进入模板函数,该函数调用 kernel.cu rng.cu 。
Everything is fine until I get to the template function which calls functions from kernel.cu and rng.cu.
这篇关于模板函数调用其他函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!