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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:47:34+00:00 2026-05-28T02:47:34+00:00

The problem : Having a .h, I want to define real to be double

  • 0

The problem:

Having a .h, I want to define real to be double if compiling for c/c++ or for cuda with computing capability >= 1.3. If compiling for cuda with computing capability < 1.3 then define real to be float.

After many hours I came to this (which does not work )

#   if defined(__CUDACC__)

#       warning * making definitions for cuda

#       if defined(__CUDA_ARCH__)
#           warning __CUDA_ARCH__ is defined
#       else
#           warning __CUDA_ARCH__ is NOT defined
#       endif

#       if (__CUDA_ARCH__ >= 130)
#                       define real double
#                       warning using double in cuda
#       elif (__CUDA_ARCH__ >= 0)
#               define real float
#               warning using float in cuda
#               warning how the hell is this printed when __CUDA_ARCH__ is not defined?
#       else
#               define real 
#               error what the hell is the value of __CUDA_ARCH__ and how can I print it
#       endif

#   else
#       warning * making definitions for c/c++
#       define real double
#       warning using double for c/c++
#   endif

when I compile (note the -arch flag)

nvcc -arch compute_13  -Ilibcutil testFloatDouble.cu 

I get

* making definitions for cuda
__CUDA_ARCH__ is defined
using double in cuda

* making definitions for cuda
warning __CUDA_ARCH__ is NOT defined
warning using float in cuda
how the hell is this printed if __CUDA_ARCH__ is not defined now?

Undefined symbols for architecture i386:
  "myKernel(float*, int)", referenced from: ....

I know that files get compiled twice by nvcc. The first one is OK (CUDACC defined and CUDA_ARCH >= 130) but what happens the second time?
CUDA_DEFINED but CUDA_ARCH undefined or with value < 130? Why ?

Thanks for your time.

  • 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-28T02:47:35+00:00Added an answer on May 28, 2026 at 2:47 am

    It seems you might be conflating two things – how to differentiate between the host and device compilation trajectories when nvcc is processing CUDA code, and how to differentiate between CUDA and non-CUDA code. There is a subtle difference between the two. __CUDA_ARCH__ answers the first question, and __CUDACC__ answers the second.

    Consider the following code snippet:

    #ifdef __CUDACC__
    #warning using nvcc
    
    template <typename T>
    __global__ void add(T *x, T *y, T *z)
    {
        int idx = threadIdx.x + blockDim.x * blockIdx.x;
    
        z[idx] = x[idx] + y[idx];
    }
    
    #ifdef __CUDA_ARCH__
    #warning device code trajectory
    #if __CUDA_ARCH__ > 120
    #warning compiling with double precision
    template void add<double>(double *, double *, double *);
    #else
    #warning compiling with single precision
    template void add<float>(float *, float *, float *);
    #else
    #warning nvcc host code trajectory
    #endif
    #else
    #warning non-nvcc code trajectory
    #endif
    

    Here we have a templated CUDA kernel with CUDA architecture dependent instantiation, a separate stanza for host code steeered by nvcc, and a stanza for compilation of host code not steered by nvcc. This behaves as follows:

    $ ln -s cudaarch.cu cudaarch.cc
    $ gcc -c cudaarch.cc -o cudaarch.o
    cudaarch.cc:26:2: warning: #warning non-nvcc code trajectory
    
    $ nvcc -arch=sm_11 -Xptxas="-v" -c cudaarch.cu -o cudaarch.cu.o
    cudaarch.cu:3:2: warning: #warning using nvcc
    cudaarch.cu:14:2: warning: #warning device code trajectory
    cudaarch.cu:19:2: warning: #warning compiling with single precision
    cudaarch.cu:3:2: warning: #warning using nvcc
    cudaarch.cu:23:2: warning: #warning nvcc host code trajectory
    ptxas info    : Compiling entry function '_Z3addIfEvPT_S1_S1_' for 'sm_11'
    ptxas info    : Used 4 registers, 12+16 bytes smem
    
    $ nvcc -arch=sm_20 -Xptxas="-v" -c cudaarch.cu -o cudaarch.cu.o
    cudaarch.cu:3:2: warning: #warning using nvcc
    cudaarch.cu:14:2: warning: #warning device code trajectory
    cudaarch.cu:16:2: warning: #warning compiling with double precision
    cudaarch.cu:3:2: warning: #warning using nvcc
    cudaarch.cu:23:2: warning: #warning nvcc host code trajectory
    ptxas info    : Compiling entry function '_Z3addIdEvPT_S1_S1_' for 'sm_20'
    ptxas info    : Used 8 registers, 44 bytes cmem[0]
    

    The take away points from this are:

    • __CUDACC__ defines whether nvcc is steering compilation or not
    • __CUDA_ARCH__is always undefined when compiling host code, steered by nvcc or not
    • __CUDA_ARCH__is only defined for the device code trajectory of compilation steered by nvcc

    Those three pieces of information are always enough to have conditional compilation for device code to different CUDA architectures, host side CUDA code, and code not compiled by nvccat all. The nvccdocumentation is a bit terse at times, but all of this is covered in the discussion on compilation trajectories.

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

Sidebar

Related Questions

The problem I'm having with writing a web application architecture is that I want
I'm having a problem with curl_multi_* , I want to create a class /
I am having a problem with selecting a certain child node. What I want
Good day, I'm having a problem with asp.net 2.0 viewstate. Basically, I want to
I'm having a problem with the following code. I want to use one UIImageView
I'm having an awkward problem in Visual Studio 2008. I'm trying to define a
Okay, I'm having a problem saving after I've deleted all the objects I have
I'm having problems to define a regex in javascript. I want to replace format
I'm having a problem with UTF-8 character on the page title, I want to
I am having problems here if I want to check if eerste points to

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.