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 8514305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:49:40+00:00 2026-06-11T04:49:40+00:00

I would appreciate some help involving CUDA device memory pointers. Basically I want to

  • 0

I would appreciate some help involving CUDA device memory pointers. Basically I want to split my CUDA kernel code into multiple files for readability and because it is a large program. So what I want to do is be able to pass the same device memory pointers to multiple CUDA kernels, not simultaneously. Below is a rough example of what I need

//random.h
class random{
public:
    int* dev_pointer_numbers;
};

so the object simply needs to store the pointer to device memory

//random_kernel.cu
__global__ void doSomething(int *values){
//do some processing}

extern "C" init_memory(int *devPtr,int *host_memory,int arraysize)
{
    cudaMalloc(&devPtr,arraysize*sizeof(int));
    cudaMemcpy(devPtr,host_memory,arraysize*sizeof(int),cudaMemcpyHostToDevice);
}

extern "C" runKernel(int *devPtr){
    doSomething<<<1,1>>>(devPtr);
}

and the main file:

//main.cpp
//ignoring all the details etc
random rnd;
void CUDA(int *hostArray)
{
    init_memory(rnd.dev_pointer_numbers,hostArray,10);
    runKernel(rnd.dev_pointer_numbers);
}

I understand that when I run the kernel code with the object pointer it isnt mapped in device memory so thats why the kernel code fails. What I want to know is how can I store to the pointer to a particular block in device memory in my main file so that it can be reused amongst other cuda kernel files?

  • 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-11T04:49:42+00:00Added an answer on June 11, 2026 at 4:49 am

    You’re losing your pointer!

    Check out your init_memory function:

    init_memory(int *devPtr,int *host_memory,int arraysize)
    {
      cudaMalloc(&devPtr,arraysize*sizeof(int));
      cudaMemcpy(devPtr,host_memory,arraysize*sizeof(int),cudaMemcpyHostToDevice);
    }
    

    So you pass in a pointer, at which point you have a local copy named devPtr. Then you call cudaMalloc() with the address of the local copy of the pointer. When the function returns the local copy (on the stack) is destroyed, so you have lost the pointer.

    Instead try this:

    init_memory(int **devPtr,int *host_memory,int arraysize)
    {
      cudaMalloc(devPtr,arraysize*sizeof(int));
      cudaMemcpy(*devPtr,host_memory,arraysize*sizeof(int),cudaMemcpyHostToDevice);
    }
    
    ...
    
    init_memory(&rnd.dev_pointer_numbers,hostArray,10);
    

    As a side note, consider removing the extern "C", since you’re calling this from C++ (main.cpp) there’s no point and it just clutters your code.

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

Sidebar

Related Questions

I would appreciate some help with an UPDATE statement. I want to update tblOrderHead
I would appreciate some help here: What I want to do: remove &itemsperpage=10 from:
I would appreciate some help refactoring my code. If I pass an instance of
I am new android and I would appreciate some help. I have this code:
I'm pretty new to Android development and would appreciate some help. All I want
I would appreciate some help with something I working on and have not done
I would appreciate some help on creating the proper SQL to retrieve only one
I have a problem finding references to this subject and would appreciate some help.
I'm pretty new to Python programming and would appreciate some help to a problem
I'm a newbie in Spring Batch, and I would appreciate some help to resolve

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.