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/cdpSimplePrint/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/3_CUDA_Features/cdpSimplePrint/.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/cdpSimplePrint/.vscode/extensions.json
vendored
Normal file
7
cpp/3_CUDA_Features/cdpSimplePrint/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
43
cpp/3_CUDA_Features/cdpSimplePrint/CMakeLists.txt
Normal file
43
cpp/3_CUDA_Features/cdpSimplePrint/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(cdpSimplePrint 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 cdpSimplePrint
|
||||
add_executable(cdpSimplePrint cdpSimplePrint.cu)
|
||||
|
||||
target_compile_options(cdpSimplePrint PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(cdpSimplePrint PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(cdpSimplePrint PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
34
cpp/3_CUDA_Features/cdpSimplePrint/README.md
Normal file
34
cpp/3_CUDA_Features/cdpSimplePrint/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# cdpSimplePrint - Simple Print (CUDA Dynamic Parallelism)
|
||||
|
||||
## Description
|
||||
|
||||
This sample demonstrates simple printf 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)
|
||||
cudaDeviceSynchronize, cudaGetLastError, cudaGetDeviceProperties, cudaDeviceSetLimit
|
||||
|
||||
## 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)
|
||||
161
cpp/3_CUDA_Features/cdpSimplePrint/cdpSimplePrint.cu
Normal file
161
cpp/3_CUDA_Features/cdpSimplePrint/cdpSimplePrint.cu
Normal file
@@ -0,0 +1,161 @@
|
||||
/* 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 <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_string.h>
|
||||
#include <iostream>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Variable on the GPU used to generate unique identifiers of blocks.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__device__ int g_uids = 0;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Print a simple message to signal the block which is currently executing.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__device__ void print_info(int depth, int thread, int uid, int parent_uid)
|
||||
{
|
||||
if (threadIdx.x == 0) {
|
||||
if (depth == 0)
|
||||
printf("BLOCK %d launched by the host\n", uid);
|
||||
else {
|
||||
char buffer[32];
|
||||
|
||||
for (int i = 0; i < depth; ++i) {
|
||||
buffer[3 * i + 0] = '|';
|
||||
buffer[3 * i + 1] = ' ';
|
||||
buffer[3 * i + 2] = ' ';
|
||||
}
|
||||
|
||||
buffer[3 * depth] = '\0';
|
||||
printf("%sBLOCK %d launched by thread %d of block %d\n", buffer, uid, thread, parent_uid);
|
||||
}
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// The kernel using CUDA dynamic parallelism.
|
||||
//
|
||||
// It generates a unique identifier for each block. Prints the information
|
||||
// about that block. Finally, if the 'max_depth' has not been reached, the
|
||||
// block launches new blocks directly from the GPU.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void cdp_kernel(int max_depth, int depth, int thread, int parent_uid)
|
||||
{
|
||||
// We create a unique ID per block. Thread 0 does that and shares the value
|
||||
// with the other threads.
|
||||
__shared__ int s_uid;
|
||||
|
||||
if (threadIdx.x == 0) {
|
||||
s_uid = atomicAdd(&g_uids, 1);
|
||||
}
|
||||
|
||||
__syncthreads();
|
||||
|
||||
// We print the ID of the block and information about its parent.
|
||||
print_info(depth, thread, s_uid, parent_uid);
|
||||
|
||||
// We launch new blocks if we haven't reached the max_depth yet.
|
||||
if (++depth >= max_depth) {
|
||||
return;
|
||||
}
|
||||
|
||||
cdp_kernel<<<gridDim.x, blockDim.x>>>(max_depth, depth, threadIdx.x, s_uid);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Main entry point.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
printf("starting Simple Print (CUDA Dynamic Parallelism)\n");
|
||||
|
||||
// Parse a few command-line arguments.
|
||||
int max_depth = 2;
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help") || checkCmdLineFlag(argc, (const char **)argv, "h")) {
|
||||
printf("Usage: %s depth=<max_depth>\t(where max_depth is a value between 1 "
|
||||
"and 8).\n",
|
||||
argv[0]);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "depth")) {
|
||||
max_depth = getCmdLineArgumentInt(argc, (const char **)argv, "depth");
|
||||
|
||||
if (max_depth < 1 || max_depth > 8) {
|
||||
printf("depth parameter has to be between 1 and 8\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
// Find/set the device.
|
||||
int device = -1;
|
||||
cudaDeviceProp deviceProp;
|
||||
device = findCudaDevice(argc, (const char **)argv);
|
||||
checkCudaErrors(cudaGetDeviceProperties(&deviceProp, device));
|
||||
|
||||
if (!(deviceProp.major > 3 || (deviceProp.major == 3 && deviceProp.minor >= 5))) {
|
||||
printf("GPU %d - %s does not support CUDA Dynamic Parallelism\n Exiting.", device, deviceProp.name);
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
// Print a message describing what the sample does.
|
||||
printf("*********************************************************************"
|
||||
"******\n");
|
||||
printf("The CPU launches 2 blocks of 2 threads each. On the device each thread "
|
||||
"will\n");
|
||||
printf("launch 2 blocks of 2 threads each. The GPU we will do that "
|
||||
"recursively\n");
|
||||
printf("until it reaches max_depth=%d\n\n", max_depth);
|
||||
printf("In total 2");
|
||||
int num_blocks = 2, sum = 2;
|
||||
|
||||
for (int i = 1; i < max_depth; ++i) {
|
||||
num_blocks *= 4;
|
||||
printf("+%d", num_blocks);
|
||||
sum += num_blocks;
|
||||
}
|
||||
|
||||
printf("=%d blocks are launched!!! (%d from the GPU)\n", sum, sum - 2);
|
||||
printf("************************************************************************"
|
||||
"***\n\n");
|
||||
|
||||
// Launch the kernel from the CPU.
|
||||
printf("Launching cdp_kernel() with CUDA Dynamic Parallelism:\n\n");
|
||||
cdp_kernel<<<2, 2>>>(max_depth, 0, 0, -1);
|
||||
checkCudaErrors(cudaGetLastError());
|
||||
|
||||
// Finalize.
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
Reference in New Issue
Block a user