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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:46:34+00:00 2026-06-10T13:46:34+00:00

I am trying to implement a structure that holds arrays of data and I

  • 0

I am trying to implement a structure that holds arrays of data
and I want to implement dynamic array, something like:

struct myStruct {
  float3 *data0, *data1;
};

__global__ void kernel(myStruct input) {
  unsigned int N = 2;
  while(someStatements) {
    data0 = new float3[N];
    // do somethings
    N *= 2;
  }
}

How can I do something like this in a CUDA kernel?

  • 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-10T13:46:36+00:00Added an answer on June 10, 2026 at 1:46 pm

    If you are going to run this code on either a compute capability 2.x or 3,x device, with a recent version of CUDA, your kernel code is very nearly correct. The C++ new operator is supported in CUDA 4.x and 5.0 on Fermi and Kepler hardware. Note that memory which is allocated using new or malloc is allocated on runtime heap on the device. It has the lifespan of the context in which is was created, but you currently cannot directly access it from the CUDA host API (so via cudaMemcpy or similar).

    I turned your structure and kernel into a simple example code which you can try for yourself to see how it works:

    #include <cstdio>
    
    struct myStruct {
        float *data;
    };
    
    __device__ 
    void fill(float * x, unsigned int n)
    {
        for(int i=0; i<n; i++) x[i] = (float)i;
    }
    
    __global__ 
    void kernel(myStruct *input, const unsigned int imax)
    {
        for(unsigned int i=0,N=1; i<imax; i++, N*=2) {
            float * p = new float[N];
            fill(p, N);
            input[i].data = p;
        }
    }
    
    __global__
    void kernel2(myStruct *input, float *output, const unsigned int imax)
    {
        for(unsigned int i=0,N=1; i<imax; i++, N*=2) {
            output[i] = input[i].data[N-1];
        }
    }
    
    inline void gpuAssert(cudaError_t code, char * file, int line, bool Abort=true)
    {
        if (code != 0) {
            fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code),file,line);
            if (Abort) exit(code);
        }       
    }
    #define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
    
    int main(void)
    {
    
        const unsigned int nvals = 16;
        struct myStruct * _s;
        float * _f, * f;
    
        gpuErrchk( cudaMalloc((void **)&_s, sizeof(struct myStruct) * size_t(nvals)) );
        size_t sz = sizeof(float) * size_t(nvals);
        gpuErrchk( cudaMalloc((void **)&_f, sz) );
        f = new float[nvals];
    
        kernel<<<1,1>>>(_s, nvals);
        gpuErrchk( cudaPeekAtLastError() );
    
        kernel2<<<1,1>>>(_s, _f, nvals);
        gpuErrchk( cudaPeekAtLastError() );
        gpuErrchk( cudaMemcpy(f, _f, sz, cudaMemcpyDeviceToHost) );
        gpuErrchk( cudaDeviceReset() );
    
        for(int i=0; i<nvals; i++) {
            fprintf(stdout, "%d %f\n", i, f[i]);
        }
    
        return 0;
    }
    

    A few points to note:

    1. This code will only compile and run with CUDA 4.x or 5.0 on a Fermi or Kepler GPU
    2. You must pass the correct architecture for your GPU to nvcc to compile it (for example I used nvcc -arch=sm_30 -Xptxas="-v" -o dynstruct dynstruct.cu to compile for a GTX 670 on linux)
    3. The example code uses a “gather” kernel to copy data from the structure in runtime heap to an allocation which the host API can access so that the results can be printed out. This is a work around for the limitation I mentioned earlier regarding cudaMemcpy not being able to copy directly from addresses in runtime heap memory. I was hoping this might be fixed in CUDA 5.0, but the most recent release candidate still has this restriction.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement something that requires a structure like this: struct abc
I am trying to implement a resource data structure that includes an array of
I am trying to implement a complicated data structure that looks like Map<A,Set<B>> map
I'm trying to implement EF with an existing table structure that I'm not allowed
I am trying to implement the linked list data structure using java, it works
I'm trying to implement a tree-like structure with two classes: Tree and Node .
I was trying to implement a Piece Table data structure using Linked Lists in
I have a directed graph data structure, where I am trying to implement individual
I need to implement a data structure that supports insertion deletion and search in
I am trying to implement a data structure which allows me to keep track

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.