【问题标题】:cuda accessing 3d arraycuda 访问 3d 数组
【发布时间】:2012-08-08 04:51:31
【问题描述】:

我试图弄清楚如何在设备上分配一个 3d 数组,填充它,然后将它返回给主机代码。

我尝试使用“Cuda C 编程指南”第 3.2.2 节第 3.2.2 节第 21 和 22 节中的代码。当我尝试编译此代码时,出现此错误:

错误:“void *”类型的值不能用于初始化实体 类型为“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 x64
  • Matlab 2012a
  • Cuda SDK 4.2
  • 特斯拉 C2050 GPU

【问题讨论】:

  • 只要char* devPtr = (char *)devPitchedPtr.ptr....

标签: 3d cuda


【解决方案1】:

正如@talonmies 所指出的,您需要将 void* 指针转换为正确的类型。在这种情况下,char*:

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

【讨论】:

    猜你喜欢
    • 2020-05-12
    • 2011-09-11
    • 2015-04-16
    • 2021-12-30
    • 2022-06-10
    • 2015-08-13
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多