|
1 | 1 | # CUDA Libraries Samples |
2 | 2 |
|
3 | | -## 6.1 simpleCUBLAS |
| 3 | +> v13.4 note: upstream removed `simpleCUBLAS`, `batchCUBLAS`, `simpleCUFFT`, |
| 4 | +> `conjugateGradient`, and `histEqualizationNPP`. For pure cuBLAS API patterns |
| 5 | +> (`cublasSgemm`, `cublasSgemmBatched`, `cublasLtMatmul`) consult the local |
| 6 | +> `cuda-knowledge` cublas docs (`cublas-docs/2-using-the-cublas-api/`). |
4 | 7 |
|
5 | | -- **Path**: `cpp/4_CUDA_Libraries/simpleCUBLAS/simpleCUBLAS.cpp` |
6 | | -- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/simpleCUBLAS/simpleCUBLAS.cpp> |
7 | | -- **Pattern**: Complete cuBLAS workflow: `cublasCreate` → `cublasSetVector` → `cublasSgemm` → `cublasGetVector` → `cublasDestroy` |
| 8 | +## 6.1 conjugateGradientCudaGraphs |
| 9 | + |
| 10 | +- **Path**: `cpp/4_CUDA_Libraries/conjugateGradientCudaGraphs/conjugateGradientCudaGraphs.cu` |
| 11 | +- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/conjugateGradientCudaGraphs/conjugateGradientCudaGraphs.cu> |
| 12 | +- **Pattern**: Complete Conjugate Gradient solver combining cuBLAS (dot, axpy) + cuSPARSE (`cusparseSpMV`), with the whole CG iteration captured into a CUDA Graph via Stream Capture and replayed — the reference pattern for graph-accelerated numerical solvers |
8 | 13 | - **Arch**: All |
9 | | -- **Lines**: ~253 |
| 14 | +- **Lines**: ~460 |
10 | 15 |
|
11 | 16 | ```cuda |
12 | | -cublasHandle_t handle; cublasCreate(&handle); |
13 | | -cublasSetVector(n2, sizeof(float), h_A, 1, d_A, 1); // upload data |
14 | | -float alpha = 1.0f, beta = 0.0f; |
15 | | -cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, N, N, N, |
16 | | - &alpha, d_A, N, d_B, N, &beta, d_C, N); |
17 | | -cublasGetVector(n2, sizeof(float), d_C, 1, h_C, 1); // download result |
18 | | -cublasDestroy(handle); |
| 17 | +// Capture one full CG iteration (cusparseSpMV + cublasDdot/axpy) into a graph |
| 18 | +cudaStreamBeginCapture(stream1, cudaStreamCaptureModeGlobal); |
| 19 | +cusparseSpMV(cusparseHandle, CUSPARSE_OPERATION_NON_TRANSPOSE, |
| 20 | + &alpha, matA, vecP, &beta, vecAp, ...); // Ap = A * p |
| 21 | +cublasDdot(cublasHandle, N, d_p, 1, d_Ap, 1, &dot); // pAp |
| 22 | +cudaStreamEndCapture(stream1, &graph); |
| 23 | +cudaGraphInstantiate(&graphExec, graph, NULL, NULL, 0); |
| 24 | +// Replay iterations with zero per-launch CPU overhead |
| 25 | +cudaGraphLaunch(graphExec, streamForGraph); |
19 | 26 | ``` |
20 | 27 |
|
21 | | -## 6.2 batchCUBLAS |
| 28 | +## 6.2 conjugateGradientMultiBlockCG |
22 | 29 |
|
23 | | -- **Path**: `cpp/4_CUDA_Libraries/batchCUBLAS/batchCUBLAS.cpp` |
24 | | -- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/batchCUBLAS/batchCUBLAS.cpp> |
25 | | -- **Pattern**: `cublasSgemmBatched` and `cublasSgemmStridedBatched` — batched small-GEMM patterns essential for multi-head attention and ensemble models |
| 30 | +- **Path**: `cpp/4_CUDA_Libraries/conjugateGradientMultiBlockCG/conjugateGradientMultiBlockCG.cu` |
| 31 | +- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/conjugateGradientMultiBlockCG/conjugateGradientMultiBlockCG.cu> |
| 32 | +- **Pattern**: Pure-CUDA Conjugate Gradient with a multi-block (grid-level) dot-product reduction using cooperative groups grid sync — no library calls, shows how to hand-roll the BLAS primitives a CG solver needs |
26 | 33 | - **Arch**: All |
27 | | -- **Lines**: ~350 |
| 34 | +- **Lines**: ~529 |
28 | 35 |
|
29 | 36 | ```cuda |
30 | | -// Pointer array variant |
31 | | -float *d_Aarray[batchCount], *d_Barray[batchCount], *d_Carray[batchCount]; |
32 | | -cublasSgemmBatched(handle, CUBLAS_OP_N, CUBLAS_OP_N, M, N, K, |
33 | | - &alpha, d_Aarray, M, d_Barray, K, &beta, d_Carray, M, batchCount); |
34 | | -// Strided variant (single contiguous buffer) |
35 | | -cublasSgemmStridedBatched(handle, CUBLAS_OP_N, CUBLAS_OP_N, M, N, K, |
36 | | - &alpha, d_A, M, strideA, d_B, K, strideB, &beta, d_C, M, strideC, batchCount); |
| 37 | +// Grid-wide dot product via cooperative groups (multi-block reduction) |
| 38 | +grid_group grid = cg::this_grid(); |
| 39 | +__shared__ float partial[...]; |
| 40 | +// per-block partial reduction, then grid.sync() + final combine kernel |
37 | 41 | ``` |
38 | 42 |
|
39 | | -## 6.3 conjugateGradient |
| 43 | +## 6.3 oceanFFT |
40 | 44 |
|
41 | | -- **Path**: `cpp/4_CUDA_Libraries/conjugateGradient/main.cpp` |
42 | | -- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/conjugateGradient/main.cpp> |
43 | | -- **Pattern**: Complete Conjugate Gradient solver combining CUBLAS (BLAS ops: dot, axpy, gemv) and CUSPARSE (sparse matrix-vector multiply) — the reference pattern for numerical solvers |
| 45 | +- **Path**: `cpp/4_CUDA_Libraries/oceanFFT/oceanFFT.cpp` |
| 46 | +- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/oceanFFT/oceanFFT.cpp> |
| 47 | +- **Pattern**: Frequency-domain ocean simulation via cuFFT 2D: `cufftPlan2d` → forward `cufftExecC2C` → spectrum multiply (dispersion kernel in `oceanFFT_kernel.cu`) → inverse transform, with OpenGL interop for display |
44 | 48 | - **Arch**: All |
45 | | -- **Lines**: ~400 |
| 49 | +- **Lines**: ~903 |
46 | 50 |
|
47 | 51 | ```cuda |
48 | | -// CG iteration combining cuBLAS + cuSPARSE |
49 | | -cublasDdot(handle, N, d_r, 1, d_r, 1, &r1); // residual norm |
50 | | -cusparseSpMV(handle_sp, CUSPARSE_OPERATION_NON_TRANSPOSE, |
51 | | - &alpha, matA, vecX, &beta, vecY, CUDA_R_64F, ...); // Ap = A * p |
52 | | -cublasDaxpy(handle, N, &alpha, d_p, 1, d_x, 1); // x = x + alpha * p |
53 | | -cublasDaxpy(handle, N, &neg_beta, d_p, 1, d_r, 1); // r = r - alpha * Ap |
| 52 | +cufftHandle fftPlan; |
| 53 | +cufftPlan2d(&fftPlan, meshSize, meshSize, CUFFT_C2R); |
| 54 | +cufftExecC2C(fftPlan, h_fftData, h_fftData, CUFFT_FORWARD); // height spectrum |
| 55 | +generateSpectrumKernel<<<...>>>(...); // dispersion multiply |
| 56 | +cufftExecC2C(fftPlan, d_fftData, d_fftData, CUFFT_INVERSE); // back to spatial |
| 57 | +cufftDestroy(fftPlan); |
54 | 58 | ``` |
55 | 59 |
|
56 | | -## 6.4 simpleCUFFT |
| 60 | +## 6.4 cubDeviceFind |
57 | 61 |
|
58 | | -- **Path**: `cpp/4_CUDA_Libraries/simpleCUFFT/simpleCUFFT.cu` |
59 | | -- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/simpleCUFFT/simpleCUFFT.cu> |
60 | | -- **Pattern**: Frequency-domain convolution via cuFFT: forward FFT → complex multiply → inverse FFT. Both simple and advanced plan APIs. |
| 62 | +- **Path**: `cpp/4_CUDA_Libraries/cubDeviceFind/cubDeviceFind.cu` |
| 63 | +- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/cubDeviceFind/cubDeviceFind.cu> |
| 64 | +- **Pattern**: CUB device-wide search primitives introduced in CCCL 3.3 — `cub::DeviceFind::FindIf` (predicate search) and `LowerBound`/`UpperBound` (parallel binary search) — using the standard two-pass temp-storage idiom |
61 | 65 | - **Arch**: All |
62 | | -- **Lines**: ~170 |
| 66 | +- **Lines**: ~248 |
63 | 67 |
|
64 | 68 | ```cuda |
65 | | -cufftHandle plan; cufftPlan1d(&plan, N, CUFFT_C2C, 1); |
66 | | -cufftExecC2C(plan, d_signal, d_signal, CUFFT_FORWARD); // FFT |
67 | | -complex_pointwise_multiply<<<grid, block>>>(d_signal, d_filter, N); |
68 | | -cufftExecC2C(plan, d_signal, d_signal, CUFFT_INVERSE); // IFFT |
| 69 | +// Two-pass CUB idiom: query temp storage, allocate, then run |
| 70 | +std::size_t temp_bytes = 0; |
| 71 | +cub::DeviceFind::FindIf(nullptr, temp_bytes, d_in.begin(), d_out.begin(), predicate, num_items); |
| 72 | +thrust::device_vector<std::uint8_t> temp(temp_bytes); |
| 73 | +cub::DeviceFind::FindIf(thrust::raw_pointer_cast(temp.data()), |
| 74 | + temp_bytes, d_in.begin(), d_out.begin(), predicate, num_items); |
69 | 75 | ``` |
70 | 76 |
|
71 | | -## 6.5 histEqualizationNPP |
| 77 | +## 6.5 cubDeviceSegmentedScan |
72 | 78 |
|
73 | | -- **Path**: `cpp/4_CUDA_Libraries/histEqualizationNPP/histEqualizationNPP.cpp` |
74 | | -- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/histEqualizationNPP/histEqualizationNPP.cpp> |
75 | | -- **Pattern**: GPU-accelerated image histogram equalization using NPP (NVIDIA Performance Primitives) — demonstrates the complete NPP workflow: compute histogram → build cumulative LUT → apply LUT transform |
| 79 | +- **Path**: `cpp/4_CUDA_Libraries/cubDeviceSegmentedScan/cubDeviceSegmentedScan.cu` |
| 80 | +- **URL**: <https://github.com/NVIDIA/cuda-samples/blob/master/cpp/4_CUDA_Libraries/cubDeviceSegmentedScan/cubDeviceSegmentedScan.cu> |
| 81 | +- **Pattern**: `cub::DeviceSegmentedScan::ExclusiveSum` — per-segment prefix sums over offset-delimited ranges, the primitive behind grouped/windowed reductions (segmented softmax, per-row scan) |
76 | 82 | - **Arch**: All |
77 | | -- **Lines**: ~300 |
78 | | - |
79 | | -```cpp |
80 | | -// NPP histogram equalization workflow |
81 | | -nppiHistogramEvenGetBufferSize_8u_C1R_Ctx(oSizeROI, levelCount, &nDeviceBufferSize, nppStreamCtx); |
82 | | -cudaMalloc(&pDeviceBuffer, nDeviceBufferSize); |
83 | | -nppiEvenLevelsHost_32s(levelsHost, levelCount, 0, binCount); |
84 | | -// Compute histogram on GPU |
85 | | -nppiHistogramEven_8u_C1R_Ctx(oDeviceSrc.data(), oDeviceSrc.pitch(), oSizeROI, |
86 | | - histDevice, levelCount, 0, binCount, |
87 | | - pDeviceBuffer, nppStreamCtx); |
88 | | -cudaMemcpy(histHost, histDevice, binCount * sizeof(Npp32s), cudaMemcpyDeviceToHost); |
89 | | -// Build cumulative LUT on host, upload to device, apply |
90 | | -cudaMemcpy(lutDevice, lutHost, sizeof(Npp32s) * levelCount, cudaMemcpyHostToDevice); |
91 | | -nppiLUT_Linear_8u_C1R_Ctx(oDeviceSrc.data(), oDeviceSrc.pitch(), |
92 | | - oDeviceDst.data(), oDeviceDst.pitch(), |
93 | | - oSizeROI, lutDevice, lvlsDevice, nppStreamCtx); |
| 83 | +- **Lines**: ~188 |
| 84 | + |
| 85 | +```cuda |
| 86 | +std::size_t temp_bytes = 0; |
| 87 | +cub::DeviceSegmentedScan::ExclusiveSum(nullptr, temp_bytes, d_in, d_out, |
| 88 | + num_segments, d_offsets, d_offsets + 1, num_items); |
| 89 | +// ... allocate temp, rerun with real pointers |
94 | 90 | ``` |
0 commit comments