mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-13 00:08:26 +08:00
CUDA 13.2 samples update (#432)
- Added Python samples for CUDA Python 1.0 release - Renamed top-level `Samples` directory to `cpp` to accommodate Python samples.
This commit is contained in:
18
cpp/3_CUDA_Features/cdpBezierTessellation/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/3_CUDA_Features/cdpBezierTessellation/.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
cpp/3_CUDA_Features/cdpBezierTessellation/.vscode/extensions.json
vendored
Normal file
7
cpp/3_CUDA_Features/cdpBezierTessellation/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
205
cpp/3_CUDA_Features/cdpBezierTessellation/BezierLineCDP.cu
Normal file
205
cpp/3_CUDA_Features/cdpBezierTessellation/BezierLineCDP.cu
Normal file
@@ -0,0 +1,205 @@
|
||||
/* 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_runtime_api.h>
|
||||
#include <helper_cuda.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
__forceinline__ __device__ float2 operator+(float2 a, float2 b)
|
||||
{
|
||||
float2 c;
|
||||
c.x = a.x + b.x;
|
||||
c.y = a.y + b.y;
|
||||
return c;
|
||||
}
|
||||
|
||||
__forceinline__ __device__ float2 operator-(float2 a, float2 b)
|
||||
{
|
||||
float2 c;
|
||||
c.x = a.x - b.x;
|
||||
c.y = a.y - b.y;
|
||||
return c;
|
||||
}
|
||||
|
||||
__forceinline__ __device__ float2 operator*(float a, float2 b)
|
||||
{
|
||||
float2 c;
|
||||
c.x = a * b.x;
|
||||
c.y = a * b.y;
|
||||
return c;
|
||||
}
|
||||
|
||||
__forceinline__ __device__ float length(float2 a) { return sqrtf(a.x * a.x + a.y * a.y); }
|
||||
|
||||
#define MAX_TESSELLATION 32
|
||||
struct BezierLine
|
||||
{
|
||||
float2 CP[3];
|
||||
float2 *vertexPos;
|
||||
int nVertices;
|
||||
};
|
||||
|
||||
__global__ void computeBezierLinePositions(int lidx, BezierLine *bLines, int nTessPoints)
|
||||
{
|
||||
int idx = threadIdx.x + blockDim.x * blockIdx.x;
|
||||
|
||||
if (idx < nTessPoints) {
|
||||
float u = (float)idx / (float)(nTessPoints - 1);
|
||||
float omu = 1.0f - u;
|
||||
|
||||
float B3u[3];
|
||||
|
||||
B3u[0] = omu * omu;
|
||||
B3u[1] = 2.0f * u * omu;
|
||||
B3u[2] = u * u;
|
||||
|
||||
float2 position = {0, 0};
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
position = position + B3u[i] * bLines[lidx].CP[i];
|
||||
}
|
||||
|
||||
bLines[lidx].vertexPos[idx] = position;
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void computeBezierLinesCDP(BezierLine *bLines, int nLines)
|
||||
{
|
||||
int lidx = threadIdx.x + blockDim.x * blockIdx.x;
|
||||
|
||||
if (lidx < nLines) {
|
||||
float curvature = length(bLines[lidx].CP[1] - 0.5f * (bLines[lidx].CP[0] + bLines[lidx].CP[2]))
|
||||
/ length(bLines[lidx].CP[2] - bLines[lidx].CP[0]);
|
||||
int nTessPoints = min(max((int)(curvature * 16.0f), 4), MAX_TESSELLATION);
|
||||
|
||||
if (bLines[lidx].vertexPos == NULL) {
|
||||
bLines[lidx].nVertices = nTessPoints;
|
||||
cudaMalloc((void **)&bLines[lidx].vertexPos, nTessPoints * sizeof(float2));
|
||||
}
|
||||
|
||||
computeBezierLinePositions<<<ceilf((float)bLines[lidx].nVertices / 32.0f), 32>>>(
|
||||
lidx, bLines, bLines[lidx].nVertices);
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void freeVertexMem(BezierLine *bLines, int nLines)
|
||||
{
|
||||
int lidx = threadIdx.x + blockDim.x * blockIdx.x;
|
||||
|
||||
if (lidx < nLines)
|
||||
cudaFree(bLines[lidx].vertexPos);
|
||||
}
|
||||
|
||||
unsigned int checkCapableSM35Device(int argc, char **argv)
|
||||
{
|
||||
// Get device properties
|
||||
cudaDeviceProp properties;
|
||||
int device_count = 0, device = -1;
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
|
||||
device = getCmdLineArgumentInt(argc, (const char **)argv, "device");
|
||||
|
||||
cudaDeviceProp properties;
|
||||
checkCudaErrors(cudaGetDeviceProperties(&properties, device));
|
||||
|
||||
if (properties.major > 3 || (properties.major == 3 && properties.minor >= 5)) {
|
||||
printf("Running on GPU %d (%s)\n", device, properties.name);
|
||||
}
|
||||
else {
|
||||
printf("cdpBezierTessellation requires GPU devices with compute SM 3.5 or "
|
||||
"higher.");
|
||||
printf("Current GPU device has compute SM %d.%d. Exiting...\n", properties.major, properties.minor);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
checkCudaErrors(cudaGetDeviceCount(&device_count));
|
||||
|
||||
for (int i = 0; i < device_count; ++i) {
|
||||
checkCudaErrors(cudaGetDeviceProperties(&properties, i));
|
||||
|
||||
if (properties.major > 3 || (properties.major == 3 && properties.minor >= 5)) {
|
||||
device = i;
|
||||
printf("Running on GPU %d (%s)\n", i, properties.name);
|
||||
break;
|
||||
}
|
||||
|
||||
printf("GPU %d %s does not support CUDA Dynamic Parallelism\n", i, properties.name);
|
||||
}
|
||||
}
|
||||
if (device == -1) {
|
||||
fprintf(stderr,
|
||||
"cdpBezierTessellation requires GPU devices with compute SM 3.5 or "
|
||||
"higher. Exiting...\n");
|
||||
return EXIT_WAIVED;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
#define N_LINES 256
|
||||
#define BLOCK_DIM 64
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
BezierLine *bLines_h = new BezierLine[N_LINES];
|
||||
|
||||
float2 last = {0, 0};
|
||||
|
||||
for (int i = 0; i < N_LINES; i++) {
|
||||
bLines_h[i].CP[0] = last;
|
||||
|
||||
for (int j = 1; j < 3; j++) {
|
||||
bLines_h[i].CP[j].x = (float)rand() / (float)RAND_MAX;
|
||||
bLines_h[i].CP[j].y = (float)rand() / (float)RAND_MAX;
|
||||
}
|
||||
|
||||
last = bLines_h[i].CP[2];
|
||||
bLines_h[i].vertexPos = NULL;
|
||||
bLines_h[i].nVertices = 0;
|
||||
}
|
||||
|
||||
unsigned int sm35Ret = checkCapableSM35Device(argc, argv);
|
||||
if (sm35Ret != EXIT_SUCCESS) {
|
||||
exit(sm35Ret);
|
||||
}
|
||||
|
||||
BezierLine *bLines_d;
|
||||
checkCudaErrors(cudaMalloc((void **)&bLines_d, N_LINES * sizeof(BezierLine)));
|
||||
checkCudaErrors(cudaMemcpy(bLines_d, bLines_h, N_LINES * sizeof(BezierLine), cudaMemcpyHostToDevice));
|
||||
printf("Computing Bezier Lines (CUDA Dynamic Parallelism Version) ... ");
|
||||
computeBezierLinesCDP<<<(unsigned int)ceil((float)N_LINES / (float)BLOCK_DIM), BLOCK_DIM>>>(bLines_d, N_LINES);
|
||||
printf("Done!\n");
|
||||
|
||||
// Do something to draw the lines here
|
||||
|
||||
freeVertexMem<<<(unsigned int)ceil((float)N_LINES / (float)BLOCK_DIM), BLOCK_DIM>>>(bLines_d, N_LINES);
|
||||
checkCudaErrors(cudaFree(bLines_d));
|
||||
delete[] bLines_h;
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
43
cpp/3_CUDA_Features/cdpBezierTessellation/CMakeLists.txt
Normal file
43
cpp/3_CUDA_Features/cdpBezierTessellation/CMakeLists.txt
Normal file
@@ -0,0 +1,43 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(cdpBezierTessellation LANGUAGES C CXX CUDA)
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
# The aarch64/sbsa_aarch64 CUDA toolkit are support on Tegra since 13.0, so need to check which version of the toolkit is installed
|
||||
string(FIND "${CUDAToolkit_INCLUDE_DIRS}" "aarch64-linux" _aarch64_linux_ctk)
|
||||
string(FIND "${CUDAToolkit_INCLUDE_DIRS}" "aarch64-qnx" _aarch64_qnx_ctk)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" AND (NOT _aarch64_linux_ctk EQUAL -1 OR NOT _aarch64_qnx_ctk EQUAL -1))
|
||||
set(CMAKE_CUDA_ARCHITECTURES 87 110)
|
||||
else()
|
||||
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 89 90 100 110 120)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||
|
||||
if(ENABLE_CUDA_DEBUG)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (may significantly affect performance on some targets)
|
||||
else()
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo") # add line information to all builds for debug tools (exclusive to -G option)
|
||||
endif()
|
||||
|
||||
# Include directories and libraries
|
||||
include_directories(../../../Common)
|
||||
|
||||
# Source file
|
||||
# Add target for cdpBezierTessellation
|
||||
add_executable(cdpBezierTessellation BezierLineCDP.cu)
|
||||
|
||||
target_compile_options(cdpBezierTessellation PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(cdpBezierTessellation PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(cdpBezierTessellation PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
34
cpp/3_CUDA_Features/cdpBezierTessellation/README.md
Normal file
34
cpp/3_CUDA_Features/cdpBezierTessellation/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# cdpBezierTessellation - Bezier Line Tessellation (CUDA Dynamic Parallelism)
|
||||
|
||||
## Description
|
||||
|
||||
This sample demonstrates bezier tessellation of lines implemented using CUDA Dynamic Parallelism. This sample requires devices with compute capability 3.5 or higher.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
CUDA Dynamic Parallelism
|
||||
|
||||
## 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)
|
||||
cudaMemcpy, cudaFree, cudaGetDeviceCount, cudaMalloc, cudaGetDeviceProperties
|
||||
|
||||
## Dependencies needed to build/run
|
||||
[CDP](../../../README.md#cuda-dynamic-parallellism)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit](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)
|
||||
Reference in New Issue
Block a user