forked from bisqwit/cpp_parallelization_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuda_reference.html
More file actions
436 lines (319 loc) · 18.2 KB
/
Copy pathcuda_reference.html
File metadata and controls
436 lines (319 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<!-- From http://www.icl.utk.edu/~mgates3/docs/cuda.html -->
<html>
<head>
<title>CUDA reference</title>
<link href="../style.css" type="text/css" rel="StyleSheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
th, td {
padding: 1px 1em 1px 0; /* trbl */
vertical-align: top;
text-align: left;
}
.right {
text-align: right;
}
code {
white-space: pre;
}
code, pre {
font-size: 9pt;
}
.comment {
color: #0000cc;
font-family: serif;
}
hr {
margin: 1em;
}
</style>
</head>
<body>
<div class="nav">
<a href="../">Home</a>
<a href="./">Docs</a>
</div>
<div class="content">
<h1>CUDA syntax</h1>
<p>Source code is in .cu files, which contain mixture of host (CPU) and device
(GPU) code.</p>
<h2>Declaring functions</h2>
<table>
<tr><td><code>__global__ </code></td> <td>declares kernel, which is called on host and executed on device</td></tr>
<tr><td><code>__device__ </code></td> <td>declares device function, which is called and executed on device</td></tr>
<tr><td><code>__host__ </code></td> <td>declares host function, which is called and executed on host</td></tr>
<tr><td><code>__noinline__ </code></td> <td>to avoid inlining</td></tr>
<tr><td><code>__forceinline__</code></td> <td>to force inlining</td></tr>
</table>
<h2>Declaring variables</h2>
<table>
<tr><td><code>__device__ </code></td> <td>declares device variable in global memory, accessible from all threads, with lifetime of application</td></tr>
<tr><td><code>__constant__</code></td> <td>declares device variable in constant memory, accessible from all threads, with lifetime of application</td></tr>
<tr><td><code>__shared__ </code></td> <td>declares device varibale in block's shared memory, accessible from all threads within a block, with lifetime of block</td></tr>
<tr><td><code>__restrict__</code></td> <td>standard C definition that pointers are not aliased</td></tr>
</table>
<h2>Types</h2>
Most routines return an error code of type <code>cudaError_t</code>.
<h2>Vector types</h2>
<pre>
char1, uchar1, short1, ushort1, int1, uint1, long1, ulong1, float1
char2, uchar2, short2, ushort2, int2, uint2, long2, ulong2, float2
char3, uchar3, short3, ushort3, int3, uint3, long3, ulong3, float3
char4, uchar4, short4, ushort4, int4, uint4, long4, ulong4, float4
longlong1, ulonglong1, double1
longlong2, ulonglong2, double2
dim3
</pre>
Components are accessible as
<code>variable.x, </code>
<code>variable.y, </code>
<code>variable.z, </code>
<code>variable.w. </code><br>
Constructor is <code>make_<type>( x, ... )</code>, for example:
<pre>
float2 xx = make_float2( 1., 2. );
</pre>
dim3 can take 1, 2, or 3 argumetns:
<pre>
dim3 blocks1D( 5 );
dim3 blocks2D( 5, 5 );
dim3 blocks3D( 5, 5, 5 );
</pre>
<h2>Pre-defined variables</h2>
<table>
<tr><td><code>dim3 gridDim </code></td> <td>dimensions of grid </td></tr>
<tr><td><code>dim3 blockDim </code></td> <td>dimensions of block </td></tr>
<tr><td><code>uint3 blockIdx </code></td> <td>block index within grid </td></tr>
<tr><td><code>uint3 threadIdx</code></td> <td>thread index within block</td></tr>
<tr><td><code>int warpSize </code></td> <td>number of threads in warp</td></tr>
</table>
<h2>Kernel invocation</h2>
<pre>
__global__ void kernel( ... ) { ... }
dim3 blocks( nx, ny, nz ); <span class="comment">// cuda 1.x has 1D and 2D grids, cuda 2.x adds 3D grids</span>
dim3 threadsPerBlock( mx, my, mz ); <span class="comment">// cuda 1.x has 1D, 2D, and 3D blocks</span>
kernel<<< blocks, threadsPerBlock >>>( ... );
</pre>
<h2>Thread management</h2>
<table>
<tr><td><code>__threadfence_block(); </code></td> <td>wait until memory accesses are visible to block </td></tr>
<tr><td><code>__threadfence(); </code></td> <td>wait until memory accesses are visible to block and device </td></tr>
<tr><td><code>__threadfence_system();</code></td> <td>wait until memory accesses are visible to block and device and host (2.x)</td></tr>
<tr><td><code>__syncthreads(); </code></td> <td>wait until all threads reach sync </td></tr>
</table>
<h2>Memory management</h2>
<!-- TODO
cudaPitchedPtr
cudaExtent
cudaMallocPitch();
cudaMalloc3D();
cudaMemcpy3D();
-->
<pre>
__device__ float* pointer;
cudaMalloc( (void**) &pointer, size );
cudaFree( pointer );
__constant__ float dev_data[n];
float host_data[n];
cudaMemcpyToSymbol ( dev_data, host_data, sizeof(host_data) ); <span class="comment">// dev_data = host_data</span>
cudaMemcpyFromSymbol( host_data, dev_data, sizeof(host_data) ); <span class="comment">// host_data = dev_data</span>
<span class="comment">// direction is one of <code>cudaMemcpyHostToDevice</code> or <code>cudaMemcpyDeviceToHost</code></span>
cudaMemcpy ( dst_pointer, src_pointer, size, direction );
cudaMemcpyAsync( dst_pointer, src_pointer, size, direction, stream );
<span class="comment">// using column-wise notation</span>
<span class="comment">// (the CUDA docs describe it for images; a “row” there equals a matrix column)</span>
<span class="comment">// _bytes indicates arguments that must be specified in bytes</span>
cudaMemcpy2D ( A_dst, lda_bytes, B_src, ldb_bytes, m_bytes, n, direction );
cudaMemcpy2DAsync( A_dst, lda_bytes, B_src, ldb_bytes, m_bytes, n, direction, stream );
<span class="comment">// cublas makes copies easier for matrices, e.g., less use of sizeof</span>
<span class="comment">// copy x => y</span>
cublasSetVector ( n, elemSize, x_src_host, incx, y_dst_dev, incy );
cublasGetVector ( n, elemSize, x_src_dev, incx, y_dst_host, incy );
cublasSetVectorAsync( n, elemSize, x_src_host, incx, y_dst_dev, incy, stream );
cublasGetVectorAsync( n, elemSize, x_src_dev, incx, y_dst_host, incy, stream );
<span class="comment">// copy A => B</span>
cublasSetMatrix ( rows, cols, elemSize, A_src_host, lda, B_dst_dev, ldb );
cublasGetMatrix ( rows, cols, elemSize, A_src_dev, lda, B_dst_host, ldb );
cublasSetMatrixAsync( rows, cols, elemSize, A_src_host, lda, B_dst_dev, ldb, stream );
cublasGetMatrixAsync( rows, cols, elemSize, A_src_dev, lda, B_dst_host, ldb, stream );
</pre>
Also, <code>malloc</code> and <code>free</code> work inside a kernel (2.x), but
memory allocated in a kernel must be deallocated in a kernel (not the host). It
can be freed in a different kernel, though.
<h2>Atomic functions</h2>
<pre>
old = atomicAdd ( &addr, value ); <span class="comment">// old = *addr; *addr += value</span>
old = atomicSub ( &addr, value ); <span class="comment">// old = *addr; *addr –= value</span>
old = atomicExch( &addr, value ); <span class="comment">// old = *addr; *addr = value</span>
old = atomicMin ( &addr, value ); <span class="comment">// old = *addr; *addr = min( old, value )</span>
old = atomicMax ( &addr, value ); <span class="comment">// old = *addr; *addr = max( old, value )</span>
<span class="comment">// increment up to value, then reset to 0 </span>
<span class="comment">// decrement down to 0, then reset to value</span>
old = atomicInc ( &addr, value ); <span class="comment">// old = *addr; *addr = ((old >= value) ? 0 : old+1 )</span>
old = atomicDec ( &addr, value ); <span class="comment">// old = *addr; *addr = ((old == 0) or (old > val) ? val : old–1 )</span>
old = atomicAnd ( &addr, value ); <span class="comment">// old = *addr; *addr &= value</span>
old = atomicOr ( &addr, value ); <span class="comment">// old = *addr; *addr |= value</span>
old = atomicXor ( &addr, value ); <span class="comment">// old = *addr; *addr ^= value</span>
<span class="comment">// compare-and-store</span>
old = atomicCAS ( &addr, compare, value ); <span class="comment">// old = *addr; *addr = ((old == compare) ? value : old)</span>
</pre>
<h2>Warp vote</h2>
<pre>
int __all ( predicate );
int __any ( predicate );
int __ballot( predicate ); <span class="comment">// nth thread sets nth bit to predicate</span>
</pre>
<h2>Timer</h2>
wall clock cycle counter
<pre>
clock_t clock();
</pre>
<h2>Texture</h2>
can also return float2 or float4, depending on texRef.
<pre>
<span class="comment">// integer index</span>
float tex1Dfetch( texRef, ix );
<span class="comment">// float index</span>
float tex1D( texRef, x );
float tex2D( texRef, x, y );
float tex3D( texRef, x, y, z );
float tex1DLayered( texRef, x );
float tex2DLayered( texRef, x, y );
</pre>
<hr>
<h2>Low-level Driver API</h2>
<pre>
#include <cuda.h>
CUdevice dev;
CUdevprop properties;
char name[n];
int major, minor;
size_t bytes;
cuInit( 0 ); <span class="comment">// takes flags for future use</span>
cuDeviceGetCount ( &cnt );
cuDeviceGet ( &dev, index );
cuDeviceGetName ( name, sizeof(name), dev );
cuDeviceComputeCapability( &major, &minor, dev );
cuDeviceTotalMem ( &bytes, dev );
cuDeviceGetProperties ( &properties, dev ); <span class="comment">// max threads, etc.</span>
</pre>
<hr>
<h2>cuBLAS</h2>
Matrices are column-major. Indices are 1-based; this affects result of
i<t>amax and i<t>amin.
<pre>
#include <cublas_v2.h>
cublasHandle_t handle;
cudaStream_t stream;
cublasCreate( &handle );
cublasDestroy( handle );
cublasGetVersion( handle, &version );
cublasSetStream( handle, stream );
cublasGetStream( handle, &stream );
cublasSetPointerMode( handle, mode );
cublasGetPointerMode( handle, &mode );
</pre>
<h3>Constants</h3>
<table>
<tr><th>argument </th> <th>constants </th> <th>description (Fortran letter) </th></tr>
<tr><td><code>trans</code></td> <td><code>CUBLAS_OP_N </code></td> <td>non-transposed ('N') </td></tr>
<tr><td> </td> <td><code>CUBLAS_OP_T </code></td> <td>transposed ('T') </td></tr>
<tr><td> </td> <td><code>CUBLAS_OP_C </code></td> <td>conjugate transposed ('C') </td></tr>
<tr><td> </td></tr>
<tr><td><code>uplo</code></td> <td><code>CUBLAS_FILL_MODE_LOWER </code></td> <td>lower part filled ('L') </td></tr>
<tr><td> </td> <td><code>CUBLAS_FILL_MODE_UPPER </code></td> <td>upper part filled ('U') </td></tr>
<tr><td> </td></tr>
<tr><td><code>side</code></td> <td><code>CUBLAS_SIDE_LEFT </code></td> <td>matrix on left ('L') </td></tr>
<tr><td> </td> <td><code>CUBLAS_SIDE_RIGHT </code></td> <td>matrix on right ('R') </td></tr>
<tr><td> </td></tr>
<tr><td><code>mode</code></td> <td><code>CUBLAS_POINTER_MODE_HOST </code></td> <td>alpha and beta scalars passed on host </td></tr>
<tr><td> </td> <td><code>CUBLAS_POINTER_MODE_DEVICE</code></td> <td>alpha and beta scalars passed on device</td></tr>
</table>
<p>BLAS functions have <code>cublas</code> prefix and first letter of usual BLAS
function name is capitalized. Arguments are the same as standard BLAS, with
these exceptions:</p>
<ul>
<li>All functions add handle as first argument.
<li>All functions return cublasStatus_t error code.
<li>Constants alpha and beta are passed by pointer. All other scalars (n, incx, etc.) are bassed by value.
<li>Functions that return a value, such as ddot, add result as last argument, and save value to result.
<li>Constants are given in table above, instead of using characters.
</ul>
<p>Examples:</p>
<pre>
cublasDdot ( handle, n, x, incx, y, incy, &result ); // result = ddot( n, x, incx, y, incy );
cublasDaxpy( handle, n, &alpha, x, incx, y, incy ); // daxpy( n, alpha, x, incx, y, incy );
</pre>
<hr>
<h2>Compiler</h2>
<p><code>nvcc</code>, often found in <code>/usr/local/cuda/bin</code></p>
<p>Defines <code>__CUDACC__</code></p>
<h3>Flags common with cc</h3>
<table>
<tr><th>Short flag </th> <th>Long flag </th> <th>Output or Description </th></tr>
<tr><td><code>-c</code> </td> <td><code>--compile</code> </td> <td>.o object file </td></tr>
<tr><td><code>-E</code> </td> <td><code>--preprocess</code> </td> <td>on standard output </td></tr>
<tr><td><code>-M</code> </td> <td><code>--generate-dependencies</code> </td> <td>on standard output </td></tr>
<tr><td><code>-o <i>file</i></code> </td> <td><code>--output-file <i>file</i></code> </td></tr>
<tr><td><code>-I <i>directory</i></code></td> <td><code>--include-path <i>directory</i></code></td> <td>header search path </td></tr>
<tr><td><code>-L <i>directory</i></code></td> <td><code>--library-path <i>directory</i></code></td> <td>library search path </td></tr>
<tr><td><code>-l <i>lib</i></code> </td> <td><code>--library <i>lib</i></code> </td> <td>link with library </td></tr>
<tr><td><code>-lib</code> </td> <td><code></code> </td> <td>generate library </td></tr>
<tr><td><code>-shared</code> </td> <td><code></code> </td> <td>generate shared library</td></tr>
<tr><td><code>-pg</code> </td> <td><code>--profile</code> </td> <td>for gprof </td></tr>
<tr><td><code>-g <i>level</i></code> </td> <td><code>--debug <i>level</i></code> </td></tr>
<tr><td><code>-G</code> </td> <td><code>--device-debug</code> </td></tr>
<tr><td><code>-O <i>level</i></code> </td> <td><code>--optimize <i>level</i></code> </td></tr>
<tr><td> </td>
<tr><th colspan="3">Undocumented (but in sample makefiles)</th></tr>
<tr><td><code>-m32</code> </td> <td> </td> <td>compile 32-bit i386 host CPU code</td></tr>
<tr><td><code>-m64</code> </td> <td> </td> <td>compile 64-bit x86_64 host CPU code</td></tr>
</table>
<h3>Flags specific to nvcc</h3>
<table>
<tr><td><code>-v</code> </td> <td>list compilation commands as they are executed </td></tr>
<tr><td><code>-dryrun</code> </td> <td>list compilation commands, without executing </td></tr>
<tr><td><code>-keep</code> </td> <td>saves intermediate files (e.g., pre-processed) for debugging </td></tr>
<tr><td><code>-clean</code> </td> <td>removes output files (with same exact compiler options) </td></tr>
<tr><td><code>-arch=<compute_xy></code> </td> <td>generate PTX for capability x.y </td></tr>
<tr><td><code>-code=<sm_xy></code> </td> <td>generate binary for capability x.y, by default same as <code>-arch</code></td></tr>
<tr><td><code>-gencode arch=...,code=...</code></td> <td>same as <code>-arch</code> and <code>-code</code>, but may be repeated </td></tr>
</table>
<h3>Argumenents for <code>-arch</code> and <code>-code</code></h3>
<p>It makes most sense (to me) to give <code>-arch</code> a virtual architecture
and <code>-code</code> a real architecture, though both flags accept both
virtual and real architectures (at times).</p>
<table>
<tr><th> </th> <th>Virtual architecture </th> <th>Real architecture </td> <th>Features </th></tr>
<tr><th>Tesla</th> <td><code>compute_10</code></td> <td><code>sm_10</code></td> <td>Basic features </td></tr>
<tr><th> </th> <td><code>compute_11</code></td> <td><code>sm_11</code></td> <td>+ atomic memory ops on global memory</td></tr>
<tr><th> </th> <td><code>compute_12</code></td> <td><code>sm_12</code></td> <td>+ atomic memory ops on shared memory<br>
+ vote instructions </td></tr>
<tr><th> </th> <td><code>compute_13</code></td> <td><code>sm_13</code></td> <td>+ double precision </td></tr>
<tr><th>Fermi</th> <td><code>compute_20</code></td> <td><code>sm_20</code></td> <td>+ Fermi </td></tr>
<tr></tr>
</table>
<hr>
<h2>Some hardware constraints</h2>
<table>
<tr><th> </th> <th class="right"> 1.x</th> <th class="right"> 2.x</th></tr>
<tr><td>max x- or y-dimension of block</td> <td class="right"> 512</td> <td class="right"> 1024</td></tr>
<tr><td>max z-dimension of block </td> <td class="right"> 64</td> <td class="right"> 64</td></tr>
<tr><td>max threads per block </td> <td class="right"> 512</td> <td class="right"> 1024</td></tr>
<tr><td>warp size </td> <td class="right"> 32</td> <td class="right"> 32</td></tr>
<tr><td>max blocks per MP </td> <td class="right"> 8</td> <td class="right"> 8</td></tr>
<tr><td>max warps per MP </td> <td class="right"> 32</td> <td class="right"> 48</td></tr>
<tr><td>max threads per MP </td> <td class="right"> 1024</td> <td class="right"> 1536</td></tr>
<tr><td>max 32-bit registers per MP </td> <td class="right"> 16k</td> <td class="right"> 32k</td></tr>
<tr><td>max shared memory per MP </td> <td class="right"> 16 KB</td> <td class="right"> 48 KB</td></tr>
<tr><td>shared memory banks </td> <td class="right"> 16</td> <td class="right"> 32</td></tr>
<tr><td>local memory per thread </td> <td class="right"> 16 KB</td> <td class="right">512 KB</td></tr>
<tr><td>const memory </td> <td class="right"> 64 KB</td> <td class="right"> 64 KB</td></tr>
<tr><td>const cache </td> <td class="right"> 8 KB</td> <td class="right"> 8 KB</td></tr>
<tr><td>texture cache </td> <td class="right"> 8 KB</td> <td class="right"> 8 KB</td></tr>
</table>
</div>
</body>
</html>