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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:37:00+00:00 2026-05-23T01:37:00+00:00

How can I create global variables in CUDA?? Could you please give me an

  • 0

How can I create global variables in CUDA??
Could you please give me an example?

How can create arrays inside a CUDA function for example

__global__ void test()
{
  int *a = new int[10];
}

or How can I create a global array and access it from this function. for example

__device__ int *a;
__global__ void test()
{
  a[0] = 2;
}

Or How can I use like the following..

__global__ void ProcessData(int img)
{
   int *neighborhood = new int[8]; 
   getNeighbourhood(img, neighbourhood);
}

Still I have some problem with this. I found that compare to

__device__

if I define

"__device__ __constant__" (read only)

will improve the memory access.
But my problem is I have an array in host memory say

 float *arr = new float[sizeOfTheArray]; 

I want to make it as a variable array in device and I need to modify this in device memory and I need to copy this back to host. How can I do it??

  • 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-05-23T01:37:00+00:00Added an answer on May 23, 2026 at 1:37 am

    The C++ new operator is supported on compute capability 2.0 and 2.1 (ie. Fermi) with CUDA 4.0, so you could use new to allocate global memory onto a device symbol, although neither of your first two code snippets are how it would be done in practice.

    On older hardware, and/or with pre CUDA 4.0 toolkits, the standard approach is to use the cudaMemcpyToSymbol API in host code:

    __device__ float *a;
    
    int main()
    {
        const size_t sz = 10 * sizeof(float);
    
        float *ah;
        cudaMalloc((void **)&ah, sz);
        cudaMemcpyToSymbol("a", &ah, sizeof(float *), size_t(0),cudaMemcpyHostToDevice);
    }
    

    which copies a dynamically allocated device pointer onto a symbol which can be used directly in device code.


    EDIT: Answering this question is a bit like hitting a moving target. For the constant memory case you now seem interested in, here is a complete working example:

    #include <cstdio>
    
    #define nn (10)
    
    __constant__ float a[nn];
    
    __global__ void kernel(float *out)
    {
        if (threadIdx.x < nn)
            out[threadIdx.x] = a[threadIdx.x];
    
    }
    
    int main()
    {
        const size_t sz = size_t(nn) * sizeof(float);
        const float avals[nn]={ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10. };
        float ah[nn];
    
        cudaMemcpyToSymbol("a", &avals[0], sz, size_t(0),cudaMemcpyHostToDevice);
    
        float *ad;
        cudaMalloc((void **)&ad, sz);
    
        kernel<<<dim3(1),dim3(16)>>>(ad);
    
        cudaMemcpy(&ah[0],ad,sz,cudaMemcpyDeviceToHost);
    
        for(int i=0; i<nn; i++) {
            printf("%d %f\n", i, ah[i]);
        }
    }
    

    This shows copying data onto a constant memory symbol, and using that data inside a kernel.

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

Sidebar

Related Questions

How can I create global variables in CUDA? __device__ float *devD; cudaMalloc((void**)&devD, s); calculateDT_T2B<<<dimGrid,
In Sinatra, I'm unable to create global variables which are assigned values only once
I need to create some global android variables. Some posts here suggested me to
I can create a menu item in the Windows Explorer context menu by adding
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
I can create a literal long by appending an L to the value; why
In Postgresql you can create additional Aggregate Functions with CREATE AGGREGATE name(...); But this
this question can create a misunderstanding: I know I have to use CSS to
Is there anyway I can create a not in clause like I would have
I want to know if i can create a custom google maps application,on which

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.