按照书上的字段写的遍历cuda三维数组的程序,源码如下:
#include <stdio.h>
#include <stdlib.h>
#include <cutil.h>
#include <cutil_inline.h>
//
/ Init CUDA /
//
#if DEVICE_EMULATION
bool InitCUDA(void){return true;}
#else
bool InitCUDA(void)
{
int count = 0;
int i = 0;
cudaGetDeviceCount(&count);
if(count == 0) {
fprintf(stderr, “There is no device.\n”);
return false;
}
for(i = 0; i < count; i++) {
cudaDeviceProp prop;
if(cudaGetDeviceProperties(&prop, i) == cudaSuccess) {
if(prop.major >= 1) {
break;
}
}
}
if(i == count) {
fprintf(stderr, “There is no device supporting CUDA.\n”);
return false;
}
cudaSetDevice(i);
printf(“CUDA initialized.\n”);
return true;
}
#endif
//
/ Example /
//
global void myKernel(cudaPitchedPtr devPitchedPtr, cudaExtent extent)
{
char* devPtr=(char*)devPitchedPtr.ptr;
size_t pitch=devPitchedPtr.pitch;
size_t slicePitch=pitchextent.height;
for(unsigned int z=0; z<extent.depth; ++z)
{
char slice=devPtr+zslicePitch;
for(unsigned int y=0; y<extent.height; ++y)
{
float row=(float*)(slice+ypitch);
for(unsigned int x=0; x<extent.width; ++x)
{
float element=row[x];
}
}
}
}
//
/ /
//
int main(int argc, char argv)
{
if(!InitCUDA()) {
return 0;
}
cudaPitchedPtr devPitchedPtr;
cudaExtent extent=make_cudaExtent(16*sizeof(float),16,16);
cudaMalloc3D(&devPitchedPtr,extent);
unsigned int timer = 0;
cutilCheckError( cutCreateTimer( &timer));
cutilCheckError( cutStartTimer( timer));
myKernel<<<1,32>>>(devPitchedPtr,extent);
cudaThreadSynchronize();
cutilCheckError( cutStopTimer( timer));
printf(“Processing time: %f (ms)\n”, cutGetTimerValue( timer));
cutilCheckError( cutDeleteTimer( timer));
cutilSafeCall( cudaFree(&devPitchedPtr));
return 0;
}
调试时运行框显示一下就关闭了,然后有如下的错误:
practice.exe 中的 0x7c812afb 处最可能的异常: Microsoft C++ 异常: 内存位置 0x0012fdf4 处的 cudaError_enum。
practice.exe 中的 0x7c812afb 处最可能的异常: Microsoft C++ 异常: 内存位置 0x0012fe14 处的 cudaError。
g:/pratice/pratice/sample.cu(99) : cudaSafeCall() Runtime API error : invalid device pointer.
线程 ‘Win32 线程’ (0xcf8) 已退出,返回值为 -1 (0xffffffff)。
程序“[3928] practice.exe: 本机”已退出,返回值为 -1 (0xffffffff)。
小妹是菜鸟,希望大侠指教!!!坐等回帖~~~