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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:12:27+00:00 2026-05-27T21:12:27+00:00

I am using a global variable say d_myVar, which will be allocated device memory

  • 0

I am using a global variable say d_myVar, which will be allocated device memory using cudaMalloc in main function. I am not clear, should I use __ device __ in front of it while doing global declaration? I ask this, since if it were local variable in host and was passed to a kernel, we would not write __ device __ in front of it. Let me know if I am wrong.

  • 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-27T21:12:28+00:00Added an answer on May 27, 2026 at 9:12 pm

    Globally-scoped __device__ variables are not allocated with cudaMalloc. Simply annotate a variable in the global scope with __device__:

     #include <stdio.h>
    
     __device__ int d_myVar;
    
     __global__ void foo()
     {
       printf("d_myVar is %d\n", d_myVar);
     }
    
     int main()
     {
       int h_myVar = 13;
       cudaMemcpyToSymbol(d_myVar, &h_myVar, sizeof(int), 0, cudaMemcpyHostToDevice);
       foo<<<1,1>>>();
       cudaThreadSynchronize();
       return 0;
     }
    

    The result:

    $ nvcc -arch=sm_20 test.cu -run
    d_myVar is 13
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for an alternative to using an object/variable in global scope --
I am using a variable/propery of my application delegate as a global. (I don't
Suppose there exists a function which returns a message say of the following format:
Say, I have index.htm and routines.php. index.htm will call eventually call routines.php using JS
Let's say a 3rd-party DLL X has a global variable G I write two
I'm implementing Inter-Thread Communication using global variable. //global var volatile bool is_true = true;
I always hear using global variables are dangerous. Does this apply to Drupal? Take
I'm using global variables to pass around a response from a AJAX call: window.response
Hey right now I'm using jQuery and I have some global variables to hold
I am using Global.asax to perform logging at the end of each request via

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.