Cuda Compiler Driver NVCC: Reference Guide
Cuda Compiler Driver NVCC: Reference Guide
Reference Guide
CHANGES FROM PREVIOUS VERSION
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | ii
TABLE OF CONTENTS
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | iii
5.7.2.3. Shorthand 3....................................................................................27
5.7.3. Extended Notation................................................................................. 27
5.7.4. Virtual Architecture Identification Macro...................................................... 28
Chapter 6. Using Separate Compilation in CUDA........................................................ 29
6.1. Code Changes for Separate Compilation............................................................ 29
6.2. NVCC Options for Separate Compilation............................................................ 29
6.3. Libraries...................................................................................................30
6.4. Examples.................................................................................................. 31
6.5. Potential Separate Compilation Issues...............................................................32
6.5.1. Object Compatibility.............................................................................. 32
6.5.2. JIT Linking Support................................................................................ 33
6.5.3. Implicit CUDA Host Code......................................................................... 33
6.5.4. Using __CUDA_ARCH__............................................................................ 33
6.5.5. Device Code in Libraries..........................................................................34
Chapter 7. Miscellaneous NVCC Usage..................................................................... 35
7.1. Cross Compilation....................................................................................... 35
7.2. Keeping Intermediate Phase Files.................................................................... 35
7.3. Cleaning Up Generated Files.......................................................................... 35
7.4. Printing Code Generation Statistics.................................................................. 36
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | iv
LIST OF FIGURES
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | v
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | vi
Chapter 1.
INTRODUCTION
1.1. Overview
1.1.1. CUDA Programming Model
The CUDA Toolkit targets a class of applications whose control part runs as a process
on a general purpose computing device, and which use one or more NVIDIA GPUs as
coprocessors for accelerating single program, multiple data (SPMD) parallel jobs. Such jobs
are self-contained, in the sense that they can be executed and completed by a batch of
GPU threads entirely without intervention by the host process, thereby gaining optimal
benefit from the parallel graphics hardware.
The GPU code is implemented as a collection of functions in a language that is
essentially C++, but with some annotations for distinguishing them from the host code,
plus annotations for distinguishing different types of data memory that exists on the
GPU. Such functions may have parameters, and they can be called using a syntax that is
very similar to regular C function calling, but slightly extended for being able to specify
the matrix of GPU threads that must execute the called function. During its life time, the
host process may dispatch many parallel GPU tasks.
For more information on the CUDA programming model, consult the CUDA C
Programming Guide.
1.1.2. CUDA Sources
Source files for CUDA applications consist of a mixture of conventional C++ host code,
plus GPU device functions. The CUDA compilation trajectory separates the device
functions from the host code, compiles the device functions using the proprietary
NVIDIA compilers and assembler, compiles the host code using a C++ host compiler
that is available, and afterwards embeds the compiled GPU functions as fatbinary
images in the host object file. In the linking stage, specific CUDA runtime libraries are
added for supporting remote SPMD procedure calling and for providing explicit GPU
manipulation such as allocation of GPU memory buffers and host-GPU data transfer.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 1
Introduction
1.1.3. Purpose of NVCC
The compilation trajectory involves several splitting, compilation, preprocessing, and
merging steps for each CUDA source file. It is the purpose of nvcc, the CUDA compiler
driver, to hide the intricate details of CUDA compilation from developers. It accepts a
range of conventional compiler options, such as for defining macros and include/library
paths, and for steering the compilation process. All non-CUDA compilation steps are
forwarded to a C++ host compiler that is supported by nvcc, and nvcc translates its
options to appropriate host compiler command line options.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 2
Chapter 2.
COMPILATION PHASES
2.2. NVCC Phases
A compilation phase is the a logical translation step that can be selected by command
line options to nvcc. A single compilation phase can still be broken up by nvcc into
smaller steps, but these smaller steps are just implementations of the phase: they depend
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 3
Compilation Phases
on seemingly arbitrary capabilities of the internal tools that nvcc uses, and all of these
internals may change with a new release of the CUDA Toolkit. Hence, only compilation
phases are stable across releases, and although nvcc provides options to display the
compilation steps that it executes, these are for debugging purposes only and must not
be copied and used into build scripts.
nvcc phases are selected by a combination of command line options and input file
name suffixes, and the execution of these phases may be modified by other command
line options. In phase selection, the input file suffix defines the phase input, while the
command line option defines the required output of the phase.
The following paragraphs will list the recognized file name suffixes and the supported
compilation phases. A full explanation of the nvcc command line options can be found
in the next chapter.
Note that nvcc does not make any distinction between object, library or resource files. It
just passes files of these types to the linker when the linking phase is executed.
2.4. Supported Phases
The following table specifies the supported compilation phases, plus the option to
nvcc that enables execution of this phase. It also lists the default name of the output
file generated by this phase, which will take effect when no explicit output file name is
specified using option --output-file:
nvcc Option
Phase Default Output File Name
Long Name Short Name
CUDA --cuda -cuda .cpp.ii appended to source file name, as in
compilation to x.cu.cpp.ii. This output file can be compiled
C/C++ source by the host compiler that was used by nvcc to
file preprocess the .cu file.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 4
Compilation Phases
nvcc Option
Phase Default Output File Name
Long Name Short Name
C/C++ --preprocess -E <result on standard output>
preprocessing
C/C++ --compile -c Source file name with suffix replaced by o on Linux
compilation to and Mac OS X, or obj on Windows
object file
Cubin --cubin -cubin Source file name with suffix replaced by cubin
generation
from CUDA
source files
Cubin --cubin -cubin Source file name with suffix replaced by cubin
generation
from PTX
intermediate
files.
PTX generation --ptx -ptx Source file name with suffix replaced by ptx
from CUDA
source files
Fatbinary --fatbin -fatbin Source file name with suffix replaced by fatbin
generation
from source,
PTX or cubin
files
Linking --device- -dlink a_dlink.obj on Windows or a_dlink.o on other
relocatable link platforms
device code.
Cubin --device- -dlink - a_dlink.cubin
generation link -- cubin
from linked cubin
relocatable
device code.
Fatbinary --device- -dlink - a_dlink.fatbin
generation link -- fatbin
from linked fatbin
relocatable
device code
Linking an <no phase option> a.exe on Windows or a.out on other platforms
executable
Constructing --lib -lib a.lib on Windows or a.a on other platforms
an object file
archive, or
library
make --generate- -M <result on standard output>
dependency dependencies
generation
Running an --run -run
executable
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 5
Compilation Phases
Notes:
‣ The last phase in this list is more of a convenience phase. It allows running the
compiled and linked executable without having to explicitly set the library path to
the CUDA dynamic libraries.
‣ Unless a phase option is specified, nvcc will compile and link all its input files.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 6
Chapter 3.
NVCC COMMAND OPTIONS
Long option names are used throughout the document, unless specified otherwise,
however, short names can be used instead of long names to have the same effect.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 7
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 8
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 9
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 10
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 11
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 12
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 13
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 14
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 15
NVCC Command Options
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 16
NVCC Command Options
3.2.9. Phase Options
The following sections lists some useful options to lower level compilation tools.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 17
NVCC Command Options
3.2.9.1. Ptxas Options
The following table lists some useful ptxas options which can be specified with nvcc
option -Xptxas.
Long Name Short Name Description
--allow-expensive- -allow-expensive- Enable (disable) to allow compiler to
optimizations optimizations perform expensive optimizations using
maximum available resources (memory
and compile-time).
If unspecified, default behavior is to
enable this feature for optimization level
>= O2.
--compile-only -c Generate relocatable object.
--def-load-cache -dlcm Default cache modifier on global/generic
load. Default value: ca.
--def-store-cache -dscm Default cache modifier on global/generic
store.
--device-debug -g Semantics same as nvcc option --
device-debug.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 18
NVCC Command Options
3.2.9.2. NVLINK Options
The following table lists some useful nvlink options which can be specified with nvcc
option --nvlink-options.
Long Name Short Name Description
--disable-warnings -w Inhibit all warning messages.
--preserve-relocs -preserve-relocs Preserve resolved relocations in linked
executable.
--verbose -v Enable verbose mode which prints code
generation statistics.
--warning-as-error -Werror Make all warnings into errors.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 19
Chapter 4.
THE CUDA COMPILATION TRAJECTORY
The CUDA phase converts a source file coded in the extended CUDA language into
a regular ANSI C++ source file that can be handed over to a general purpose C++ host
compiler for further compilation and linking. The exact steps that are followed to
achieve this are displayed in Figure 1.
CUDA compilation works as follows: the input program is preprocessed for device
compilation compilation and is compiled to CUDA binary (cubin) and/or PTX
intermediate code, which are placed in a fatbinary. The input program is preprocesed
once again for host compilation and is synthesized to embed the fatbinary and transform
CUDA specific C++ extensions into standard C++ constructs. Then the C++ host compiler
compiles the synthesized host code with the embedded fatbinary into a host object.
The embedded fatbinary is inspected by the CUDA runtime system whenever the device
code is launched by the host program to obtain an appropriate fatbinary image for the
current GPU.
The CUDA compilation trajectory is more complicated in the separate compilation
mode. For more information, see Using Separate Compilation in CUDA.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 20
The CUDA Compilation Trajectory
.cu
A B
C+ + Preprocessor C+ + Preprocessor
A is passed to B as an input file.
.cpp4.ii .cpp1.ii A B
A is #include'd in B.
ptx as
.cubin
.o / .obj .fatbin.c
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 21
Chapter 5.
GPU COMPILATION
This chapter describes the GPU compilation model that is maintained by nvcc, in
cooperation with the CUDA driver. It goes through some technical sections, with
concrete examples at the end.
5.1. GPU Generations
In order to allow for architectural evolution, NVIDIA GPUs are released in different
generations. New generations introduce major improvements in functionality and/
or chip architecture, while GPU models within the same generation show minor
configuration differences that moderately affect functionality, performance, or both.
Binary compatibility of GPU applications is not guaranteed across different generations.
For example, a CUDA application that has been compiled for a Fermi GPU will
very likely not run on a Kepler GPU (and vice versa). This is the instruction set and
instruction encodings of a geneartion is different from those of of other generations.
Binary compatibility within one GPU generation can be guaranteed under certain
conditions because they share the basic instruction set. This is the case between two GPU
versions that do not show functional differences at all (for instance when one version is
a scaled down version of the other), or when one version is functionally included in the
other. An example of the latter is the base Kepler version sm_30 whose functionality is
a subset of all other Kepler versions: any code compiled for sm_30 will run on all other
Kepler GPUs.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 22
GPU Compilation
sm_x2y2. From this it indeed follows that sm_30 is the base Kepler model, and it also
explains why higher entries in the tables are always functional extensions to the lower
entries. This is denoted by the plus sign in the table. Moreover, if we abstract from the
instruction encoding, it implies that sm_30's functionality will continue to be included
in all later GPU generations. As we will see next, this property will be the foundation for
application compatibility support by nvcc.
sm_30 and sm_32 Basic features
+ Kepler support
+ Unified memory programming
sm_35 + Dynamic parallelism support
sm_50, sm_52, and + Maxwell support
sm_53
5.3. Application Compatibility
Binary code compatibility over CPU generations, together with a published instruction
set architecture is the usual mechanism for ensuring that distributed applications out
there in the field will continue to run on newer versions of the CPU when these become
mainstream.
This situation is different for GPUs, because NVIDIA cannot guarantee binary
compatibility without sacrificing regular opportunities for GPU improvements. Rather,
as is already conventional in the graphics programming domain, nvcc relies on a
two stage compilation model for ensuring application compatibility with future GPU
generations.
5.4. Virtual Architectures
GPU compilation is performed via an intermediate representation, PTX, which can be
considered as assembly for a virtual GPU architecture. Contrary to an actual graphics
processor, such a virtual GPU is defined entirely by the set of capabilities, or features,
that it provides to the application. In particular, a virtual GPU architecture provides a
(largely) generic instruction set, and binary instruction encoding is a non-issue because
PTX programs are always represented in text format.
Hence, a nvcc compilation command always uses two architectures: a virtual
intermediate architecture, plus a real GPU architecture to specify the intended processor
to execute on. For such an nvcc command to be valid, the real architecture must be an
implementation of the virtual architecture. This is further explained below.
The chosen virtual architecture is more of a statement on the GPU capabilities that
the application requires: using a smallest virtual architecture still allows a widest range
of actual architectures for the second nvcc stage. Conversely, specifying a virtual
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 23
GPU Compilation
architecture that provides features unused by the application unnecessarily restricts the
set of possible GPUs that can be specified in the second nvcc stage.
From this it follows that the virtual architecture should always be chosen as low as
possible, thereby maximizing the actual GPUs to run on. The real architecture should be
chosen as high as possible (assuming that this always generates better code), but this is
only possible with knowledge of the actual GPUs on which the application is expected
to run. As we will see later, in the situation of just in time compilation, where the driver
has this exact knowledge: the runtime GPU is the one on which the program is about to
be launched/executed.
NVCC
virtual com pute architecture
Stage 1
(PTX Generation)
x .ptx
CUDA Runtim e
real sm architecture
Stage 2
(Cubin Generation)
x .cubin Ex ecute
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 24
GPU Compilation
The above table lists the currently defined virtual architectures. The virtual architecture
naming scheme is the same as the real architecture naming scheme shown in Section
GPU Feature List.
5.6. Further Mechanisms
Clearly, compilation staging in itself does not help towards the goal of application
compatibility with future GPUs. For this we need the two other mechanisms by CUDA
Samples: just in time compilation (JIT) and fatbinaries.
5.6.1. Just-in-Time Compilation
The compilation step to an actual GPU binds the code to one generation of GPUs. Within
that generation, it involves a choice between GPU coverage and possible performance.
For example, compiling to sm_30 allows the code to run on all Kepler-generation GPUs,
but compiling to sm_35 would probably yield better code if Kepler GK110 and later are
the only targets.
NVCC
virtual com pute architecture
Stage 1
(PTX Generation)
x .ptx
CUDA Runtim e
real sm architecture
Stage 2
(Cubin Generation)
x .cubin
Ex ecute
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 25
GPU Compilation
5.6.2. Fatbinaries
A different solution to overcome startup delay by JIT while still allowing execution on
newer GPUs is to specify multiple code instances, as in
nvcc x.cu --gpu-architecture=compute_50 --gpu-code=compute_50,sm_50,sm_52
This command generates exact code for two Kepler variants, plus PTX code for use by
JIT in case a next-generation GPU is encountered. nvcc organizes its device code in
fatbinaries, which are able to hold multiple translations of the same GPU source code. At
runtime, the CUDA driver will select the most appropriate translation when the device
function is launched.
5.7. NVCC Examples
5.7.1. Base Notation
nvcc provides the options --gpu-architecture and --gpu-code for specifying the
target architectures for both translation stages. Except for allowed short hands described
below, the --gpu-architecture option takes a single value, which must be the name
of a virtual compute architecture, while option --gpu-code takes a list of values which
must all be the names of actual GPUs. nvcc performs a stage 2 translation for each of
these GPUs, and will embed the result in the result of compilation (which usually is a
host object file or executable).
Example
nvcc x.cu --gpu-architecture=compute_50 --gpu-code=sm_50,sm_52
5.7.2. Shorthand
nvcc allows a number of shorthands for simple cases.
5.7.2.1. Shorthand 1
--gpu-code arguments can be virtual architectures. In this case the stage 2 translation
will be omitted for such virtual architecture, and the stage 1 PTX result will be
embedded instead. At application launch, and in case the driver does not find a better
alternative, the stage 2 compilation will be invoked by the driver with the PTX as input.
Example
nvcc x.cu --gpu-architecture=compute_50 --gpu-code=compute_50,sm_50,sm_52
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 26
GPU Compilation
5.7.2.2. Shorthand 2
The --gpu-code option can be omitted. Only in this case, the --gpu-architecture
value can be a non-virtual architecture. The --gpu-code values default to the
closest virtual architecture that is implemented by the GPU specified with --gpu-
architecture, plus the --gpu-architecture, value itself. The closest virtual
architecture is used as the effective --gpu-architecture, value. If the --gpu-
architecture value is a virtual architecture, it is also used as the effective --gpu-code
value.
Example
nvcc x.cu --gpu-architecture=sm_52
nvcc x.cu --gpu-architecture=compute_50
are equivalent to
5.7.2.3. Shorthand 3
Both --gpu-architecture and --gpu-code options can be omitted.
Example
nvcc x.cu
is equivalent to
5.7.3. Extended Notation
The options --gpu-architecture and --gpu-code can be used in all cases where
code is to be generated for one or more GPUs using a common virtual architecture. This
will cause a single invocation of nvcc stage 1 (that is, preprocessing and generation of
virtual PTX assembly code), followed by a compilation stage 2 (binary code generation)
repeated for each specified GPU.
Using a common virtual architecture means that all assumed GPU features are fixed
for the entire nvcc compilation. For instance, the following nvcc command assumes no
half-precision floating-point operation support for both the sm_50 code and the sm_53
code:
nvcc x.cu --gpu-architecture=compute_50 --gpu-code=compute_50,sm_50,sm_53
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 27
GPU Compilation
be confused with their main option equivalents, but behave similarly. If repeated
architecture compilation is used, then the device code must use conditional compilation
based on the value of the architecture identification macro __CUDA_ARCH__, which is
described in the next section.
For example, the following assumes absence of half-precision floating-point operation
support for the sm_50 and sm_52 code, but full support on sm_53:
nvcc x.cu \
--generate-code arch=compute_50,code=sm_50 \
--generate-code arch=compute_50,code=sm_52 \
--generate-code arch=compute_53,code=sm_53
Or, leaving actual GPU code generation to the JIT compiler in the CUDA driver:
nvcc x.cu \
--generate-code arch=compute_50,code=compute_50 \
--generate-code arch=compute_53,code=compute_53
The code sub-options can be combined with a slightly more complex syntax:
nvcc x.cu \
--generate-code arch=compute_50,code=[sm_50,sm_52] \
--generate-code arch=compute_53,code=sm_53
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 28
Chapter 6.
USING SEPARATE COMPILATION IN CUDA
Prior to the 5.0 release, CUDA did not support separate compilation, so CUDA code
could not call device functions or access variables across files. Such compilation is
referred to as whole program compilation. We have always supported the separate
compilation of host code, it was just the device CUDA code that needed to all be within
one file. Starting with CUDA 5.0, separate compilation of device code is supported,
but the old whole program mode is still the default, so there are new options to invoke
separate compilation.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 29
Using Separate Compilation in CUDA
To invoke just the device linker, the --device-link option can be used, which emits
a host object containing the embedded executable device code. The output of that must
then be passed to the host linker. Or:
nvcc <objects>
can be used to implicitly call both the device and host linkers. This works because if the
device linker does not see any relocatable code it does not do anything.
Figure 4 shows the flow (nvcc --device-c has the same flow as Figure 1).
x .cu y.cu z.cpp
Device Linker
a_dlink.o / a_dlink.obj
Host Linker
ex ecutable / library
6.3. Libraries
The device linker has the ability to read the static host library formats (.a on Linux and
Mac OS X, .lib on Windows). It ignores any dynamic (.so or .dll) libraries. The --
library and --library-path options can be used to pass libraries to both the device
and host linker. The library name is specified without the library file extension when the
--library option is used.
Alternatively, the library name, including the library file extension, can be used without
the --library option on Windows.
nvcc --gpu-architecture=sm_50 a.obj b.obj foo.lib --library-path=<path>
Note that the device linker ignores any objects that do not have relocatable device code.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 30
Using Separate Compilation in CUDA
6.4. Examples
Suppose we have the following files:
//---------- b.h ----------
#define N 8
__syncthreads();
bar();
}
foo<<<1, N>>>();
if(cudaGetSymbolAddress((void**)&dg, g)){
printf("couldn't get the symbol addr\n");
return 1;
}
if(cudaMemcpy(hg, dg, N * sizeof(int), cudaMemcpyDeviceToHost)){
printf("couldn't memcpy\n");
return 1;
}
return 0;
}
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 31
Using Separate Compilation in CUDA
These can be compiled with the following commands (these examples are for Linux):
nvcc --gpu-architecture=sm_50 --device-c a.cu b.cu
nvcc --gpu-architecture=sm_50 a.o b.o
If you want to invoke the device and host linker separately, you can do:
nvcc --gpu-architecture=sm_50 --device-c a.cu b.cu
nvcc --gpu-architecture=sm_50 --device-link a.o b.o --output-file link.o
g++ a.o b.o link.o --library-path=<path> --library=cudart
Note that all desired target architectures must be passed to the device linker, as that
specifies what will be in the final executable (some objects or libraries may contain
device code for multiple architectures, and the link step can then choose what to put in
the final executable).
If you want to use the driver API to load a linked cubin, you can request just the cubin:
nvcc --gpu-architecture=sm_50 --device-link a.o b.o \
--cubin --output-file link.cubin
Note that only static libraries are supported by the device linker.
A PTX file can be compiled to a host object file and then linked by using:
nvcc --gpu-architecture=sm_50 --device-c a.ptx
An example that uses libraries, host linker, and dynamic parallelism would be:
nvcc --gpu-architecture=sm_50 --device-c a.cu b.cu
nvcc --gpu-architecture=sm_50 --device-link a.o b.o --output-file link.o
nvcc --lib --output-file libgpu.a a.o b.o link.o
g++ host.o --library=gpu --library-path=<path> \
--library=cudadevrt --library=cudart
It is possible to do multiple device links within a single host executable, as long as each
device link is independent of the other. This requirement of independence means that
they cannot share code across device executables, nor can they share addresses (e.g.,
a device function address can be passed from host to device for a callback only if the
device link sees both the caller and potential callback callee; you cannot pass an address
from one device executable to another, as those are separate address spaces).
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 32
Using Separate Compilation in CUDA
An object could have been compiled for a different architecture but also have PTX
available, in which case the device linker will JIT the PTX to cubin for the desired
architecture and then link. Relocatable device code requires CUDA 5.0 or later Toolkit.
If a kernel is limited to a certain number of registers with the launch_bounds attribute
or the --maxrregcount option, then all functions that the kernel calls must not use
more than that number of registers; if they exceed the limit, then a link error will be
given.
6.5.4. Using __CUDA_ARCH__
In separate compilation, __CUDA_ARCH__ must not be used in headers such that
different objects could contain different behavior. Or, it must be guaranteed that all
objects will compile for the same compute_arch. If a weak function or template function
is defined in a header and its behavior depends on __CUDA_ARCH__, then the instances
of that function in the objects could conflict if the objects are compiled for different
compute arch. For example, if an a.h contains:
template<typename T>
__device__ T* getptr(void)
{
#if __CUDA_ARCH__ == 500
return NULL; /* no address */
#else
__shared__ T arr[256];
return arr;
#endif
}
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 33
Using Separate Compilation in CUDA
Then if a.cu and b.cu both include a.h and instantiate getptr for the same type, and
b.cu expects a non-NULL address, and compile with:
nvcc --gpu-architecture=compute_50 --device-c a.cu
nvcc --gpu-architecture=compute_52 --device-c b.cu
nvcc --gpu-architecture=sm_52 a.o b.o
At link time only one version of the getptr is used, so the behavior would depend
on which version is picked. To avoid this, either a.cu and b.cu must be compiled for
the same compute arch, or __CUDA_ARCH__ should not be used in the shared header
function.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 34
Chapter 7.
MISCELLANEOUS NVCC USAGE
7.1. Cross Compilation
Cross compilation is controlled by using the following nvcc command line options:
‣ --compiler-bindir is used for cross compilation, where the underlying host
compiler is capable of generating objects for the target platform.
‣ --machine=32. This option signals that the target platform is a 32-bit platform. Use
this when the host platform is a 64-bit platform.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 35
Miscellaneous NVCC Usage
Because using --clean-targets will remove exactly what the original nvcc command
created, it is important to exactly repeat all of the options in the original command. For
instance, in the following example, omitting --keep, or adding --compile will have
different cleanup effects.
nvcc acos.cu --keep
nvcc acos.cu --keep --clean-targets
As shown in the above example, the amounts of statically allocated global memory
(gmem) and constant memory in bank 14 (cmem) are listed.
Global memory and some of the constant banks are module scoped resources and
not per kernel resources. Allocation of constant variables to constant banks is profile
specific.
Followed by this, per kernel resource information is printed.
Stack frame is per thread stack usage used by this function. Spill stores and loads
represent stores and loads done on stack memory which are being used for storing
variables that couldn't be allocated to physical registers.
Similarly number of registers, amount of shared memory and total space in constant
bank allocated is shown.
www.nvidia.com
CUDA Compiler Driver NVCC TRM-06721-001_v9.0 | 36
Notice
ALL NVIDIA DESIGN SPECIFICATIONS, REFERENCE BOARDS, FILES, DRAWINGS,
DIAGNOSTICS, LISTS, AND OTHER DOCUMENTS (TOGETHER AND SEPARATELY,
"MATERIALS") ARE BEING PROVIDED "AS IS." NVIDIA MAKES NO WARRANTIES,
EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO THE
MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF
NONINFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR
PURPOSE.
Information furnished is believed to be accurate and reliable. However, NVIDIA
Corporation assumes no responsibility for the consequences of use of such
information or for any infringement of patents or other rights of third parties
that may result from its use. No license is granted by implication of otherwise
under any patent rights of NVIDIA Corporation. Specifications mentioned in this
publication are subject to change without notice. This publication supersedes and
replaces all other information previously supplied. NVIDIA Corporation products
are not authorized as critical components in life support devices or systems
without express written approval of NVIDIA Corporation.
Trademarks
NVIDIA and the NVIDIA logo are trademarks or registered trademarks of NVIDIA
Corporation in the U.S. and other countries. Other company and product names
may be trademarks of the respective companies with which they are associated.
Copyright
© 2007-2018 NVIDIA Corporation. All rights reserved.
www.nvidia.com