Confused about 2D and 3D Memory trying to understand width, height, depth, in context of 2d and 3d m

Okay, trying to understand which dimension is width, height and depth.

Looking at the code below, please ignore the fact that it’s not syntactically correct.

So on the host side, I’ve created an array. It has the dimensions xDim, yDim, and zDim.

I can refer to memory using the line: hostPtr[x * slicePitch + y * pitch + z]

Conceptually, I have structured the memory so that the memory looks like this:

[ ( {} ) … () ] …

Where x refers to which bracket i’m in, y refers to which parenthesis, and z refers to which curly brace.

Let’s say x goes right, y goes up, and z goes in. xDim is width, yDim is height, zDim is depth.

Type * hostPtr;

size_t xDim;

size_t yDim;

size_t zDim;

size_t memorySize = sizeof(Type) * xDim * yDim * zDim;

cudaMallocHost((void**) &hostPtr, memorySize);


size_t slicePitch = yDim * zDim;

size_t pitch = yDim;

hostPtr[x * slicePitch + y * pitch + z];

Now, here comes the confusion.

I want to allocate memory on the device so I can copy my data over and refer to it with the same x, y, and z coordinates.

cudaExtent is defined here with xDim width, yDim height, and zDim depth.

But in order to access the elements, we use: (Type*)(devicePtr + z * slicePitch + y * pitch)

Here, we see that z indexes the slices, y the pitch, and x the element.

Which is the opposite of what I had on the host side.

[ ( {} ) … () ] …

Where z refers to which bracket i’m in, y refers to which parenthesis, and x refers to which curly brace.

cudaPitchedPtr devicePitchedPtr;

size_t xDim; 

size_t yDim; 

size_t zDim; 

cudaExtent deviceExtent = make_cudaExtent(xDim, yDim, zDim);

cudaMalloc3D(&devicePitchedPtr, deviceExtent);


char* devicePointer = devicePitchedPtr.ptr;

size_t pitch = devicePitchedPtr.pitch;

size_t slicePitch = pitch * extent.height;

(Type)(devicePtr + z * slicePitch + y * pitch + x * sizeof(Type));

(Type*)(devicePtr + z * slicePitch + y * pitch)[x]

I just seem to be having trouble with mapping width height depth with x y and z, and the layout of the memory in relation to that.

Can someone clairfy this for me?