annotate docs/CompileCudaWithLLVM.rst @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1 =========================
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
2 Compiling CUDA with clang
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
3 =========================
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 .. contents::
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 :local:
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 Introduction
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 ============
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
11 This document describes how to compile CUDA code with clang, and gives some
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
12 details about LLVM and clang's CUDA implementations.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
13
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
14 This document assumes a basic familiarity with CUDA. Information about CUDA
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
15 programming can be found in the
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 `CUDA programming guide
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 <http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html>`_.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
19 Compiling CUDA Code
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
20 ===================
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
22 Prerequisites
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
23 -------------
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
25 CUDA is supported in llvm 3.9, but it's still in active development, so we
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
26 recommend you `compile clang/LLVM from HEAD
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
27 <http://llvm.org/docs/GettingStarted.html>`_.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
29 Before you build CUDA code, you'll need to have installed the appropriate
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
30 driver for your nvidia GPU and the CUDA SDK. See `NVIDIA's CUDA installation
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
31 guide <https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html>`_
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
32 for details. Note that clang `does not support
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
33 <https://llvm.org/bugs/show_bug.cgi?id=26966>`_ the CUDA toolkit as installed
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
34 by many Linux package managers; you probably need to install nvidia's package.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
36 You will need CUDA 7.0, 7.5, or 8.0 to compile with clang.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
38 CUDA compilation is supported on Linux, on MacOS as of 2016-11-18, and on
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
39 Windows as of 2017-01-05.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
41 Invoking clang
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
42 --------------
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
44 Invoking clang for CUDA compilation works similarly to compiling regular C++.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
45 You just need to be aware of a few additional flags.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
47 You can use `this <https://gist.github.com/855e277884eb6b388cd2f00d956c2fd4>`_
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
48 program as a toy example. Save it as ``axpy.cu``. (Clang detects that you're
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
49 compiling CUDA code by noticing that your filename ends with ``.cu``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
50 Alternatively, you can pass ``-x cuda``.)
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
52 To build and run, run the following commands, filling in the parts in angle
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
53 brackets as described below:
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 .. code-block:: console
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
57 $ clang++ axpy.cu -o axpy --cuda-gpu-arch=<GPU arch> \
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
58 -L<CUDA install path>/<lib64 or lib> \
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
59 -lcudart_static -ldl -lrt -pthread
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 $ ./axpy
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 y[0] = 2
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 y[1] = 4
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 y[2] = 6
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 y[3] = 8
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
66 On MacOS, replace `-lcudart_static` with `-lcudart`; otherwise, you may get
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
67 "CUDA driver version is insufficient for CUDA runtime version" errors when you
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
68 run your program.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
69
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
70 * ``<CUDA install path>`` -- the directory where you installed CUDA SDK.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
71 Typically, ``/usr/local/cuda``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
72
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
73 Pass e.g. ``-L/usr/local/cuda/lib64`` if compiling in 64-bit mode; otherwise,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
74 pass e.g. ``-L/usr/local/cuda/lib``. (In CUDA, the device code and host code
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
75 always have the same pointer widths, so if you're compiling 64-bit code for
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
76 the host, you're also compiling 64-bit code for the device.)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
77
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
78 * ``<GPU arch>`` -- the `compute capability
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
79 <https://developer.nvidia.com/cuda-gpus>`_ of your GPU. For example, if you
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
80 want to run your program on a GPU with compute capability of 3.5, specify
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
81 ``--cuda-gpu-arch=sm_35``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
82
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
83 Note: You cannot pass ``compute_XX`` as an argument to ``--cuda-gpu-arch``;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
84 only ``sm_XX`` is currently supported. However, clang always includes PTX in
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
85 its binaries, so e.g. a binary compiled with ``--cuda-gpu-arch=sm_30`` would be
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
86 forwards-compatible with e.g. ``sm_35`` GPUs.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
87
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
88 You can pass ``--cuda-gpu-arch`` multiple times to compile for multiple archs.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
89
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
90 The `-L` and `-l` flags only need to be passed when linking. When compiling,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
91 you may also need to pass ``--cuda-path=/path/to/cuda`` if you didn't install
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
92 the CUDA SDK into ``/usr/local/cuda``, ``/usr/local/cuda-7.0``, or
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
93 ``/usr/local/cuda-7.5``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
94
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
95 Flags that control numerical code
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
96 ---------------------------------
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
97
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
98 If you're using GPUs, you probably care about making numerical code run fast.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
99 GPU hardware allows for more control over numerical operations than most CPUs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
100 but this results in more compiler options for you to juggle.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
101
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
102 Flags you may wish to tweak include:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
103
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
104 * ``-ffp-contract={on,off,fast}`` (defaults to ``fast`` on host and device when
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
105 compiling CUDA) Controls whether the compiler emits fused multiply-add
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
106 operations.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
107
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
108 * ``off``: never emit fma operations, and prevent ptxas from fusing multiply
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
109 and add instructions.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
110 * ``on``: fuse multiplies and adds within a single statement, but never
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
111 across statements (C11 semantics). Prevent ptxas from fusing other
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
112 multiplies and adds.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
113 * ``fast``: fuse multiplies and adds wherever profitable, even across
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
114 statements. Doesn't prevent ptxas from fusing additional multiplies and
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
115 adds.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
116
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
117 Fused multiply-add instructions can be much faster than the unfused
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
118 equivalents, but because the intermediate result in an fma is not rounded,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
119 this flag can affect numerical code.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
121 * ``-fcuda-flush-denormals-to-zero`` (default: off) When this is enabled,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
122 floating point operations may flush `denormal
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
123 <https://en.wikipedia.org/wiki/Denormal_number>`_ inputs and/or outputs to 0.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
124 Operations on denormal numbers are often much slower than the same operations
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
125 on normal numbers.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
126
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
127 * ``-fcuda-approx-transcendentals`` (default: off) When this is enabled, the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
128 compiler may emit calls to faster, approximate versions of transcendental
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
129 functions, instead of using the slower, fully IEEE-compliant versions. For
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
130 example, this flag allows clang to emit the ptx ``sin.approx.f32``
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
131 instruction.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
132
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
133 This is implied by ``-ffast-math``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
134
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
135 Standard library support
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
136 ========================
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
137
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
138 In clang and nvcc, most of the C++ standard library is not supported on the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
139 device side.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
140
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
141 ``<math.h>`` and ``<cmath>``
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
142 ----------------------------
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
143
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
144 In clang, ``math.h`` and ``cmath`` are available and `pass
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
145 <https://github.com/llvm-mirror/test-suite/blob/master/External/CUDA/math_h.cu>`_
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
146 `tests
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
147 <https://github.com/llvm-mirror/test-suite/blob/master/External/CUDA/cmath.cu>`_
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
148 adapted from libc++'s test suite.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
149
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
150 In nvcc ``math.h`` and ``cmath`` are mostly available. Versions of ``::foof``
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
151 in namespace std (e.g. ``std::sinf``) are not available, and where the standard
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
152 calls for overloads that take integral arguments, these are usually not
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
153 available.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
154
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
155 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
156
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
157 #include <math.h>
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
158 #include <cmath.h>
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
159
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
160 // clang is OK with everything in this function.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
161 __device__ void test() {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
162 std::sin(0.); // nvcc - ok
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
163 std::sin(0); // nvcc - error, because no std::sin(int) override is available.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
164 sin(0); // nvcc - same as above.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
165
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
166 sinf(0.); // nvcc - ok
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
167 std::sinf(0.); // nvcc - no such function
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
168 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
169
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
170 ``<std::complex>``
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
171 ------------------
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
172
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
173 nvcc does not officially support ``std::complex``. It's an error to use
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
174 ``std::complex`` in ``__device__`` code, but it often works in ``__host__
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
175 __device__`` code due to nvcc's interpretation of the "wrong-side rule" (see
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
176 below). However, we have heard from implementers that it's possible to get
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
177 into situations where nvcc will omit a call to an ``std::complex`` function,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
178 especially when compiling without optimizations.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
179
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
180 As of 2016-11-16, clang supports ``std::complex`` without these caveats. It is
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
181 tested with libstdc++ 4.8.5 and newer, but is known to work only with libc++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
182 newer than 2016-11-16.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
183
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
184 ``<algorithm>``
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
185 ---------------
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
186
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
187 In C++14, many useful functions from ``<algorithm>`` (notably, ``std::min`` and
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
188 ``std::max``) become constexpr. You can therefore use these in device code,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
189 when compiling with clang.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
190
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
191 Detecting clang vs NVCC from code
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
192 =================================
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
193
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
194 Although clang's CUDA implementation is largely compatible with NVCC's, you may
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
195 still want to detect when you're compiling CUDA code specifically with clang.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
196
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
197 This is tricky, because NVCC may invoke clang as part of its own compilation
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
198 process! For example, NVCC uses the host compiler's preprocessor when
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
199 compiling for device code, and that host compiler may in fact be clang.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
200
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
201 When clang is actually compiling CUDA code -- rather than being used as a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
202 subtool of NVCC's -- it defines the ``__CUDA__`` macro. ``__CUDA_ARCH__`` is
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
203 defined only in device mode (but will be defined if NVCC is using clang as a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
204 preprocessor). So you can use the following incantations to detect clang CUDA
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
205 compilation, in host and device modes:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
206
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
207 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
208
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
209 #if defined(__clang__) && defined(__CUDA__) && !defined(__CUDA_ARCH__)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
210 // clang compiling CUDA code, host mode.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
211 #endif
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
212
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
213 #if defined(__clang__) && defined(__CUDA__) && defined(__CUDA_ARCH__)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
214 // clang compiling CUDA code, device mode.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
215 #endif
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
216
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
217 Both clang and nvcc define ``__CUDACC__`` during CUDA compilation. You can
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
218 detect NVCC specifically by looking for ``__NVCC__``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
219
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
220 Dialect Differences Between clang and nvcc
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
221 ==========================================
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
222
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
223 There is no formal CUDA spec, and clang and nvcc speak slightly different
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
224 dialects of the language. Below, we describe some of the differences.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
225
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
226 This section is painful; hopefully you can skip this section and live your life
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
227 blissfully unaware.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
228
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
229 Compilation Models
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
230 ------------------
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
231
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
232 Most of the differences between clang and nvcc stem from the different
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
233 compilation models used by clang and nvcc. nvcc uses *split compilation*,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
234 which works roughly as follows:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
235
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
236 * Run a preprocessor over the input ``.cu`` file to split it into two source
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
237 files: ``H``, containing source code for the host, and ``D``, containing
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
238 source code for the device.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
239
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
240 * For each GPU architecture ``arch`` that we're compiling for, do:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
241
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
242 * Compile ``D`` using nvcc proper. The result of this is a ``ptx`` file for
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
243 ``P_arch``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
244
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
245 * Optionally, invoke ``ptxas``, the PTX assembler, to generate a file,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
246 ``S_arch``, containing GPU machine code (SASS) for ``arch``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
247
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
248 * Invoke ``fatbin`` to combine all ``P_arch`` and ``S_arch`` files into a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
249 single "fat binary" file, ``F``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
250
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
251 * Compile ``H`` using an external host compiler (gcc, clang, or whatever you
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
252 like). ``F`` is packaged up into a header file which is force-included into
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
253 ``H``; nvcc generates code that calls into this header to e.g. launch
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
254 kernels.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
255
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
256 clang uses *merged parsing*. This is similar to split compilation, except all
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
257 of the host and device code is present and must be semantically-correct in both
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
258 compilation steps.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
259
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
260 * For each GPU architecture ``arch`` that we're compiling for, do:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
261
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
262 * Compile the input ``.cu`` file for device, using clang. ``__host__`` code
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
263 is parsed and must be semantically correct, even though we're not
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
264 generating code for the host at this time.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
265
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
266 The output of this step is a ``ptx`` file ``P_arch``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
267
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
268 * Invoke ``ptxas`` to generate a SASS file, ``S_arch``. Note that, unlike
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
269 nvcc, clang always generates SASS code.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
270
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
271 * Invoke ``fatbin`` to combine all ``P_arch`` and ``S_arch`` files into a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
272 single fat binary file, ``F``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
273
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
274 * Compile ``H`` using clang. ``__device__`` code is parsed and must be
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
275 semantically correct, even though we're not generating code for the device
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
276 at this time.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
277
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
278 ``F`` is passed to this compilation, and clang includes it in a special ELF
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
279 section, where it can be found by tools like ``cuobjdump``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
280
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
281 (You may ask at this point, why does clang need to parse the input file
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
282 multiple times? Why not parse it just once, and then use the AST to generate
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
283 code for the host and each device architecture?
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
284
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
285 Unfortunately this can't work because we have to define different macros during
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
286 host compilation and during device compilation for each GPU architecture.)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
287
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
288 clang's approach allows it to be highly robust to C++ edge cases, as it doesn't
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
289 need to decide at an early stage which declarations to keep and which to throw
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
290 away. But it has some consequences you should be aware of.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
291
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
292 Overloading Based on ``__host__`` and ``__device__`` Attributes
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
293 ---------------------------------------------------------------
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
294
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
295 Let "H", "D", and "HD" stand for "``__host__`` functions", "``__device__``
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
296 functions", and "``__host__ __device__`` functions", respectively. Functions
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
297 with no attributes behave the same as H.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
298
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
299 nvcc does not allow you to create H and D functions with the same signature:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
300
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
301 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
302
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
303 // nvcc: error - function "foo" has already been defined
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
304 __host__ void foo() {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
305 __device__ void foo() {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
306
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
307 However, nvcc allows you to "overload" H and D functions with different
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
308 signatures:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
309
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
310 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
311
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
312 // nvcc: no error
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
313 __host__ void foo(int) {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
314 __device__ void foo() {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
315
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
316 In clang, the ``__host__`` and ``__device__`` attributes are part of a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
317 function's signature, and so it's legal to have H and D functions with
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
318 (otherwise) the same signature:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
319
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
320 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
321
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
322 // clang: no error
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
323 __host__ void foo() {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
324 __device__ void foo() {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
325
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
326 HD functions cannot be overloaded by H or D functions with the same signature:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
327
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
328 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
329
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
330 // nvcc: error - function "foo" has already been defined
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
331 // clang: error - redefinition of 'foo'
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
332 __host__ __device__ void foo() {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
333 __device__ void foo() {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
334
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
335 // nvcc: no error
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
336 // clang: no error
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
337 __host__ __device__ void bar(int) {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
338 __device__ void bar() {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
339
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
340 When resolving an overloaded function, clang considers the host/device
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
341 attributes of the caller and callee. These are used as a tiebreaker during
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
342 overload resolution. See `IdentifyCUDAPreference
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
343 <http://clang.llvm.org/doxygen/SemaCUDA_8cpp.html>`_ for the full set of rules,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
344 but at a high level they are:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
345
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
346 * D functions prefer to call other Ds. HDs are given lower priority.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
347
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
348 * Similarly, H functions prefer to call other Hs, or ``__global__`` functions
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
349 (with equal priority). HDs are given lower priority.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
350
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
351 * HD functions prefer to call other HDs.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
352
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
353 When compiling for device, HDs will call Ds with lower priority than HD, and
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
354 will call Hs with still lower priority. If it's forced to call an H, the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
355 program is malformed if we emit code for this HD function. We call this the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
356 "wrong-side rule", see example below.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
357
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
358 The rules are symmetrical when compiling for host.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
359
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
360 Some examples:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
361
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
362 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
363
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
364 __host__ void foo();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
365 __device__ void foo();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
366
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
367 __host__ void bar();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
368 __host__ __device__ void bar();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
369
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
370 __host__ void test_host() {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
371 foo(); // calls H overload
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
372 bar(); // calls H overload
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
373 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
374
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
375 __device__ void test_device() {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
376 foo(); // calls D overload
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
377 bar(); // calls HD overload
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
378 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
379
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
380 __host__ __device__ void test_hd() {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
381 foo(); // calls H overload when compiling for host, otherwise D overload
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
382 bar(); // always calls HD overload
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
383 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
384
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
385 Wrong-side rule example:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
386
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
387 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
388
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
389 __host__ void host_only();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
390
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
391 // We don't codegen inline functions unless they're referenced by a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
392 // non-inline function. inline_hd1() is called only from the host side, so
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
393 // does not generate an error. inline_hd2() is called from the device side,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
394 // so it generates an error.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
395 inline __host__ __device__ void inline_hd1() { host_only(); } // no error
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
396 inline __host__ __device__ void inline_hd2() { host_only(); } // error
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
397
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
398 __host__ void host_fn() { inline_hd1(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
399 __device__ void device_fn() { inline_hd2(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
400
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
401 // This function is not inline, so it's always codegen'ed on both the host
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
402 // and the device. Therefore, it generates an error.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
403 __host__ __device__ void not_inline_hd() { host_only(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
404
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
405 For the purposes of the wrong-side rule, templated functions also behave like
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
406 ``inline`` functions: They aren't codegen'ed unless they're instantiated
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
407 (usually as part of the process of invoking them).
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
408
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
409 clang's behavior with respect to the wrong-side rule matches nvcc's, except
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
410 nvcc only emits a warning for ``not_inline_hd``; device code is allowed to call
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
411 ``not_inline_hd``. In its generated code, nvcc may omit ``not_inline_hd``'s
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
412 call to ``host_only`` entirely, or it may try to generate code for
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
413 ``host_only`` on the device. What you get seems to depend on whether or not
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
414 the compiler chooses to inline ``host_only``.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
415
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
416 Member functions, including constructors, may be overloaded using H and D
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
417 attributes. However, destructors cannot be overloaded.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
418
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
419 Using a Different Class on Host/Device
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
420 --------------------------------------
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
421
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
422 Occasionally you may want to have a class with different host/device versions.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
423
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
424 If all of the class's members are the same on the host and device, you can just
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
425 provide overloads for the class's member functions.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
426
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
427 However, if you want your class to have different members on host/device, you
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
428 won't be able to provide working H and D overloads in both classes. In this
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
429 case, clang is likely to be unhappy with you.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
430
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
431 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
432
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
433 #ifdef __CUDA_ARCH__
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
434 struct S {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
435 __device__ void foo() { /* use device_only */ }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
436 int device_only;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
437 };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
438 #else
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
439 struct S {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
440 __host__ void foo() { /* use host_only */ }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
441 double host_only;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
442 };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
443
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
444 __device__ void test() {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
445 S s;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
446 // clang generates an error here, because during host compilation, we
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
447 // have ifdef'ed away the __device__ overload of S::foo(). The __device__
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
448 // overload must be present *even during host compilation*.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
449 S.foo();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
450 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
451 #endif
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
452
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
453 We posit that you don't really want to have classes with different members on H
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
454 and D. For example, if you were to pass one of these as a parameter to a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
455 kernel, it would have a different layout on H and D, so would not work
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
456 properly.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
457
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
458 To make code like this compatible with clang, we recommend you separate it out
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
459 into two classes. If you need to write code that works on both host and
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
460 device, consider writing an overloaded wrapper function that returns different
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
461 types on host and device.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
462
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
463 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
464
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
465 struct HostS { ... };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
466 struct DeviceS { ... };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
467
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
468 __host__ HostS MakeStruct() { return HostS(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
469 __device__ DeviceS MakeStruct() { return DeviceS(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
470
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
471 // Now host and device code can call MakeStruct().
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
472
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
473 Unfortunately, this idiom isn't compatible with nvcc, because it doesn't allow
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
474 you to overload based on the H/D attributes. Here's an idiom that works with
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
475 both clang and nvcc:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
476
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
477 .. code-block:: c++
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
478
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
479 struct HostS { ... };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
480 struct DeviceS { ... };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
481
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
482 #ifdef __NVCC__
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
483 #ifndef __CUDA_ARCH__
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
484 __host__ HostS MakeStruct() { return HostS(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
485 #else
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
486 __device__ DeviceS MakeStruct() { return DeviceS(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
487 #endif
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
488 #else
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
489 __host__ HostS MakeStruct() { return HostS(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
490 __device__ DeviceS MakeStruct() { return DeviceS(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
491 #endif
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
492
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
493 // Now host and device code can call MakeStruct().
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
494
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
495 Hopefully you don't have to do this sort of thing often.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
496
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
497 Optimizations
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
498 =============
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
499
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
500 Modern CPUs and GPUs are architecturally quite different, so code that's fast
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
501 on a CPU isn't necessarily fast on a GPU. We've made a number of changes to
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
502 LLVM to make it generate good GPU code. Among these changes are:
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
503
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
504 * `Straight-line scalar optimizations <https://goo.gl/4Rb9As>`_ -- These
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
505 reduce redundancy within straight-line code.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
506
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
507 * `Aggressive speculative execution
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
508 <http://llvm.org/docs/doxygen/html/SpeculativeExecution_8cpp_source.html>`_
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
509 -- This is mainly for promoting straight-line scalar optimizations, which are
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
510 most effective on code along dominator paths.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
511
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
512 * `Memory space inference
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
513 <http://llvm.org/doxygen/NVPTXInferAddressSpaces_8cpp_source.html>`_ --
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
514 In PTX, we can operate on pointers that are in a paricular "address space"
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
515 (global, shared, constant, or local), or we can operate on pointers in the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
516 "generic" address space, which can point to anything. Operations in a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
517 non-generic address space are faster, but pointers in CUDA are not explicitly
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
518 annotated with their address space, so it's up to LLVM to infer it where
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
519 possible.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
520
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
521 * `Bypassing 64-bit divides
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
522 <http://llvm.org/docs/doxygen/html/BypassSlowDivision_8cpp_source.html>`_ --
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
523 This was an existing optimization that we enabled for the PTX backend.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
524
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
525 64-bit integer divides are much slower than 32-bit ones on NVIDIA GPUs.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
526 Many of the 64-bit divides in our benchmarks have a divisor and dividend
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
527 which fit in 32-bits at runtime. This optimization provides a fast path for
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
528 this common case.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
529
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
530 * Aggressive loop unrooling and function inlining -- Loop unrolling and
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
531 function inlining need to be more aggressive for GPUs than for CPUs because
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
532 control flow transfer in GPU is more expensive. More aggressive unrolling and
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
533 inlining also promote other optimizations, such as constant propagation and
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
534 SROA, which sometimes speed up code by over 10x.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
535
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
536 (Programmers can force unrolling and inline using clang's `loop unrolling pragmas
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
537 <http://clang.llvm.org/docs/AttributeReference.html#pragma-unroll-pragma-nounroll>`_
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
538 and ``__attribute__((always_inline))``.)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
539
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
540 Publication
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
541 ===========
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
542
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
543 The team at Google published a paper in CGO 2016 detailing the optimizations
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
544 they'd made to clang/LLVM. Note that "gpucc" is no longer a meaningful name:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
545 The relevant tools are now just vanilla clang/LLVM.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
546
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
547 | `gpucc: An Open-Source GPGPU Compiler <http://dl.acm.org/citation.cfm?id=2854041>`_
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
548 | Jingyue Wu, Artem Belevich, Eli Bendersky, Mark Heffernan, Chris Leary, Jacques Pienaar, Bjarke Roune, Rob Springer, Xuetian Weng, Robert Hundt
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
549 | *Proceedings of the 2016 International Symposium on Code Generation and Optimization (CGO 2016)*
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
550 |
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
551 | `Slides from the CGO talk <http://wujingyue.com/docs/gpucc-talk.pdf>`_
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
552 |
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
553 | `Tutorial given at CGO <http://wujingyue.com/docs/gpucc-tutorial.pdf>`_
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents:
diff changeset
554
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
555 Obtaining Help
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
556 ==============
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
557
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
558 To obtain help on LLVM in general and its CUDA support, see `the LLVM
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
559 community <http://llvm.org/docs/#mailing-lists>`_.