2012-08-08 61 views
1

我想弄清楚如何在設備上分配一個3D數組,填充它,然後將其返回給主機代碼。cuda訪問3D數組

我的「CUDA C語言編程指南」第3.2.2節第21和22當我嘗試編譯這段代碼中使用的代碼試過,我得到這個錯誤:

error: a value of type "void *" cannot be used to initialize an entity of type "char *"

/* host code */ 
#include <stdio.h> 
#include <math.h> 
#include "mex.h" 

/* Kernel function */ 
#include "simulate3DArrays.cpp" 

/* Define some constants. */ 
#define width 5 
#define height 9 
#define depth 5 

void mexFunction(int  nlhs, 
      mxArray *plhs[], 
      int  nrhs, 
      mxArray *prhs[]) 
{ 

double *output; 
mwSize ndim3 = 3; 
mwSize dims3[] = {height, width, depth}; 

plhs[0] = mxCreateNumericArray(ndim3, dims3, mxDOUBLE_CLASS, mxREAL); 
output = mxGetPr(plhs[0]); 

cudaExtent extent = make_cudaExtent(width * sizeof(double), height, depth); 
cudaPitchedPtr devicePointer; 
cudaMalloc3D(&devicePointer, extent); 

cudaMemcpy3DParms deviceOuput = { 0 }; 
deviceOuput.srcPtr.ptr = devicePointer.ptr; 
deviceOuput.srcPtr.pitch = devicePointer.pitch; 
deviceOuput.srcPtr.xsize = width; 
deviceOuput.srcPtr.ysize = height; 

deviceOuput.dstPtr.ptr = output; 
deviceOuput.dstPtr.pitch = devicePointer.pitch; 
deviceOuput.dstPtr.xsize = width; 
deviceOuput.dstPtr.ysize = height; 

deviceOuput.kind = cudaMemcpyDeviceToHost; 

simulate3DArrays<<<1,depth>>>(devicePointer, width, height, depth); 

/* copy 3d array back to 'ouput' */ 
cudaMemcpy3D(&deviceOuput); 

return; 
} /* End Mexfunction */ 

/* device code from pg 22. */ 
__global__ void simulate3DArrays(cudaPitchedPtr devPitchedPtr, 
          int width, 
          int height, 
          int depth) 
{ 
char* devPtr = devPitchedPtr.ptr; /* << error occurs here */ 
size_t pitch = devPitchedPtr.pitch; 
size_t slicePitch = pitch * height; 

for (int z = 0; z < depth; ++z) 
{ 
    char* slice = devPtr + z * slicePitch; 
    for (int y = 0; y < height; ++y) 
    { 
     float* row = (float*)(slice + y * pitch); 
     for (int x = 0; x < width; ++x) 
     { 
      float element = row[x]; 
     } 
    } 
} 
} 

不知道是否是這個問題真的很重要,但我的環境是:

  • Windows 7的64位
  • Matlab的2012A
  • CUDA軟件開發包4.2
  • 的Tesla C2050 GPU
+0

只是做'字符* devPtr =(字符*)devPitchedPtr.ptr' .... – talonmies 2012-08-08 05:16:10

回答

2

正如@talonmies指出,需要將無效*指針轉換爲正確的類型。在這種情況下,字符*:

char* devPtr = (char *)devPitchedPtr.ptr;