使用C++模板时,我看到NVIDIA NVCC(CUDA 4.0和4.1测试过)的奇怪行为。 我将它简化为一个演示行为的简单示例。使用模板特征类型时,NVIDIA NVCC更改编译时间常数
这已经处于错误报告状态。不过,我把它挂在这里,因为这个网站是一个越来越可靠的错误和修复的来源。所以,我保持这个页面更新。
代码:
#include"stdio.h"
#define PETE_DEVICE __device__
template<class T, int N> class ILattice;
template<class T> class IScalar;
template<class T, int IL> struct AddILattice {};
template<class T>
PETE_DEVICE
void printType() {
printf("%s\n",__PRETTY_FUNCTION__);
}
template<class T> class IScalar {
T F;
};
template<class T, int N> class ILattice {
T F[N];
};
template<class T, int N>
struct AddILattice<IScalar<T> , N> {
typedef ILattice< T , N > Type_t;
};
#define IL 16
__global__ void kernel()
{
printf("IL=%d\n",IL); // Here IL==16
typedef typename AddILattice<IScalar<float> ,IL>::Type_t Tnew;
// This still works fine. Output:
// void printType() [with T = ILattice<float, 16>]
//
printType<Tnew>();
// Now problems begin: Output:
// T=4 Tnew=0 IL=64
// Here IL should still be 16
// sizeof(Tnew) should be 16*sizeof(float)
//
printf("T=%d Tnew=%d IL=%d\n",sizeof(IScalar<float>),sizeof(Tnew),IL);
}
int main()
{
dim3 blocksPerGrid(1 , 1 , 1);
dim3 threadsPerBlock(1 , 1, 1);
kernel<<< blocksPerGrid , threadsPerBlock , 48*1024 >>>();
cudaDeviceSynchronize();
cudaError_t kernel_call = cudaGetLastError();
printf("call: %s\n",cudaGetErrorString(kernel_call));
}
为什么编译器改变从16到64 IL
任何想法?
是不是标准:: COUT提供您的系统上? – PlasmaHH