输出错误时告诉我的电脑用C(INT)-1的CUDA内核的printf给人以%d符
printf("%d",(int)-1);
我也想到,通常可以得到,同样,一个 '-1' 的回应。然而,在我的Tesla M2090 NVIDIA显卡从我的基于Ubuntu的Cuda的5.0这个无辜的演示程序
/** cheese.cu */
#include <iostream>
#include <cuda.h>
#include <cstdio>
using namespace std;
template<typename T> struct SpacePtr
{ size_t size; T* ptr; };
__global__ void f(SpacePtr<int>* sp)
{
printf("This _is_ a 'minus one' or ain't it: %d\n",(int)-1);
// Note: All appears to work fine with %lu instead of %zd
// Still: How does the size value affect the -1?
printf("On DEV: Size: %zd, Minus One: %d\n",sp->size,(int)-1);
}
int main()
{
SpacePtr<int> data; data.ptr = 0; data.size = 168;
SpacePtr<int>* devPtr = 0;
cudaMalloc(&devPtr,1);
cudaMemcpy(devPtr,&data,sizeof(SpacePtr<int>),cudaMemcpyHostToDevice);
f<<<1,1,0,0>>>(devPtr);
cudaError_t err = cudaGetLastError();
cout << "The last error code was " << err << " (" <<
cudaGetErrorString(err) << ")" << endl;
cudaDeviceSynchronize();
}
编译并通过
nvcc -arch=sm_20 cheese.cu && ./a.out
称为谈过产生的输出:
The last error code was 0 (no error)
This _is_ a 'minus one' or ain't it: -1
On DEV: Size: 168, Minus One: 10005640
最后一个数字实际上是某种随机数(两个子查询返回不同的结果),就好像内存分配有什么不对。 -1之前的(int)已经是试错了。原来的程序没有它。
这里是问题:有没有人看,为什么-1不写,如果是这样,请你告诉我,为什么?非常感谢,Markus。
唯一可能的不确定的行为可能是内存分配。如果不。不要忘记重新编译代码然后执行。我猜:)对于'printf(“%d”,(int)-1)defiantly没有问题;' – 2013-03-04 09:51:08
这是我自己的问题的更新:如果我使用“%lu”作为size_t和“ %d“为臭名昭着的”-1“。所以可能与设备出于某种原因需要C90风格%lu有关。仍然:在内核代码%zd和%d之外工作正常。我保持开放的问题,我仍然没有看到一个大小值错误的格式化程序如何破坏-1的格式。 – 2013-03-04 10:02:11