在我之前的帖子之后:CUDA NPP - GPU 错误检查时出现未知错误
我曾尝试使用 CUDA NPP 库对图像中的所有像素求和,在一些开发人员的帮助下,我终于得到了可以编译的代码。但是,当我尝试partialSum
通过将其复制到double
变量中来打印存储的值时(与 CUDA v4.2 的 NPP 指南一致),我收到此错误:
Unhandled exception at 0x00fdf7f4 in MedianFilter.exe: 0xC0000005: Access violation reading location 0x40000000.
我一直试图摆脱它,但到目前为止我一直没有成功。请帮忙!我在这段小代码上已经有大约两天的时间了。
代码:
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) getchar();
}
}
// processing image starts here
// device_pointer initializations
unsigned char *device_input;
unsigned char *device_output;
size_t d_ipimgSize = input.step * input.rows;
size_t d_opimgSize = output.step * output.rows;
gpuErrchk( cudaMalloc( (void**) &device_input, d_ipimgSize) );
gpuErrchk( cudaMalloc( (void**) &device_output, d_opimgSize) );
gpuErrchk( cudaMemcpy(device_input, input.data, d_ipimgSize, cudaMemcpyHostToDevice) );
// Median filter the input image here
// .......
// allocate data on the host for comparing the sum of all pixels in image with CUDA implementation
// 1st argument - allocate data for pSrc - copy device_output into this pointer
Npp8u *odata;
gpuErrchk( cudaMalloc( (void**) &odata, sizeof(Npp8u)*output.rows*output.cols ) );
gpuErrchk( cudaMemcpy(odata, device_output, sizeof(Npp8u)*output.rows*output.cols, cudaMemcpyDeviceToDevice) );
// 2nd arg - set step
int ostep = output.step;
// 3rd arg - set nppiSize
NppiSize imSize;
imSize.width = output.cols;
imSize.height = output.rows;
// 4th arg - set npp8u scratch buffer size
Npp8u *scratch;
int bytes = 0;
nppiReductionGetBufferHostSize_8u_C1R( imSize, &bytes);
gpuErrchk( cudaMalloc((void **)&scratch, bytes) );
// 5th arg - set npp64f partialSum (64 bit double will be the result)
Npp64f *partialSum;
gpuErrchk( cudaMalloc( (void**) &partialSum, sizeof(Npp64f) ) );
// nnp8u, int, nppisize, npp8u, npp64f
nppiSum_8u_C1R( odata, ostep, imSize, scratch, partialSum );
double *dev_result;
dev_result = (double*)malloc(sizeof(double)); // EDIT
gpuErrchk( cudaMemcpy(&dev_result, partialSum, sizeof(double), cudaMemcpyDeviceToHost) );
//int tot = output.rows * output.cols;
printf( "\n Total Sum cuda %f \n", *dev_result) ; // <---- access violation here