1

I'm trying to figure out how to allocate a 3d array on the device, populate it and then return it back to the host code.

I tried using the code in the "Cuda C Programming guide" Section 3.2.2 pg, 21 and 22. When I try to compile this code,I get this error:

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]; 
        }
    } 
}
}

Not sure if it's really relevant for this problem, but my environment is:

  • Windows 7 x64
  • Matlab 2012a
  • Cuda SDK 4.2
  • Tesla C2050 GPU
1
  • Just do char* devPtr = (char *)devPitchedPtr.ptr.... Commented Aug 8, 2012 at 5:16

1 Answer 1

3

As indicated by @talonmies, you need to cast the void* pointer to the right type. In this case, char*:

char* devPtr = (char *)devPitchedPtr.ptr;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.