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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:21:55+00:00 2026-06-13T00:21:55+00:00

I have a dynamically declared 2D array in my C program, the contents of

  • 0

I have a dynamically declared 2D array in my C program, the contents of which I want to transfer to a CUDA kernel for further processing. Once processed, I want to populate the dynamically declared 2D array in my C code with the CUDA processed data. I am able to do this with static 2D C arrays but not with dynamically declared C arrays. Any inputs would be welcome!

I mean the dynamic array of dynamic arrays. The test code that I have written is as below.

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>

const int nItt = 10;
const int nP = 5;

__device__ int d_nItt = 10;
__device__ int d_nP = 5;


__global__ void arr_chk(float *d_x_k, float *d_w_k, int row_num)
{

int index = (blockIdx.x * blockDim.x) + threadIdx.x;
int index1 = (row_num * d_nP) + index; 
if ( (index1 >= row_num * d_nP) && (index1 < ((row_num +1)*d_nP)))              //Modifying only one row data pertaining to one particular iteration
{

        d_x_k[index1] = row_num * d_nP;
        d_w_k[index1] = index;
}

}

float **mat_create2(int r, int c)
{
float **dynamicArray;
dynamicArray = (float **) malloc (sizeof (float)*r);
for(int i=0; i<r; i++)
    {
    dynamicArray[i] = (float *) malloc (sizeof (float)*c);
        for(int j= 0; j<c;j++)
        {
            dynamicArray[i][j] = 0;
        }
    }
return dynamicArray;
}

/* Freeing memory - here only number of rows are passed*/
void cleanup2d(float **mat_arr, int x)
{
int i;
for(i=0; i<x; i++)
{
    free(mat_arr[i]);
}
free(mat_arr);
}

int main()
{

//float w_k[nItt][nP]; //Static array declaration - works!
//float x_k[nItt][nP];
// if I uncomment this dynamic declaration and comment the static one, it does not work.....
float **w_k = mat_create2(nItt,nP); 
float **x_k = mat_create2(nItt,nP);
float *d_w_k, *d_x_k;       // Device variables for w_k and x_k
int nblocks, blocksize, nthreads;
for(int i=0;i<nItt;i++)
{
    for(int j=0;j<nP;j++)
    {
        x_k[i][j] = (nP*i);
        w_k[i][j] = j;
    }
}

for(int i=0;i<nItt;i++)
{
    for(int j=0;j<nP;j++)
    {
        printf("x_k[%d][%d] = %f\t",i,j,x_k[i][j]);
        printf("w_k[%d][%d] = %f\n",i,j,w_k[i][j]);
    }
}
int size1 = nItt * nP * sizeof(float);
printf("\nThe array size in memory bytes is: %d\n",size1);
cudaMalloc( (void**)&d_x_k, size1 );
cudaMalloc( (void**)&d_w_k, size1 );

if((nP*nItt)<32)    
{
    blocksize = nP*nItt;
    nblocks = 1;
}
else
{
    blocksize = 32;     // Defines the number of threads running per block. Taken equal to warp size
    nthreads = blocksize;
    nblocks =  ceil(float(nP*nItt) / nthreads);     // Calculated total number of blocks thus required
}

for(int i = 0; i< nItt; i++)
{
    cudaMemcpy( d_x_k, x_k, size1,cudaMemcpyHostToDevice ); //copy of x_k to device
    cudaMemcpy( d_w_k, w_k, size1,cudaMemcpyHostToDevice ); //copy of w_k to device
    arr_chk<<<nblocks, blocksize>>>(d_x_k,d_w_k,i);
    cudaMemcpy( x_k, d_x_k, size1, cudaMemcpyDeviceToHost );
    cudaMemcpy( w_k, d_w_k, size1, cudaMemcpyDeviceToHost );
}
printf("\nVerification after return from gpu\n");
for(int i = 0; i<nItt; i++)
{
    for(int j=0;j<nP;j++)
    {
        printf("x_k[%d][%d] = %f\t",i,j,x_k[i][j]);
        printf("w_k[%d][%d] = %f\n",i,j,w_k[i][j]);
    }
}
cudaFree( d_x_k );
cudaFree( d_w_k );
cleanup2d(x_k,nItt);
cleanup2d(w_k,nItt);
getch();
return 0;
  • 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-13T00:21:56+00:00Added an answer on June 13, 2026 at 12:21 am

    I mean the dynamic array of dynamic arrays.

    Well, that’s exactly where the problem lies. A dynamic array of dynamic arrays consists of a whole bunch of disjoint memory blocks, one for each line in the array (as is clearly seen from the malloc inside you for loop in mat_create2). So you can’t copy such a data structure to device memory with just one call to cudaMemcpy*. Instead, you have to do either

    • Also use dynamic arrays of dynamic arrays on CUDA. To do this, you have to basically recreate your mat_create2 function, using cudaMalloc instead of malloc, then copy each row seperately.

    • Use a “tight” 2d array on CUDA, like you do now (which is a good thing, at least performance-wise!). But if you keep using dyn-dyn-arrays on host memory, you still have copy each row seperately, like

      for(int i=0; i<r; ++i){
        cudaMemcpy(d_x_k + i*c, x_k[i], c*sizeof(float), cudaMemcpyHostToDevice)
      }
      

    You may wonder “why did it work with a static 2d array, then”? Well, static 2d arrays in C are proper, tight arrays that can be copied in one go. It’s a bit confusing that these are indexed with exactly the same syntax as dyn-dyn arrays (arr[x][y]), because it actually works completely different.

    But you should consider using tight arrays on host memory, too, perhaps with an object-oriented wrapper like

    typedef struct {
      float* data;
      int n_rows, n_cols;
    } tight2dFloatArray;
    
    #define INDEX_TIGHT2DARRAY(arr, y, x)\
      (arr).data[(y)*(arr).n_cols + (x)]
    

    such an approach of course can be implemented much safer as a C++ class.


    *You also can’t copy it inside main memory with just one memcpy: that only copies the array of pointers, not the actual data.

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

Sidebar

Related Questions

I have a dynamic class, which has an array declared normally, before runtime. Usually,
I have dynamically generated strings like @#@!efq@!#! , and I want to remove specific
I have a 2D jagged array declared in my main() block. This is to
I have a jsp page in which there is a html table. The contents
I have Python classes with object attributes which are only declared as part of
This is the code that I have to dynamically declaring an array inside a
I need some help with allocating arrays dynamically. I have an array whose size
I have declared a method where it will dynamically create a database query string
I have an array which stores a dictionary of Types: //The dictionary: Dictionary<CacheKey,Type> TypeLookup;
I use Visual Studio 2008. I have dynamically declared the variable big_massive: unsigned int

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.