mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-11-18 01:48:16 +08:00
Compare commits
254 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db3eea2394 | ||
|
|
04f3686bbe | ||
|
|
0e87b76137 | ||
|
|
fb6fcb0110 | ||
|
|
14b8ceb56f | ||
|
|
a6737fd72b | ||
|
|
96901090bc | ||
|
|
8b2b51e2a5 | ||
|
|
dcce6e1f14 | ||
|
|
cc3d94f81c | ||
|
|
7ee6db679e | ||
|
|
24a617c043 | ||
|
|
93f1c78c5b | ||
|
|
5932d18738 | ||
|
|
5206607816 | ||
|
|
f8f3e1b347 | ||
|
|
4fcfa82d7d | ||
|
|
7a9bd38ecc | ||
|
|
9582bb03a9 | ||
|
|
11bc856cad | ||
|
|
95308ffc23 | ||
|
|
56852fbb50 | ||
|
|
152ba4b941 | ||
|
|
a8138b60fe | ||
|
|
c7bfd4418b | ||
|
|
eb378b2fce | ||
|
|
fd5674f9e5 | ||
|
|
bb2aebaad8 | ||
|
|
2539826a99 | ||
|
|
1eb3c947c3 | ||
|
|
9d03b030a6 | ||
|
|
78f83ca02d | ||
|
|
e8a041d783 | ||
|
|
c631850c15 | ||
|
|
9539ca5fa3 | ||
|
|
0f4bdfad99 | ||
|
|
e3a5ae4aca | ||
|
|
30b411ad56 | ||
|
|
941670671c | ||
|
|
be5012ef69 | ||
|
|
9824a63101 | ||
|
|
5f6e6072d9 | ||
|
|
a80688012e | ||
|
|
aad89bb0ab | ||
|
|
ab0b386ac4 | ||
|
|
5cffc8815a | ||
|
|
27c0a166f3 | ||
|
|
08fae276b4 | ||
|
|
0f697f1819 | ||
|
|
b203467419 | ||
|
|
41d5f63ec5 | ||
|
|
3de3f2f46c | ||
|
|
95128a1cf3 | ||
|
|
3848a7c63c | ||
|
|
d9633314f8 | ||
|
|
576c24f97e | ||
|
|
970a6a1151 | ||
|
|
76e2d2052c | ||
|
|
03a7300081 | ||
|
|
ccb341a5e3 | ||
|
|
421e49450f | ||
|
|
886860a123 | ||
|
|
9a454d3ba9 | ||
|
|
1a466282da | ||
|
|
545194e7aa | ||
|
|
e2d3c20bd3 | ||
|
|
48e70c8b31 | ||
|
|
b518bfe9be | ||
|
|
e633580eff | ||
|
|
df9ea79df4 | ||
|
|
eacf41c980 | ||
|
|
f8fbd04007 | ||
|
|
01956cfecc | ||
|
|
380c065a0c | ||
|
|
6c548b5d43 | ||
|
|
11c70243db | ||
|
|
415ad05c98 | ||
|
|
2d0314212b | ||
|
|
feffc60cbf | ||
|
|
d3ded4a251 | ||
|
|
62b96a65b5 | ||
|
|
450038ea73 | ||
|
|
ce045e2ae9 | ||
|
|
f753e86e7a | ||
|
|
a1cf9e4183 | ||
|
|
e8492d1a78 | ||
|
|
11355321a0 | ||
|
|
3532ede709 | ||
|
|
8f1d565faa | ||
|
|
f00f52d154 | ||
|
|
dc24cde377 | ||
|
|
2c5c6dc7d4 | ||
|
|
3fc438428a | ||
|
|
5409227cf8 | ||
|
|
e36545ac1c | ||
|
|
41a65b94c8 | ||
|
|
89a84131fb | ||
|
|
bb121fe02b | ||
|
|
e228ad9389 | ||
|
|
1e0c660f22 | ||
|
|
e52cdee6c4 | ||
|
|
9045dd3a1a | ||
|
|
3e7bb5f2e8 | ||
|
|
88e5f4a395 | ||
|
|
5c9e573ece | ||
|
|
c08a2f31ff | ||
|
|
6fe4a6bab8 | ||
|
|
66631e4f96 | ||
|
|
7e1a257265 | ||
|
|
d9efeae3bb | ||
|
|
9c4287fc51 | ||
|
|
8d06d246b3 | ||
|
|
1292881b37 | ||
|
|
a55d6682d2 | ||
|
|
f9dd4323af | ||
|
|
fcd39008ec | ||
|
|
01b7ee41f6 | ||
|
|
25b33d2d04 | ||
|
|
bfd956bc5e | ||
|
|
0f5821a8c8 | ||
|
|
c1301d000a | ||
|
|
7f5859dda9 | ||
|
|
abb97e1dfb | ||
|
|
22bedd5cf0 | ||
|
|
d54d4d7419 | ||
|
|
03719b7623 | ||
|
|
11fc617794 | ||
|
|
7d7e0777a5 | ||
|
|
a60b4a984e | ||
|
|
7b0068a433 | ||
|
|
2ad2272e3d | ||
|
|
cbfab74480 | ||
|
|
00999c1789 | ||
|
|
090f957854 | ||
|
|
ea694a4b0d | ||
|
|
281daef279 | ||
|
|
23928df4ff | ||
|
|
8d2e39c395 | ||
|
|
1bb070deba | ||
|
|
07e5fc5473 | ||
|
|
de204853cf | ||
|
|
3f0693b37e | ||
|
|
4fc7cdb95c | ||
|
|
ba77e8d23b | ||
|
|
10e9b975c4 | ||
|
|
8b5c84cd22 | ||
|
|
6929eea2ed | ||
|
|
cd485da765 | ||
|
|
223e658beb | ||
|
|
c9794ff283 | ||
|
|
035dcfd357 | ||
|
|
37922e6429 | ||
|
|
05d2f991de | ||
|
|
b6f3065605 | ||
|
|
461fc3c649 | ||
|
|
b1837c0e4e | ||
|
|
7fde420160 | ||
|
|
0d161038a2 | ||
|
|
ee8ff3cf5b | ||
|
|
7568673fa6 | ||
|
|
4543e7bbab | ||
|
|
a3be0d3cd8 | ||
|
|
89f2e5c0c3 | ||
|
|
f93a9ab81c | ||
|
|
fb1eaa8323 | ||
|
|
2f826e305a | ||
|
|
9bebdf7ef4 | ||
|
|
6fd8228242 | ||
|
|
76210c84f7 | ||
|
|
62d32b38d7 | ||
|
|
dd73281bc6 | ||
|
|
df5ff58ca4 | ||
|
|
c7fe3b2f4c | ||
|
|
cd51392c0f | ||
|
|
0eaf6d4198 | ||
|
|
551f0dbebe | ||
|
|
29bde74c86 | ||
|
|
a60926da2c | ||
|
|
039e445f93 | ||
|
|
3fd2fbce00 | ||
|
|
b284c28ce5 | ||
|
|
74107d2da1 | ||
|
|
12d096790b | ||
|
|
5fab4d73f5 | ||
|
|
8a2c175abc | ||
|
|
0dc8ecc1f6 | ||
|
|
a14d734664 | ||
|
|
56ad17f97c | ||
|
|
7013a0b70a | ||
|
|
a461e61485 | ||
|
|
769a225af3 | ||
|
|
53a02af443 | ||
|
|
9b70771583 | ||
|
|
7ce058b479 | ||
|
|
8663c8bf19 | ||
|
|
2dfdd55e29 | ||
|
|
c52cfc339f | ||
|
|
bcf4a0dd31 | ||
|
|
9efe753a74 | ||
|
|
2dee482699 | ||
|
|
29a2098575 | ||
|
|
a0774d28f1 | ||
|
|
190d0cbaa7 | ||
|
|
e53fcad712 | ||
|
|
d06e42bf06 | ||
|
|
fd2c269ce3 | ||
|
|
a1cd67ca87 | ||
|
|
9a207a910a | ||
|
|
2a4d019282 | ||
|
|
8e03cbfcf9 | ||
|
|
9d8f61431e | ||
|
|
ff264a798f | ||
|
|
274836a1a2 | ||
|
|
c15a0c4bbf | ||
|
|
912f37963a | ||
|
|
82bcada84c | ||
|
|
9c688d7ff7 | ||
|
|
5f97d7d0df | ||
|
|
3559ca4d08 | ||
|
|
cd3bc1fa8e | ||
|
|
e8568c4173 | ||
|
|
b5c84e6996 | ||
|
|
c46754b877 | ||
|
|
03309a2d42 | ||
|
|
5688ee0013 | ||
|
|
8004ad59ab | ||
|
|
e612904184 | ||
|
|
81cf058e30 | ||
|
|
26665bf33b | ||
|
|
00bb9bc367 | ||
|
|
e4789153d5 | ||
|
|
1c2efac7c8 | ||
|
|
3d553b2ea1 | ||
|
|
9316529638 | ||
|
|
2b689228b7 | ||
|
|
81992093d2 | ||
|
|
b312abaa07 | ||
|
|
8f21b899b6 | ||
|
|
0cbe5f2d82 | ||
|
|
805e60bdfc | ||
|
|
9d4c014f60 | ||
|
|
bf8c6dd043 | ||
|
|
2e41896e1b | ||
|
|
11de19f00c | ||
|
|
471dd47f84 | ||
|
|
0563025cde | ||
|
|
3a05f29b94 | ||
|
|
e64c65a0d3 | ||
|
|
af0e1af181 | ||
|
|
01789304f0 | ||
|
|
1f76a2d110 | ||
|
|
3342d604fe | ||
|
|
ba04faaf73 | ||
|
|
8789eb6266 |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
build
|
||||||
|
.vs
|
||||||
|
.clangd
|
||||||
187
CHANGELOG.md
Normal file
187
CHANGELOG.md
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
## Changelog
|
||||||
|
|
||||||
|
### CUDA 12.8
|
||||||
|
* Updated build system across the repository to CMake. Removed Visual Studio project files and Makefiles.
|
||||||
|
* Removed the following outdated samples:
|
||||||
|
* `0_Introduction`
|
||||||
|
* `c++11_cuda` demonstrating CUDA and C++ 11 interoperability (reason: obsolete)
|
||||||
|
* `concurrentKernels` demonstrating the ability to run multiple kernels simultaneously (reason: obsolete)
|
||||||
|
* `cppIntegration` demonstrating calling between .cu and .cpp files (reason: obsolete)
|
||||||
|
* `cppOverload` demonstrating C++ function overloading (reason: obsolete)
|
||||||
|
* `simpleSeparateCompilation` demonstrating NVCC compilation to a static library (reason: trivial)
|
||||||
|
* `simpleTemplates_nvrtc` demonstrating NVRTC usage for `simpleTemplates` sample (reason: redundant)
|
||||||
|
* `simpleVoteIntrinsics_nvrtc` demonstrating NVRTC usage for `simpleVoteIntrinsics` sample (reason: redundant)
|
||||||
|
* `2_Concepts_and_Techniques`
|
||||||
|
* `cuHook` demonstrating dlsym hooks. (reason: incompatible with modern `glibc`)
|
||||||
|
* `4_CUDA_Libraries`
|
||||||
|
* `batchedLabelMarkersAndLabelCompressionNPP` demonstrating NPP features (reason: some functionality removed from library)
|
||||||
|
* `5_Domain_Specific`
|
||||||
|
* Legacy Direct3D 9 and 10 interoperability samples:
|
||||||
|
* `fluidsD3D9`
|
||||||
|
* `simpleD3D10`
|
||||||
|
* `simpleD3D10RenderTarget`
|
||||||
|
* `simpleD3D10Texture`
|
||||||
|
* `simpleD3D9`
|
||||||
|
* `simpleD3D9Texture`
|
||||||
|
* `SLID3D10Texture`
|
||||||
|
* `VFlockingD3D10`
|
||||||
|
* `8_Platform_Specific/Tegra`
|
||||||
|
* Temporarily removed the following two samples pending updates:
|
||||||
|
* `nbody_screen` demonstrating the nbody sample in QNX
|
||||||
|
* `simpleGLES_screen` demonstrating GLES interop in QNX
|
||||||
|
* Moved the following Tegra-specific samples to a dedicated subdirectory: `8_Platform_Specific/Tegra`
|
||||||
|
* `EGLSync_CUDAEvent_Interop`
|
||||||
|
* `cuDLAErrorReporting`
|
||||||
|
* `cuDLAHybridMode`
|
||||||
|
* `cuDLALayerwiseStatsHybrid`
|
||||||
|
* `cuDLALayerwiseStatsStandalone`
|
||||||
|
* `cuDLAStandaloneMode`
|
||||||
|
* `cudaNvSciNvMedia`
|
||||||
|
* `fluidsGLES`
|
||||||
|
* `nbody_opengles`
|
||||||
|
* `simpleGLES`
|
||||||
|
* `simpleGLES_EGLOutput`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### CUDA 12.5
|
||||||
|
|
||||||
|
### CUDA 12.4
|
||||||
|
* Added graphConditionalNodes Sample
|
||||||
|
|
||||||
|
### CUDA 12.3
|
||||||
|
* Added cuDLA samples
|
||||||
|
* Fixed jitLto regression
|
||||||
|
|
||||||
|
### CUDA 12.2
|
||||||
|
* libNVVM samples received updates
|
||||||
|
* Fixed jitLto Case issues
|
||||||
|
* Enabled HOST_COMPILER flag to the makefiles for GCC which is untested but may still work.
|
||||||
|
|
||||||
|
### CUDA 12.1
|
||||||
|
* Added new sample for Large Kernels
|
||||||
|
|
||||||
|
### CUDA 12.0
|
||||||
|
* Added new flags for JIT compiling
|
||||||
|
* Removed deprecated APIs in Hopper Architecture
|
||||||
|
|
||||||
|
### CUDA 11.6
|
||||||
|
* Added new folder structure for samples
|
||||||
|
* Added support of Visual Studio 2022 to all samples supported on [Windows](#windows-1).
|
||||||
|
* All CUDA samples are now only available on [GitHub](https://github.com/nvidia/cuda-samples). They are no longer available via CUDA toolkit.
|
||||||
|
|
||||||
|
### CUDA 11.5
|
||||||
|
* Added `cuDLAHybridMode`. Demonstrate usage of cuDLA in hybrid mode.
|
||||||
|
* Added `cuDLAStandaloneMode`. Demonstrate usage of cuDLA in standalone mode.
|
||||||
|
* Added `cuDLAErrorReporting`. Demonstrate DLA error detection via CUDA.
|
||||||
|
* Added `graphMemoryNodes`. Demonstrates memory allocations and frees within CUDA graphs using Graph APIs and Stream Capture APIs.
|
||||||
|
* Added `graphMemoryFootprint`. Demonstrates how graph memory nodes re-use virtual addresses and physical memory.
|
||||||
|
* All samples from CUDA toolkit are now available on [GitHub](https://github.com/nvidia/cuda-samples).
|
||||||
|
|
||||||
|
### CUDA 11.4 update 1
|
||||||
|
* Added support for VS Code on linux platform.
|
||||||
|
|
||||||
|
### CUDA 11.4
|
||||||
|
* Added `cdpQuadtree`. Demonstrates Quad Trees implementation using CUDA Dynamic Parallelism.
|
||||||
|
* Updated `simpleVulkan`, `simpleVulkanMMAP` and `vulkanImageCUDA`. Demonstrates use of SPIR-V shaders.
|
||||||
|
|
||||||
|
### CUDA 11.3
|
||||||
|
* Added `streamOrderedAllocationIPC`. Demonstrates Inter Process Communication using one process per GPU for computation.
|
||||||
|
* Added `simpleCUBLAS_LU`. Demonstrates batched matrix LU decomposition using cuBLAS API `cublas<t>getrfBatched()`
|
||||||
|
* Updated `simpleVulkan`. Demonstrates use of timeline semaphore.
|
||||||
|
* Updated multiple samples to use pinned memory using `cudaMallocHost()`.
|
||||||
|
|
||||||
|
### CUDA 11.2
|
||||||
|
* Added `streamOrderedAllocation`. Demonstrates stream ordered memory allocation on a GPU using cudaMallocAsync and cudaMemPool family of APIs.
|
||||||
|
* Added `streamOrderedAllocationP2P`. Demonstrates peer-to-peer access of stream ordered memory allocated using cudaMallocAsync and cudaMemPool family of APIs.
|
||||||
|
* Dropped Visual Studio 2015 support from all the windows supported samples.
|
||||||
|
* FreeImage is no longer distributed with the CUDA Samples. On Windows, see the [Dependencies](./README.md#freeimage) section for more details on how to set up FreeImage. On Linux, it is recommended to install FreeImage with your distribution's package manager.
|
||||||
|
* All the samples using CUDA Pipeline & Arrive-wait barriers are been updated to use new `cuda::pipeline` and `cuda::barrier` interfaces.
|
||||||
|
* Updated all the samples to build with parallel build option `--threads` of `nvcc` cuda compiler.
|
||||||
|
* Added `cudaNvSciNvMedia`. Demonstrates CUDA-NvMedia interop via NvSciBuf/NvSciSync APIs.
|
||||||
|
* Added `simpleGL`. Demonstrates interoperability between CUDA and OpenGL.
|
||||||
|
|
||||||
|
### CUDA 11.1
|
||||||
|
* Added `watershedSegmentationNPP`. Demonstrates how to use the NPP watershed segmentation function.
|
||||||
|
* Added `batchedLabelMarkersAndLabelCompressionNPP`. Demonstrates how to use the NPP label markers generation and label compression functions based on a Union Find (UF) algorithm including both single image and batched image versions.
|
||||||
|
* Dropped Visual Studio 2012, 2013 support from all the windows supported samples.
|
||||||
|
* Added kernel performing warp aggregated atomic max in multi buckets using cg::labeled_partition & cg::reduce in `warpAggregatedAtomicsCG`.
|
||||||
|
* Added extended CG shuffle mechanics to `shfl_scan` sample.
|
||||||
|
* Added `cudaOpenMP`. Demonstrates how to use OpenMP API to write an application for multiple GPUs.
|
||||||
|
* Added `simpleZeroCopy`. Demonstrates how to use zero copy, kernels can read and write directly to pinned system memory.
|
||||||
|
|
||||||
|
### CUDA 11.0
|
||||||
|
* Added `dmmaTensorCoreGemm`. Demonstrates double precision GEMM computation using the Double precision Warp Matrix Multiply and Accumulate (WMMA) API introduced with CUDA 11 in Ampere chip family tensor cores.
|
||||||
|
* Added `bf16TensorCoreGemm`. Demonstrates __nv_bfloat16 (e8m7) GEMM computation using the __nv_bfloat16 WMMA API introduced with CUDA 11 in Ampere chip family tensor cores.
|
||||||
|
* Added `tf32TensorCoreGemm`. Demonstrates tf32 (e8m10) GEMM computation using the tf32 WMMA API introduced with CUDA 11 in Ampere chip family tensor cores.
|
||||||
|
* Added `globalToShmemAsyncCopy`. Demonstrates async copy of data from global to shared memory when on compute capability 8.0 or higher. Also demonstrates arrive-wait barrier for synchronization.
|
||||||
|
* Added `simpleAWBarrier`. Demonstrates arrive wait barriers.
|
||||||
|
* Added `simpleAttributes`. Demonstrates the stream attributes that affect L2 locality.
|
||||||
|
* Added warp aggregated atomic multi bucket increments kernel using labeled_partition cooperative groups in `warpAggregatedAtomicsCG` which can be used on compute capability 7.0 and above GPU architectures.
|
||||||
|
* Added `binaryPartitionCG`. Demonstrates binary partition cooperative groups and reduction within the thread block.
|
||||||
|
* Added two new reduction kernels in `reduction` one which demonstrates reduce_add_sync intrinstic supported on compute capability 8.0 and another which uses cooperative_groups::reduce function which does thread_block_tile level reduction introduced from CUDA 11.0.
|
||||||
|
* Added `cudaCompressibleMemory`. Demonstrates compressible memory allocation using cuMemMap API.
|
||||||
|
* Added `simpleVulkanMMAP`. Demonstrates Vulkan CUDA Interop via cuMemMap APIs.
|
||||||
|
* Added `concurrentKernels`. Demonstrates the use of CUDA streams for concurrent execution of several kernels on a GPU.
|
||||||
|
* Dropped Mac OSX support from all samples.
|
||||||
|
|
||||||
|
### CUDA 10.2
|
||||||
|
* Added `simpleD3D11`. Demonstrates CUDA-D3D11 External Resource Interoperability APIs for updating D3D11 buffers from CUDA and synchronization between D3D11 and CUDA with Keyed Mutexes.
|
||||||
|
* Added `simpleDrvRuntime`. Demonstrates CUDA Driver and Runtime APIs working together to load fatbinary of a CUDA kernel.
|
||||||
|
* Added `vectorAddMMAP`. Demonstrates how cuMemMap API allows the user to specify the physical properties of their memory while retaining the contiguous nature of their access.
|
||||||
|
* Added `memMapIPCDrv`. Demonstrates Inter Process Communication using cuMemMap APIs.
|
||||||
|
* Added `cudaNvSci`. Demonstrates CUDA-NvSciBuf/NvSciSync Interop.
|
||||||
|
* Added `jacobiCudaGraphs`. Demonstrates Instantiated CUDA Graph Update with Jacobi Iterative Method using different approaches.
|
||||||
|
* Added `cuSolverSp_LinearSolver`. Demonstrates cuSolverSP's LU, QR and Cholesky factorization.
|
||||||
|
* Added `MersenneTwisterGP11213`. Demonstrates the Mersenne Twister random number generator GP11213 in cuRAND.
|
||||||
|
|
||||||
|
### CUDA 10.1 Update 2
|
||||||
|
* Added `vulkanImageCUDA`. Demonstrates how to perform Vulkan image - CUDA Interop.
|
||||||
|
* Added `nvJPEG_encoder`. Demonstrates encoding of jpeg images using NVJPEG Library.
|
||||||
|
* Added Windows OS support to `nvJPEG` sample.
|
||||||
|
* Added `boxFilterNPP`. Demonstrates how to use NPP FilterBox function to perform a box filter.
|
||||||
|
* Added `cannyEdgeDetectorNPP`. Demonstrates the nppiFilterCannyBorder_8u_C1R Canny Edge Detection image filter function.
|
||||||
|
|
||||||
|
### CUDA 10.1 Update 1
|
||||||
|
* Added `NV12toBGRandResize`. Demonstrates how to convert and resize NV12 frames to BGR planars frames using CUDA in batch.
|
||||||
|
* Added `EGLStream_CUDA_Interop`. Demonstrates data exchange between CUDA and EGL Streams.
|
||||||
|
* Added `cuSolverDn_LinearSolver`. Demonstrates cuSolverDN's LU, QR and Cholesky factorization.
|
||||||
|
* Added support of Visual Studio 2019 to all samples supported on [Windows](./README.md#windows-1).
|
||||||
|
|
||||||
|
### CUDA 10.1
|
||||||
|
* Added `immaTensorCoreGemm`. Demonstrates integer GEMM computation using the Warp Matrix Multiply and Accumulate (WMMA) API for integers employing the Tensor Cores.
|
||||||
|
* Added `simpleIPC`. Demonstrates Inter Process Communication with one process per GPU for computation.
|
||||||
|
* Added `nvJPEG`. Demonstrates single and batched decoding of jpeg images using NVJPEG Library.
|
||||||
|
* Added `bandwidthTest`. It measures the memcopy bandwidth of the GPU and memcpy bandwidth across PCI-e.
|
||||||
|
* Added `reduction`. Demonstrates several important optimization strategies for Data-Parallel Algorithms like reduction.
|
||||||
|
* Update all the samples to support CUDA 10.1.
|
||||||
|
|
||||||
|
|
||||||
|
### CUDA 10.0
|
||||||
|
* Added `simpleCudaGraphs`. Demonstrates CUDA Graphs creation, instantiation and launch using Graphs APIs and Stream Capture APIs.
|
||||||
|
* Added `conjugateGradientCudaGraphs`. Demonstrates conjugate gradient solver on GPU using CUBLAS and CUSPARSE library calls captured and called using CUDA Graph APIs.
|
||||||
|
* Added `simpleVulkan`. Demonstrates Vulkan - CUDA Interop.
|
||||||
|
* Added `simpleD3D12`. Demonstrates DX12 - CUDA Interop.
|
||||||
|
* Added `UnifiedMemoryPerf`. Demonstrates performance comparision of various memory types involved in system.
|
||||||
|
* Added `p2pBandwidthLatencyTest`. Demonstrates Peer-To-Peer (P2P) data transfers between pairs of GPUs and computes latency and bandwidth.
|
||||||
|
* Added `systemWideAtomics`. Demonstrates system wide atomic instructions.
|
||||||
|
* Added `simpleCUBLASXT`. Demonstrates CUBLAS-XT library which performs GEMM operations over multiple GPUs.
|
||||||
|
* Added Windows OS support to `conjugateGradientMultiDeviceCG` sample.
|
||||||
|
* Removed support of Visual Studio 2010 from all samples.
|
||||||
|
|
||||||
|
|
||||||
|
### CUDA 9.2
|
||||||
|
|
||||||
|
This is the first release of CUDA Samples on GitHub:
|
||||||
|
* Added `vectorAdd_nvrtc`. Demonstrates runtime compilation library using NVRTC of a simple vectorAdd kernel.
|
||||||
|
* Added `warpAggregatedAtomicsCG`. Demonstrates warp aggregated atomics using Cooperative Groups.
|
||||||
|
* Added `deviceQuery`. Enumerates the properties of the CUDA devices present in the system.
|
||||||
|
* Added `matrixMul`. Demonstrates a matrix multiplication using shared memory through tiled approach.
|
||||||
|
* Added `matrixMulDrv`. Demonstrates a matrix multiplication using shared memory through tiled approach, uses CUDA Driver API.
|
||||||
|
* Added `cudaTensorCoreGemm`. Demonstrates a GEMM computation using the Warp Matrix Multiply and Accumulate (WMMA) API introduced in CUDA 9, as well as the new Tensor Cores introduced in the Volta chip family.
|
||||||
|
* Added `simpleVoteIntrinsics` which uses *_sync equivalent of the vote intrinsics _any, _all added since CUDA 9.0.
|
||||||
|
* Added `shfl_scan` which uses *_sync equivalent of the shfl intrinsics added since CUDA 9.0.
|
||||||
|
* Added `conjugateGradientMultiBlockCG`. Demonstrates a conjugate gradient solver on GPU using Multi Block Cooperative Groups.
|
||||||
|
* Added `conjugateGradientMultiDeviceCG`. Demonstrates a conjugate gradient solver on multiple GPUs using Multi Device Cooperative Groups, also uses unified memory prefetching and usage hints APIs.
|
||||||
|
* Added `simpleCUBLAS`. Demonstrates how perform GEMM operations using CUBLAS library.
|
||||||
|
* Added `simpleCUFFT`. Demonstrates how perform FFT operations using CUFFT library.
|
||||||
25
CMakeLists.txt
Normal file
25
CMakeLists.txt
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||||
|
|
||||||
|
project(cuda-samples LANGUAGES C CXX CUDA)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit REQUIRED)
|
||||||
|
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_STANDARD 17)
|
||||||
|
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 50 52 60 61 70 72 75 80 86 87 89 90 100 101 120)
|
||||||
|
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (expensive)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --extended-lambda")
|
||||||
|
|
||||||
|
add_subdirectory(Samples)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Binary file not shown.
BIN
Common/data/teapot512.pgm
Normal file
BIN
Common/data/teapot512.pgm
Normal file
Binary file not shown.
BIN
Common/data/teapot_512x512_8u.raw
Normal file
BIN
Common/data/teapot_512x512_8u.raw
Normal file
Binary file not shown.
BIN
Common/data/teapot_512x512_8u_Gray.raw
Normal file
BIN
Common/data/teapot_512x512_8u_Gray.raw
Normal file
Binary file not shown.
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -665,6 +665,12 @@ inline int _ConvertSMVer2Cores(int major, int minor) {
|
|||||||
{0x75, 64},
|
{0x75, 64},
|
||||||
{0x80, 64},
|
{0x80, 64},
|
||||||
{0x86, 128},
|
{0x86, 128},
|
||||||
|
{0x87, 128},
|
||||||
|
{0x89, 128},
|
||||||
|
{0x90, 128},
|
||||||
|
{0xa0, 128},
|
||||||
|
{0xa1, 128},
|
||||||
|
{0xc0, 128},
|
||||||
{-1, -1}};
|
{-1, -1}};
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -711,6 +717,12 @@ inline const char* _ConvertSMVer2ArchName(int major, int minor) {
|
|||||||
{0x75, "Turing"},
|
{0x75, "Turing"},
|
||||||
{0x80, "Ampere"},
|
{0x80, "Ampere"},
|
||||||
{0x86, "Ampere"},
|
{0x86, "Ampere"},
|
||||||
|
{0x87, "Ampere"},
|
||||||
|
{0x89, "Ada"},
|
||||||
|
{0x90, "Hopper"},
|
||||||
|
{0xa0, "Blackwell"},
|
||||||
|
{0xa1, "Blackwell"},
|
||||||
|
{0xc0, "Blackwell"},
|
||||||
{-1, "Graphics Device"}};
|
{-1, "Graphics Device"}};
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -113,6 +113,12 @@ inline int _ConvertSMVer2CoresDRV(int major, int minor) {
|
|||||||
{0x75, 64},
|
{0x75, 64},
|
||||||
{0x80, 64},
|
{0x80, 64},
|
||||||
{0x86, 128},
|
{0x86, 128},
|
||||||
|
{0x87, 128},
|
||||||
|
{0x89, 128},
|
||||||
|
{0x90, 128},
|
||||||
|
{0xa0, 128},
|
||||||
|
{0xa1, 128},
|
||||||
|
{0xc0, 128},
|
||||||
{-1, -1}};
|
{-1, -1}};
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -402,4 +408,3 @@ bool inline findFatbinPath(const char *module_file, std::string &module_path, ch
|
|||||||
// end of CUDA Helper Functions
|
// end of CUDA Helper Functions
|
||||||
|
|
||||||
#endif // COMMON_HELPER_CUDA_DRVAPI_H_
|
#endif // COMMON_HELPER_CUDA_DRVAPI_H_
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -168,7 +168,7 @@ int waitProcess(Process *process) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__linux__)
|
#if defined(__linux__) || defined(__QNX__)
|
||||||
int ipcCreateSocket(ipcHandle *&handle, const char *name,
|
int ipcCreateSocket(ipcHandle *&handle, const char *name,
|
||||||
const std::vector<Process> &processes) {
|
const std::vector<Process> &processes) {
|
||||||
int server_fd;
|
int server_fd;
|
||||||
@@ -262,41 +262,48 @@ int ipcRecvShareableHandle(ipcHandle *handle, ShareableHandle *shHandle) {
|
|||||||
// Union to guarantee alignment requirements for control array
|
// Union to guarantee alignment requirements for control array
|
||||||
union {
|
union {
|
||||||
struct cmsghdr cm;
|
struct cmsghdr cm;
|
||||||
char control[CMSG_SPACE(sizeof(int))];
|
// This will not work on QNX as QNX CMSG_SPACE calls __cmsg_alignbytes
|
||||||
|
// And __cmsg_alignbytes is a runtime function instead of compile-time macros
|
||||||
|
// char control[CMSG_SPACE(sizeof(int))]
|
||||||
|
char* control;
|
||||||
} control_un;
|
} control_un;
|
||||||
|
|
||||||
|
size_t sizeof_control = CMSG_SPACE(sizeof(int)) * sizeof(char);
|
||||||
|
control_un.control = (char*) malloc(sizeof_control);
|
||||||
struct cmsghdr *cmptr;
|
struct cmsghdr *cmptr;
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
int receivedfd;
|
int receivedfd;
|
||||||
char dummy_buffer[1];
|
char dummy_buffer[1];
|
||||||
ssize_t sendResult;
|
ssize_t sendResult;
|
||||||
|
|
||||||
msg.msg_control = control_un.control;
|
msg.msg_control = control_un.control;
|
||||||
msg.msg_controllen = sizeof(control_un.control);
|
msg.msg_controllen = sizeof_control;
|
||||||
|
|
||||||
iov[0].iov_base = (void *)dummy_buffer;
|
iov[0].iov_base = (void *)dummy_buffer;
|
||||||
iov[0].iov_len = sizeof(dummy_buffer);
|
iov[0].iov_len = sizeof(dummy_buffer);
|
||||||
|
|
||||||
msg.msg_iov = iov;
|
msg.msg_iov = iov;
|
||||||
msg.msg_iovlen = 1;
|
msg.msg_iovlen = 1;
|
||||||
|
|
||||||
if ((n = recvmsg(handle->socket, &msg, 0)) <= 0) {
|
if ((n = recvmsg(handle->socket, &msg, 0)) <= 0) {
|
||||||
perror("IPC failure: Receiving data over socket failed");
|
perror("IPC failure: Receiving data over socket failed");
|
||||||
|
free(control_un.control);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (((cmptr = CMSG_FIRSTHDR(&msg)) != NULL) &&
|
if (((cmptr = CMSG_FIRSTHDR(&msg)) != NULL) &&
|
||||||
(cmptr->cmsg_len == CMSG_LEN(sizeof(int)))) {
|
(cmptr->cmsg_len == CMSG_LEN(sizeof(int)))) {
|
||||||
if ((cmptr->cmsg_level != SOL_SOCKET) || (cmptr->cmsg_type != SCM_RIGHTS)) {
|
if ((cmptr->cmsg_level != SOL_SOCKET) || (cmptr->cmsg_type != SCM_RIGHTS)) {
|
||||||
|
free(control_un.control);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memmove(&receivedfd, CMSG_DATA(cmptr), sizeof(receivedfd));
|
memmove(&receivedfd, CMSG_DATA(cmptr), sizeof(receivedfd));
|
||||||
*(int *)shHandle = receivedfd;
|
*(int *)shHandle = receivedfd;
|
||||||
} else {
|
} else {
|
||||||
|
free(control_un.control);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(control_un.control);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,9 +347,12 @@ int ipcSendShareableHandle(ipcHandle *handle,
|
|||||||
|
|
||||||
union {
|
union {
|
||||||
struct cmsghdr cm;
|
struct cmsghdr cm;
|
||||||
char control[CMSG_SPACE(sizeof(int))];
|
char* control;
|
||||||
} control_un;
|
} control_un;
|
||||||
|
|
||||||
|
size_t sizeof_control = CMSG_SPACE(sizeof(int)) * sizeof(char);
|
||||||
|
control_un.control = (char*) malloc(sizeof_control);
|
||||||
|
|
||||||
struct cmsghdr *cmptr;
|
struct cmsghdr *cmptr;
|
||||||
ssize_t readResult;
|
ssize_t readResult;
|
||||||
struct sockaddr_un cliaddr;
|
struct sockaddr_un cliaddr;
|
||||||
@@ -360,7 +370,7 @@ int ipcSendShareableHandle(ipcHandle *handle,
|
|||||||
int sendfd = (int)shareableHandles[data];
|
int sendfd = (int)shareableHandles[data];
|
||||||
|
|
||||||
msg.msg_control = control_un.control;
|
msg.msg_control = control_un.control;
|
||||||
msg.msg_controllen = sizeof(control_un.control);
|
msg.msg_controllen = sizeof_control;
|
||||||
|
|
||||||
cmptr = CMSG_FIRSTHDR(&msg);
|
cmptr = CMSG_FIRSTHDR(&msg);
|
||||||
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
|
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
|
||||||
@@ -380,9 +390,11 @@ int ipcSendShareableHandle(ipcHandle *handle,
|
|||||||
ssize_t sendResult = sendmsg(handle->socket, &msg, 0);
|
ssize_t sendResult = sendmsg(handle->socket, &msg, 0);
|
||||||
if (sendResult <= 0) {
|
if (sendResult <= 0) {
|
||||||
perror("IPC failure: Sending data over socket failed");
|
perror("IPC failure: Sending data over socket failed");
|
||||||
|
free(control_un.control);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(control_un.control);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -84,7 +84,7 @@ int waitProcess(Process *process);
|
|||||||
#define checkIpcErrors(ipcFuncResult) \
|
#define checkIpcErrors(ipcFuncResult) \
|
||||||
if (ipcFuncResult == -1) { fprintf(stderr, "Failure at %u %s\n", __LINE__, __FILE__); exit(EXIT_FAILURE); }
|
if (ipcFuncResult == -1) { fprintf(stderr, "Failure at %u %s\n", __LINE__, __FILE__); exit(EXIT_FAILURE); }
|
||||||
|
|
||||||
#if defined(__linux__)
|
#if defined(__linux__) || defined(__QNX__)
|
||||||
struct ipcHandle_st {
|
struct ipcHandle_st {
|
||||||
int socket;
|
int socket;
|
||||||
char *socketName;
|
char *socketName;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -399,10 +399,10 @@ int getInputDir(std::string &input_dir, const char *executable_path) {
|
|||||||
// Search in default paths for input images.
|
// Search in default paths for input images.
|
||||||
std::string pathname = "";
|
std::string pathname = "";
|
||||||
const char *searchPath[] = {
|
const char *searchPath[] = {
|
||||||
"./images", "../../../../Samples/<executable_name>/images",
|
"./images",
|
||||||
"../../../Samples/<executable_name>/images",
|
"../../../../Samples/4_CUDA_Libraries/<executable_name>/images",
|
||||||
"../../Samples/<executable_name>/images"};
|
"../../../Samples/4_CUDA_Libraries/<executable_name>/images",
|
||||||
|
"../../Samples/4_CUDA_Libraries/<executable_name>/images"};
|
||||||
|
|
||||||
for (unsigned int i = 0; i < sizeof(searchPath) / sizeof(char *); ++i) {
|
for (unsigned int i = 0; i < sizeof(searchPath) / sizeof(char *); ++i) {
|
||||||
std::string pathname(searchPath[i]);
|
std::string pathname(searchPath[i]);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -287,12 +287,71 @@ inline char *sdkFindFilePath(const char *filename,
|
|||||||
const char *searchPath[] = {
|
const char *searchPath[] = {
|
||||||
"./", // same dir
|
"./", // same dir
|
||||||
"./data/", // same dir
|
"./data/", // same dir
|
||||||
|
|
||||||
"../../../../Samples/<executable_name>/", // up 4 in tree
|
"../../../../Samples/<executable_name>/", // up 4 in tree
|
||||||
"../../../Samples/<executable_name>/", // up 3 in tree
|
"../../../Samples/<executable_name>/", // up 3 in tree
|
||||||
"../../Samples/<executable_name>/", // up 2 in tree
|
"../../Samples/<executable_name>/", // up 2 in tree
|
||||||
|
|
||||||
"../../../../Samples/<executable_name>/data/", // up 4 in tree
|
"../../../../Samples/<executable_name>/data/", // up 4 in tree
|
||||||
"../../../Samples/<executable_name>/data/", // up 3 in tree
|
"../../../Samples/<executable_name>/data/", // up 3 in tree
|
||||||
"../../Samples/<executable_name>/data/", // up 2 in tree
|
"../../Samples/<executable_name>/data/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/0_Introduction/<executable_name>/", // up 4 in tree
|
||||||
|
"../../../Samples/0_Introduction/<executable_name>/", // up 3 in tree
|
||||||
|
"../../Samples/0_Introduction/<executable_name>/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/1_Utilities/<executable_name>/", // up 4 in tree
|
||||||
|
"../../../Samples/1_Utilities/<executable_name>/", // up 3 in tree
|
||||||
|
"../../Samples/1_Utilities/<executable_name>/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/2_Concepts_and_Techniques/<executable_name>/", // up 4 in tree
|
||||||
|
"../../../Samples/2_Concepts_and_Techniques/<executable_name>/", // up 3 in tree
|
||||||
|
"../../Samples/2_Concepts_and_Techniques/<executable_name>/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/3_CUDA_Features/<executable_name>/", // up 4 in tree
|
||||||
|
"../../../Samples/3_CUDA_Features/<executable_name>/", // up 3 in tree
|
||||||
|
"../../Samples/3_CUDA_Features/<executable_name>/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/4_CUDA_Libraries/<executable_name>/", // up 4 in tree
|
||||||
|
"../../../Samples/4_CUDA_Libraries/<executable_name>/", // up 3 in tree
|
||||||
|
"../../Samples/4_CUDA_Libraries/<executable_name>/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/5_Domain_Specific/<executable_name>/", // up 4 in tree
|
||||||
|
"../../../Samples/5_Domain_Specific/<executable_name>/", // up 3 in tree
|
||||||
|
"../../Samples/5_Domain_Specific/<executable_name>/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/6_Performance/<executable_name>/", // up 4 in tree
|
||||||
|
"../../../Samples/6_Performance/<executable_name>/", // up 3 in tree
|
||||||
|
"../../Samples/6_Performance/<executable_name>/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/0_Introduction/<executable_name>/data/", // up 4 in tree
|
||||||
|
"../../../Samples/0_Introduction/<executable_name>/data/", // up 3 in tree
|
||||||
|
"../../Samples/0_Introduction/<executable_name>/data/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/1_Utilities/<executable_name>/data/", // up 4 in tree
|
||||||
|
"../../../Samples/1_Utilities/<executable_name>/data/", // up 3 in tree
|
||||||
|
"../../Samples/1_Utilities/<executable_name>/data/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/2_Concepts_and_Techniques/<executable_name>/data/", // up 4 in tree
|
||||||
|
"../../../Samples/2_Concepts_and_Techniques/<executable_name>/data/", // up 3 in tree
|
||||||
|
"../../Samples/2_Concepts_and_Techniques/<executable_name>/data/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/3_CUDA_Features/<executable_name>/data/", // up 4 in tree
|
||||||
|
"../../../Samples/3_CUDA_Features/<executable_name>/data/", // up 3 in tree
|
||||||
|
"../../Samples/3_CUDA_Features/<executable_name>/data/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/4_CUDA_Libraries/<executable_name>/data/", // up 4 in tree
|
||||||
|
"../../../Samples/4_CUDA_Libraries/<executable_name>/data/", // up 3 in tree
|
||||||
|
"../../Samples/4_CUDA_Libraries/<executable_name>/data/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/5_Domain_Specific/<executable_name>/data/", // up 4 in tree
|
||||||
|
"../../../Samples/5_Domain_Specific/<executable_name>/data/", // up 3 in tree
|
||||||
|
"../../Samples/5_Domain_Specific/<executable_name>/data/", // up 2 in tree
|
||||||
|
|
||||||
|
"../../../../Samples/6_Performance/<executable_name>/data/", // up 4 in tree
|
||||||
|
"../../../Samples/6_Performance/<executable_name>/data/", // up 3 in tree
|
||||||
|
"../../Samples/6_Performance/<executable_name>/data/", // up 2 in tree
|
||||||
|
|
||||||
"../../../../Common/data/", // up 4 in tree
|
"../../../../Common/data/", // up 4 in tree
|
||||||
"../../../Common/data/", // up 3 in tree
|
"../../../Common/data/", // up 3 in tree
|
||||||
"../../Common/data/" // up 2 in tree
|
"../../Common/data/" // up 2 in tree
|
||||||
@@ -362,6 +421,7 @@ inline char *sdkFindFilePath(const char *filename,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// File not found
|
// File not found
|
||||||
|
printf("\nerror: sdkFindFilePath: file <%s> not found!\n", filename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
78
Common/multithreading.cpp
Normal file
78
Common/multithreading.cpp
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <multithreading.h>
|
||||||
|
|
||||||
|
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||||
|
// Create thread
|
||||||
|
CUTThread cutStartThread(CUT_THREADROUTINE func, void *data) {
|
||||||
|
return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, data, 0, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for thread to finish
|
||||||
|
void cutEndThread(CUTThread thread) {
|
||||||
|
WaitForSingleObject(thread, INFINITE);
|
||||||
|
CloseHandle(thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy thread
|
||||||
|
void cutDestroyThread(CUTThread thread) {
|
||||||
|
TerminateThread(thread, 0);
|
||||||
|
CloseHandle(thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for multiple threads
|
||||||
|
void cutWaitForThreads(const CUTThread *threads, int num) {
|
||||||
|
WaitForMultipleObjects(num, threads, true, INFINITE);
|
||||||
|
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
CloseHandle(threads[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
// Create thread
|
||||||
|
CUTThread cutStartThread(CUT_THREADROUTINE func, void *data) {
|
||||||
|
pthread_t thread;
|
||||||
|
pthread_create(&thread, NULL, func, data);
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for thread to finish
|
||||||
|
void cutEndThread(CUTThread thread) { pthread_join(thread, NULL); }
|
||||||
|
|
||||||
|
// Destroy thread
|
||||||
|
void cutDestroyThread(CUTThread thread) { pthread_cancel(thread); }
|
||||||
|
|
||||||
|
// Wait for multiple threads
|
||||||
|
void cutWaitForThreads(const CUTThread *threads, int num) {
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
cutEndThread(threads[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
76
Common/multithreading.h
Normal file
76
Common/multithreading.h
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MULTITHREADING_H
|
||||||
|
#define MULTITHREADING_H
|
||||||
|
|
||||||
|
|
||||||
|
//Simple portable thread library.
|
||||||
|
|
||||||
|
//Windows threads.
|
||||||
|
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
typedef HANDLE CUTThread;
|
||||||
|
typedef unsigned(WINAPI *CUT_THREADROUTINE)(void *);
|
||||||
|
|
||||||
|
#define CUT_THREADPROC unsigned WINAPI
|
||||||
|
#define CUT_THREADEND return 0
|
||||||
|
|
||||||
|
#else
|
||||||
|
//POSIX threads.
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
typedef pthread_t CUTThread;
|
||||||
|
typedef void *(*CUT_THREADROUTINE)(void *);
|
||||||
|
|
||||||
|
#define CUT_THREADPROC void
|
||||||
|
#define CUT_THREADEND
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//Create thread.
|
||||||
|
CUTThread cutStartThread(CUT_THREADROUTINE, void *data);
|
||||||
|
|
||||||
|
//Wait for thread to finish.
|
||||||
|
void cutEndThread(CUTThread thread);
|
||||||
|
|
||||||
|
//Destroy thread.
|
||||||
|
void cutDestroyThread(CUTThread thread);
|
||||||
|
|
||||||
|
//Wait for multiple threads.
|
||||||
|
void cutWaitForThreads(const CUTThread *threads, int num);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} //extern "C"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif //MULTITHREADING_H
|
||||||
111
Common/nvMath.h
Normal file
111
Common/nvMath.h
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//
|
||||||
|
// Template math library for common 3D functionality
|
||||||
|
//
|
||||||
|
// This code is in part deriver from glh, a cross platform glut helper library.
|
||||||
|
// The copyright for glh follows this notice.
|
||||||
|
//
|
||||||
|
// Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2000 Cass Everitt
|
||||||
|
Copyright (c) 2000 NVIDIA Corporation
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or
|
||||||
|
without modification, are permitted provided that the following
|
||||||
|
conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above
|
||||||
|
copyright notice, this list of conditions and the following
|
||||||
|
disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following
|
||||||
|
disclaimer in the documentation and/or other materials
|
||||||
|
provided with the distribution.
|
||||||
|
|
||||||
|
* The names of contributors to this software may not be used
|
||||||
|
to endorse or promote products derived from this software
|
||||||
|
without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
|
Cass Everitt - cass@r3.nu
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef NV_MATH_H
|
||||||
|
#define NV_MATH_H
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include <nvVector.h>
|
||||||
|
#include <nvMatrix.h>
|
||||||
|
#include <nvQuaternion.h>
|
||||||
|
|
||||||
|
#define NV_PI float(3.1415926535897932384626433832795)
|
||||||
|
|
||||||
|
namespace nv
|
||||||
|
{
|
||||||
|
|
||||||
|
typedef vec2<float> vec2f;
|
||||||
|
typedef vec3<float> vec3f;
|
||||||
|
typedef vec3<int> vec3i;
|
||||||
|
typedef vec3<unsigned int> vec3ui;
|
||||||
|
typedef vec4<float> vec4f;
|
||||||
|
typedef matrix4<float> matrix4f;
|
||||||
|
typedef quaternion<float> quaternionf;
|
||||||
|
|
||||||
|
|
||||||
|
inline void applyRotation(const quaternionf &r)
|
||||||
|
{
|
||||||
|
float angle;
|
||||||
|
vec3f axis;
|
||||||
|
r.get_value(axis, angle);
|
||||||
|
glRotatef(angle/3.1415926f * 180.0f, axis[0], axis[1], axis[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
540
Common/nvMatrix.h
Normal file
540
Common/nvMatrix.h
Normal file
@@ -0,0 +1,540 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//
|
||||||
|
// Template math library for common 3D functionality
|
||||||
|
//
|
||||||
|
// nvMatrix.h - template matrix code
|
||||||
|
//
|
||||||
|
// This code is in part deriver from glh, a cross platform glut helper library.
|
||||||
|
// The copyright for glh follows this notice.
|
||||||
|
//
|
||||||
|
// Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2000 Cass Everitt
|
||||||
|
Copyright (c) 2000 NVIDIA Corporation
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or
|
||||||
|
without modification, are permitted provided that the following
|
||||||
|
conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above
|
||||||
|
copyright notice, this list of conditions and the following
|
||||||
|
disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following
|
||||||
|
disclaimer in the documentation and/or other materials
|
||||||
|
provided with the distribution.
|
||||||
|
|
||||||
|
* The names of contributors to this software may not be used
|
||||||
|
to endorse or promote products derived from this software
|
||||||
|
without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
|
Cass Everitt - cass@r3.nu
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef NV_MATRIX_H
|
||||||
|
#define NV_MATRIX_H
|
||||||
|
|
||||||
|
namespace nv
|
||||||
|
{
|
||||||
|
|
||||||
|
template <class T> class vec2;
|
||||||
|
template <class T> class vec3;
|
||||||
|
template <class T> class vec4;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Matrix
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
template<class T>
|
||||||
|
class matrix4
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
matrix4()
|
||||||
|
{
|
||||||
|
make_identity();
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix4(T t)
|
||||||
|
{
|
||||||
|
set_value(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix4(const T *m)
|
||||||
|
{
|
||||||
|
set_value(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix4(T a00, T a01, T a02, T a03,
|
||||||
|
T a10, T a11, T a12, T a13,
|
||||||
|
T a20, T a21, T a22, T a23,
|
||||||
|
T a30, T a31, T a32, T a33) :
|
||||||
|
_11(a00), _12(a01), _13(a02), _14(a03),
|
||||||
|
_21(a10), _22(a11), _23(a12), _24(a13),
|
||||||
|
_31(a20), _32(a21), _33(a22), _34(a23),
|
||||||
|
_41(a30), _42(a31), _43(a32), _44(a33)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
void get_value(T *mp) const
|
||||||
|
{
|
||||||
|
int c = 0;
|
||||||
|
|
||||||
|
for (int j=0; j < 4; j++)
|
||||||
|
for (int i=0; i < 4; i++)
|
||||||
|
{
|
||||||
|
mp[c++] = element(i,j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const T *get_value() const
|
||||||
|
{
|
||||||
|
return _array;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_value(T *mp)
|
||||||
|
{
|
||||||
|
int c = 0;
|
||||||
|
|
||||||
|
for (int j=0; j < 4; j++)
|
||||||
|
for (int i=0; i < 4; i++)
|
||||||
|
{
|
||||||
|
element(i,j) = mp[c++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_value(T r)
|
||||||
|
{
|
||||||
|
for (int i=0; i < 4; i++)
|
||||||
|
for (int j=0; j < 4; j++)
|
||||||
|
{
|
||||||
|
element(i,j) = r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_identity()
|
||||||
|
{
|
||||||
|
element(0,0) = 1.0;
|
||||||
|
element(0,1) = 0.0;
|
||||||
|
element(0,2) = 0.0;
|
||||||
|
element(0,3) = 0.0;
|
||||||
|
|
||||||
|
element(1,0) = 0.0;
|
||||||
|
element(1,1) = 1.0;
|
||||||
|
element(1,2) = 0.0;
|
||||||
|
element(1,3) = 0.0;
|
||||||
|
|
||||||
|
element(2,0) = 0.0;
|
||||||
|
element(2,1) = 0.0;
|
||||||
|
element(2,2) = 1.0;
|
||||||
|
element(2,3) = 0.0;
|
||||||
|
|
||||||
|
element(3,0) = 0.0;
|
||||||
|
element(3,1) = 0.0;
|
||||||
|
element(3,2) = 0.0;
|
||||||
|
element(3,3) = 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set a uniform scale
|
||||||
|
void set_scale(T s)
|
||||||
|
{
|
||||||
|
element(0,0) = s;
|
||||||
|
element(1,1) = s;
|
||||||
|
element(2,2) = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_scale(const vec3<T> &s)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
element(i,i) = s[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void set_translate(const vec3<T> &t)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
element(i,3) = t[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_row(int r, const vec4<T> &t)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
element(r,i) = t[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_column(int c, const vec4<T> &t)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
element(i,c) = t[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vec4<T> get_row(int r) const
|
||||||
|
{
|
||||||
|
vec4<T> v;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
v[i] = element(r,i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec4<T> get_column(int c) const
|
||||||
|
{
|
||||||
|
vec4<T> v;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
v[i] = element(i,c);
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend matrix4 inverse(const matrix4 &m)
|
||||||
|
{
|
||||||
|
matrix4 minv;
|
||||||
|
|
||||||
|
T r1[8], r2[8], r3[8], r4[8];
|
||||||
|
T *s[4], *tmprow;
|
||||||
|
|
||||||
|
s[0] = &r1[0];
|
||||||
|
s[1] = &r2[0];
|
||||||
|
s[2] = &r3[0];
|
||||||
|
s[3] = &r4[0];
|
||||||
|
|
||||||
|
register int i,j,p,jj;
|
||||||
|
|
||||||
|
for (i=0; i<4; i++)
|
||||||
|
{
|
||||||
|
for (j=0; j<4; j++)
|
||||||
|
{
|
||||||
|
s[i][j] = m.element(i,j);
|
||||||
|
|
||||||
|
if (i==j)
|
||||||
|
{
|
||||||
|
s[i][j+4] = 1.0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s[i][j+4] = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
T scp[4];
|
||||||
|
|
||||||
|
for (i=0; i<4; i++)
|
||||||
|
{
|
||||||
|
scp[i] = T(fabs(s[i][0]));
|
||||||
|
|
||||||
|
for (j=1; j<4; j++)
|
||||||
|
if (T(fabs(s[i][j])) > scp[i])
|
||||||
|
{
|
||||||
|
scp[i] = T(fabs(s[i][j]));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scp[i] == 0.0)
|
||||||
|
{
|
||||||
|
return minv; // singular matrix!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int pivot_to;
|
||||||
|
T scp_max;
|
||||||
|
|
||||||
|
for (i=0; i<4; i++)
|
||||||
|
{
|
||||||
|
// select pivot row
|
||||||
|
pivot_to = i;
|
||||||
|
scp_max = T(fabs(s[i][i]/scp[i]));
|
||||||
|
|
||||||
|
// find out which row should be on top
|
||||||
|
for (p=i+1; p<4; p++)
|
||||||
|
if (T(fabs(s[p][i]/scp[p])) > scp_max)
|
||||||
|
{
|
||||||
|
scp_max = T(fabs(s[p][i]/scp[p]));
|
||||||
|
pivot_to = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pivot if necessary
|
||||||
|
if (pivot_to != i)
|
||||||
|
{
|
||||||
|
tmprow = s[i];
|
||||||
|
s[i] = s[pivot_to];
|
||||||
|
s[pivot_to] = tmprow;
|
||||||
|
T tmpscp;
|
||||||
|
tmpscp = scp[i];
|
||||||
|
scp[i] = scp[pivot_to];
|
||||||
|
scp[pivot_to] = tmpscp;
|
||||||
|
}
|
||||||
|
|
||||||
|
T mji;
|
||||||
|
|
||||||
|
// perform gaussian elimination
|
||||||
|
for (j=i+1; j<4; j++)
|
||||||
|
{
|
||||||
|
mji = s[j][i]/s[i][i];
|
||||||
|
s[j][i] = 0.0;
|
||||||
|
|
||||||
|
for (jj=i+1; jj<8; jj++)
|
||||||
|
{
|
||||||
|
s[j][jj] -= mji*s[i][jj];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s[3][3] == 0.0)
|
||||||
|
{
|
||||||
|
return minv; // singular matrix!
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Now we have an upper triangular matrix.
|
||||||
|
//
|
||||||
|
// x x x x | y y y y
|
||||||
|
// 0 x x x | y y y y
|
||||||
|
// 0 0 x x | y y y y
|
||||||
|
// 0 0 0 x | y y y y
|
||||||
|
//
|
||||||
|
// we'll back substitute to get the inverse
|
||||||
|
//
|
||||||
|
// 1 0 0 0 | z z z z
|
||||||
|
// 0 1 0 0 | z z z z
|
||||||
|
// 0 0 1 0 | z z z z
|
||||||
|
// 0 0 0 1 | z z z z
|
||||||
|
//
|
||||||
|
|
||||||
|
T mij;
|
||||||
|
|
||||||
|
for (i=3; i>0; i--)
|
||||||
|
{
|
||||||
|
for (j=i-1; j > -1; j--)
|
||||||
|
{
|
||||||
|
mij = s[j][i]/s[i][i];
|
||||||
|
|
||||||
|
for (jj=j+1; jj<8; jj++)
|
||||||
|
{
|
||||||
|
s[j][jj] -= mij*s[i][jj];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i=0; i<4; i++)
|
||||||
|
for (j=0; j<4; j++)
|
||||||
|
{
|
||||||
|
minv(i,j) = s[i][j+4] / s[i][i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return minv;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
friend matrix4 transpose(const matrix4 &m)
|
||||||
|
{
|
||||||
|
matrix4 mtrans;
|
||||||
|
|
||||||
|
for (int i=0; i<4; i++)
|
||||||
|
for (int j=0; j<4; j++)
|
||||||
|
{
|
||||||
|
mtrans(i,j) = m.element(j,i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mtrans;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix4 &operator *= (const matrix4 &rhs)
|
||||||
|
{
|
||||||
|
matrix4 mt(*this);
|
||||||
|
set_value(T(0));
|
||||||
|
|
||||||
|
for (int i=0; i < 4; i++)
|
||||||
|
for (int j=0; j < 4; j++)
|
||||||
|
for (int c=0; c < 4; c++)
|
||||||
|
{
|
||||||
|
element(i,j) += mt(i,c) * rhs(c,j);
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend matrix4 operator * (const matrix4 &lhs, const matrix4 &rhs)
|
||||||
|
{
|
||||||
|
matrix4 r(T(0));
|
||||||
|
|
||||||
|
for (int i=0; i < 4; i++)
|
||||||
|
for (int j=0; j < 4; j++)
|
||||||
|
for (int c=0; c < 4; c++)
|
||||||
|
{
|
||||||
|
r.element(i,j) += lhs(i,c) * rhs(c,j);
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// dst = M * src
|
||||||
|
vec4<T> operator *(const vec4<T> &src) const
|
||||||
|
{
|
||||||
|
vec4<T> r;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
r[i] = (src[0] * element(i,0) + src[1] * element(i,1) +
|
||||||
|
src[2] * element(i,2) + src[3] * element(i,3));
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// dst = src * M
|
||||||
|
friend vec4<T> operator *(const vec4<T> &lhs, const matrix4 &rhs)
|
||||||
|
{
|
||||||
|
vec4<T> r;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
r[i] = (lhs[0] * rhs.element(0,i) + lhs[1] * rhs.element(1,i) +
|
||||||
|
lhs[2] * rhs.element(2,i) + lhs[3] * rhs.element(3,i));
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
T &operator()(int row, int col)
|
||||||
|
{
|
||||||
|
return element(row,col);
|
||||||
|
}
|
||||||
|
|
||||||
|
const T &operator()(int row, int col) const
|
||||||
|
{
|
||||||
|
return element(row,col);
|
||||||
|
}
|
||||||
|
|
||||||
|
T &element(int row, int col)
|
||||||
|
{
|
||||||
|
return _array[row | (col<<2)];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T &element(int row, int col) const
|
||||||
|
{
|
||||||
|
return _array[row | (col<<2)];
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix4 &operator *= (const T &r)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 4; ++i)
|
||||||
|
{
|
||||||
|
element(0,i) *= r;
|
||||||
|
element(1,i) *= r;
|
||||||
|
element(2,i) *= r;
|
||||||
|
element(3,i) *= r;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix4 &operator += (const matrix4 &mat)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 4; ++i)
|
||||||
|
{
|
||||||
|
element(0,i) += mat.element(0,i);
|
||||||
|
element(1,i) += mat.element(1,i);
|
||||||
|
element(2,i) += mat.element(2,i);
|
||||||
|
element(3,i) += mat.element(3,i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
friend bool operator == (const matrix4 &lhs, const matrix4 &rhs)
|
||||||
|
{
|
||||||
|
bool r = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < 16; i++)
|
||||||
|
{
|
||||||
|
r &= lhs._array[i] == rhs._array[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend bool operator != (const matrix4 &lhs, const matrix4 &rhs)
|
||||||
|
{
|
||||||
|
bool r = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < 16; i++)
|
||||||
|
{
|
||||||
|
r &= lhs._array[i] != rhs._array[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
T _11, _12, _13, _14; // standard names for components
|
||||||
|
T _21, _22, _23, _24; // standard names for components
|
||||||
|
T _31, _32, _33, _34; // standard names for components
|
||||||
|
T _41, _42, _43, _44; // standard names for components
|
||||||
|
};
|
||||||
|
T _array[16]; // array access
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
530
Common/nvQuaternion.h
Normal file
530
Common/nvQuaternion.h
Normal file
@@ -0,0 +1,530 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//
|
||||||
|
// Template math library for common 3D functionality
|
||||||
|
//
|
||||||
|
// nvQuaterion.h - quaternion template and utility functions
|
||||||
|
//
|
||||||
|
// This code is in part deriver from glh, a cross platform glut helper library.
|
||||||
|
// The copyright for glh follows this notice.
|
||||||
|
//
|
||||||
|
// Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright (c) 2000 Cass Everitt
|
||||||
|
Copyright (c) 2000 NVIDIA Corporation
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or
|
||||||
|
without modification, are permitted provided that the following
|
||||||
|
conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above
|
||||||
|
copyright notice, this list of conditions and the following
|
||||||
|
disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following
|
||||||
|
disclaimer in the documentation and/or other materials
|
||||||
|
provided with the distribution.
|
||||||
|
|
||||||
|
* The names of contributors to this software may not be used
|
||||||
|
to endorse or promote products derived from this software
|
||||||
|
without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
|
Cass Everitt - cass@r3.nu
|
||||||
|
*/
|
||||||
|
#ifndef NV_QUATERNION_H
|
||||||
|
#define NV_QUATERNION_H
|
||||||
|
|
||||||
|
namespace nv
|
||||||
|
{
|
||||||
|
|
||||||
|
template <class T> class vec2;
|
||||||
|
template <class T> class vec3;
|
||||||
|
template <class T> class vec4;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Quaternion
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template< class T>
|
||||||
|
class quaternion
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
quaternion() : x(0.0), y(0.0), z(0.0), w(0.0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion(const T v[4])
|
||||||
|
{
|
||||||
|
set_value(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
quaternion(T q0, T q1, T q2, T q3)
|
||||||
|
{
|
||||||
|
set_value(q0, q1, q2, q3);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
quaternion(const matrix4<T> &m)
|
||||||
|
{
|
||||||
|
set_value(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
quaternion(const vec3<T> &axis, T radians)
|
||||||
|
{
|
||||||
|
set_value(axis, radians);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
quaternion(const vec3<T> &rotateFrom, const vec3<T> &rotateTo)
|
||||||
|
{
|
||||||
|
set_value(rotateFrom, rotateTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion(const vec3<T> &from_look, const vec3<T> &from_up,
|
||||||
|
const vec3<T> &to_look, const vec3<T> &to_up)
|
||||||
|
{
|
||||||
|
set_value(from_look, from_up, to_look, to_up);
|
||||||
|
}
|
||||||
|
|
||||||
|
const T *get_value() const
|
||||||
|
{
|
||||||
|
return &_array[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_value(T &q0, T &q1, T &q2, T &q3) const
|
||||||
|
{
|
||||||
|
q0 = _array[0];
|
||||||
|
q1 = _array[1];
|
||||||
|
q2 = _array[2];
|
||||||
|
q3 = _array[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion &set_value(T q0, T q1, T q2, T q3)
|
||||||
|
{
|
||||||
|
_array[0] = q0;
|
||||||
|
_array[1] = q1;
|
||||||
|
_array[2] = q2;
|
||||||
|
_array[3] = q3;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_value(vec3<T> &axis, T &radians) const
|
||||||
|
{
|
||||||
|
radians = T(acos(_array[3]) * T(2.0));
|
||||||
|
|
||||||
|
if (radians == T(0.0))
|
||||||
|
{
|
||||||
|
axis = vec3<T>(0.0, 0.0, 1.0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
axis[0] = _array[0];
|
||||||
|
axis[1] = _array[1];
|
||||||
|
axis[2] = _array[2];
|
||||||
|
axis = normalize(axis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_value(matrix4<T> &m) const
|
||||||
|
{
|
||||||
|
T s, xs, ys, zs, wx, wy, wz, xx, xy, xz, yy, yz, zz;
|
||||||
|
|
||||||
|
T norm = _array[0] * _array[0] + _array[1] * _array[1] + _array[2] * _array[2] + _array[3] * _array[3];
|
||||||
|
|
||||||
|
s = (norm == T(0.0)) ? T(0.0) : (T(2.0) / norm);
|
||||||
|
|
||||||
|
xs = _array[0] * s;
|
||||||
|
ys = _array[1] * s;
|
||||||
|
zs = _array[2] * s;
|
||||||
|
|
||||||
|
wx = _array[3] * xs;
|
||||||
|
wy = _array[3] * ys;
|
||||||
|
wz = _array[3] * zs;
|
||||||
|
|
||||||
|
xx = _array[0] * xs;
|
||||||
|
xy = _array[0] * ys;
|
||||||
|
xz = _array[0] * zs;
|
||||||
|
|
||||||
|
yy = _array[1] * ys;
|
||||||
|
yz = _array[1] * zs;
|
||||||
|
zz = _array[2] * zs;
|
||||||
|
|
||||||
|
m(0,0) = T(T(1.0) - (yy + zz));
|
||||||
|
m(1,0) = T(xy + wz);
|
||||||
|
m(2,0) = T(xz - wy);
|
||||||
|
|
||||||
|
m(0,1) = T(xy - wz);
|
||||||
|
m(1,1) = T(T(1.0) - (xx + zz));
|
||||||
|
m(2,1) = T(yz + wx);
|
||||||
|
|
||||||
|
m(0,2) = T(xz + wy);
|
||||||
|
m(1,2) = T(yz - wx);
|
||||||
|
m(2,2) = T(T(1.0) - (xx + yy));
|
||||||
|
|
||||||
|
m(3,0) = m(3,1) = m(3,2) = m(0,3) = m(1,3) = m(2,3) = T(0.0);
|
||||||
|
m(3,3) = T(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion &set_value(const T *qp)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
_array[i] = qp[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion &set_value(const matrix4<T> &m)
|
||||||
|
{
|
||||||
|
T tr, s;
|
||||||
|
int i, j, k;
|
||||||
|
const int nxt[3] = { 1, 2, 0 };
|
||||||
|
|
||||||
|
tr = m(0,0) + m(1,1) + m(2,2);
|
||||||
|
|
||||||
|
if (tr > T(0))
|
||||||
|
{
|
||||||
|
s = T(sqrt(tr + m(3,3)));
|
||||||
|
_array[3] = T(s * 0.5);
|
||||||
|
s = T(0.5) / s;
|
||||||
|
|
||||||
|
_array[0] = T((m(1,2) - m(2,1)) * s);
|
||||||
|
_array[1] = T((m(2,0) - m(0,2)) * s);
|
||||||
|
_array[2] = T((m(0,1) - m(1,0)) * s);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
|
||||||
|
if (m(1,1) > m(0,0))
|
||||||
|
{
|
||||||
|
i = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m(2,2) > m(i,i))
|
||||||
|
{
|
||||||
|
i = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
j = nxt[i];
|
||||||
|
k = nxt[j];
|
||||||
|
|
||||||
|
s = T(sqrt((m(i,j) - (m(j,j) + m(k,k))) + T(1.0)));
|
||||||
|
|
||||||
|
_array[i] = T(s * 0.5);
|
||||||
|
s = T(0.5 / s);
|
||||||
|
|
||||||
|
_array[3] = T((m(j,k) - m(k,j)) * s);
|
||||||
|
_array[j] = T((m(i,j) + m(j,i)) * s);
|
||||||
|
_array[k] = T((m(i,k) + m(k,i)) * s);
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion &set_value(const vec3<T> &axis, T theta)
|
||||||
|
{
|
||||||
|
T sqnorm = square_norm(axis);
|
||||||
|
|
||||||
|
if (sqnorm == T(0.0))
|
||||||
|
{
|
||||||
|
// axis too small.
|
||||||
|
x = y = z = T(0.0);
|
||||||
|
w = T(1.0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
theta *= T(0.5);
|
||||||
|
T sin_theta = T(sin(theta));
|
||||||
|
|
||||||
|
if (sqnorm != T(1))
|
||||||
|
{
|
||||||
|
sin_theta /= T(sqrt(sqnorm));
|
||||||
|
}
|
||||||
|
|
||||||
|
x = sin_theta * axis[0];
|
||||||
|
y = sin_theta * axis[1];
|
||||||
|
z = sin_theta * axis[2];
|
||||||
|
w = T(cos(theta));
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion &set_value(const vec3<T> &rotateFrom, const vec3<T> &rotateTo)
|
||||||
|
{
|
||||||
|
vec3<T> p1, p2;
|
||||||
|
T alpha;
|
||||||
|
|
||||||
|
p1 = normalize(rotateFrom);
|
||||||
|
p2 = normalize(rotateTo);
|
||||||
|
|
||||||
|
alpha = dot(p1, p2);
|
||||||
|
|
||||||
|
if (alpha == T(1.0))
|
||||||
|
{
|
||||||
|
*this = quaternion();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensures that the anti-parallel case leads to a positive dot
|
||||||
|
if (alpha == T(-1.0))
|
||||||
|
{
|
||||||
|
vec3<T> v;
|
||||||
|
|
||||||
|
if (p1[0] != p1[1] || p1[0] != p1[2])
|
||||||
|
{
|
||||||
|
v = vec3<T>(p1[1], p1[2], p1[0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
v = vec3<T>(-p1[0], p1[1], p1[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
v -= p1 * dot(p1, v);
|
||||||
|
v = normalize(v);
|
||||||
|
|
||||||
|
set_value(v, T(3.1415926));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
p1 = normalize(cross(p1, p2));
|
||||||
|
|
||||||
|
set_value(p1,T(acos(alpha)));
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion &set_value(const vec3<T> &from_look, const vec3<T> &from_up,
|
||||||
|
const vec3<T> &to_look, const vec3<T> &to_up)
|
||||||
|
{
|
||||||
|
quaternion r_look = quaternion(from_look, to_look);
|
||||||
|
|
||||||
|
vec3<T> rotated_from_up(from_up);
|
||||||
|
r_look.mult_vec(rotated_from_up);
|
||||||
|
|
||||||
|
quaternion r_twist = quaternion(rotated_from_up, to_up);
|
||||||
|
|
||||||
|
*this = r_twist;
|
||||||
|
*this *= r_look;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
quaternion &operator *= (const quaternion<T> &qr)
|
||||||
|
{
|
||||||
|
quaternion ql(*this);
|
||||||
|
|
||||||
|
w = ql.w * qr.w - ql.x * qr.x - ql.y * qr.y - ql.z * qr.z;
|
||||||
|
x = ql.w * qr.x + ql.x * qr.w + ql.y * qr.z - ql.z * qr.y;
|
||||||
|
y = ql.w * qr.y + ql.y * qr.w + ql.z * qr.x - ql.x * qr.z;
|
||||||
|
z = ql.w * qr.z + ql.z * qr.w + ql.x * qr.y - ql.y * qr.x;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend quaternion normalize(const quaternion<T> &q)
|
||||||
|
{
|
||||||
|
quaternion r(q);
|
||||||
|
T rnorm = T(1.0) / T(sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z));
|
||||||
|
|
||||||
|
r.x *= rnorm;
|
||||||
|
r.y *= rnorm;
|
||||||
|
r.z *= rnorm;
|
||||||
|
r.w *= rnorm;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend quaternion<T> conjugate(const quaternion<T> &q)
|
||||||
|
{
|
||||||
|
quaternion<T> r(q);
|
||||||
|
r._array[0] *= T(-1.0);
|
||||||
|
r._array[1] *= T(-1.0);
|
||||||
|
r._array[2] *= T(-1.0);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend quaternion<T> inverse(const quaternion<T> &q)
|
||||||
|
{
|
||||||
|
return conjugate(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Quaternion multiplication with cartesian vector
|
||||||
|
// v' = q*v*q(star)
|
||||||
|
//
|
||||||
|
void mult_vec(const vec3<T> &src, vec3<T> &dst) const
|
||||||
|
{
|
||||||
|
T v_coef = w * w - x * x - y * y - z * z;
|
||||||
|
T u_coef = T(2.0) * (src[0] * x + src[1] * y + src[2] * z);
|
||||||
|
T c_coef = T(2.0) * w;
|
||||||
|
|
||||||
|
dst.v[0] = v_coef * src.v[0] + u_coef * x + c_coef * (y * src.v[2] - z * src.v[1]);
|
||||||
|
dst.v[1] = v_coef * src.v[1] + u_coef * y + c_coef * (z * src.v[0] - x * src.v[2]);
|
||||||
|
dst.v[2] = v_coef * src.v[2] + u_coef * z + c_coef * (x * src.v[1] - y * src.v[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mult_vec(vec3<T> &src_and_dst) const
|
||||||
|
{
|
||||||
|
mult_vec(vec3<T>(src_and_dst), src_and_dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
void scale_angle(T scaleFactor)
|
||||||
|
{
|
||||||
|
vec3<T> axis;
|
||||||
|
T radians;
|
||||||
|
|
||||||
|
get_value(axis, radians);
|
||||||
|
radians *= scaleFactor;
|
||||||
|
set_value(axis, radians);
|
||||||
|
}
|
||||||
|
|
||||||
|
friend quaternion<T> slerp(const quaternion<T> &p, const quaternion<T> &q, T alpha)
|
||||||
|
{
|
||||||
|
quaternion r;
|
||||||
|
|
||||||
|
T cos_omega = p.x * q.x + p.y * q.y + p.z * q.z + p.w * q.w;
|
||||||
|
// if B is on opposite hemisphere from A, use -B instead
|
||||||
|
|
||||||
|
int bflip;
|
||||||
|
|
||||||
|
if ((bflip = (cos_omega < T(0))))
|
||||||
|
{
|
||||||
|
cos_omega = -cos_omega;
|
||||||
|
}
|
||||||
|
|
||||||
|
// complementary interpolation parameter
|
||||||
|
T beta = T(1) - alpha;
|
||||||
|
|
||||||
|
if (cos_omega >= T(1))
|
||||||
|
{
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
T omega = T(acos(cos_omega));
|
||||||
|
T one_over_sin_omega = T(1.0) / T(sin(omega));
|
||||||
|
|
||||||
|
beta = T(sin(omega*beta) * one_over_sin_omega);
|
||||||
|
alpha = T(sin(omega*alpha) * one_over_sin_omega);
|
||||||
|
|
||||||
|
if (bflip)
|
||||||
|
{
|
||||||
|
alpha = -alpha;
|
||||||
|
}
|
||||||
|
|
||||||
|
r.x = beta * p._array[0]+ alpha * q._array[0];
|
||||||
|
r.y = beta * p._array[1]+ alpha * q._array[1];
|
||||||
|
r.z = beta * p._array[2]+ alpha * q._array[2];
|
||||||
|
r.w = beta * p._array[3]+ alpha * q._array[3];
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
T &operator [](int i)
|
||||||
|
{
|
||||||
|
return _array[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
const T &operator [](int i) const
|
||||||
|
{
|
||||||
|
return _array[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
friend bool operator == (const quaternion<T> &lhs, const quaternion<T> &rhs)
|
||||||
|
{
|
||||||
|
bool r = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
r &= lhs._array[i] == rhs._array[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend bool operator != (const quaternion<T> &lhs, const quaternion<T> &rhs)
|
||||||
|
{
|
||||||
|
bool r = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
r &= lhs._array[i] == rhs._array[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend quaternion<T> operator * (const quaternion<T> &lhs, const quaternion<T> &rhs)
|
||||||
|
{
|
||||||
|
quaternion r(lhs);
|
||||||
|
r *= rhs;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
T x;
|
||||||
|
T y;
|
||||||
|
T z;
|
||||||
|
T w;
|
||||||
|
};
|
||||||
|
T _array[4];
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
260
Common/nvShaderUtils.h
Normal file
260
Common/nvShaderUtils.h
Normal file
@@ -0,0 +1,260 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Utility functions for compiling shaders and programs
|
||||||
|
*
|
||||||
|
* Author: Evan Hart
|
||||||
|
* Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef NV_SHADER_UTILS_H
|
||||||
|
#define NV_SHADER_UTILS_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
namespace nv {
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
inline GLuint CompileGLSLShader(GLenum target, const char *shader) {
|
||||||
|
GLuint object;
|
||||||
|
|
||||||
|
object = glCreateShader(target);
|
||||||
|
|
||||||
|
if (!object) {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
glShaderSource(object, 1, &shader, NULL);
|
||||||
|
|
||||||
|
glCompileShader(object);
|
||||||
|
|
||||||
|
// check if shader compiled
|
||||||
|
GLint compiled = 0;
|
||||||
|
glGetShaderiv(object, GL_COMPILE_STATUS, &compiled);
|
||||||
|
|
||||||
|
if (!compiled) {
|
||||||
|
#ifdef NV_REPORT_COMPILE_ERRORS
|
||||||
|
char temp[256] = "";
|
||||||
|
glGetShaderInfoLog(object, 256, NULL, temp);
|
||||||
|
fprintf(stderr, "Compile failed:\n%s\n", temp);
|
||||||
|
#endif
|
||||||
|
glDeleteShader(object);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
inline GLuint CompileGLSLShaderFromFile(GLenum target, const char *filename) {
|
||||||
|
FILE *shaderFile;
|
||||||
|
char *text;
|
||||||
|
long size;
|
||||||
|
size_t fsize = 0;
|
||||||
|
|
||||||
|
// read files as binary to prevent problems from newline translation
|
||||||
|
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||||
|
|
||||||
|
if (fopen_s(&shaderFile, filename, "rb") != 0)
|
||||||
|
#else
|
||||||
|
if ((shaderFile = fopen(filename, "rb")) == 0)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the length of the file
|
||||||
|
fseek(shaderFile, 0, SEEK_END);
|
||||||
|
size = ftell(shaderFile);
|
||||||
|
|
||||||
|
// Read the file contents from the start, then close file and add a null
|
||||||
|
// terminator
|
||||||
|
fseek(shaderFile, 0, SEEK_SET);
|
||||||
|
text = new char[size + 1];
|
||||||
|
fsize = fread(text, size, 1, shaderFile);
|
||||||
|
fclose(shaderFile);
|
||||||
|
|
||||||
|
if (fsize == 0) {
|
||||||
|
printf("CompileGLSLShaderFromFile(), error... fsize = 0\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
text[size] = '\0';
|
||||||
|
|
||||||
|
GLuint object = CompileGLSLShader(target, text);
|
||||||
|
|
||||||
|
delete[] text;
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a program composed of vertex and fragment shaders.
|
||||||
|
inline GLuint LinkGLSLProgram(GLuint vertexShader, GLuint fragmentShader) {
|
||||||
|
GLuint program = glCreateProgram();
|
||||||
|
glAttachShader(program, vertexShader);
|
||||||
|
glAttachShader(program, fragmentShader);
|
||||||
|
glLinkProgram(program);
|
||||||
|
|
||||||
|
#ifdef NV_REPORT_COMPILE_ERRORS
|
||||||
|
// Get error log.
|
||||||
|
GLint charsWritten, infoLogLength;
|
||||||
|
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||||
|
|
||||||
|
char *infoLog = new char[infoLogLength];
|
||||||
|
glGetProgramInfoLog(program, infoLogLength, &charsWritten, infoLog);
|
||||||
|
printf(infoLog);
|
||||||
|
delete[] infoLog;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Test linker result.
|
||||||
|
GLint linkSucceed = GL_FALSE;
|
||||||
|
glGetProgramiv(program, GL_LINK_STATUS, &linkSucceed);
|
||||||
|
|
||||||
|
if (linkSucceed == GL_FALSE) {
|
||||||
|
glDeleteProgram(program);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return program;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a program composed of vertex, geometry and fragment shaders.
|
||||||
|
inline GLuint LinkGLSLProgram(GLuint vertexShader, GLuint geometryShader,
|
||||||
|
GLint inputType, GLint vertexOut,
|
||||||
|
GLint outputType, GLuint fragmentShader) {
|
||||||
|
GLuint program = glCreateProgram();
|
||||||
|
glAttachShader(program, vertexShader);
|
||||||
|
glAttachShader(program, geometryShader);
|
||||||
|
glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT, inputType);
|
||||||
|
glProgramParameteriEXT(program, GL_GEOMETRY_VERTICES_OUT_EXT, vertexOut);
|
||||||
|
glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT, outputType);
|
||||||
|
glAttachShader(program, fragmentShader);
|
||||||
|
glLinkProgram(program);
|
||||||
|
|
||||||
|
#ifdef NV_REPORT_COMPILE_ERRORS
|
||||||
|
// Get error log.
|
||||||
|
GLint charsWritten, infoLogLength;
|
||||||
|
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||||
|
|
||||||
|
char *infoLog = new char[infoLogLength];
|
||||||
|
glGetProgramInfoLog(program, infoLogLength, &charsWritten, infoLog);
|
||||||
|
printf(infoLog);
|
||||||
|
delete[] infoLog;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Test linker result.
|
||||||
|
GLint linkSucceed = GL_FALSE;
|
||||||
|
glGetProgramiv(program, GL_LINK_STATUS, &linkSucceed);
|
||||||
|
|
||||||
|
if (linkSucceed == GL_FALSE) {
|
||||||
|
glDeleteProgram(program);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return program;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
inline GLuint CompileASMShader(GLenum program_type, const char *code) {
|
||||||
|
GLuint program_id;
|
||||||
|
glGenProgramsARB(1, &program_id);
|
||||||
|
glBindProgramARB(program_type, program_id);
|
||||||
|
glProgramStringARB(program_type, GL_PROGRAM_FORMAT_ASCII_ARB,
|
||||||
|
(GLsizei)strlen(code), (GLubyte *)code);
|
||||||
|
|
||||||
|
GLint error_pos;
|
||||||
|
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
|
||||||
|
|
||||||
|
if (error_pos != -1) {
|
||||||
|
#ifdef NV_REPORT_COMPILE_ERRORS
|
||||||
|
const GLubyte *error_string;
|
||||||
|
error_string = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
|
||||||
|
fprintf(stderr, "Program error at position: %d\n%s\n", (int)error_pos,
|
||||||
|
error_string);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return program_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
inline GLuint CompileASMShaderFromFile(GLenum target, const char *filename) {
|
||||||
|
FILE *shaderFile;
|
||||||
|
char *text;
|
||||||
|
long size;
|
||||||
|
size_t fsize = 0;
|
||||||
|
|
||||||
|
// read files as binary to prevent problems from newline translation
|
||||||
|
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||||
|
|
||||||
|
if (fopen_s(&shaderFile, filename, "rb") != 0)
|
||||||
|
#else
|
||||||
|
if ((shaderFile = fopen(filename, "rb")) == 0)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the length of the file
|
||||||
|
fseek(shaderFile, 0, SEEK_END);
|
||||||
|
size = ftell(shaderFile);
|
||||||
|
|
||||||
|
// Read the file contents from the start, then close file and add a null
|
||||||
|
// terminator
|
||||||
|
fseek(shaderFile, 0, SEEK_SET);
|
||||||
|
text = new char[size + 1];
|
||||||
|
fsize = fread(text, size, 1, shaderFile);
|
||||||
|
fclose(shaderFile);
|
||||||
|
|
||||||
|
if (fsize == 0) {
|
||||||
|
printf("CompileGLSLShaderFromFile(), error... fsize = 0\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
text[size] = '\0';
|
||||||
|
|
||||||
|
GLuint program_id = CompileASMShader(target, text);
|
||||||
|
|
||||||
|
delete[] text;
|
||||||
|
|
||||||
|
return program_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nv
|
||||||
|
#endif
|
||||||
1074
Common/nvVector.h
Normal file
1074
Common/nvVector.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -49,6 +49,11 @@
|
|||||||
|
|
||||||
void compileFileToCUBIN(char *filename, int argc, char **argv, char **cubinResult,
|
void compileFileToCUBIN(char *filename, int argc, char **argv, char **cubinResult,
|
||||||
size_t *cubinResultSize, int requiresCGheaders) {
|
size_t *cubinResultSize, int requiresCGheaders) {
|
||||||
|
if (!filename) {
|
||||||
|
std::cerr << "\nerror: filename is empty for compileFileToCUBIN()!\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
std::ifstream inputFile(filename,
|
std::ifstream inputFile(filename,
|
||||||
std::ios::in | std::ios::binary | std::ios::ate);
|
std::ios::in | std::ios::binary | std::ios::ate);
|
||||||
|
|
||||||
@@ -111,7 +116,12 @@ void compileFileToCUBIN(char *filename, int argc, char **argv, char **cubinResul
|
|||||||
|
|
||||||
compileOptions = "--include-path=";
|
compileOptions = "--include-path=";
|
||||||
|
|
||||||
std::string path = sdkFindFilePath(HeaderNames, argv[0]);
|
char *strPath = sdkFindFilePath(HeaderNames, argv[0]);
|
||||||
|
if (!strPath) {
|
||||||
|
std::cerr << "\nerror: header file " << HeaderNames << " not found!\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
std::string path = strPath;
|
||||||
if (!path.empty()) {
|
if (!path.empty()) {
|
||||||
std::size_t found = path.find(HeaderNames);
|
std::size_t found = path.find(HeaderNames);
|
||||||
path.erase(found);
|
path.erase(found);
|
||||||
@@ -120,6 +130,7 @@ void compileFileToCUBIN(char *filename, int argc, char **argv, char **cubinResul
|
|||||||
"\nCooperativeGroups headers not found, please install it in %s "
|
"\nCooperativeGroups headers not found, please install it in %s "
|
||||||
"sample directory..\n Exiting..\n",
|
"sample directory..\n Exiting..\n",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
compileOptions += path.c_str();
|
compileOptions += path.c_str();
|
||||||
compileParams[numCompileOptions] = reinterpret_cast<char *>(
|
compileParams[numCompileOptions] = reinterpret_cast<char *>(
|
||||||
|
|||||||
236
Common/param.h
Normal file
236
Common/param.h
Normal file
@@ -0,0 +1,236 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Simple parameter system
|
||||||
|
sgreen@nvidia.com 4/2001
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PARAM_H
|
||||||
|
#define PARAM_H
|
||||||
|
|
||||||
|
#include <iomanip>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// base class for named parameter
|
||||||
|
class ParamBase {
|
||||||
|
public:
|
||||||
|
ParamBase(const char *name) : m_name(name) {}
|
||||||
|
virtual ~ParamBase() {}
|
||||||
|
|
||||||
|
std::string &GetName() { return m_name; }
|
||||||
|
|
||||||
|
virtual float GetFloatValue() = 0;
|
||||||
|
virtual int GetIntValue() = 0;
|
||||||
|
virtual std::string GetValueString() = 0;
|
||||||
|
|
||||||
|
virtual void Reset() = 0;
|
||||||
|
virtual void Increment() = 0;
|
||||||
|
virtual void Decrement() = 0;
|
||||||
|
|
||||||
|
virtual float GetPercentage() = 0;
|
||||||
|
virtual void SetPercentage(float p) = 0;
|
||||||
|
|
||||||
|
virtual void Write(std::ostream &stream) = 0;
|
||||||
|
virtual void Read(std::istream &stream) = 0;
|
||||||
|
|
||||||
|
virtual bool IsList() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::string m_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
// derived class for single-valued parameter
|
||||||
|
template <class T>
|
||||||
|
class Param : public ParamBase {
|
||||||
|
public:
|
||||||
|
Param(const char *name, T value = 0, T min = 0, T max = 10000, T step = 1,
|
||||||
|
T *ptr = 0)
|
||||||
|
: ParamBase(name),
|
||||||
|
m_default(value),
|
||||||
|
m_min(min),
|
||||||
|
m_max(max),
|
||||||
|
m_step(step),
|
||||||
|
m_precision(3) {
|
||||||
|
if (ptr) {
|
||||||
|
m_ptr = ptr;
|
||||||
|
} else {
|
||||||
|
m_ptr = &m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
*m_ptr = value;
|
||||||
|
}
|
||||||
|
~Param() {}
|
||||||
|
|
||||||
|
T GetValue() const { return *m_ptr; }
|
||||||
|
T SetValue(const T value) { *m_ptr = value; }
|
||||||
|
|
||||||
|
float GetFloatValue() { return (float)*m_ptr; }
|
||||||
|
int GetIntValue() { return (int)*m_ptr; }
|
||||||
|
|
||||||
|
std::string GetValueString() {
|
||||||
|
std::ostringstream ost;
|
||||||
|
ost << std::setprecision(m_precision) << std::fixed;
|
||||||
|
ost << *m_ptr;
|
||||||
|
return ost.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetPrecision(int x) { m_precision = x; }
|
||||||
|
|
||||||
|
float GetPercentage() { return (*m_ptr - m_min) / (float)(m_max - m_min); }
|
||||||
|
|
||||||
|
void SetPercentage(float p) { *m_ptr = (T)(m_min + p * (m_max - m_min)); }
|
||||||
|
|
||||||
|
void Reset() { *m_ptr = m_default; }
|
||||||
|
|
||||||
|
void Increment() {
|
||||||
|
*m_ptr += m_step;
|
||||||
|
|
||||||
|
if (*m_ptr > m_max) {
|
||||||
|
*m_ptr = m_max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Decrement() {
|
||||||
|
*m_ptr -= m_step;
|
||||||
|
|
||||||
|
if (*m_ptr < m_min) {
|
||||||
|
*m_ptr = m_min;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Write(std::ostream &stream) {
|
||||||
|
stream << m_name << " " << *m_ptr << '\n';
|
||||||
|
}
|
||||||
|
void Read(std::istream &stream) { stream >> m_name >> *m_ptr; }
|
||||||
|
|
||||||
|
bool IsList() { return false; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
T m_value;
|
||||||
|
T *m_ptr; // pointer to value declared elsewhere
|
||||||
|
T m_default, m_min, m_max, m_step;
|
||||||
|
int m_precision; // number of digits after decimal point in string output
|
||||||
|
};
|
||||||
|
|
||||||
|
const Param<int> dummy("error");
|
||||||
|
|
||||||
|
// list of parameters
|
||||||
|
class ParamList : public ParamBase {
|
||||||
|
public:
|
||||||
|
ParamList(const char *name = "") : ParamBase(name) { active = true; }
|
||||||
|
~ParamList() {}
|
||||||
|
|
||||||
|
float GetFloatValue() { return 0.0f; }
|
||||||
|
int GetIntValue() { return 0; }
|
||||||
|
|
||||||
|
void AddParam(ParamBase *param) {
|
||||||
|
m_params.push_back(param);
|
||||||
|
m_map[param->GetName()] = param;
|
||||||
|
m_current = m_params.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
// look-up parameter based on name
|
||||||
|
ParamBase *GetParam(char *name) {
|
||||||
|
ParamBase *p = m_map[name];
|
||||||
|
|
||||||
|
if (p) {
|
||||||
|
return p;
|
||||||
|
} else {
|
||||||
|
return (ParamBase *)&dummy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ParamBase *GetParam(int i) { return m_params[i]; }
|
||||||
|
|
||||||
|
ParamBase *GetCurrent() { return *m_current; }
|
||||||
|
|
||||||
|
int GetSize() { return (int)m_params.size(); }
|
||||||
|
|
||||||
|
std::string GetValueString() { return m_name; }
|
||||||
|
|
||||||
|
// functions to traverse list
|
||||||
|
void Reset() { m_current = m_params.begin(); }
|
||||||
|
|
||||||
|
void Increment() {
|
||||||
|
m_current++;
|
||||||
|
|
||||||
|
if (m_current == m_params.end()) {
|
||||||
|
m_current = m_params.begin();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Decrement() {
|
||||||
|
if (m_current == m_params.begin()) {
|
||||||
|
m_current = m_params.end() - 1;
|
||||||
|
} else {
|
||||||
|
m_current--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float GetPercentage() { return 0.0f; }
|
||||||
|
void SetPercentage(float /*p*/) {}
|
||||||
|
|
||||||
|
void Write(std::ostream &stream) {
|
||||||
|
stream << m_name << '\n';
|
||||||
|
|
||||||
|
for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
|
||||||
|
p != m_params.end(); ++p) {
|
||||||
|
(*p)->Write(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Read(std::istream &stream) {
|
||||||
|
stream >> m_name;
|
||||||
|
|
||||||
|
for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
|
||||||
|
p != m_params.end(); ++p) {
|
||||||
|
(*p)->Read(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsList() { return true; }
|
||||||
|
|
||||||
|
void ResetAll() {
|
||||||
|
for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
|
||||||
|
p != m_params.end(); ++p) {
|
||||||
|
(*p)->Reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool active;
|
||||||
|
std::vector<ParamBase *> m_params;
|
||||||
|
std::map<std::string, ParamBase *> m_map;
|
||||||
|
std::vector<ParamBase *>::const_iterator m_current;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
307
Common/paramgl.h
Normal file
307
Common/paramgl.h
Normal file
@@ -0,0 +1,307 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
ParamListGL
|
||||||
|
- class derived from ParamList to do simple OpenGL rendering of a parameter
|
||||||
|
list sgg 8/2001
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PARAMGL_H
|
||||||
|
#define PARAMGL_H
|
||||||
|
|
||||||
|
#if defined(__APPLE__) || defined(MACOSX)
|
||||||
|
#include <GLUT/glut.h>
|
||||||
|
#else
|
||||||
|
#include <GL/freeglut.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <param.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
inline void beginWinCoords(void) {
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glPushMatrix();
|
||||||
|
glLoadIdentity();
|
||||||
|
glTranslatef(0.0, (GLfloat)(glutGet(GLUT_WINDOW_HEIGHT) - 1.0), 0.0);
|
||||||
|
glScalef(1.0, -1.0, 1.0);
|
||||||
|
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glPushMatrix();
|
||||||
|
glLoadIdentity();
|
||||||
|
glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), 0, glutGet(GLUT_WINDOW_HEIGHT), -1, 1);
|
||||||
|
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void endWinCoords(void) {
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glPopMatrix();
|
||||||
|
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glPopMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void glPrint(int x, int y, const char *s, void *font) {
|
||||||
|
glRasterPos2f((GLfloat)x, (GLfloat)y);
|
||||||
|
int len = (int)strlen(s);
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
glutBitmapCharacter(font, s[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void glPrintShadowed(int x, int y, const char *s, void *font,
|
||||||
|
float *color) {
|
||||||
|
glColor3f(0.0, 0.0, 0.0);
|
||||||
|
glPrint(x - 1, y - 1, s, font);
|
||||||
|
|
||||||
|
glColor3fv((GLfloat *)color);
|
||||||
|
glPrint(x, y, s, font);
|
||||||
|
}
|
||||||
|
|
||||||
|
class ParamListGL : public ParamList {
|
||||||
|
public:
|
||||||
|
ParamListGL(const char *name = "")
|
||||||
|
: ParamList(name),
|
||||||
|
m_active(true),
|
||||||
|
m_text_color_selected(1.0, 1.0, 1.0),
|
||||||
|
m_text_color_unselected(0.75, 0.75, 0.75),
|
||||||
|
m_text_color_shadow(0.0, 0.0, 0.0),
|
||||||
|
m_bar_color_outer(0.25, 0.25, 0.25),
|
||||||
|
m_bar_color_inner(1.0, 1.0, 1.0) {
|
||||||
|
m_font = (void *)GLUT_BITMAP_9_BY_15; // GLUT_BITMAP_8_BY_13;
|
||||||
|
m_font_h = 15;
|
||||||
|
m_bar_x = 260;
|
||||||
|
m_bar_w = 250;
|
||||||
|
m_bar_h = 10;
|
||||||
|
m_bar_offset = 5;
|
||||||
|
m_text_x = 5;
|
||||||
|
m_separation = 15;
|
||||||
|
m_value_x = 200;
|
||||||
|
m_start_x = 0;
|
||||||
|
m_start_y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Render(int x, int y, bool shadow = false) {
|
||||||
|
beginWinCoords();
|
||||||
|
|
||||||
|
m_start_x = x;
|
||||||
|
m_start_y = y;
|
||||||
|
|
||||||
|
for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
|
||||||
|
p != m_params.end(); ++p) {
|
||||||
|
if ((*p)->IsList()) {
|
||||||
|
ParamListGL *list = (ParamListGL *)(*p);
|
||||||
|
list->Render(x + 10, y);
|
||||||
|
y += m_separation * list->GetSize();
|
||||||
|
} else {
|
||||||
|
if (p == m_current) {
|
||||||
|
glColor3fv(&m_text_color_selected.r);
|
||||||
|
} else {
|
||||||
|
glColor3fv(&m_text_color_unselected.r);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shadow) {
|
||||||
|
glPrintShadowed(x + m_text_x, y + m_font_h, (*p)->GetName().c_str(),
|
||||||
|
m_font,
|
||||||
|
(p == m_current) ? &m_text_color_selected.r
|
||||||
|
: &m_text_color_unselected.r);
|
||||||
|
glPrintShadowed(x + m_value_x, y + m_font_h,
|
||||||
|
(*p)->GetValueString().c_str(), m_font,
|
||||||
|
(p == m_current) ? &m_text_color_selected.r
|
||||||
|
: &m_text_color_unselected.r);
|
||||||
|
} else {
|
||||||
|
glPrint(x + m_text_x, y + m_font_h, (*p)->GetName().c_str(), m_font);
|
||||||
|
glPrint(x + m_value_x, y + m_font_h, (*p)->GetValueString().c_str(),
|
||||||
|
m_font);
|
||||||
|
}
|
||||||
|
|
||||||
|
glColor3fv((GLfloat *)&m_bar_color_outer.r);
|
||||||
|
glBegin(GL_LINE_LOOP);
|
||||||
|
glVertex2f((GLfloat)(x + m_bar_x), (GLfloat)(y + m_bar_offset));
|
||||||
|
glVertex2f((GLfloat)(x + m_bar_x + m_bar_w),
|
||||||
|
(GLfloat)(y + m_bar_offset));
|
||||||
|
glVertex2f((GLfloat)(x + m_bar_x + m_bar_w),
|
||||||
|
(GLfloat)(y + m_bar_offset + m_bar_h));
|
||||||
|
glVertex2f((GLfloat)(x + m_bar_x),
|
||||||
|
(GLfloat)(y + m_bar_offset + m_bar_h));
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
glColor3fv((GLfloat *)&m_bar_color_inner.r);
|
||||||
|
glRectf(
|
||||||
|
(GLfloat)(x + m_bar_x), (GLfloat)(y + m_bar_offset + m_bar_h),
|
||||||
|
(GLfloat)(x + m_bar_x + ((m_bar_w - 1) * (*p)->GetPercentage())),
|
||||||
|
(GLfloat)(y + m_bar_offset + 1));
|
||||||
|
|
||||||
|
y += m_separation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
endWinCoords();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Mouse(int x, int y, int button = GLUT_LEFT_BUTTON,
|
||||||
|
int state = GLUT_DOWN) {
|
||||||
|
if ((y < m_start_y) ||
|
||||||
|
(y > (int)(m_start_y + (m_separation * m_params.size()) - 1))) {
|
||||||
|
m_active = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_active = true;
|
||||||
|
|
||||||
|
int i = (y - m_start_y) / m_separation;
|
||||||
|
|
||||||
|
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) {
|
||||||
|
#if defined(__GNUC__) && (__GNUC__ < 3)
|
||||||
|
m_current = &m_params[i];
|
||||||
|
#else
|
||||||
|
|
||||||
|
// MJH: workaround since the version of vector::at used here is
|
||||||
|
// non-standard
|
||||||
|
for (m_current = m_params.begin(); m_current != m_params.end() && i > 0;
|
||||||
|
m_current++, i--)
|
||||||
|
;
|
||||||
|
|
||||||
|
// m_current = (std::vector<ParamBase
|
||||||
|
// *>::const_iterator)&m_params.at(i);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if ((x > m_bar_x) && (x < m_bar_x + m_bar_w)) {
|
||||||
|
Motion(x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Motion(int x, int y) {
|
||||||
|
if ((y < m_start_y) ||
|
||||||
|
(y > m_start_y + (m_separation * (int)m_params.size()) - 1)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x < m_bar_x) {
|
||||||
|
(*m_current)->SetPercentage(0.0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x > m_bar_x + m_bar_w) {
|
||||||
|
(*m_current)->SetPercentage(1.0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
(*m_current)->SetPercentage((x - m_bar_x) / (float)m_bar_w);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Special(int key, int x, int y) {
|
||||||
|
if (!m_active) return;
|
||||||
|
|
||||||
|
switch (key) {
|
||||||
|
case GLUT_KEY_DOWN:
|
||||||
|
Increment();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GLUT_KEY_UP:
|
||||||
|
Decrement();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GLUT_KEY_RIGHT:
|
||||||
|
GetCurrent()->Increment();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GLUT_KEY_LEFT:
|
||||||
|
GetCurrent()->Decrement();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GLUT_KEY_HOME:
|
||||||
|
GetCurrent()->Reset();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GLUT_KEY_END:
|
||||||
|
GetCurrent()->SetPercentage(1.0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
glutPostRedisplay();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetFont(void *font, int height) {
|
||||||
|
m_font = font;
|
||||||
|
m_font_h = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetSelectedColor(float r, float g, float b) {
|
||||||
|
m_text_color_selected = Color(r, g, b);
|
||||||
|
}
|
||||||
|
void SetUnSelectedColor(float r, float g, float b) {
|
||||||
|
m_text_color_unselected = Color(r, g, b);
|
||||||
|
}
|
||||||
|
void SetBarColorInner(float r, float g, float b) {
|
||||||
|
m_bar_color_inner = Color(r, g, b);
|
||||||
|
}
|
||||||
|
void SetBarColorOuter(float r, float g, float b) {
|
||||||
|
m_bar_color_outer = Color(r, g, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetActive(bool b) { m_active = b; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void *m_font;
|
||||||
|
int m_font_h; // font height
|
||||||
|
|
||||||
|
int m_bar_x; // bar start x position
|
||||||
|
int m_bar_w; // bar width
|
||||||
|
int m_bar_h; // bar height
|
||||||
|
int m_text_x; // text start x position
|
||||||
|
int m_separation; // bar separation in y
|
||||||
|
int m_value_x; // value text x position
|
||||||
|
int m_bar_offset; // bar offset in y
|
||||||
|
|
||||||
|
int m_start_x, m_start_y;
|
||||||
|
|
||||||
|
bool m_active;
|
||||||
|
|
||||||
|
struct Color {
|
||||||
|
Color(float _r, float _g, float _b) {
|
||||||
|
r = _r;
|
||||||
|
g = _g;
|
||||||
|
b = _b;
|
||||||
|
}
|
||||||
|
float r, g, b;
|
||||||
|
};
|
||||||
|
|
||||||
|
Color m_text_color_selected;
|
||||||
|
Color m_text_color_unselected;
|
||||||
|
Color m_text_color_shadow;
|
||||||
|
Color m_bar_color_outer;
|
||||||
|
Color m_bar_color_inner;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
|||||||
1347
Common/rendercheck_gl.h
Normal file
1347
Common/rendercheck_gl.h
Normal file
File diff suppressed because it is too large
Load Diff
1312
Common/rendercheck_gles.h
Normal file
1312
Common/rendercheck_gles.h
Normal file
File diff suppressed because it is too large
Load Diff
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
modification, are permitted provided that the following conditions
|
modification, are permitted provided that the following conditions
|
||||||
|
|||||||
64
Makefile
64
Makefile
@@ -1,64 +0,0 @@
|
|||||||
###############################################################################
|
|
||||||
#
|
|
||||||
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
|
||||||
#
|
|
||||||
# Redistribution and use in source and binary forms, with or without
|
|
||||||
# modification, are permitted provided that the following conditions
|
|
||||||
# are met:
|
|
||||||
# * Redistributions of source code must retain the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer.
|
|
||||||
# * Redistributions in binary form must reproduce the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer in the
|
|
||||||
# documentation and/or other materials provided with the distribution.
|
|
||||||
# * Neither the name of NVIDIA CORPORATION nor the names of its
|
|
||||||
# contributors may be used to endorse or promote products derived
|
|
||||||
# from this software without specific prior written permission.
|
|
||||||
#
|
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
|
||||||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
||||||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
||||||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
||||||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
||||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
||||||
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
||||||
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
#
|
|
||||||
###############################################################################
|
|
||||||
#
|
|
||||||
# CUDA Samples
|
|
||||||
#
|
|
||||||
###############################################################################
|
|
||||||
|
|
||||||
TARGET_ARCH ?= $(shell uname -m)
|
|
||||||
|
|
||||||
# Project folders that contain CUDA samples
|
|
||||||
PROJECTS ?= $(shell find Samples -name Makefile)
|
|
||||||
|
|
||||||
FILTER_OUT :=
|
|
||||||
|
|
||||||
PROJECTS := $(filter-out $(FILTER_OUT),$(PROJECTS))
|
|
||||||
|
|
||||||
%.ph_build :
|
|
||||||
+@$(MAKE) -C $(dir $*) $(MAKECMDGOALS)
|
|
||||||
|
|
||||||
%.ph_clean :
|
|
||||||
+@$(MAKE) -C $(dir $*) clean $(USE_DEVICE)
|
|
||||||
|
|
||||||
%.ph_clobber :
|
|
||||||
+@$(MAKE) -C $(dir $*) clobber $(USE_DEVICE)
|
|
||||||
|
|
||||||
all: $(addsuffix .ph_build,$(PROJECTS))
|
|
||||||
@echo "Finished building CUDA samples"
|
|
||||||
|
|
||||||
build: $(addsuffix .ph_build,$(PROJECTS))
|
|
||||||
|
|
||||||
tidy:
|
|
||||||
@find * | egrep "#" | xargs rm -f
|
|
||||||
@find * | egrep "\~" | xargs rm -f
|
|
||||||
|
|
||||||
clean: tidy $(addsuffix .ph_clean,$(PROJECTS))
|
|
||||||
|
|
||||||
clobber: clean $(addsuffix .ph_clobber,$(PROJECTS))
|
|
||||||
306
README.md
306
README.md
@@ -1,119 +1,20 @@
|
|||||||
# CUDA Samples
|
# CUDA Samples
|
||||||
|
|
||||||
Samples for CUDA Developers which demonstrates features in CUDA Toolkit. This version supports [CUDA Toolkit 11.4](https://developer.nvidia.com/cuda-downloads).
|
Samples for CUDA Developers which demonstrates features in CUDA Toolkit. This version supports [CUDA Toolkit 12.6](https://developer.nvidia.com/cuda-downloads).
|
||||||
|
|
||||||
## Release Notes
|
## Release Notes
|
||||||
|
|
||||||
This section describes the release notes for the CUDA Samples on GitHub only.
|
This section describes the release notes for the CUDA Samples on GitHub only.
|
||||||
|
|
||||||
### CUDA 11.4
|
### Change Log
|
||||||
* Added `cdpQuadtree`. Demonstrates Quad Trees implementation using CUDA Dynamic Parallelism.
|
|
||||||
* Updated `simpleVulkan`, `simpleVulkanMMAP` and `vulkanImageCUDA`. Demonstrates use of SPIR-V shaders.
|
|
||||||
|
|
||||||
### CUDA 11.3
|
### [Revision History](./CHANGELOG.md)
|
||||||
* Added `streamOrderedAllocationIPC`. Demonstrates Inter Process Communication using one process per GPU for computation.
|
|
||||||
* Added `simpleCUBLAS_LU`. Demonstrates batched matrix LU decomposition using cuBLAS API `cublas<t>getrfBatched()`
|
|
||||||
* Updated `simpleVulkan`. Demonstrates use of timeline semaphore.
|
|
||||||
* Updated multiple samples to use pinned memory using `cudaMallocHost()`.
|
|
||||||
|
|
||||||
### CUDA 11.2
|
|
||||||
* Added `streamOrderedAllocation`. Demonstrates stream ordered memory allocation on a GPU using cudaMallocAsync and cudaMemPool family of APIs.
|
|
||||||
* Added `streamOrderedAllocationP2P`. Demonstrates peer-to-peer access of stream ordered memory allocated using cudaMallocAsync and cudaMemPool family of APIs.
|
|
||||||
* Dropped Visual Studio 2015 support from all the windows supported samples.
|
|
||||||
* FreeImage is no longer distributed with the CUDA Samples. On Windows, see the [Dependencies](#freeimage) section for more details on how to set up FreeImage. On Linux, it is recommended to install FreeImage with your distribution's package manager.
|
|
||||||
* All the samples using CUDA Pipeline & Arrive-wait barriers are been updated to use new `cuda::pipeline` and `cuda::barrier` interfaces.
|
|
||||||
* Updated all the samples to build with parallel build option `--threads` of `nvcc` cuda compiler.
|
|
||||||
* Added `cudaNvSciNvMedia`. Demonstrates CUDA-NvMedia interop via NvSciBuf/NvSciSync APIs.
|
|
||||||
* Added `simpleGL`. Demonstrates interoperability between CUDA and OpenGL.
|
|
||||||
|
|
||||||
### CUDA 11.1
|
|
||||||
* Added `watershedSegmentationNPP`. Demonstrates how to use the NPP watershed segmentation function.
|
|
||||||
* Added `batchedLabelMarkersAndLabelCompressionNPP`. Demonstrates how to use the NPP label markers generation and label compression functions based on a Union Find (UF) algorithm including both single image and batched image versions.
|
|
||||||
* Dropped Visual Studio 2012, 2013 support from all the windows supported samples.
|
|
||||||
* Added kernel performing warp aggregated atomic max in multi buckets using cg::labeled_partition & cg::reduce in `warpAggregatedAtomicsCG`.
|
|
||||||
* Added extended CG shuffle mechanics to `shfl_scan` sample.
|
|
||||||
* Added `cudaOpenMP`. Demonstrates how to use OpenMP API to write an application for multiple GPUs.
|
|
||||||
* Added `simpleZeroCopy`. Demonstrates how to use zero copy, kernels can read and write directly to pinned system memory.
|
|
||||||
|
|
||||||
### CUDA 11.0
|
|
||||||
* Added `dmmaTensorCoreGemm`. Demonstrates double precision GEMM computation using the Double precision Warp Matrix Multiply and Accumulate (WMMA) API introduced with CUDA 11 in Ampere chip family tensor cores.
|
|
||||||
* Added `bf16TensorCoreGemm`. Demonstrates __nv_bfloat16 (e8m7) GEMM computation using the __nv_bfloat16 WMMA API introduced with CUDA 11 in Ampere chip family tensor cores.
|
|
||||||
* Added `tf32TensorCoreGemm`. Demonstrates tf32 (e8m10) GEMM computation using the tf32 WMMA API introduced with CUDA 11 in Ampere chip family tensor cores.
|
|
||||||
* Added `globalToShmemAsyncCopy`. Demonstrates async copy of data from global to shared memory when on compute capability 8.0 or higher. Also demonstrates arrive-wait barrier for synchronization.
|
|
||||||
* Added `simpleAWBarrier`. Demonstrates arrive wait barriers.
|
|
||||||
* Added `simpleAttributes`. Demonstrates the stream attributes that affect L2 locality.
|
|
||||||
* Added warp aggregated atomic multi bucket increments kernel using labeled_partition cooperative groups in `warpAggregatedAtomicsCG` which can be used on compute capability 7.0 and above GPU architectures.
|
|
||||||
* Added `binaryPartitionCG`. Demonstrates binary partition cooperative groups and reduction within the thread block.
|
|
||||||
* Added two new reduction kernels in `reduction` one which demonstrates reduce_add_sync intrinstic supported on compute capability 8.0 and another which uses cooperative_groups::reduce function which does thread_block_tile level reduction introduced from CUDA 11.0.
|
|
||||||
* Added `cudaCompressibleMemory`. Demonstrates compressible memory allocation using cuMemMap API.
|
|
||||||
* Added `simpleVulkanMMAP`. Demonstrates Vulkan CUDA Interop via cuMemMap APIs.
|
|
||||||
* Added `concurrentKernels`. Demonstrates the use of CUDA streams for concurrent execution of several kernels on a GPU.
|
|
||||||
* Dropped Mac OSX support from all samples.
|
|
||||||
|
|
||||||
### CUDA 10.2
|
|
||||||
* Added `simpleD3D11`. Demonstrates CUDA-D3D11 External Resource Interoperability APIs for updating D3D11 buffers from CUDA and synchronization between D3D11 and CUDA with Keyed Mutexes.
|
|
||||||
* Added `simpleDrvRuntime`. Demonstrates CUDA Driver and Runtime APIs working together to load fatbinary of a CUDA kernel.
|
|
||||||
* Added `vectorAddMMAP`. Demonstrates how cuMemMap API allows the user to specify the physical properties of their memory while retaining the contiguous nature of their access.
|
|
||||||
* Added `memMapIPCDrv`. Demonstrates Inter Process Communication using cuMemMap APIs.
|
|
||||||
* Added `cudaNvSci`. Demonstrates CUDA-NvSciBuf/NvSciSync Interop.
|
|
||||||
* Added `jacobiCudaGraphs`. Demonstrates Instantiated CUDA Graph Update with Jacobi Iterative Method using different approaches.
|
|
||||||
* Added `cuSolverSp_LinearSolver`. Demonstrates cuSolverSP's LU, QR and Cholesky factorization.
|
|
||||||
* Added `MersenneTwisterGP11213`. Demonstrates the Mersenne Twister random number generator GP11213 in cuRAND.
|
|
||||||
|
|
||||||
### CUDA 10.1 Update 2
|
|
||||||
* Added `vulkanImageCUDA`. Demonstrates how to perform Vulkan image - CUDA Interop.
|
|
||||||
* Added `nvJPEG_encoder`. Demonstrates encoding of jpeg images using NVJPEG Library.
|
|
||||||
* Added Windows OS support to `nvJPEG` sample.
|
|
||||||
* Added `boxFilterNPP`. Demonstrates how to use NPP FilterBox function to perform a box filter.
|
|
||||||
* Added `cannyEdgeDetectorNPP`. Demonstrates the nppiFilterCannyBorder_8u_C1R Canny Edge Detection image filter function.
|
|
||||||
|
|
||||||
### CUDA 10.1 Update 1
|
|
||||||
* Added `NV12toBGRandResize`. Demonstrates how to convert and resize NV12 frames to BGR planars frames using CUDA in batch.
|
|
||||||
* Added `EGLStream_CUDA_Interop`. Demonstrates data exchange between CUDA and EGL Streams.
|
|
||||||
* Added `cuSolverDn_LinearSolver`. Demonstrates cuSolverDN's LU, QR and Cholesky factorization.
|
|
||||||
* Added support of Visual Studio 2019 to all samples supported on [Windows](#windows-1).
|
|
||||||
|
|
||||||
### CUDA 10.1
|
|
||||||
* Added `immaTensorCoreGemm`. Demonstrates integer GEMM computation using the Warp Matrix Multiply and Accumulate (WMMA) API for integers employing the Tensor Cores.
|
|
||||||
* Added `simpleIPC`. Demonstrates Inter Process Communication with one process per GPU for computation.
|
|
||||||
* Added `nvJPEG`. Demonstrates single and batched decoding of jpeg images using NVJPEG Library.
|
|
||||||
* Added `bandwidthTest`. It measures the memcopy bandwidth of the GPU and memcpy bandwidth across PCI-e.
|
|
||||||
* Added `reduction`. Demonstrates several important optimization strategies for Data-Parallel Algorithms like reduction.
|
|
||||||
* Update all the samples to support CUDA 10.1.
|
|
||||||
|
|
||||||
### CUDA 10.0
|
|
||||||
* Added `simpleCudaGraphs`. Demonstrates CUDA Graphs creation, instantiation and launch using Graphs APIs and Stream Capture APIs.
|
|
||||||
* Added `conjugateGradientCudaGraphs`. Demonstrates conjugate gradient solver on GPU using CUBLAS and CUSPARSE library calls captured and called using CUDA Graph APIs.
|
|
||||||
* Added `simpleVulkan`. Demonstrates Vulkan - CUDA Interop.
|
|
||||||
* Added `simpleD3D12`. Demonstrates DX12 - CUDA Interop.
|
|
||||||
* Added `UnifiedMemoryPerf`. Demonstrates performance comparision of various memory types involved in system.
|
|
||||||
* Added `p2pBandwidthLatencyTest`. Demonstrates Peer-To-Peer (P2P) data transfers between pairs of GPUs and computes latency and bandwidth.
|
|
||||||
* Added `systemWideAtomics`. Demonstrates system wide atomic instructions.
|
|
||||||
* Added `simpleCUBLASXT`. Demonstrates CUBLAS-XT library which performs GEMM operations over multiple GPUs.
|
|
||||||
* Added Windows OS support to `conjugateGradientMultiDeviceCG` sample.
|
|
||||||
* Removed support of Visual Studio 2010 from all samples.
|
|
||||||
|
|
||||||
### CUDA 9.2
|
|
||||||
|
|
||||||
This is the first release of CUDA Samples on GitHub:
|
|
||||||
* Added `vectorAdd_nvrtc`. Demonstrates runtime compilation library using NVRTC of a simple vectorAdd kernel.
|
|
||||||
* Added `warpAggregatedAtomicsCG`. Demonstrates warp aggregated atomics using Cooperative Groups.
|
|
||||||
* Added `deviceQuery`. Enumerates the properties of the CUDA devices present in the system.
|
|
||||||
* Added `matrixMul`. Demonstrates a matrix multiplication using shared memory through tiled approach.
|
|
||||||
* Added `matrixMulDrv`. Demonstrates a matrix multiplication using shared memory through tiled approach, uses CUDA Driver API.
|
|
||||||
* Added `cudaTensorCoreGemm`. Demonstrates a GEMM computation using the Warp Matrix Multiply and Accumulate (WMMA) API introduced in CUDA 9, as well as the new Tensor Cores introduced in the Volta chip family.
|
|
||||||
* Added `simpleVoteIntrinsics` which uses *_sync equivalent of the vote intrinsics _any, _all added since CUDA 9.0.
|
|
||||||
* Added `shfl_scan` which uses *_sync equivalent of the shfl intrinsics added since CUDA 9.0.
|
|
||||||
* Added `conjugateGradientMultiBlockCG`. Demonstrates a conjugate gradient solver on GPU using Multi Block Cooperative Groups.
|
|
||||||
* Added `conjugateGradientMultiDeviceCG`. Demonstrates a conjugate gradient solver on multiple GPUs using Multi Device Cooperative Groups, also uses unified memory prefetching and usage hints APIs.
|
|
||||||
* Added `simpleCUBLAS`. Demonstrates how perform GEMM operations using CUBLAS library.
|
|
||||||
* Added `simpleCUFFT`. Demonstrates how perform FFT operations using CUFFT library.
|
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
|
|
||||||
Download and install the [CUDA Toolkit 11.4](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
Download and install the [CUDA Toolkit 12.8](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||||
For system requirements and installation instructions of cuda toolkit, please refer to the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/), and the [Windows Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html).
|
For system requirements and installation instructions of cuda toolkit, please refer to the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/), and the [Windows Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html).
|
||||||
|
|
||||||
### Getting the CUDA Samples
|
### Getting the CUDA Samples
|
||||||
@@ -127,82 +28,131 @@ Without using git the easiest way to use these samples is to download the zip fi
|
|||||||
|
|
||||||
## Building CUDA Samples
|
## Building CUDA Samples
|
||||||
|
|
||||||
### Windows
|
### Building CUDA Samples
|
||||||
|
|
||||||
The Windows samples are built using the Visual Studio IDE. Solution files (.sln) are provided for each supported version of Visual Studio, using the format:
|
The CUDA Samples are built using CMake. Follow the instructions below for building on Linux, Windows, and for cross-compilation to Tegra devices.
|
||||||
```
|
|
||||||
*_vs<version>.sln - for Visual Studio <version>
|
|
||||||
```
|
|
||||||
Complete samples solution files exist at parent directory of the repo:
|
|
||||||
|
|
||||||
Each individual sample has its own set of solution files at:
|
|
||||||
`<CUDA_SAMPLES_REPO>\Samples\<sample_dir>\`
|
|
||||||
|
|
||||||
To build/examine all the samples at once, the complete solution files should be used. To build/examine a single sample, the individual sample solution files should be used.
|
|
||||||
|
|
||||||
### Linux
|
### Linux
|
||||||
The Linux samples are built using makefiles. To use the makefiles, change the current directory to the sample directory you wish to build, and run make:
|
|
||||||
```
|
|
||||||
$ cd <sample_dir>
|
|
||||||
$ make
|
|
||||||
```
|
|
||||||
The samples makefiles can take advantage of certain options:
|
|
||||||
* **TARGET_ARCH=<arch>** - cross-compile targeting a specific architecture. Allowed architectures are x86_64, ppc64le, armv7l, aarch64.
|
|
||||||
By default, TARGET_ARCH is set to HOST_ARCH. On a x86_64 machine, not setting TARGET_ARCH is the equivalent of setting TARGET_ARCH=x86_64.<br/>
|
|
||||||
`$ make TARGET_ARCH=x86_64` <br/> `$ make TARGET_ARCH=ppc64le` <br/> `$ make TARGET_ARCH=armv7l` <br/> `$ make TARGET_ARCH=aarch64` <br/>
|
|
||||||
See [here](http://docs.nvidia.com/cuda/cuda-samples/index.html#cross-samples) for more details on cross platform compilation of cuda samples.
|
|
||||||
* **dbg=1** - build with debug symbols
|
|
||||||
```
|
|
||||||
$ make dbg=1
|
|
||||||
```
|
|
||||||
* **SMS="A B ..."** - override the SM architectures for which the sample will be built, where `"A B ..."` is a space-delimited list of SM architectures. For example, to generate SASS for SM 50 and SM 60, use `SMS="50 60"`.
|
|
||||||
```
|
|
||||||
$ make SMS="50 60"
|
|
||||||
```
|
|
||||||
|
|
||||||
* **HOST_COMPILER=<host_compiler>** - override the default g++ host compiler. See the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements) for a list of supported host compilers.
|
Ensure that CMake (version 3.20 or later) is installed. Install it using your package manager if necessary:
|
||||||
```
|
|
||||||
$ make HOST_COMPILER=g++
|
e.g.
|
||||||
```
|
```sudo apt install cmake```
|
||||||
|
|
||||||
|
Navigate to the root of the cloned repository and create a build directory:
|
||||||
|
```
|
||||||
|
mkdir build && cd build
|
||||||
|
```
|
||||||
|
Configure the project with CMake:
|
||||||
|
```
|
||||||
|
cmake ..
|
||||||
|
```
|
||||||
|
Build the samples:
|
||||||
|
```
|
||||||
|
make -j$(nproc)
|
||||||
|
```
|
||||||
|
Run the samples from their respective directories in the build folder. You can also follow this process from and subdirectory of the samples repo, or from within any individual sample.
|
||||||
|
|
||||||
|
### Windows
|
||||||
|
|
||||||
|
Language services for CMake are available in Visual Studio 2019 version 16.5 or later, and you can directly import the CUDA samples repository from either the root level or from any
|
||||||
|
subdirectory or individual sample.
|
||||||
|
|
||||||
|
To build from the command line, open the `x64 Native Tools Command Prompt for VS` provided with your Visual Studio installation.
|
||||||
|
|
||||||
|
Navigate to the root of the cloned repository and create a build directory:
|
||||||
|
```
|
||||||
|
mkdir build && cd build
|
||||||
|
```
|
||||||
|
Configure the project with CMake - for example:
|
||||||
|
```
|
||||||
|
cmake .. -G "Visual Studio 16 2019" -A x64
|
||||||
|
```
|
||||||
|
Open the generated solution file CUDA_Samples.sln in Visual Studio. Build the samples by selecting the desired configuration (e.g., Debug or Release) and pressing F7 (Build Solution).
|
||||||
|
|
||||||
|
Run the samples from the output directories specified in Visual Studio.
|
||||||
|
|
||||||
|
### Platform-Specific Samples
|
||||||
|
|
||||||
|
Some CUDA samples are specific to certain platforms, and require passing flags into CMake to enable. In particular, we define the following platform-specific flags:
|
||||||
|
|
||||||
|
* `BUILD_TEGRA` - for Tegra-specific samples
|
||||||
|
|
||||||
|
To build these samples, set the variables either on the command line or through your CMake GUI. For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
cmake -DBUILD_TEGRA=True ..
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cross-Compilation for Tegra Platforms
|
||||||
|
|
||||||
|
Install the NVIDIA toolchain and cross-compilation environment for Tegra devices as described in the Tegra Development Guide.
|
||||||
|
|
||||||
|
Ensure that CMake (version 3.20 or later) is installed.
|
||||||
|
|
||||||
|
Navigate to the root of the cloned repository and create a build directory:
|
||||||
|
```
|
||||||
|
mkdir build && cd build
|
||||||
|
```
|
||||||
|
Configure the project with CMake, specifying the Tegra toolchain file:
|
||||||
|
```
|
||||||
|
cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/tegra/toolchain.cmake
|
||||||
|
```
|
||||||
|
Build the samples:
|
||||||
|
```
|
||||||
|
make -j$(nproc)
|
||||||
|
```
|
||||||
|
Transfer the built binaries to the Tegra device and execute them there.
|
||||||
|
|
||||||
|
### Building for Automotive Linux Platforms
|
||||||
|
|
||||||
|
These platforms require additional information to be passed to CMake on the command line to ensure proper resolution of all necessary include and library files.
|
||||||
|
Instead of being in the default location, `/usr/local/cuda/include` or `/usr/local/cuda/lib64`, you must point to architecture-specific paths:
|
||||||
|
|
||||||
|
`/usr/local/cuda/<ARCH>/targets/aarch64-linux/lib`
|
||||||
|
and
|
||||||
|
`/usr/local/cuda-12.8/<ARCH>/include`
|
||||||
|
|
||||||
|
An example build might look like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ mkdir build
|
||||||
|
$ cd build
|
||||||
|
|
||||||
|
$ cmake -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DCMAKE_LIBRARY_PATH=/usr/local/cuda/orin/lib64/ -DCMAKE_INCLUDE_PATH=/usr/local/cuda/orin/include -DBUILD_TEGRA=True ..
|
||||||
|
```
|
||||||
|
|
||||||
|
### QNX
|
||||||
|
|
||||||
|
Note that in the current branch sample cross-compilation for QNX is not fully validated. This placeholder will be updated in the
|
||||||
|
near future with QNX cross-compilation instructions. In the meantime, if you want to cross-compile for QNX please check out one
|
||||||
|
of the previous tags prior to the CMake build system transition in 12.8.
|
||||||
|
|
||||||
## Samples list
|
## Samples list
|
||||||
|
|
||||||
### Samples by OS
|
### [0. Introduction](./Samples/0_Introduction/README.md)
|
||||||
|
Basic CUDA samples for beginners that illustrate key concepts with using CUDA and CUDA runtime APIs.
|
||||||
|
|
||||||
#### Linux
|
### [1. Utilities](./Samples/1_Utilities/README.md)
|
||||||
**[vectorAdd_nvrtc](./Samples/vectorAdd_nvrtc)** | **[shfl_scan](./Samples/shfl_scan)** | **[binaryPartitionCG](./Samples/binaryPartitionCG)** | **[concurrentKernels](./Samples/concurrentKernels)** |
|
Utility samples that demonstrate how to query device capabilities and measure GPU/CPU bandwidth.
|
||||||
---|---|---|---|
|
|
||||||
**[streamOrderedAllocationIPC](./Samples/streamOrderedAllocationIPC)** | **[UnifiedMemoryPerf](./Samples/UnifiedMemoryPerf)** | **[bf16TensorCoreGemm](./Samples/bf16TensorCoreGemm)** | **[cuSolverSp_LinearSolver](./Samples/cuSolverSp_LinearSolver)** |
|
|
||||||
**[nvJPEG](./Samples/nvJPEG)** | **[watershedSegmentationNPP](./Samples/watershedSegmentationNPP)** | **[vulkanImageCUDA](./Samples/vulkanImageCUDA)** | **[boxFilterNPP](./Samples/boxFilterNPP)** |
|
|
||||||
**[matrixMul](./Samples/matrixMul)** | **[nvJPEG_encoder](./Samples/nvJPEG_encoder)** | **[simpleCudaGraphs](./Samples/simpleCudaGraphs)** | **[simpleCUFFT](./Samples/simpleCUFFT)** |
|
|
||||||
**[cudaTensorCoreGemm](./Samples/cudaTensorCoreGemm)** | **[cudaOpenMP](./Samples/cudaOpenMP)** | **[NV12toBGRandResize](./Samples/NV12toBGRandResize)** | **[cudaNvSci](./Samples/cudaNvSci)** |
|
|
||||||
**[immaTensorCoreGemm](./Samples/immaTensorCoreGemm)** | **[simpleVulkan](./Samples/simpleVulkan)** | **[simpleCUBLAS](./Samples/simpleCUBLAS)** | **[jacobiCudaGraphs](./Samples/jacobiCudaGraphs)** |
|
|
||||||
**[streamOrderedAllocationP2P](./Samples/streamOrderedAllocationP2P)** | **[dmmaTensorCoreGemm](./Samples/dmmaTensorCoreGemm)** | **[EGLStream_CUDA_Interop](./Samples/EGLStream_CUDA_Interop)** | **[vectorAddMMAP](./Samples/vectorAddMMAP)** |
|
|
||||||
**[MersenneTwisterGP11213](./Samples/MersenneTwisterGP11213)** | **[streamOrderedAllocation](./Samples/streamOrderedAllocation)** | **[cuSolverDn_LinearSolver](./Samples/cuSolverDn_LinearSolver)** | **[cudaCompressibleMemory](./Samples/cudaCompressibleMemory)** |
|
|
||||||
**[simpleAWBarrier](./Samples/simpleAWBarrier)** | **[simpleGL](./Samples/simpleGL)** | **[p2pBandwidthLatencyTest](./Samples/p2pBandwidthLatencyTest)** | **[conjugateGradientCudaGraphs](./Samples/conjugateGradientCudaGraphs)** |
|
|
||||||
**[globalToShmemAsyncCopy](./Samples/globalToShmemAsyncCopy)** | **[deviceQuery](./Samples/deviceQuery)** | **[systemWideAtomics](./Samples/systemWideAtomics)** | **[matrixMulDrv](./Samples/matrixMulDrv)** |
|
|
||||||
**[conjugateGradientMultiBlockCG](./Samples/conjugateGradientMultiBlockCG)** | **[simpleZeroCopy](./Samples/simpleZeroCopy)** | **[simpleAttributes](./Samples/simpleAttributes)** | **[batchedLabelMarkersAndLabelCompressionNPP](./Samples/batchedLabelMarkersAndLabelCompressionNPP)** |
|
|
||||||
**[conjugateGradientMultiDeviceCG](./Samples/conjugateGradientMultiDeviceCG)** | **[simpleIPC](./Samples/simpleIPC)** | **[simpleVulkanMMAP](./Samples/simpleVulkanMMAP)** | **[cudaNvSciNvMedia](./Samples/cudaNvSciNvMedia)** |
|
|
||||||
**[simpleVoteIntrinsics](./Samples/simpleVoteIntrinsics)** | **[simpleCUBLAS_LU](./Samples/simpleCUBLAS_LU)** | **[bandwidthTest](./Samples/bandwidthTest)** | **[cdpQuadtree](./Samples/cdpQuadtree)** |
|
|
||||||
**[simpleCUBLASXT](./Samples/simpleCUBLASXT)** | **[tf32TensorCoreGemm](./Samples/tf32TensorCoreGemm)** | **[simpleDrvRuntime](./Samples/simpleDrvRuntime)** | **[reduction](./Samples/reduction)** |
|
|
||||||
**[memMapIPCDrv](./Samples/memMapIPCDrv)** | **[warpAggregatedAtomicsCG](./Samples/warpAggregatedAtomicsCG)** | **[cannyEdgeDetectorNPP](./Samples/cannyEdgeDetectorNPP)** |
|
|
||||||
|
|
||||||
#### Windows
|
### [2. Concepts and Techniques](./Samples/2_Concepts_and_Techniques/README.md)
|
||||||
**[vectorAdd_nvrtc](./Samples/vectorAdd_nvrtc)** | **[shfl_scan](./Samples/shfl_scan)** | **[binaryPartitionCG](./Samples/binaryPartitionCG)** | **[concurrentKernels](./Samples/concurrentKernels)** |
|
Samples that demonstrate CUDA related concepts and common problem solving techniques.
|
||||||
---|---|---|---|
|
|
||||||
**[UnifiedMemoryPerf](./Samples/UnifiedMemoryPerf)** | **[bf16TensorCoreGemm](./Samples/bf16TensorCoreGemm)** | **[cuSolverSp_LinearSolver](./Samples/cuSolverSp_LinearSolver)** | **[nvJPEG](./Samples/nvJPEG)** |
|
### [3. CUDA Features](./Samples/3_CUDA_Features/README.md)
|
||||||
**[watershedSegmentationNPP](./Samples/watershedSegmentationNPP)** | **[vulkanImageCUDA](./Samples/vulkanImageCUDA)** | **[boxFilterNPP](./Samples/boxFilterNPP)** | **[matrixMul](./Samples/matrixMul)** |
|
Samples that demonstrate CUDA Features (Cooperative Groups, CUDA Dynamic Parallelism, CUDA Graphs etc).
|
||||||
**[nvJPEG_encoder](./Samples/nvJPEG_encoder)** | **[simpleCudaGraphs](./Samples/simpleCudaGraphs)** | **[simpleCUFFT](./Samples/simpleCUFFT)** | **[cudaTensorCoreGemm](./Samples/cudaTensorCoreGemm)** |
|
|
||||||
**[cudaOpenMP](./Samples/cudaOpenMP)** | **[NV12toBGRandResize](./Samples/NV12toBGRandResize)** | **[immaTensorCoreGemm](./Samples/immaTensorCoreGemm)** | **[simpleVulkan](./Samples/simpleVulkan)** |
|
### [4. CUDA Libraries](./Samples/4_CUDA_Libraries/README.md)
|
||||||
**[simpleCUBLAS](./Samples/simpleCUBLAS)** | **[jacobiCudaGraphs](./Samples/jacobiCudaGraphs)** | **[streamOrderedAllocationP2P](./Samples/streamOrderedAllocationP2P)** | **[dmmaTensorCoreGemm](./Samples/dmmaTensorCoreGemm)** |
|
Samples that demonstrate how to use CUDA platform libraries (NPP, NVJPEG, NVGRAPH cuBLAS, cuFFT, cuSPARSE, cuSOLVER and cuRAND).
|
||||||
**[vectorAddMMAP](./Samples/vectorAddMMAP)** | **[MersenneTwisterGP11213](./Samples/MersenneTwisterGP11213)** | **[streamOrderedAllocation](./Samples/streamOrderedAllocation)** | **[cuSolverDn_LinearSolver](./Samples/cuSolverDn_LinearSolver)** |
|
|
||||||
**[cudaCompressibleMemory](./Samples/cudaCompressibleMemory)** | **[simpleAWBarrier](./Samples/simpleAWBarrier)** | **[simpleGL](./Samples/simpleGL)** | **[p2pBandwidthLatencyTest](./Samples/p2pBandwidthLatencyTest)** |
|
### [5. Domain Specific](./Samples/5_Domain_Specific/README.md)
|
||||||
**[conjugateGradientCudaGraphs](./Samples/conjugateGradientCudaGraphs)** | **[simpleD3D11](./Samples/simpleD3D11)** | **[globalToShmemAsyncCopy](./Samples/globalToShmemAsyncCopy)** | **[deviceQuery](./Samples/deviceQuery)** |
|
Samples that are specific to domain (Graphics, Finance, Image Processing).
|
||||||
**[matrixMulDrv](./Samples/matrixMulDrv)** | **[conjugateGradientMultiBlockCG](./Samples/conjugateGradientMultiBlockCG)** | **[simpleZeroCopy](./Samples/simpleZeroCopy)** | **[simpleAttributes](./Samples/simpleAttributes)** |
|
|
||||||
**[simpleD3D12](./Samples/simpleD3D12)** | **[batchedLabelMarkersAndLabelCompressionNPP](./Samples/batchedLabelMarkersAndLabelCompressionNPP)** | **[conjugateGradientMultiDeviceCG](./Samples/conjugateGradientMultiDeviceCG)** | **[simpleIPC](./Samples/simpleIPC)** |
|
### [6. Performance](./Samples/6_Performance/README.md)
|
||||||
**[simpleVulkanMMAP](./Samples/simpleVulkanMMAP)** | **[simpleVoteIntrinsics](./Samples/simpleVoteIntrinsics)** | **[simpleCUBLAS_LU](./Samples/simpleCUBLAS_LU)** | **[bandwidthTest](./Samples/bandwidthTest)** |
|
Samples that demonstrate performance optimization.
|
||||||
**[cdpQuadtree](./Samples/cdpQuadtree)** | **[simpleCUBLASXT](./Samples/simpleCUBLASXT)** | **[tf32TensorCoreGemm](./Samples/tf32TensorCoreGemm)** | **[simpleDrvRuntime](./Samples/simpleDrvRuntime)** |
|
|
||||||
**[reduction](./Samples/reduction)** | **[memMapIPCDrv](./Samples/memMapIPCDrv)** | **[warpAggregatedAtomicsCG](./Samples/warpAggregatedAtomicsCG)** | **[cannyEdgeDetectorNPP](./Samples/cannyEdgeDetectorNPP)** |
|
### [7. libNVVM](./Samples/7_libNVVM/README.md)
|
||||||
|
Samples that demonstrate the use of libNVVVM and NVVM IR.
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
@@ -220,7 +170,7 @@ These third-party dependencies are required by some CUDA samples. If available,
|
|||||||
|
|
||||||
FreeImage is an open source imaging library. FreeImage can usually be installed on Linux using your distribution's package manager system. FreeImage can also be downloaded from the FreeImage website.
|
FreeImage is an open source imaging library. FreeImage can usually be installed on Linux using your distribution's package manager system. FreeImage can also be downloaded from the FreeImage website.
|
||||||
|
|
||||||
To set up FreeImage on a Windows system, extract the FreeImage DLL distribution into the folder `../../Common/FreeImage/Dist/x64` such that it contains the .h, .dll, and .lib files.
|
To set up FreeImage on a Windows system, extract the FreeImage DLL distribution into the folder `../../../Common/FreeImage/Dist/x64` such that it contains the .h and .lib files. Copy the .dll file to the Release/ Debug/ execution folder or pass the FreeImage folder when cmake configuring with the `-DFREEIMAGE_INCLUDE_DIR` and `-DFREEIMAGE_LIBRARY` options.
|
||||||
|
|
||||||
#### Message Passing Interface
|
#### Message Passing Interface
|
||||||
|
|
||||||
@@ -250,6 +200,11 @@ OpenGL ES is an embedded systems graphics library used for 2D and 3D rendering.
|
|||||||
|
|
||||||
Vulkan is a low-overhead, cross-platform 3D graphics and compute API. Vulkan targets high-performance realtime 3D graphics applications such as video games and interactive media across all platforms. On systems which support Vulkan, NVIDIA's Vulkan implementation is provided with the CUDA Driver. For building and running Vulkan applications one needs to install the [Vulkan SDK](https://www.lunarg.com/vulkan-sdk/).
|
Vulkan is a low-overhead, cross-platform 3D graphics and compute API. Vulkan targets high-performance realtime 3D graphics applications such as video games and interactive media across all platforms. On systems which support Vulkan, NVIDIA's Vulkan implementation is provided with the CUDA Driver. For building and running Vulkan applications one needs to install the [Vulkan SDK](https://www.lunarg.com/vulkan-sdk/).
|
||||||
|
|
||||||
|
#### GLFW
|
||||||
|
GLFW is a lightweight, open-source library designed for managing OpenGL, OpenGL ES, and Vulkan contexts. It simplifies the process of creating and managing windows, handling user input (keyboard, mouse, and joystick), and working with multiple monitors in a cross-platform manner.
|
||||||
|
|
||||||
|
To set up GLFW on a Windows system, Download the pre-built binaries from [GLFW website](https://www.glfw.org/download.html) and extract the zip file into the folder, pass the GLFW include header as `-DGLFW_INCLUDE_DIR` for cmake configuring and follow the Build_instructions.txt in the sample folder to set up the t.
|
||||||
|
|
||||||
#### OpenMP
|
#### OpenMP
|
||||||
|
|
||||||
OpenMP is an API for multiprocessing programming. OpenMP can be installed using your Linux distribution's package manager system. It usually comes preinstalled with GCC. It can also be found at the [OpenMP website](http://openmp.org/).
|
OpenMP is an API for multiprocessing programming. OpenMP can be installed using your Linux distribution's package manager system. It usually comes preinstalled with GCC. It can also be found at the [OpenMP website](http://openmp.org/).
|
||||||
@@ -358,6 +313,10 @@ FP16 is a 16-bit floating-point format. One bit is used for the sign, five bits
|
|||||||
|
|
||||||
NVCC support of [C++11 features](https://en.wikipedia.org/wiki/C++11).
|
NVCC support of [C++11 features](https://en.wikipedia.org/wiki/C++11).
|
||||||
|
|
||||||
|
#### CMake
|
||||||
|
|
||||||
|
The libNVVM samples are built using [CMake](https://cmake.org/) 3.10 or later.
|
||||||
|
|
||||||
## Contributors Guide
|
## Contributors Guide
|
||||||
|
|
||||||
We welcome your input on issues and suggestions for samples. At this time we are not accepting contributions from the public, check back here as we evolve our contribution model.
|
We welcome your input on issues and suggestions for samples. At this time we are not accepting contributions from the public, check back here as we evolve our contribution model.
|
||||||
@@ -371,5 +330,8 @@ Answers to frequently asked questions about CUDA can be found at http://develope
|
|||||||
## References
|
## References
|
||||||
|
|
||||||
* [CUDA Programming Guide](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html)
|
* [CUDA Programming Guide](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html)
|
||||||
* [Accelerated Computing Blog](https://devblogs.nvidia.com/category/accelerated-computing/)
|
* [Accelerated Computing Blog](https://developer.nvidia.com/blog/?tags=accelerated-computing)
|
||||||
|
|
||||||
|
## Attributions
|
||||||
|
|
||||||
|
* Teapot image is obtained from [Wikimedia](https://en.wikipedia.org/wiki/File:Original_Utah_Teapot.jpg) and is licensed under the Creative Commons [Attribution-Share Alike 2.0](https://creativecommons.org/licenses/by-sa/2.0/deed.en) Generic license. The image is modified for samples use cases.
|
||||||
|
|||||||
62
Samples/0_Introduction/CMakeLists.txt
Normal file
62
Samples/0_Introduction/CMakeLists.txt
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||||
|
|
||||||
|
project(simpleCallback LANGUAGES C CXX CUDA)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit REQUIRED)
|
||||||
|
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 50 52 60 61 70 72 75 80 86 87 89 90 100 101 120)
|
||||||
|
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (expensive)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
add_subdirectory(UnifiedMemoryStreams)
|
||||||
|
add_subdirectory(asyncAPI)
|
||||||
|
add_subdirectory(clock)
|
||||||
|
add_subdirectory(clock_nvrtc)
|
||||||
|
add_subdirectory(cudaOpenMP)
|
||||||
|
add_subdirectory(fp16ScalarProduct)
|
||||||
|
add_subdirectory(matrixMul)
|
||||||
|
add_subdirectory(matrixMulDrv)
|
||||||
|
add_subdirectory(matrixMulDynlinkJIT)
|
||||||
|
add_subdirectory(matrixMul_nvrtc)
|
||||||
|
add_subdirectory(mergeSort)
|
||||||
|
add_subdirectory(simpleAWBarrier)
|
||||||
|
add_subdirectory(simpleAssert)
|
||||||
|
add_subdirectory(simpleAssert_nvrtc)
|
||||||
|
add_subdirectory(simpleAtomicIntrinsics)
|
||||||
|
add_subdirectory(simpleAtomicIntrinsics_nvrtc)
|
||||||
|
add_subdirectory(simpleAttributes)
|
||||||
|
add_subdirectory(simpleCUDA2GL)
|
||||||
|
add_subdirectory(simpleCallback)
|
||||||
|
add_subdirectory(simpleCooperativeGroups)
|
||||||
|
add_subdirectory(simpleCubemapTexture)
|
||||||
|
add_subdirectory(simpleDrvRuntime)
|
||||||
|
add_subdirectory(simpleHyperQ)
|
||||||
|
add_subdirectory(simpleIPC)
|
||||||
|
add_subdirectory(simpleLayeredTexture)
|
||||||
|
add_subdirectory(simpleMPI)
|
||||||
|
add_subdirectory(simpleMultiCopy)
|
||||||
|
add_subdirectory(simpleMultiGPU)
|
||||||
|
add_subdirectory(simpleOccupancy)
|
||||||
|
add_subdirectory(simpleP2P)
|
||||||
|
add_subdirectory(simplePitchLinearTexture)
|
||||||
|
add_subdirectory(simplePrintf)
|
||||||
|
add_subdirectory(simpleStreams)
|
||||||
|
add_subdirectory(simpleSurfaceWrite)
|
||||||
|
add_subdirectory(simpleTemplates)
|
||||||
|
add_subdirectory(simpleTexture)
|
||||||
|
add_subdirectory(simpleTexture3D)
|
||||||
|
add_subdirectory(simpleTextureDrv)
|
||||||
|
add_subdirectory(simpleVoteIntrinsics)
|
||||||
|
add_subdirectory(simpleZeroCopy)
|
||||||
|
add_subdirectory(systemWideAtomics)
|
||||||
|
add_subdirectory(vectorAdd)
|
||||||
|
add_subdirectory(vectorAddDrv)
|
||||||
|
add_subdirectory(vectorAddMMAP)
|
||||||
|
add_subdirectory(vectorAdd_nvrtc)
|
||||||
140
Samples/0_Introduction/README.md
Normal file
140
Samples/0_Introduction/README.md
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
# 0. Introduction
|
||||||
|
|
||||||
|
|
||||||
|
### [asyncAPI](./asyncAPI)
|
||||||
|
This sample illustrates the usage of CUDA events for both GPU timing and overlapping CPU and GPU execution. Events are inserted into a stream of CUDA calls. Since CUDA stream calls are asynchronous, the CPU can perform computations while GPU is executing (including DMA memcopies between the host and device). CPU can query CUDA events to determine whether GPU has completed tasks.
|
||||||
|
|
||||||
|
### [clock](./clock)
|
||||||
|
This example shows how to use the clock function to measure the performance of block of threads of a kernel accurately.
|
||||||
|
|
||||||
|
### [clock_nvrtc](./clock_nvrtc)
|
||||||
|
This example shows how to use the clock function using libNVRTC to measure the performance of block of threads of a kernel accurately.
|
||||||
|
|
||||||
|
### [cudaOpenMP](./cudaOpenMP)
|
||||||
|
This sample demonstrates how to use OpenMP API to write an application for multiple GPUs.
|
||||||
|
|
||||||
|
### [fp16ScalarProduct](./fp16ScalarProduct)
|
||||||
|
Calculates scalar product of two vectors of FP16 numbers.
|
||||||
|
|
||||||
|
### [matrixMul](./matrixMul)
|
||||||
|
This sample implements matrix multiplication and is exactly the same as Chapter 6 of the programming guide. It has been written for clarity of exposition to illustrate various CUDA programming principles, not with the goal of providing the most performant generic kernel for matrix multiplication. To illustrate GPU performance for matrix multiply, this sample also shows how to use the new CUDA 4.0 interface for CUBLAS to demonstrate high-performance performance for matrix multiplication.
|
||||||
|
|
||||||
|
### [matrixMul_nvrtc](./matrixMul_nvrtc)
|
||||||
|
This sample implements matrix multiplication and is exactly the same as Chapter 6 of the programming guide. It has been written for clarity of exposition to illustrate various CUDA programming principles, not with the goal of providing the most performant generic kernel for matrix multiplication. To illustrate GPU performance for matrix multiply, this sample also shows how to use the new CUDA 4.0 interface for CUBLAS to demonstrate high-performance performance for matrix multiplication.
|
||||||
|
|
||||||
|
### [matrixMulDrv](./matrixMulDrv)
|
||||||
|
This sample implements matrix multiplication and uses the new CUDA 4.0 kernel launch Driver API. It has been written for clarity of exposition to illustrate various CUDA programming principles, not with the goal of providing the most performant generic kernel for matrix multiplication. CUBLAS provides high-performance matrix multiplication.
|
||||||
|
|
||||||
|
### [matrixMulDynlinkJIT](./matrixMulDynlinkJIT)
|
||||||
|
This sample revisits matrix multiplication using the CUDA driver API. It demonstrates how to link to CUDA driver at runtime and how to use JIT (just-in-time) compilation from PTX code. It has been written for clarity of exposition to illustrate various CUDA programming principles, not with the goal of providing the most performant generic kernel for matrix multiplication. CUBLAS provides high-performance matrix multiplication.
|
||||||
|
|
||||||
|
### [mergeSort](./mergeSort)
|
||||||
|
This sample implements a merge sort (also known as Batcher's sort), algorithms belonging to the class of sorting networks. While generally subefficient on large sequences compared to algorithms with better asymptotic algorithmic complexity (i.e. merge sort or radix sort), may be the algorithms of choice for sorting batches of short- to mid-sized (key, value) array pairs. Refer to the excellent tutorial by H. W. Lang http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/networks/indexen.htm
|
||||||
|
|
||||||
|
### [simpleAssert](./simpleAssert)
|
||||||
|
This CUDA Runtime API sample is a very basic sample that implements how to use the assert function in the device code. Requires Compute Capability 2.0 .
|
||||||
|
|
||||||
|
### [simpleAssert_nvrtc](./simpleAssert_nvrtc)
|
||||||
|
This CUDA Runtime API sample is a very basic sample that implements how to use the assert function in the device code. Requires Compute Capability 2.0 .
|
||||||
|
|
||||||
|
### [simpleAtomicIntrinsics](./simpleAtomicIntrinsics)
|
||||||
|
A simple demonstration of global memory atomic instructions.
|
||||||
|
|
||||||
|
### [simpleAtomicIntrinsics_nvrtc](./simpleAtomicIntrinsics_nvrtc)
|
||||||
|
A simple demonstration of global memory atomic instructions.This sample makes use of NVRTC for Runtime Compilation.
|
||||||
|
|
||||||
|
### [simpleAttributes](./simpleAttributes)
|
||||||
|
This CUDA Runtime API sample is a very basic example that implements how to use the stream attributes that affect L2 locality. Performance improvement due to use of L2 access policy window can only be noticed on Compute capability 8.0 or higher.
|
||||||
|
|
||||||
|
### [simpleAWBarrier](./simpleAWBarrier)
|
||||||
|
A simple demonstration of arrive wait barriers.
|
||||||
|
|
||||||
|
### [simpleCallback](./simpleCallback)
|
||||||
|
This sample implements multi-threaded heterogeneous computing workloads with the new CPU callbacks for CUDA streams and events introduced with CUDA 5.0.
|
||||||
|
|
||||||
|
### [simpleCooperativeGroups](./simpleCooperativeGroups)
|
||||||
|
This sample is a simple code that illustrates basic usage of cooperative groups within the thread block.
|
||||||
|
|
||||||
|
### [simpleCubemapTexture](./simpleCubemapTexture)
|
||||||
|
Simple example that demonstrates how to use a new CUDA 4.1 feature to support cubemap Textures in CUDA C.
|
||||||
|
|
||||||
|
### [simpleCUDA2GL](./simpleCUDA2GL)
|
||||||
|
This sample shows how to copy CUDA image back to OpenGL using the most efficient methods.
|
||||||
|
|
||||||
|
### [simpleDrvRuntime](./simpleDrvRuntime)
|
||||||
|
A simple example which demonstrates how CUDA Driver and Runtime APIs can work together to load cuda fatbinary of vector add kernel and performing vector addition.
|
||||||
|
|
||||||
|
### [simpleHyperQ](./simpleHyperQ)
|
||||||
|
This sample demonstrates the use of CUDA streams for concurrent execution of several kernels on devices which provide HyperQ (SM 3.5). Devices without HyperQ (SM 2.0 and SM 3.0) will run a maximum of two kernels concurrently.
|
||||||
|
|
||||||
|
### [simpleIPC](./simpleIPC)
|
||||||
|
This CUDA Runtime API sample is a very basic sample that demonstrates Inter Process Communication with one process per GPU for computation. Requires Compute Capability 3.0 or higher and a Linux Operating System, or a Windows Operating System with TCC enabled GPUs
|
||||||
|
|
||||||
|
### [simpleLayeredTexture](./simpleLayeredTexture)
|
||||||
|
Simple example that demonstrates how to use a new CUDA 4.0 feature to support layered Textures in CUDA C.
|
||||||
|
|
||||||
|
### [simpleMPI](./simpleMPI)
|
||||||
|
Simple example demonstrating how to use MPI in combination with CUDA.
|
||||||
|
|
||||||
|
### [simpleMultiCopy](./simpleMultiCopy)
|
||||||
|
Supported in GPUs with Compute Capability 1.1, overlapping compute with one memcopy is possible from the host system. For Quadro and Tesla GPUs with Compute Capability 2.0, a second overlapped copy operation in either direction at full speed is possible (PCI-e is symmetric). This sample illustrates the usage of CUDA streams to achieve overlapping of kernel execution with data copies to and from the device.
|
||||||
|
|
||||||
|
### [simpleMultiGPU](./simpleMultiGPU)
|
||||||
|
This application demonstrates how to use the new CUDA 4.0 API for CUDA context management and multi-threaded access to run CUDA kernels on multiple-GPUs.
|
||||||
|
|
||||||
|
### [simpleOccupancy](./simpleOccupancy)
|
||||||
|
This sample demonstrates the basic usage of the CUDA occupancy calculator and occupancy-based launch configurator APIs by launching a kernel with the launch configurator, and measures the utilization difference against a manually configured launch.
|
||||||
|
|
||||||
|
### [simpleP2P](./simpleP2P)
|
||||||
|
This application demonstrates CUDA APIs that support Peer-To-Peer (P2P) copies, Peer-To-Peer (P2P) addressing, and Unified Virtual Memory Addressing (UVA) between multiple GPUs. In general, P2P is supported between two same GPUs with some exceptions, such as some Tesla and Quadro GPUs.
|
||||||
|
|
||||||
|
### [simplePitchLinearTexture](./simplePitchLinearTexture)
|
||||||
|
Use of Pitch Linear Textures
|
||||||
|
|
||||||
|
### [simplePrintf](./simplePrintf)
|
||||||
|
This basic CUDA Runtime API sample demonstrates how to use the printf function in the device code.
|
||||||
|
|
||||||
|
### [simpleStreams](./simpleStreams)
|
||||||
|
This sample uses CUDA streams to overlap kernel executions with memory copies between the host and a GPU device. This sample uses a new CUDA 4.0 feature that supports pinning of generic host memory. Requires Compute Capability 2.0 or higher.
|
||||||
|
|
||||||
|
### [simpleSurfaceWrite](./simpleSurfaceWrite)
|
||||||
|
Simple example that demonstrates the use of 2D surface references (Write-to-Texture)
|
||||||
|
|
||||||
|
### [simpleTemplates](./simpleTemplates)
|
||||||
|
This sample is a templatized version of the template project. It also shows how to correctly templatize dynamically allocated shared memory arrays.
|
||||||
|
|
||||||
|
### [simpleTexture](./simpleTexture)
|
||||||
|
Simple example that demonstrates use of Textures in CUDA.
|
||||||
|
|
||||||
|
### [simpleTexture3D](./simpleTexture3D)
|
||||||
|
Simple example that demonstrates use of 3D Textures in CUDA.
|
||||||
|
|
||||||
|
### [simpleTextureDrv](./simpleTextureDrv)
|
||||||
|
Simple example that demonstrates use of Textures in CUDA. This sample uses the new CUDA 4.0 kernel launch Driver API.
|
||||||
|
|
||||||
|
### [simpleVoteIntrinsics](./simpleVoteIntrinsics)
|
||||||
|
Simple program which demonstrates how to use the Vote (__any_sync, __all_sync) intrinsic instruction in a CUDA kernel.
|
||||||
|
|
||||||
|
### [simpleZeroCopy](./simpleZeroCopy)
|
||||||
|
This sample illustrates how to use Zero MemCopy, kernels can read and write directly to pinned system memory.
|
||||||
|
|
||||||
|
### [systemWideAtomics](./systemWideAtomics)
|
||||||
|
A simple demonstration of system wide atomic instructions.
|
||||||
|
|
||||||
|
### [template](./template)
|
||||||
|
A trivial template project that can be used as a starting point to create new CUDA projects.
|
||||||
|
|
||||||
|
### [UnifiedMemoryStreams](./UnifiedMemoryStreams)
|
||||||
|
This sample demonstrates the use of OpenMP and streams with Unified Memory on a single GPU.
|
||||||
|
|
||||||
|
### [vectorAdd](./vectorAdd)
|
||||||
|
This CUDA Runtime API sample is a very basic sample that implements element by element vector addition. It is the same as the sample illustrating Chapter 3 of the programming guide with some additions like error checking.
|
||||||
|
|
||||||
|
### [vectorAdd_nvrtc](./vectorAdd_nvrtc)
|
||||||
|
This CUDA Driver API sample uses NVRTC for runtime compilation of vector addition kernel. Vector addition kernel demonstrated is the same as the sample illustrating Chapter 3 of the programming guide.
|
||||||
|
|
||||||
|
### [vectorAddDrv](./vectorAddDrv)
|
||||||
|
This Vector Addition sample is a basic sample that is implemented element by element. It is the same as the sample illustrating Chapter 3 of the programming guide with some additions like error checking. This sample also uses the new CUDA 4.0 kernel launch Driver API.
|
||||||
|
|
||||||
|
### [vectorAddMMAP](./vectorAddMMAP)
|
||||||
|
This sample replaces the device allocation in the vectorAddDrv sample with cuMemMap-ed allocations. This sample demonstrates that the cuMemMap api allows the user to specify the physical properties of their memory while retaining the contiguous nature of their access, thus not requiring a change in their program structure.
|
||||||
18
Samples/0_Introduction/UnifiedMemoryStreams/.vscode/c_cpp_properties.json
vendored
Normal file
18
Samples/0_Introduction/UnifiedMemoryStreams/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/../../../Common"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||||
|
"cStandard": "gnu17",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"configurationProvider": "ms-vscode.makefile-tools"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
7
Samples/0_Introduction/UnifiedMemoryStreams/.vscode/extensions.json
vendored
Normal file
7
Samples/0_Introduction/UnifiedMemoryStreams/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"nvidia.nsight-vscode-edition",
|
||||||
|
"ms-vscode.cpptools",
|
||||||
|
"ms-vscode.makefile-tools"
|
||||||
|
]
|
||||||
|
}
|
||||||
10
Samples/0_Introduction/UnifiedMemoryStreams/.vscode/launch.json
vendored
Normal file
10
Samples/0_Introduction/UnifiedMemoryStreams/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "CUDA C++: Launch",
|
||||||
|
"type": "cuda-gdb",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/UnifiedMemoryStreams"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
15
Samples/0_Introduction/UnifiedMemoryStreams/.vscode/tasks.json
vendored
Normal file
15
Samples/0_Introduction/UnifiedMemoryStreams/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "sample",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "make dbg=1",
|
||||||
|
"problemMatcher": ["$nvcc"],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
39
Samples/0_Introduction/UnifiedMemoryStreams/CMakeLists.txt
Normal file
39
Samples/0_Introduction/UnifiedMemoryStreams/CMakeLists.txt
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||||
|
|
||||||
|
project(UnifiedMemoryStreams LANGUAGES C CXX CUDA)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit REQUIRED)
|
||||||
|
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 50 52 60 61 70 72 75 80 86 87 89 90 100 101 120)
|
||||||
|
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (expensive)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Include directories and libraries
|
||||||
|
include_directories(../../../Common)
|
||||||
|
|
||||||
|
# Source file
|
||||||
|
find_package(OpenMP REQUIRED)
|
||||||
|
|
||||||
|
if(${OpenMP_FOUND})
|
||||||
|
# Add target for UnifiedMemoryStreams
|
||||||
|
add_executable(UnifiedMemoryStreams UnifiedMemoryStreams.cu)
|
||||||
|
|
||||||
|
target_compile_options(UnifiedMemoryStreams PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||||
|
|
||||||
|
target_compile_features(UnifiedMemoryStreams PRIVATE cxx_std_17 cuda_std_17)
|
||||||
|
|
||||||
|
set_target_properties(UnifiedMemoryStreams PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||||
|
|
||||||
|
target_link_libraries(UnifiedMemoryStreams PUBLIC
|
||||||
|
CUDA::cublas
|
||||||
|
OpenMP::OpenMP_CXX
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
message(STATUS "OpenMP not found - will not build sample 'UnifiedMemoryStreams'")
|
||||||
|
endif()
|
||||||
34
Samples/0_Introduction/UnifiedMemoryStreams/README.md
Normal file
34
Samples/0_Introduction/UnifiedMemoryStreams/README.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# UnifiedMemoryStreams - Unified Memory Streams
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
This sample demonstrates the use of OpenMP and streams with Unified Memory on a single GPU.
|
||||||
|
|
||||||
|
## Key Concepts
|
||||||
|
|
||||||
|
CUDA Systems Integration, OpenMP, CUBLAS, Multithreading, Unified Memory, CUDA Streams and Events
|
||||||
|
|
||||||
|
## Supported SM Architectures
|
||||||
|
|
||||||
|
## Supported OSes
|
||||||
|
|
||||||
|
Linux, Windows
|
||||||
|
|
||||||
|
## Supported CPU Architecture
|
||||||
|
|
||||||
|
x86_64, armv7l
|
||||||
|
|
||||||
|
## CUDA APIs involved
|
||||||
|
|
||||||
|
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||||
|
cudaStreamDestroy, cudaFree, cudaMallocManaged, cudaStreamAttachMemAsync, cudaSetDevice, cudaDeviceSynchronize, cudaStreamSynchronize, cudaStreamCreate, cudaGetDeviceProperties
|
||||||
|
|
||||||
|
## Dependencies needed to build/run
|
||||||
|
[OpenMP](../../../README.md#openmp), [UVM](../../../README.md#uvm), [CUBLAS](../../../README.md#cublas)
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||||
|
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||||
|
|
||||||
|
## References (for more details)
|
||||||
@@ -0,0 +1,341 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This sample implements a simple task consumer using threads and streams
|
||||||
|
* with all data in Unified Memory, and tasks consumed by both host and device
|
||||||
|
*/
|
||||||
|
|
||||||
|
// system includes
|
||||||
|
#include <cstdio>
|
||||||
|
#include <ctime>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
#ifdef USE_PTHREADS
|
||||||
|
#include <pthread.h>
|
||||||
|
#else
|
||||||
|
#include <omp.h>
|
||||||
|
#endif
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// cuBLAS
|
||||||
|
#include <cublas_v2.h>
|
||||||
|
|
||||||
|
// utilities
|
||||||
|
#include <helper_cuda.h>
|
||||||
|
|
||||||
|
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||||
|
// SRAND48 and DRAND48 don't exist on windows, but these are the equivalent
|
||||||
|
// functions
|
||||||
|
void srand48(long seed) { srand((unsigned int)seed); }
|
||||||
|
double drand48() { return double(rand()) / RAND_MAX; }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const char *sSDKname = "UnifiedMemoryStreams";
|
||||||
|
|
||||||
|
// simple task
|
||||||
|
template <typename T>
|
||||||
|
struct Task {
|
||||||
|
unsigned int size, id;
|
||||||
|
T *data;
|
||||||
|
T *result;
|
||||||
|
T *vector;
|
||||||
|
|
||||||
|
Task() : size(0), id(0), data(NULL), result(NULL), vector(NULL){};
|
||||||
|
Task(unsigned int s) : size(s), id(0), data(NULL), result(NULL) {
|
||||||
|
// allocate unified memory -- the operation performed in this example will
|
||||||
|
// be a DGEMV
|
||||||
|
checkCudaErrors(cudaMallocManaged(&data, sizeof(T) * size * size));
|
||||||
|
checkCudaErrors(cudaMallocManaged(&result, sizeof(T) * size));
|
||||||
|
checkCudaErrors(cudaMallocManaged(&vector, sizeof(T) * size));
|
||||||
|
checkCudaErrors(cudaDeviceSynchronize());
|
||||||
|
}
|
||||||
|
|
||||||
|
~Task() {
|
||||||
|
// ensure all memory is deallocated
|
||||||
|
checkCudaErrors(cudaDeviceSynchronize());
|
||||||
|
checkCudaErrors(cudaFree(data));
|
||||||
|
checkCudaErrors(cudaFree(result));
|
||||||
|
checkCudaErrors(cudaFree(vector));
|
||||||
|
}
|
||||||
|
|
||||||
|
void allocate(const unsigned int s, const unsigned int unique_id) {
|
||||||
|
// allocate unified memory outside of constructor
|
||||||
|
id = unique_id;
|
||||||
|
size = s;
|
||||||
|
checkCudaErrors(cudaMallocManaged(&data, sizeof(T) * size * size));
|
||||||
|
checkCudaErrors(cudaMallocManaged(&result, sizeof(T) * size));
|
||||||
|
checkCudaErrors(cudaMallocManaged(&vector, sizeof(T) * size));
|
||||||
|
checkCudaErrors(cudaDeviceSynchronize());
|
||||||
|
|
||||||
|
// populate data with random elements
|
||||||
|
for (unsigned int i = 0; i < size * size; i++) {
|
||||||
|
data[i] = drand48();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < size; i++) {
|
||||||
|
result[i] = 0.;
|
||||||
|
vector[i] = drand48();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef USE_PTHREADS
|
||||||
|
struct threadData_t {
|
||||||
|
int tid;
|
||||||
|
Task<double> *TaskListPtr;
|
||||||
|
cudaStream_t *streams;
|
||||||
|
cublasHandle_t *handles;
|
||||||
|
int taskSize;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct threadData_t threadData;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// simple host dgemv: assume data is in row-major format and square
|
||||||
|
template <typename T>
|
||||||
|
void gemv(int m, int n, T alpha, T *A, T *x, T beta, T *result) {
|
||||||
|
// rows
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
result[i] *= beta;
|
||||||
|
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
result[i] += A[i * n + j] * x[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute a single task on either host or device depending on size
|
||||||
|
#ifdef USE_PTHREADS
|
||||||
|
void *execute(void *inpArgs) {
|
||||||
|
threadData *dataPtr = (threadData *)inpArgs;
|
||||||
|
cudaStream_t *stream = dataPtr->streams;
|
||||||
|
cublasHandle_t *handle = dataPtr->handles;
|
||||||
|
int tid = dataPtr->tid;
|
||||||
|
|
||||||
|
for (int i = 0; i < dataPtr->taskSize; i++) {
|
||||||
|
Task<double> &t = dataPtr->TaskListPtr[i];
|
||||||
|
|
||||||
|
if (t.size < 100) {
|
||||||
|
// perform on host
|
||||||
|
printf("Task [%d], thread [%d] executing on host (%d)\n", t.id, tid,
|
||||||
|
t.size);
|
||||||
|
|
||||||
|
// attach managed memory to a (dummy) stream to allow host access while
|
||||||
|
// the device is running
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaStreamAttachMemAsync(stream[0], t.data, 0, cudaMemAttachHost));
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaStreamAttachMemAsync(stream[0], t.vector, 0, cudaMemAttachHost));
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaStreamAttachMemAsync(stream[0], t.result, 0, cudaMemAttachHost));
|
||||||
|
// necessary to ensure Async cudaStreamAttachMemAsync calls have finished
|
||||||
|
checkCudaErrors(cudaStreamSynchronize(stream[0]));
|
||||||
|
// call the host operation
|
||||||
|
gemv(t.size, t.size, 1.0, t.data, t.vector, 0.0, t.result);
|
||||||
|
} else {
|
||||||
|
// perform on device
|
||||||
|
printf("Task [%d], thread [%d] executing on device (%d)\n", t.id, tid,
|
||||||
|
t.size);
|
||||||
|
double one = 1.0;
|
||||||
|
double zero = 0.0;
|
||||||
|
|
||||||
|
// attach managed memory to my stream
|
||||||
|
checkCudaErrors(cublasSetStream(handle[tid + 1], stream[tid + 1]));
|
||||||
|
checkCudaErrors(cudaStreamAttachMemAsync(stream[tid + 1], t.data, 0,
|
||||||
|
cudaMemAttachSingle));
|
||||||
|
checkCudaErrors(cudaStreamAttachMemAsync(stream[tid + 1], t.vector, 0,
|
||||||
|
cudaMemAttachSingle));
|
||||||
|
checkCudaErrors(cudaStreamAttachMemAsync(stream[tid + 1], t.result, 0,
|
||||||
|
cudaMemAttachSingle));
|
||||||
|
// call the device operation
|
||||||
|
checkCudaErrors(cublasDgemv(handle[tid + 1], CUBLAS_OP_N, t.size, t.size,
|
||||||
|
&one, t.data, t.size, t.vector, 1, &zero,
|
||||||
|
t.result, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
template <typename T>
|
||||||
|
void execute(Task<T> &t, cublasHandle_t *handle, cudaStream_t *stream,
|
||||||
|
int tid) {
|
||||||
|
if (t.size < 100) {
|
||||||
|
// perform on host
|
||||||
|
printf("Task [%d], thread [%d] executing on host (%d)\n", t.id, tid,
|
||||||
|
t.size);
|
||||||
|
|
||||||
|
// attach managed memory to a (dummy) stream to allow host access while the
|
||||||
|
// device is running
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaStreamAttachMemAsync(stream[0], t.data, 0, cudaMemAttachHost));
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaStreamAttachMemAsync(stream[0], t.vector, 0, cudaMemAttachHost));
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaStreamAttachMemAsync(stream[0], t.result, 0, cudaMemAttachHost));
|
||||||
|
// necessary to ensure Async cudaStreamAttachMemAsync calls have finished
|
||||||
|
checkCudaErrors(cudaStreamSynchronize(stream[0]));
|
||||||
|
// call the host operation
|
||||||
|
gemv(t.size, t.size, 1.0, t.data, t.vector, 0.0, t.result);
|
||||||
|
} else {
|
||||||
|
// perform on device
|
||||||
|
printf("Task [%d], thread [%d] executing on device (%d)\n", t.id, tid,
|
||||||
|
t.size);
|
||||||
|
double one = 1.0;
|
||||||
|
double zero = 0.0;
|
||||||
|
|
||||||
|
// attach managed memory to my stream
|
||||||
|
checkCudaErrors(cublasSetStream(handle[tid + 1], stream[tid + 1]));
|
||||||
|
checkCudaErrors(cudaStreamAttachMemAsync(stream[tid + 1], t.data, 0,
|
||||||
|
cudaMemAttachSingle));
|
||||||
|
checkCudaErrors(cudaStreamAttachMemAsync(stream[tid + 1], t.vector, 0,
|
||||||
|
cudaMemAttachSingle));
|
||||||
|
checkCudaErrors(cudaStreamAttachMemAsync(stream[tid + 1], t.result, 0,
|
||||||
|
cudaMemAttachSingle));
|
||||||
|
// call the device operation
|
||||||
|
checkCudaErrors(cublasDgemv(handle[tid + 1], CUBLAS_OP_N, t.size, t.size,
|
||||||
|
&one, t.data, t.size, t.vector, 1, &zero,
|
||||||
|
t.result, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// populate a list of tasks with random sizes
|
||||||
|
template <typename T>
|
||||||
|
void initialise_tasks(std::vector<Task<T> > &TaskList) {
|
||||||
|
for (unsigned int i = 0; i < TaskList.size(); i++) {
|
||||||
|
// generate random size
|
||||||
|
int size;
|
||||||
|
size = std::max((int)(drand48() * 1000.0), 64);
|
||||||
|
TaskList[i].allocate(size, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
// set device
|
||||||
|
cudaDeviceProp device_prop;
|
||||||
|
int dev_id = findCudaDevice(argc, (const char **)argv);
|
||||||
|
checkCudaErrors(cudaGetDeviceProperties(&device_prop, dev_id));
|
||||||
|
|
||||||
|
if (!device_prop.managedMemory) {
|
||||||
|
// This samples requires being run on a device that supports Unified Memory
|
||||||
|
fprintf(stderr, "Unified Memory not supported on this device\n");
|
||||||
|
|
||||||
|
exit(EXIT_WAIVED);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device_prop.computeMode == cudaComputeModeProhibited) {
|
||||||
|
// This sample requires being run with a default or process exclusive mode
|
||||||
|
fprintf(stderr,
|
||||||
|
"This sample requires a device in either default or process "
|
||||||
|
"exclusive mode\n");
|
||||||
|
|
||||||
|
exit(EXIT_WAIVED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomise task sizes
|
||||||
|
int seed = (int)time(NULL);
|
||||||
|
srand48(seed);
|
||||||
|
|
||||||
|
// set number of threads
|
||||||
|
const int nthreads = 4;
|
||||||
|
|
||||||
|
// number of streams = number of threads
|
||||||
|
cudaStream_t *streams = new cudaStream_t[nthreads + 1];
|
||||||
|
cublasHandle_t *handles = new cublasHandle_t[nthreads + 1];
|
||||||
|
|
||||||
|
for (int i = 0; i < nthreads + 1; i++) {
|
||||||
|
checkCudaErrors(cudaStreamCreate(&streams[i]));
|
||||||
|
checkCudaErrors(cublasCreate(&handles[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// create list of N tasks
|
||||||
|
unsigned int N = 40;
|
||||||
|
std::vector<Task<double> > TaskList(N);
|
||||||
|
initialise_tasks(TaskList);
|
||||||
|
|
||||||
|
printf("Executing tasks on host / device\n");
|
||||||
|
|
||||||
|
// run through all tasks using threads and streams
|
||||||
|
#ifdef USE_PTHREADS
|
||||||
|
pthread_t threads[nthreads];
|
||||||
|
threadData *InputToThreads = new threadData[nthreads];
|
||||||
|
|
||||||
|
for (int i = 0; i < nthreads; i++) {
|
||||||
|
checkCudaErrors(cudaSetDevice(dev_id));
|
||||||
|
InputToThreads[i].tid = i;
|
||||||
|
InputToThreads[i].streams = streams;
|
||||||
|
InputToThreads[i].handles = handles;
|
||||||
|
|
||||||
|
if ((TaskList.size() / nthreads) == 0) {
|
||||||
|
InputToThreads[i].taskSize = (TaskList.size() / nthreads);
|
||||||
|
InputToThreads[i].TaskListPtr =
|
||||||
|
&TaskList[i * (TaskList.size() / nthreads)];
|
||||||
|
} else {
|
||||||
|
if (i == nthreads - 1) {
|
||||||
|
InputToThreads[i].taskSize =
|
||||||
|
(TaskList.size() / nthreads) + (TaskList.size() % nthreads);
|
||||||
|
InputToThreads[i].TaskListPtr =
|
||||||
|
&TaskList[i * (TaskList.size() / nthreads) +
|
||||||
|
(TaskList.size() % nthreads)];
|
||||||
|
} else {
|
||||||
|
InputToThreads[i].taskSize = (TaskList.size() / nthreads);
|
||||||
|
InputToThreads[i].TaskListPtr =
|
||||||
|
&TaskList[i * (TaskList.size() / nthreads)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_create(&threads[i], NULL, &execute, &InputToThreads[i]);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < nthreads; i++) {
|
||||||
|
pthread_join(threads[i], NULL);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
omp_set_num_threads(nthreads);
|
||||||
|
#pragma omp parallel for schedule(dynamic)
|
||||||
|
for (int i = 0; i < TaskList.size(); i++) {
|
||||||
|
checkCudaErrors(cudaSetDevice(dev_id));
|
||||||
|
int tid = omp_get_thread_num();
|
||||||
|
execute(TaskList[i], handles, streams, tid);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
cudaDeviceSynchronize();
|
||||||
|
|
||||||
|
// Destroy CUDA Streams, cuBlas handles
|
||||||
|
for (int i = 0; i < nthreads + 1; i++) {
|
||||||
|
cudaStreamDestroy(streams[i]);
|
||||||
|
cublasDestroy(handles[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free TaskList
|
||||||
|
std::vector<Task<double> >().swap(TaskList);
|
||||||
|
|
||||||
|
printf("All Done!\n");
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
18
Samples/0_Introduction/asyncAPI/.vscode/c_cpp_properties.json
vendored
Normal file
18
Samples/0_Introduction/asyncAPI/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/../../../Common"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||||
|
"cStandard": "gnu17",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"configurationProvider": "ms-vscode.makefile-tools"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
7
Samples/0_Introduction/asyncAPI/.vscode/extensions.json
vendored
Normal file
7
Samples/0_Introduction/asyncAPI/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"nvidia.nsight-vscode-edition",
|
||||||
|
"ms-vscode.cpptools",
|
||||||
|
"ms-vscode.makefile-tools"
|
||||||
|
]
|
||||||
|
}
|
||||||
10
Samples/0_Introduction/asyncAPI/.vscode/launch.json
vendored
Normal file
10
Samples/0_Introduction/asyncAPI/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "CUDA C++: Launch",
|
||||||
|
"type": "cuda-gdb",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/asyncAPI"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
15
Samples/0_Introduction/asyncAPI/.vscode/tasks.json
vendored
Normal file
15
Samples/0_Introduction/asyncAPI/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "sample",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "make dbg=1",
|
||||||
|
"problemMatcher": ["$nvcc"],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
28
Samples/0_Introduction/asyncAPI/CMakeLists.txt
Normal file
28
Samples/0_Introduction/asyncAPI/CMakeLists.txt
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||||
|
|
||||||
|
project(asyncAPI LANGUAGES C CXX CUDA)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit REQUIRED)
|
||||||
|
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 50 52 60 61 70 72 75 80 86 87 89 90 100 101 120)
|
||||||
|
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (expensive)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Include directories and libraries
|
||||||
|
include_directories(../../../Common)
|
||||||
|
|
||||||
|
# Source file
|
||||||
|
# Add target for asyncAPI
|
||||||
|
add_executable(asyncAPI asyncAPI.cu)
|
||||||
|
|
||||||
|
target_compile_options(asyncAPI PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||||
|
|
||||||
|
target_compile_features(asyncAPI PRIVATE cxx_std_17 cuda_std_17)
|
||||||
|
|
||||||
|
set_target_properties(asyncAPI PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||||
32
Samples/0_Introduction/asyncAPI/README.md
Normal file
32
Samples/0_Introduction/asyncAPI/README.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# asyncAPI - asyncAPI
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
This sample illustrates the usage of CUDA events for both GPU timing and overlapping CPU and GPU execution. Events are inserted into a stream of CUDA calls. Since CUDA stream calls are asynchronous, the CPU can perform computations while GPU is executing (including DMA memcopies between the host and device). CPU can query CUDA events to determine whether GPU has completed tasks.
|
||||||
|
|
||||||
|
## Key Concepts
|
||||||
|
|
||||||
|
Asynchronous Data Transfers, CUDA Streams and Events
|
||||||
|
|
||||||
|
## Supported SM Architectures
|
||||||
|
|
||||||
|
[SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||||
|
|
||||||
|
## Supported OSes
|
||||||
|
|
||||||
|
Linux, Windows
|
||||||
|
|
||||||
|
## Supported CPU Architecture
|
||||||
|
|
||||||
|
x86_64, armv7l
|
||||||
|
|
||||||
|
## CUDA APIs involved
|
||||||
|
|
||||||
|
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||||
|
cudaProfilerStop, cudaMalloc, cudaMemcpyAsync, cudaFree, cudaMallocHost, cudaProfilerStart, cudaDeviceSynchronize, cudaEventRecord, cudaFreeHost, cudaMemset, cudaEventDestroy, cudaEventQuery, cudaEventElapsedTime, cudaGetDeviceProperties, cudaEventCreate
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||||
|
|
||||||
|
## References (for more details)
|
||||||
142
Samples/0_Introduction/asyncAPI/asyncAPI.cu
Normal file
142
Samples/0_Introduction/asyncAPI/asyncAPI.cu
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This sample illustrates the usage of CUDA events for both GPU timing and
|
||||||
|
* overlapping CPU and GPU execution. Events are inserted into a stream
|
||||||
|
* of CUDA calls. Since CUDA stream calls are asynchronous, the CPU can
|
||||||
|
* perform computations while GPU is executing (including DMA memcopies
|
||||||
|
* between the host and device). CPU can query CUDA events to determine
|
||||||
|
* whether GPU has completed tasks.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// includes, system
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// includes CUDA Runtime
|
||||||
|
#include <cuda_runtime.h>
|
||||||
|
#include <cuda_profiler_api.h>
|
||||||
|
|
||||||
|
// includes, project
|
||||||
|
#include <helper_cuda.h>
|
||||||
|
#include <helper_functions.h> // helper utility functions
|
||||||
|
|
||||||
|
__global__ void increment_kernel(int *g_data, int inc_value) {
|
||||||
|
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||||
|
g_data[idx] = g_data[idx] + inc_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool correct_output(int *data, const int n, const int x) {
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
if (data[i] != x) {
|
||||||
|
printf("Error! data[%d] = %d, ref = %d\n", i, data[i], x);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
int devID;
|
||||||
|
cudaDeviceProp deviceProps;
|
||||||
|
|
||||||
|
printf("[%s] - Starting...\n", argv[0]);
|
||||||
|
|
||||||
|
// This will pick the best possible CUDA capable device
|
||||||
|
devID = findCudaDevice(argc, (const char **)argv);
|
||||||
|
|
||||||
|
// get device name
|
||||||
|
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
|
||||||
|
printf("CUDA device [%s]\n", deviceProps.name);
|
||||||
|
|
||||||
|
int n = 16 * 1024 * 1024;
|
||||||
|
int nbytes = n * sizeof(int);
|
||||||
|
int value = 26;
|
||||||
|
|
||||||
|
// allocate host memory
|
||||||
|
int *a = 0;
|
||||||
|
checkCudaErrors(cudaMallocHost((void **)&a, nbytes));
|
||||||
|
memset(a, 0, nbytes);
|
||||||
|
|
||||||
|
// allocate device memory
|
||||||
|
int *d_a = 0;
|
||||||
|
checkCudaErrors(cudaMalloc((void **)&d_a, nbytes));
|
||||||
|
checkCudaErrors(cudaMemset(d_a, 255, nbytes));
|
||||||
|
|
||||||
|
// set kernel launch configuration
|
||||||
|
dim3 threads = dim3(512, 1);
|
||||||
|
dim3 blocks = dim3(n / threads.x, 1);
|
||||||
|
|
||||||
|
// create cuda event handles
|
||||||
|
cudaEvent_t start, stop;
|
||||||
|
checkCudaErrors(cudaEventCreate(&start));
|
||||||
|
checkCudaErrors(cudaEventCreate(&stop));
|
||||||
|
|
||||||
|
StopWatchInterface *timer = NULL;
|
||||||
|
sdkCreateTimer(&timer);
|
||||||
|
sdkResetTimer(&timer);
|
||||||
|
|
||||||
|
checkCudaErrors(cudaDeviceSynchronize());
|
||||||
|
float gpu_time = 0.0f;
|
||||||
|
|
||||||
|
// asynchronously issue work to the GPU (all to stream 0)
|
||||||
|
checkCudaErrors(cudaProfilerStart());
|
||||||
|
sdkStartTimer(&timer);
|
||||||
|
cudaEventRecord(start, 0);
|
||||||
|
cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, 0);
|
||||||
|
increment_kernel<<<blocks, threads, 0, 0>>>(d_a, value);
|
||||||
|
cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, 0);
|
||||||
|
cudaEventRecord(stop, 0);
|
||||||
|
sdkStopTimer(&timer);
|
||||||
|
checkCudaErrors(cudaProfilerStop());
|
||||||
|
|
||||||
|
// have CPU do some work while waiting for stage 1 to finish
|
||||||
|
unsigned long int counter = 0;
|
||||||
|
|
||||||
|
while (cudaEventQuery(stop) == cudaErrorNotReady) {
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
checkCudaErrors(cudaEventElapsedTime(&gpu_time, start, stop));
|
||||||
|
|
||||||
|
// print the cpu and gpu times
|
||||||
|
printf("time spent executing by the GPU: %.2f\n", gpu_time);
|
||||||
|
printf("time spent by CPU in CUDA calls: %.2f\n", sdkGetTimerValue(&timer));
|
||||||
|
printf("CPU executed %lu iterations while waiting for GPU to finish\n",
|
||||||
|
counter);
|
||||||
|
|
||||||
|
// check the output for correctness
|
||||||
|
bool bFinalResults = correct_output(a, n, value);
|
||||||
|
|
||||||
|
// release resources
|
||||||
|
checkCudaErrors(cudaEventDestroy(start));
|
||||||
|
checkCudaErrors(cudaEventDestroy(stop));
|
||||||
|
checkCudaErrors(cudaFreeHost(a));
|
||||||
|
checkCudaErrors(cudaFree(d_a));
|
||||||
|
|
||||||
|
exit(bFinalResults ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||||
|
}
|
||||||
18
Samples/0_Introduction/clock/.vscode/c_cpp_properties.json
vendored
Normal file
18
Samples/0_Introduction/clock/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/../../../Common"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||||
|
"cStandard": "gnu17",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"configurationProvider": "ms-vscode.makefile-tools"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
7
Samples/0_Introduction/clock/.vscode/extensions.json
vendored
Normal file
7
Samples/0_Introduction/clock/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"nvidia.nsight-vscode-edition",
|
||||||
|
"ms-vscode.cpptools",
|
||||||
|
"ms-vscode.makefile-tools"
|
||||||
|
]
|
||||||
|
}
|
||||||
10
Samples/0_Introduction/clock/.vscode/launch.json
vendored
Normal file
10
Samples/0_Introduction/clock/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "CUDA C++: Launch",
|
||||||
|
"type": "cuda-gdb",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/clock"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
15
Samples/0_Introduction/clock/.vscode/tasks.json
vendored
Normal file
15
Samples/0_Introduction/clock/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "sample",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "make dbg=1",
|
||||||
|
"problemMatcher": ["$nvcc"],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
28
Samples/0_Introduction/clock/CMakeLists.txt
Normal file
28
Samples/0_Introduction/clock/CMakeLists.txt
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||||
|
|
||||||
|
project(clock LANGUAGES C CXX CUDA)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit REQUIRED)
|
||||||
|
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 50 52 60 61 70 72 75 80 86 87 89 90 100 101 120)
|
||||||
|
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (expensive)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Include directories and libraries
|
||||||
|
include_directories(../../../Common)
|
||||||
|
|
||||||
|
# Source file
|
||||||
|
# Add target for asyncAPI
|
||||||
|
add_executable(clock clock.cu)
|
||||||
|
|
||||||
|
target_compile_options(clock PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||||
|
|
||||||
|
target_compile_features(clock PRIVATE cxx_std_17 cuda_std_17)
|
||||||
|
|
||||||
|
set_target_properties(clock PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||||
32
Samples/0_Introduction/clock/README.md
Normal file
32
Samples/0_Introduction/clock/README.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# clock - Clock
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
This example shows how to use the clock function to measure the performance of block of threads of a kernel accurately.
|
||||||
|
|
||||||
|
## Key Concepts
|
||||||
|
|
||||||
|
Performance Strategies
|
||||||
|
|
||||||
|
## Supported SM Architectures
|
||||||
|
|
||||||
|
[SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||||
|
|
||||||
|
## Supported OSes
|
||||||
|
|
||||||
|
Linux, Windows
|
||||||
|
|
||||||
|
## Supported CPU Architecture
|
||||||
|
|
||||||
|
x86_64, armv7l
|
||||||
|
|
||||||
|
## CUDA APIs involved
|
||||||
|
|
||||||
|
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||||
|
cudaMalloc, cudaMemcpy, cudaFree
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||||
|
|
||||||
|
## References (for more details)
|
||||||
153
Samples/0_Introduction/clock/clock.cu
Normal file
153
Samples/0_Introduction/clock/clock.cu
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This example shows how to use the clock function to measure the performance
|
||||||
|
* of block of threads of a kernel accurately. Blocks are executed in parallel
|
||||||
|
* and out of order. Since there's no synchronization mechanism between blocks,
|
||||||
|
* we measure the clock once for each block. The clock samples are written to
|
||||||
|
* device memory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// System includes
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// CUDA runtime
|
||||||
|
#include <cuda_runtime.h>
|
||||||
|
|
||||||
|
// helper functions and utilities to work with CUDA
|
||||||
|
#include <helper_cuda.h>
|
||||||
|
#include <helper_functions.h>
|
||||||
|
|
||||||
|
// This kernel computes a standard parallel reduction and evaluates the
|
||||||
|
// time it takes to do that for each block. The timing results are stored
|
||||||
|
// in device memory.
|
||||||
|
__global__ static void timedReduction(const float *input, float *output,
|
||||||
|
clock_t *timer) {
|
||||||
|
// __shared__ float shared[2 * blockDim.x];
|
||||||
|
extern __shared__ float shared[];
|
||||||
|
|
||||||
|
const int tid = threadIdx.x;
|
||||||
|
const int bid = blockIdx.x;
|
||||||
|
|
||||||
|
if (tid == 0) timer[bid] = clock();
|
||||||
|
|
||||||
|
// Copy input.
|
||||||
|
shared[tid] = input[tid];
|
||||||
|
shared[tid + blockDim.x] = input[tid + blockDim.x];
|
||||||
|
|
||||||
|
// Perform reduction to find minimum.
|
||||||
|
for (int d = blockDim.x; d > 0; d /= 2) {
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
if (tid < d) {
|
||||||
|
float f0 = shared[tid];
|
||||||
|
float f1 = shared[tid + d];
|
||||||
|
|
||||||
|
if (f1 < f0) {
|
||||||
|
shared[tid] = f1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write result.
|
||||||
|
if (tid == 0) output[bid] = shared[0];
|
||||||
|
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
if (tid == 0) timer[bid + gridDim.x] = clock();
|
||||||
|
}
|
||||||
|
|
||||||
|
#define NUM_BLOCKS 64
|
||||||
|
#define NUM_THREADS 256
|
||||||
|
|
||||||
|
// It's interesting to change the number of blocks and the number of threads to
|
||||||
|
// understand how to keep the hardware busy.
|
||||||
|
//
|
||||||
|
// Here are some numbers I get on my G80:
|
||||||
|
// blocks - clocks
|
||||||
|
// 1 - 3096
|
||||||
|
// 8 - 3232
|
||||||
|
// 16 - 3364
|
||||||
|
// 32 - 4615
|
||||||
|
// 64 - 9981
|
||||||
|
//
|
||||||
|
// With less than 16 blocks some of the multiprocessors of the device are idle.
|
||||||
|
// With more than 16 you are using all the multiprocessors, but there's only one
|
||||||
|
// block per multiprocessor and that doesn't allow you to hide the latency of
|
||||||
|
// the memory. With more than 32 the speed scales linearly.
|
||||||
|
|
||||||
|
// Start the main CUDA Sample here
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
printf("CUDA Clock sample\n");
|
||||||
|
|
||||||
|
// This will pick the best possible CUDA capable device
|
||||||
|
int dev = findCudaDevice(argc, (const char **)argv);
|
||||||
|
|
||||||
|
float *dinput = NULL;
|
||||||
|
float *doutput = NULL;
|
||||||
|
clock_t *dtimer = NULL;
|
||||||
|
|
||||||
|
clock_t timer[NUM_BLOCKS * 2];
|
||||||
|
float input[NUM_THREADS * 2];
|
||||||
|
|
||||||
|
for (int i = 0; i < NUM_THREADS * 2; i++) {
|
||||||
|
input[i] = (float)i;
|
||||||
|
}
|
||||||
|
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaMalloc((void **)&dinput, sizeof(float) * NUM_THREADS * 2));
|
||||||
|
checkCudaErrors(cudaMalloc((void **)&doutput, sizeof(float) * NUM_BLOCKS));
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaMalloc((void **)&dtimer, sizeof(clock_t) * NUM_BLOCKS * 2));
|
||||||
|
|
||||||
|
checkCudaErrors(cudaMemcpy(dinput, input, sizeof(float) * NUM_THREADS * 2,
|
||||||
|
cudaMemcpyHostToDevice));
|
||||||
|
|
||||||
|
timedReduction<<<NUM_BLOCKS, NUM_THREADS, sizeof(float) * 2 * NUM_THREADS>>>(
|
||||||
|
dinput, doutput, dtimer);
|
||||||
|
|
||||||
|
checkCudaErrors(cudaMemcpy(timer, dtimer, sizeof(clock_t) * NUM_BLOCKS * 2,
|
||||||
|
cudaMemcpyDeviceToHost));
|
||||||
|
|
||||||
|
checkCudaErrors(cudaFree(dinput));
|
||||||
|
checkCudaErrors(cudaFree(doutput));
|
||||||
|
checkCudaErrors(cudaFree(dtimer));
|
||||||
|
|
||||||
|
long double avgElapsedClocks = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < NUM_BLOCKS; i++) {
|
||||||
|
avgElapsedClocks += (long double)(timer[i + NUM_BLOCKS] - timer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
avgElapsedClocks = avgElapsedClocks / NUM_BLOCKS;
|
||||||
|
printf("Average clocks/block = %Lf\n", avgElapsedClocks);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
18
Samples/0_Introduction/clock_nvrtc/.vscode/c_cpp_properties.json
vendored
Normal file
18
Samples/0_Introduction/clock_nvrtc/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/../../../Common"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||||
|
"cStandard": "gnu17",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"configurationProvider": "ms-vscode.makefile-tools"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
7
Samples/0_Introduction/clock_nvrtc/.vscode/extensions.json
vendored
Normal file
7
Samples/0_Introduction/clock_nvrtc/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"nvidia.nsight-vscode-edition",
|
||||||
|
"ms-vscode.cpptools",
|
||||||
|
"ms-vscode.makefile-tools"
|
||||||
|
]
|
||||||
|
}
|
||||||
10
Samples/0_Introduction/clock_nvrtc/.vscode/launch.json
vendored
Normal file
10
Samples/0_Introduction/clock_nvrtc/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "CUDA C++: Launch",
|
||||||
|
"type": "cuda-gdb",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/clock_nvrtc"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
15
Samples/0_Introduction/clock_nvrtc/.vscode/tasks.json
vendored
Normal file
15
Samples/0_Introduction/clock_nvrtc/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "sample",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "make dbg=1",
|
||||||
|
"problemMatcher": ["$nvcc"],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
37
Samples/0_Introduction/clock_nvrtc/CMakeLists.txt
Normal file
37
Samples/0_Introduction/clock_nvrtc/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||||
|
|
||||||
|
project(clock_nvrtc LANGUAGES C CXX CUDA)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit REQUIRED)
|
||||||
|
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 50 52 60 61 70 72 75 80 86 87 89 90 100 101 120)
|
||||||
|
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (expensive)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Include directories and libraries
|
||||||
|
include_directories(../../../Common)
|
||||||
|
|
||||||
|
# Source file
|
||||||
|
# Add sample target executable
|
||||||
|
add_executable(clock_nvrtc clock.cpp)
|
||||||
|
|
||||||
|
target_compile_options(clock_nvrtc PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||||
|
|
||||||
|
target_compile_features(clock_nvrtc PRIVATE cxx_std_17 cuda_std_17)
|
||||||
|
|
||||||
|
target_link_libraries(clock_nvrtc PRIVATE
|
||||||
|
CUDA::nvrtc
|
||||||
|
CUDA::cuda_driver
|
||||||
|
)
|
||||||
|
|
||||||
|
# Copy clock_kernel.cu to the output directory
|
||||||
|
add_custom_command(TARGET clock_nvrtc POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/clock_kernel.cu ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
)
|
||||||
39
Samples/0_Introduction/clock_nvrtc/README.md
Normal file
39
Samples/0_Introduction/clock_nvrtc/README.md
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# clock_nvrtc - Clock libNVRTC
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
This example shows how to use the clock function using libNVRTC to measure the performance of block of threads of a kernel accurately.
|
||||||
|
|
||||||
|
## Key Concepts
|
||||||
|
|
||||||
|
Performance Strategies, Runtime Compilation
|
||||||
|
|
||||||
|
## Supported SM Architectures
|
||||||
|
|
||||||
|
[SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||||
|
|
||||||
|
## Supported OSes
|
||||||
|
|
||||||
|
Linux, Windows, QNX
|
||||||
|
|
||||||
|
## Supported CPU Architecture
|
||||||
|
|
||||||
|
x86_64, aarch64
|
||||||
|
|
||||||
|
## CUDA APIs involved
|
||||||
|
|
||||||
|
### [CUDA Driver API](http://docs.nvidia.com/cuda/cuda-driver-api/index.html)
|
||||||
|
cuMemcpyDtoH, cuLaunchKernel, cuMemcpyHtoD, cuCtxSynchronize, cuMemAlloc, cuMemFree, cuModuleGetFunction
|
||||||
|
|
||||||
|
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||||
|
cudaBlockSize, cudaGridSize
|
||||||
|
|
||||||
|
## Dependencies needed to build/run
|
||||||
|
[NVRTC](../../../README.md#nvrtc)
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||||
|
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||||
|
|
||||||
|
## References (for more details)
|
||||||
134
Samples/0_Introduction/clock_nvrtc/clock.cpp
Normal file
134
Samples/0_Introduction/clock_nvrtc/clock.cpp
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This example shows how to use the clock function to measure the performance
|
||||||
|
* of block of threads of a kernel accurately. Blocks are executed in parallel
|
||||||
|
* and out of order. Since there's no synchronization mechanism between blocks,
|
||||||
|
* we measure the clock once for each block. The clock samples are written to
|
||||||
|
* device memory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// System includes
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <cuda_runtime.h>
|
||||||
|
#include <nvrtc_helper.h>
|
||||||
|
|
||||||
|
// helper functions and utilities to work with CUDA
|
||||||
|
#include <helper_functions.h>
|
||||||
|
|
||||||
|
#define NUM_BLOCKS 64
|
||||||
|
|
||||||
|
#define NUM_THREADS 256
|
||||||
|
|
||||||
|
// It's interesting to change the number of blocks and the number of threads to
|
||||||
|
// understand how to keep the hardware busy.
|
||||||
|
//
|
||||||
|
|
||||||
|
// Here are some numbers I get on my G80:
|
||||||
|
// blocks - clocks
|
||||||
|
// 1 - 3096
|
||||||
|
// 8 - 3232
|
||||||
|
// 16 - 3364
|
||||||
|
// 32 - 4615
|
||||||
|
// 64 - 9981
|
||||||
|
|
||||||
|
//
|
||||||
|
// With less than 16 blocks some of the multiprocessors of the device are idle.
|
||||||
|
// With
|
||||||
|
// more than 16 you are using all the multiprocessors, but there's only one
|
||||||
|
// block per
|
||||||
|
// multiprocessor and that doesn't allow you to hide the latency of the memory.
|
||||||
|
// With
|
||||||
|
// more than 32 the speed scales linearly.
|
||||||
|
|
||||||
|
// Start the main CUDA Sample here
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
printf("CUDA Clock sample\n");
|
||||||
|
|
||||||
|
typedef long clock_t;
|
||||||
|
|
||||||
|
clock_t timer[NUM_BLOCKS * 2];
|
||||||
|
|
||||||
|
float input[NUM_THREADS * 2];
|
||||||
|
|
||||||
|
for (int i = 0; i < NUM_THREADS * 2; i++) {
|
||||||
|
input[i] = (float)i;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *cubin, *kernel_file;
|
||||||
|
size_t cubinSize;
|
||||||
|
|
||||||
|
kernel_file = sdkFindFilePath("clock_kernel.cu", argv[0]);
|
||||||
|
compileFileToCUBIN(kernel_file, argc, argv, &cubin, &cubinSize, 0);
|
||||||
|
|
||||||
|
CUmodule module = loadCUBIN(cubin, argc, argv);
|
||||||
|
CUfunction kernel_addr;
|
||||||
|
|
||||||
|
checkCudaErrors(cuModuleGetFunction(&kernel_addr, module, "timedReduction"));
|
||||||
|
|
||||||
|
dim3 cudaBlockSize(NUM_THREADS, 1, 1);
|
||||||
|
dim3 cudaGridSize(NUM_BLOCKS, 1, 1);
|
||||||
|
|
||||||
|
CUdeviceptr dinput, doutput, dtimer;
|
||||||
|
checkCudaErrors(cuMemAlloc(&dinput, sizeof(float) * NUM_THREADS * 2));
|
||||||
|
checkCudaErrors(cuMemAlloc(&doutput, sizeof(float) * NUM_BLOCKS));
|
||||||
|
checkCudaErrors(cuMemAlloc(&dtimer, sizeof(clock_t) * NUM_BLOCKS * 2));
|
||||||
|
checkCudaErrors(cuMemcpyHtoD(dinput, input, sizeof(float) * NUM_THREADS * 2));
|
||||||
|
|
||||||
|
void *arr[] = {(void *)&dinput, (void *)&doutput, (void *)&dtimer};
|
||||||
|
|
||||||
|
checkCudaErrors(cuLaunchKernel(
|
||||||
|
kernel_addr, cudaGridSize.x, cudaGridSize.y,
|
||||||
|
cudaGridSize.z, /* grid dim */
|
||||||
|
cudaBlockSize.x, cudaBlockSize.y, cudaBlockSize.z, /* block dim */
|
||||||
|
sizeof(float) * 2 * NUM_THREADS, 0, /* shared mem, stream */
|
||||||
|
&arr[0], /* arguments */
|
||||||
|
0));
|
||||||
|
|
||||||
|
checkCudaErrors(cuCtxSynchronize());
|
||||||
|
checkCudaErrors(
|
||||||
|
cuMemcpyDtoH(timer, dtimer, sizeof(clock_t) * NUM_BLOCKS * 2));
|
||||||
|
checkCudaErrors(cuMemFree(dinput));
|
||||||
|
checkCudaErrors(cuMemFree(doutput));
|
||||||
|
checkCudaErrors(cuMemFree(dtimer));
|
||||||
|
|
||||||
|
long double avgElapsedClocks = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < NUM_BLOCKS; i++) {
|
||||||
|
avgElapsedClocks += (long double)(timer[i + NUM_BLOCKS] - timer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
avgElapsedClocks = avgElapsedClocks / NUM_BLOCKS;
|
||||||
|
printf("Average clocks/block = %Lf\n", avgElapsedClocks);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
74
Samples/0_Introduction/clock_nvrtc/clock_kernel.cu
Normal file
74
Samples/0_Introduction/clock_nvrtc/clock_kernel.cu
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This example shows how to use the clock function to measure the performance
|
||||||
|
* of block of threads of a kernel accurately. Blocks are executed in parallel
|
||||||
|
* and out of order. Since there's no synchronization mechanism between blocks,
|
||||||
|
* we measure the clock once for each block. The clock samples are written to
|
||||||
|
* device memory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This kernel computes a standard parallel reduction and evaluates the
|
||||||
|
// time it takes to do that for each block. The timing results are stored
|
||||||
|
// in device memory.
|
||||||
|
|
||||||
|
extern "C" __global__ void timedReduction(const float *input, float *output,
|
||||||
|
clock_t *timer) {
|
||||||
|
// __shared__ float shared[2 * blockDim.x];
|
||||||
|
extern __shared__ float shared[];
|
||||||
|
|
||||||
|
const int tid = threadIdx.x;
|
||||||
|
const int bid = blockIdx.x;
|
||||||
|
|
||||||
|
if (tid == 0) timer[bid] = clock();
|
||||||
|
|
||||||
|
// Copy input.
|
||||||
|
shared[tid] = input[tid];
|
||||||
|
shared[tid + blockDim.x] = input[tid + blockDim.x];
|
||||||
|
|
||||||
|
// Perform reduction to find minimum.
|
||||||
|
for (int d = blockDim.x; d > 0; d /= 2) {
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
if (tid < d) {
|
||||||
|
float f0 = shared[tid];
|
||||||
|
float f1 = shared[tid + d];
|
||||||
|
|
||||||
|
if (f1 < f0) {
|
||||||
|
shared[tid] = f1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write result.
|
||||||
|
if (tid == 0) output[bid] = shared[0];
|
||||||
|
|
||||||
|
__syncthreads();
|
||||||
|
|
||||||
|
if (tid == 0) timer[bid + gridDim.x] = clock();
|
||||||
|
}
|
||||||
18
Samples/0_Introduction/cudaOpenMP/.vscode/c_cpp_properties.json
vendored
Normal file
18
Samples/0_Introduction/cudaOpenMP/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/../../../Common"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||||
|
"cStandard": "gnu17",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"configurationProvider": "ms-vscode.makefile-tools"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
7
Samples/0_Introduction/cudaOpenMP/.vscode/extensions.json
vendored
Normal file
7
Samples/0_Introduction/cudaOpenMP/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"nvidia.nsight-vscode-edition",
|
||||||
|
"ms-vscode.cpptools",
|
||||||
|
"ms-vscode.makefile-tools"
|
||||||
|
]
|
||||||
|
}
|
||||||
10
Samples/0_Introduction/cudaOpenMP/.vscode/launch.json
vendored
Normal file
10
Samples/0_Introduction/cudaOpenMP/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "CUDA C++: Launch",
|
||||||
|
"type": "cuda-gdb",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/cudaOpenMP"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
15
Samples/0_Introduction/cudaOpenMP/.vscode/tasks.json
vendored
Normal file
15
Samples/0_Introduction/cudaOpenMP/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "sample",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "make dbg=1",
|
||||||
|
"problemMatcher": ["$nvcc"],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
37
Samples/0_Introduction/cudaOpenMP/CMakeLists.txt
Normal file
37
Samples/0_Introduction/cudaOpenMP/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||||
|
|
||||||
|
project(cudaOpenMP LANGUAGES C CXX CUDA)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit REQUIRED)
|
||||||
|
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 50 52 60 61 70 72 75 80 86 87 89 90 100 101 120)
|
||||||
|
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (expensive)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Include directories and libraries
|
||||||
|
include_directories(../../../Common)
|
||||||
|
|
||||||
|
find_package(OpenMP)
|
||||||
|
|
||||||
|
# Source file
|
||||||
|
if(OpenMP_CXX_FOUND)
|
||||||
|
# Add target for asyncAPI
|
||||||
|
add_executable(cudaOpenMP cudaOpenMP.cu)
|
||||||
|
|
||||||
|
target_compile_options(cudaOpenMP PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||||
|
|
||||||
|
target_compile_features(cudaOpenMP PRIVATE cxx_std_17 cuda_std_17)
|
||||||
|
|
||||||
|
set_target_properties(cudaOpenMP PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||||
|
target_link_libraries(cudaOpenMP PUBLIC
|
||||||
|
OpenMP::OpenMP_CXX
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
message(STATUS "OpenMP not found - will not build sample 'cudaOpenMP'")
|
||||||
|
endif()
|
||||||
36
Samples/0_Introduction/cudaOpenMP/README.md
Normal file
36
Samples/0_Introduction/cudaOpenMP/README.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# cudaOpenMP - cudaOpenMP
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
This sample demonstrates how to use OpenMP API to write an application for multiple GPUs.
|
||||||
|
|
||||||
|
## Key Concepts
|
||||||
|
|
||||||
|
CUDA Systems Integration, OpenMP, Multithreading
|
||||||
|
|
||||||
|
## Supported SM Architectures
|
||||||
|
|
||||||
|
[SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||||
|
|
||||||
|
## Supported OSes
|
||||||
|
|
||||||
|
Linux, Windows
|
||||||
|
|
||||||
|
## Supported CPU Architecture
|
||||||
|
|
||||||
|
x86_64, armv7l
|
||||||
|
|
||||||
|
## CUDA APIs involved
|
||||||
|
|
||||||
|
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||||
|
cudaMemcpy, cudaGetErrorString, cudaFree, cudaGetLastError, cudaSetDevice, cudaGetDeviceCount, cudaGetDevice, cudaMemset, cudaMalloc, cudaGetDeviceProperties
|
||||||
|
|
||||||
|
## Dependencies needed to build/run
|
||||||
|
[OpenMP](../../../README.md#openmp)
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||||
|
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||||
|
|
||||||
|
## References (for more details)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
18
Samples/0_Introduction/fp16ScalarProduct/.vscode/c_cpp_properties.json
vendored
Normal file
18
Samples/0_Introduction/fp16ScalarProduct/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/../../../Common"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||||
|
"cStandard": "gnu17",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"configurationProvider": "ms-vscode.makefile-tools"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
7
Samples/0_Introduction/fp16ScalarProduct/.vscode/extensions.json
vendored
Normal file
7
Samples/0_Introduction/fp16ScalarProduct/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"nvidia.nsight-vscode-edition",
|
||||||
|
"ms-vscode.cpptools",
|
||||||
|
"ms-vscode.makefile-tools"
|
||||||
|
]
|
||||||
|
}
|
||||||
10
Samples/0_Introduction/fp16ScalarProduct/.vscode/launch.json
vendored
Normal file
10
Samples/0_Introduction/fp16ScalarProduct/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "CUDA C++: Launch",
|
||||||
|
"type": "cuda-gdb",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/fp16ScalarProduct"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
15
Samples/0_Introduction/fp16ScalarProduct/.vscode/tasks.json
vendored
Normal file
15
Samples/0_Introduction/fp16ScalarProduct/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "sample",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "make dbg=1",
|
||||||
|
"problemMatcher": ["$nvcc"],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
27
Samples/0_Introduction/fp16ScalarProduct/CMakeLists.txt
Normal file
27
Samples/0_Introduction/fp16ScalarProduct/CMakeLists.txt
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||||
|
|
||||||
|
project(fp16ScalarProduct LANGUAGES C CXX CUDA)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit REQUIRED)
|
||||||
|
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
set(CMAKE_CUDA_ARCHITECTURES 60 61 70 72 75 80 86 87 89 90 100 101 120)
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (expensive)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Include directories and libraries
|
||||||
|
include_directories(../../../Common)
|
||||||
|
|
||||||
|
# Source file
|
||||||
|
# Add target for asyncAPI
|
||||||
|
add_executable(fp16ScalarProduct fp16ScalarProduct.cu)
|
||||||
|
|
||||||
|
target_compile_options(fp16ScalarProduct PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||||
|
|
||||||
|
target_compile_features(fp16ScalarProduct PRIVATE cxx_std_17 cuda_std_17)
|
||||||
|
|
||||||
|
set_target_properties(fp16ScalarProduct PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||||
36
Samples/0_Introduction/fp16ScalarProduct/README.md
Normal file
36
Samples/0_Introduction/fp16ScalarProduct/README.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# fp16ScalarProduct - FP16 Scalar Product
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
Calculates scalar product of two vectors of FP16 numbers.
|
||||||
|
|
||||||
|
## Key Concepts
|
||||||
|
|
||||||
|
CUDA Runtime API
|
||||||
|
|
||||||
|
## Supported SM Architectures
|
||||||
|
|
||||||
|
[SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||||
|
|
||||||
|
## Supported OSes
|
||||||
|
|
||||||
|
Linux, Windows
|
||||||
|
|
||||||
|
## Supported CPU Architecture
|
||||||
|
|
||||||
|
x86_64, armv7l
|
||||||
|
|
||||||
|
## CUDA APIs involved
|
||||||
|
|
||||||
|
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||||
|
cudaMemcpy, cudaFree, cudaMallocHost, cudaFreeHost, cudaMalloc, cudaGetDeviceProperties
|
||||||
|
|
||||||
|
## Dependencies needed to build/run
|
||||||
|
[FP16](../../../README.md#fp16)
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||||
|
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||||
|
|
||||||
|
## References (for more details)
|
||||||
215
Samples/0_Introduction/fp16ScalarProduct/fp16ScalarProduct.cu
Normal file
215
Samples/0_Introduction/fp16ScalarProduct/fp16ScalarProduct.cu
Normal file
@@ -0,0 +1,215 @@
|
|||||||
|
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||||
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "cuda_fp16.h"
|
||||||
|
#include "helper_cuda.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
#define NUM_OF_BLOCKS 128
|
||||||
|
#define NUM_OF_THREADS 128
|
||||||
|
|
||||||
|
__forceinline__ __device__ void reduceInShared_intrinsics(half2 *const v) {
|
||||||
|
if (threadIdx.x < 64)
|
||||||
|
v[threadIdx.x] = __hadd2(v[threadIdx.x], v[threadIdx.x + 64]);
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 32)
|
||||||
|
v[threadIdx.x] = __hadd2(v[threadIdx.x], v[threadIdx.x + 32]);
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 16)
|
||||||
|
v[threadIdx.x] = __hadd2(v[threadIdx.x], v[threadIdx.x + 16]);
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 8)
|
||||||
|
v[threadIdx.x] = __hadd2(v[threadIdx.x], v[threadIdx.x + 8]);
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 4)
|
||||||
|
v[threadIdx.x] = __hadd2(v[threadIdx.x], v[threadIdx.x + 4]);
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 2)
|
||||||
|
v[threadIdx.x] = __hadd2(v[threadIdx.x], v[threadIdx.x + 2]);
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 1)
|
||||||
|
v[threadIdx.x] = __hadd2(v[threadIdx.x], v[threadIdx.x + 1]);
|
||||||
|
__syncthreads();
|
||||||
|
}
|
||||||
|
|
||||||
|
__forceinline__ __device__ void reduceInShared_native(half2 *const v) {
|
||||||
|
if (threadIdx.x < 64) v[threadIdx.x] = v[threadIdx.x] + v[threadIdx.x + 64];
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 32) v[threadIdx.x] = v[threadIdx.x] + v[threadIdx.x + 32];
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 16) v[threadIdx.x] = v[threadIdx.x] + v[threadIdx.x + 16];
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 8) v[threadIdx.x] = v[threadIdx.x] + v[threadIdx.x + 8];
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 4) v[threadIdx.x] = v[threadIdx.x] + v[threadIdx.x + 4];
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 2) v[threadIdx.x] = v[threadIdx.x] + v[threadIdx.x + 2];
|
||||||
|
__syncthreads();
|
||||||
|
if (threadIdx.x < 1) v[threadIdx.x] = v[threadIdx.x] + v[threadIdx.x + 1];
|
||||||
|
__syncthreads();
|
||||||
|
}
|
||||||
|
|
||||||
|
__global__ void scalarProductKernel_intrinsics(half2 const *const a,
|
||||||
|
half2 const *const b,
|
||||||
|
float *const results,
|
||||||
|
size_t const size) {
|
||||||
|
const int stride = gridDim.x * blockDim.x;
|
||||||
|
__shared__ half2 shArray[NUM_OF_THREADS];
|
||||||
|
|
||||||
|
shArray[threadIdx.x] = __float2half2_rn(0.f);
|
||||||
|
half2 value = __float2half2_rn(0.f);
|
||||||
|
|
||||||
|
for (int i = threadIdx.x + blockDim.x + blockIdx.x; i < size; i += stride) {
|
||||||
|
value = __hfma2(a[i], b[i], value);
|
||||||
|
}
|
||||||
|
|
||||||
|
shArray[threadIdx.x] = value;
|
||||||
|
__syncthreads();
|
||||||
|
reduceInShared_intrinsics(shArray);
|
||||||
|
|
||||||
|
if (threadIdx.x == 0) {
|
||||||
|
half2 result = shArray[0];
|
||||||
|
float f_result = __low2float(result) + __high2float(result);
|
||||||
|
results[blockIdx.x] = f_result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__global__ void scalarProductKernel_native(half2 const *const a,
|
||||||
|
half2 const *const b,
|
||||||
|
float *const results,
|
||||||
|
size_t const size) {
|
||||||
|
const int stride = gridDim.x * blockDim.x;
|
||||||
|
__shared__ half2 shArray[NUM_OF_THREADS];
|
||||||
|
|
||||||
|
half2 value(0.f, 0.f);
|
||||||
|
shArray[threadIdx.x] = value;
|
||||||
|
|
||||||
|
for (int i = threadIdx.x + blockDim.x + blockIdx.x; i < size; i += stride) {
|
||||||
|
value = a[i] * b[i] + value;
|
||||||
|
}
|
||||||
|
|
||||||
|
shArray[threadIdx.x] = value;
|
||||||
|
__syncthreads();
|
||||||
|
reduceInShared_native(shArray);
|
||||||
|
|
||||||
|
if (threadIdx.x == 0) {
|
||||||
|
half2 result = shArray[0];
|
||||||
|
float f_result = (float)result.y + (float)result.x;
|
||||||
|
results[blockIdx.x] = f_result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void generateInput(half2 *a, size_t size) {
|
||||||
|
for (size_t i = 0; i < size; ++i) {
|
||||||
|
half2 temp;
|
||||||
|
temp.x = static_cast<float>(rand() % 4);
|
||||||
|
temp.y = static_cast<float>(rand() % 2);
|
||||||
|
a[i] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
size_t size = NUM_OF_BLOCKS * NUM_OF_THREADS * 16;
|
||||||
|
|
||||||
|
half2 *vec[2];
|
||||||
|
half2 *devVec[2];
|
||||||
|
|
||||||
|
float *results;
|
||||||
|
float *devResults;
|
||||||
|
|
||||||
|
int devID = findCudaDevice(argc, (const char **)argv);
|
||||||
|
|
||||||
|
cudaDeviceProp devProp;
|
||||||
|
checkCudaErrors(cudaGetDeviceProperties(&devProp, devID));
|
||||||
|
|
||||||
|
if (devProp.major < 5 || (devProp.major == 5 && devProp.minor < 3)) {
|
||||||
|
printf(
|
||||||
|
"ERROR: fp16ScalarProduct requires GPU devices with compute SM 5.3 or "
|
||||||
|
"higher.\n");
|
||||||
|
return EXIT_WAIVED;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; ++i) {
|
||||||
|
checkCudaErrors(cudaMallocHost((void **)&vec[i], size * sizeof *vec[i]));
|
||||||
|
checkCudaErrors(cudaMalloc((void **)&devVec[i], size * sizeof *devVec[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaMallocHost((void **)&results, NUM_OF_BLOCKS * sizeof *results));
|
||||||
|
checkCudaErrors(
|
||||||
|
cudaMalloc((void **)&devResults, NUM_OF_BLOCKS * sizeof *devResults));
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; ++i) {
|
||||||
|
generateInput(vec[i], size);
|
||||||
|
checkCudaErrors(cudaMemcpy(devVec[i], vec[i], size * sizeof *vec[i],
|
||||||
|
cudaMemcpyHostToDevice));
|
||||||
|
}
|
||||||
|
|
||||||
|
scalarProductKernel_native<<<NUM_OF_BLOCKS, NUM_OF_THREADS>>>(
|
||||||
|
devVec[0], devVec[1], devResults, size);
|
||||||
|
|
||||||
|
checkCudaErrors(cudaMemcpy(results, devResults,
|
||||||
|
NUM_OF_BLOCKS * sizeof *results,
|
||||||
|
cudaMemcpyDeviceToHost));
|
||||||
|
|
||||||
|
float result_native = 0;
|
||||||
|
for (int i = 0; i < NUM_OF_BLOCKS; ++i) {
|
||||||
|
result_native += results[i];
|
||||||
|
}
|
||||||
|
printf("Result native operators\t: %f \n", result_native);
|
||||||
|
|
||||||
|
scalarProductKernel_intrinsics<<<NUM_OF_BLOCKS, NUM_OF_THREADS>>>(
|
||||||
|
devVec[0], devVec[1], devResults, size);
|
||||||
|
|
||||||
|
checkCudaErrors(cudaMemcpy(results, devResults,
|
||||||
|
NUM_OF_BLOCKS * sizeof *results,
|
||||||
|
cudaMemcpyDeviceToHost));
|
||||||
|
|
||||||
|
float result_intrinsics = 0;
|
||||||
|
for (int i = 0; i < NUM_OF_BLOCKS; ++i) {
|
||||||
|
result_intrinsics += results[i];
|
||||||
|
}
|
||||||
|
printf("Result intrinsics\t: %f \n", result_intrinsics);
|
||||||
|
|
||||||
|
printf("&&&& fp16ScalarProduct %s\n",
|
||||||
|
(fabs(result_intrinsics - result_native) < 0.00001) ? "PASSED"
|
||||||
|
: "FAILED");
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; ++i) {
|
||||||
|
checkCudaErrors(cudaFree(devVec[i]));
|
||||||
|
checkCudaErrors(cudaFreeHost(vec[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
checkCudaErrors(cudaFree(devResults));
|
||||||
|
checkCudaErrors(cudaFreeHost(results));
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
18
Samples/0_Introduction/matrixMul/.vscode/c_cpp_properties.json
vendored
Normal file
18
Samples/0_Introduction/matrixMul/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/../../../Common"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||||
|
"cStandard": "gnu17",
|
||||||
|
"cppStandard": "gnu++14",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64",
|
||||||
|
"configurationProvider": "ms-vscode.makefile-tools"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user