Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:49:11+00:00 2026-06-09T09:49:11+00:00

I am having trouble trying to figure out how to retrieve a 3D array

  • 0

I am having trouble trying to figure out how to retrieve a 3D array from the GPU.
I want to allocate the memory for the 3d array in the host code, call the kernel, where the array will be populated, Then retrieve the 3D array in the host code to a return variable in the mexFunction (host code).

I have made several attempts at it, here is my latest code. The results are all ‘0’s, where they should be ‘7’. Can anyone tell me where i’m going wrong? It might have something to do with the 3D parameters, i dont think i fully understand that part.

simulate3DArrays.cpp

/* Device code */
__global__ void simulate3DArrays(cudaPitchedPtr devPitchedPtr, 
                             int width, 
                             int height, 
                             int depth) 
{
int threadId;
threadId = (blockIdx.x * blockDim.x) + threadIdx.x;

size_t pitch = devPitchedPtr.pitch; 

for (int widthIndex = 0; widthIndex < width; widthIndex++) {
    for (int heightIndex = 0; heightIndex < height; heightIndex++) {

        *((double*)(((char*)devPitchedPtr.ptr + threadId * pitch * height) + heightIndex * pitch) + widthIndex) = 7.0;

    }
}    
}

mexFunction.cu

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

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

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

void displayMemoryAvailability(mxArray **MatlabMemory);

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);


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

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;
/* copy 3d array back to 'ouput' */
cudaMemcpy3D(&deviceOuput);


return;
} /* End Mexfunction */
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:49:12+00:00Added an answer on June 9, 2026 at 9:49 am

    The basic problem appears to be that you are instructing the cudaMemcpy3D to copy zero bytes, because you have not included a non-zero extent which defines the size of the transfer to the API.

    Your transfer could probably be as simple as:

    cudaMemcpy3DParms deviceOuput = { 0 };
    deviceOuput.srcPtr = devicePointer;
    deviceOuput.dstPtr.ptr = output;
    deviceOuput.extent = extent;
    
    cudaMemcpy3D(&deviceOuput);
    

    I can’t comment on whether the MEX interface you are using is correct, but the kernel looks superficially correct and I don’t see anything else obviously wrong, without going to a compiler and trying to run your code with Matlab, which I cannot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having trouble trying to figure out some code someone has suggested and was
I am really having trouble trying to figure out how to find certain records
I am having some trouble trying to figure out, how to accomplish the following.
I am having some trouble trying to figure out how to parse information collected
I'm having a bit of a trouble trying to figure this out today, i
I'm having trouble trying to figure out why my formatDate is not working correctly.
I'm having trouble trying to figure out how to get prolog to spit out
I'm having bit of a trouble trying to figure out how I would send
I am having a bit of trouble trying to figure out how to get
I'm having trouble trying to figure out how to achieve this programming challenge in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.