﻿# __CUDA_ARCH__ is a compilation-pass choice, not a runtime GPU query

Distinguish CUDA virtual architecture preprocessing, host/device compilation, runtime device discovery, and consistent shared-header definitions across separately compiled objects.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cuda-arch-macro-device-pass-header-consistency/
Language: en

## Identify the compile-time question

The [NVCC manual](https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html) defines `__CUDA_ARCH__` for device compilation according to the virtual architecture being compiled. It is not a runtime query of the GPU currently running the application. A fat binary can contain code prepared for several targets, while a preprocessing decision belongs to a particular compilation pass. Keep build-target selection separate from runtime device inspection when designing architecture-specific helpers.

## Separate host and device branches

The [CUDA language guide](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/cpp-language-extensions.html) demonstrates host/device functions using the macro to distinguish their compiled implementations. This does not authorize host behavior to depend on an assumed device-pass value. A host-side launch wrapper should use a supported runtime discovery mechanism for runtime decisions. A preprocessor branch can select source implementation during compilation, but it cannot inspect which GPU a later process invocation will select.

## Preserve shared-header definitions

The NVCC manual also warns about architecture-dependent weak or template functions defined in shared headers during separate compilation. If participating objects instantiate the same function with different behavior because they used different compute targets, linking can retain a definition inconsistent with another object's expectation. This is more than a missing binary-architecture problem. It concerns agreement about the definition of a shared device symbol across translation units that eventually participate in the same program.

## Review the build target explicitly

The [CMake architecture property](https://cmake.org/cmake/help/latest/prop_tgt/CUDA_ARCHITECTURES.html) distinguishes real and virtual code-generation targets. Consider a library refactor where targets inherited different architecture settings. Record the effective compilation flags and shared template instantiations rather than assuming a successful final link proves semantic agreement. Use a small fixture whose architecture branches produce distinguishable results, then verify the intended selected definition on the supported deployment path.

## Keep three evidence layers visible

Record preprocessing target, linked device-code contents, and runtime selected device separately. Guides to [CUDA version scopes](/journal/guide-linux-cuda-version-scopes/) and [GPU acceptance evidence](/journal/gpu-acceptance-evidence/) provide context. Test shared-header consistency before interpreting target-specific performance. A reliable architecture branch answers a precise compile-time question without substituting for runtime discovery or silently making separately compiled objects disagree about the behavior of the same device function.
