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

  • Home
  • SEARCH
  • 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 8557761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:38:52+00:00 2026-06-11T15:38:52+00:00

Using CUDA, I want to allocate memory for different arrays, one for each GPU

  • 0

Using CUDA, I want to allocate memory for different arrays, one for each GPU from a different function than main(), but I must have missed something in regard to pointer arithmetic. Here’s what I thought,

void InitThisMemory(int***, int N, int Nout, size_t* pitch, int height, int width); // This function's purpose is to initialize A and the pitch

int main(void){
   int** A;
   int N = 10; 
   int NOut = 2;
   int height = 2, width = 2;
   size_t pitch;
   InitThisMemory(&A, N, NOut, &pitch, height, width);
   return 0;
}

InitThisMemory(int ***A, int N, int Nout, size_t* pitch, int height, int width){
  int i;
  *A = (int**)malloc(Nout * sizeof(int*));
  for(i = 0;i < Nout;i++){
      cudaSetDevice(i);
      cudaMallocPitch((void**)&(*A[i]), &(*pitch), width, height);
  }
}

Disclaimer: Not my actual code but this should reproduce the error. Let me know if I missed an allocation of a variable somewhere.

Why do I think that the problem is in the arithmetic? Simply because this works pretty well if Nout = 1 (which means that I am using only one device).

Any ideas?

  • 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-11T15:38:53+00:00Added an answer on June 11, 2026 at 3:38 pm

    Your bug, I think, is writing (void**)&(*A[i]) instead of (void **) (&(*A)[i]), but I recommend you refactor as follows:

    1. use a local int ** variable to hold the malloc() return value;
    2. use that local in your call to cudaMallocPitch();
    3. pass back the malloc() return value only if all cudaMallocPitch() calls succeed.

    If you do these things, then it will be simpler to write correct cleanup code in the event that one of the cudaMallocPitch() calls fails, and you needn’t propagate the passback unless everything has succeeded.

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

Sidebar

Related Questions

I am writing a massively parallel GPU application using CUDA. I have been optimizing
I'm using a custom random number function rand48 in CUDA. The function does not
I am interested in using CUDA to program a multi-GPU application. As far as
Using different streams for CUDA kernels makes concurrent kernel execution possible. Therefore n kernels
I've been using c/c++/cuda for less than a week and not familiar with all
I want to start using CUDA. My programming environment is VS 2010, C#.Net 4.0.
I want to manipulate a texture which I use in opengl using CUDA. Knowing
I am designing a multi-gpu cuda code but I still don't have the machinery
I'm trying to implement a critical section in CUDA using atomic instructions, but I
Is using cudaDeviceReset() after computations the normal way to use the GPU from Matlab?

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.