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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:58:08+00:00 2026-06-06T03:58:08+00:00

I have a weird problem, so I thought I would ask and see if

  • 0

I have a weird problem, so I thought I would ask and see if someone more experienced than me could see a solution.

I am writing a program with CUDA C/C++, and I have some constant integers that specify various things, like coordinates of the bounds of the calculation, etc.. Currently I just have those things in global device memory. They are accessed by every thread in every kernel call, and so I figured that if they are in global memory, then they never are being cached or broadcast (right?). And so these little integers are taking up a lot (relatively) of overhead, and have a lot of ‘read redundancy.’

So I declare in a header:

__constant__ int* number;

I include that header, and, when I do memory stuff, I do:

cutilSafeCall( cudaMemcpyToSymbol(number, &(some_host_int), sizeof(int) );

I pass number into all my kernel’s then:

__global__ void magical_kernel(int* number, ...){

   //and I access 'number' like this
   int data_thingy = big_array[ *number ];

}

My code crashes. With number in global memory, it is just fine. I have determined that it crashes sometime upon accessing number within the kernel. This means that either I am accessing or allocating it wrong. If it holds the wrong value, it will also cause a crash, because it is used to index into arrays.

To conclude, I will ask a few questions. First, what am I doing wrong? As a bonus: is there a better way than constant memory to accomplish this task – I don’t know the value of number at compile time, so a simple #define won’t work. Will constant memory even speed the code up at all, or has it been cached and broadcasted all along? Could I somehow put the data in shared memory for each threadblock and have it remain in shared memory through multiple kernel calls?

  • 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-06T03:58:10+00:00Added an answer on June 6, 2026 at 3:58 am

    There are several problems here:

    1. You have declared number as a pointer, but never assigned it a value which is valid address in GPU memory
    2. You have a variable scope onflict: the argument variable int * number defined in magic_kernel is not the same variable as the __constant__ int * variable defined as compilation unit scope.
    3. The first argument of the cudaMemcpyToSymbol call is almost certainly incorrect.

    If you don’t understand why either of the first two point are true, you have some revision to do on pointers and scope in C++.

    Based on your response to a now deleted answer, I suspect what you are actually trying to do is this:

    __constant__ int number;
    
    __global__ void magical_kernel(...){
    
       int data_thingy = big_array[ number ];
    
    }
    
    cudaMemcpyToSymbol("number", &(some_host_int), sizeof(int));
    

    i.e. number is intended to be an integer in constant memory, not a pointer, and not a kernel argument.


    EDIT: here is an exmaple which shows this in action:

    #include <cstdio>
    
    __constant__ int number;
    
    __global__ void magical_kernel(int * out)
    {
       out[threadIdx.x] = number;
    }
    
    int main()
    {
        const int value = 314159;
        const size_t sz = size_t(32) * sizeof(int);
        cudaMemcpyToSymbol("number", &value, sizeof(int));
    
        int * _out, * out;
    
        out = (int *)malloc(sz);
        cudaMalloc((void **)&_out, sz);
    
        magical_kernel<<<1,32>>>(_out);
    
        cudaMemcpy(out, _out, sz, cudaMemcpyDeviceToHost);
        for(int i=0; i<32; i++)
            fprintf(stdout, "%d %d\n", i, out[i]);
    
        return 0;
    }
    

    You should be able to run this yourself and confirm it works as advertised.

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

Sidebar

Related Questions

I got a weird problem. I have a program that (does many things but
I'm trying to work with requirejs and text plugin and I have weird problem.
I have a weird problem here. When I start an Emulator, it starts up
I have a weird problem. I have a script that loads images into the
I have a weird problem. On my development server all stuff works but on
I have this weird problem, when I create a calendar with a locale, the
I have a weird problem... I use this code: String text = new String(values[0]);
I have a weird problem. While running my Android application, I receive Exception: java.lang.ClassCastException:
I have a weird problem that only started recently. When I run the app
I have a weird problem and I'm not sure how to approach it. I'm

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.