获取CUDA错误“声明与先前的”variable_name“不兼容。 [英] Getting CUDA error "declaration is incompatible with previous "variable_name"
问题描述
我试图编译一个程序,包括一个内核与MSVS 2012和CUDA。我使用共享内存,但不同于关于同一问题的此问题 ,我只使用我的变量名为这个内核的共享内存一次,所以没有重定义的问题。用这样的代码:
I'm trying to compile a program including a kernel with MSVS 2012 and CUDA. I use shared memory, but unlike in this question regarding the same problem, I only use my variable name for this kernel's shared memory once, so there's no issue of redefinition. With code like this:
template<typename T>
__global__ void mykernel(
const T* __restrict__ data,
T* __restrict__ results) {
extern __shared__ T warp_partial_results[];
/* ... */
warp_partial_results[lane_id] = something;
/* ... */
results[something_else] = warp_partial_results[something_else];
/* ... */
}
类型(例如float,int,unsigned int),我得到可怕的
which is instantiated for several types (e.g. float, int, unsigned int), I get the dreaded
声明与以前的warp_partial_results不兼容
declaration is incompatible with previous "warp_partial_results"
消息。这会导致什么?
推荐答案
CUDA不立即支持模板化函数中的动态分配共享内存数组, (显然)生成那些extern的实际定义。如果你为多个类型实例化一个模板化的函数,这些定义会发生冲突。
CUDA doesn't immediately 'support' dynamically-allocated shared memory arrays in templated functions, as it (apparently) generates actual definitions of those extern's. If you instantiate a templated function for multiple types, the definitions would conflict.
一个解决方法是通过类的模板专用化形式。请参阅:
A workaround is available in the form of template specialization via classes. See:
您使用以下解决方法:
template<class T> __global__ void foo( T* g_idata, T* g_odata)
{
// shared memory
// the size is determined by the host application
SharedMem<T> shared;
T* sdata = shared.getPointer();
// .. the rest of the code remains unchanged!
}
getPointer()
具有 * 每个类型的专用实现,它返回一个不同的指针,例如 extern __shared__ float * shared_mem_float
或 extern __shared__ int * shared_mem_int
等。
the getPointer()
has* a specialized implementation for every type, which returns a different pointer, e.g. extern __shared__ float* shared_mem_float
or extern __shared__ int* shared_mem_int
etc.
(*) - 不是真的。在NVidia提供的头文件中,他们专门针对一些基本类型,就是这样。
这篇关于获取CUDA错误“声明与先前的”variable_name“不兼容。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!