mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-13 00:08:26 +08:00
CUDA 13.4 samples update - v13.4-public
Release 13.4 of the CUDA Samples supported by CUDA Toolkit 13.4. See Changelog for more information.
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
{
|
||||
"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
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
@@ -1,34 +1,25 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/DetectCudaArch.cmake")
|
||||
|
||||
if(SAMPLE_SKIP_BUILD)
|
||||
return()
|
||||
endif()
|
||||
|
||||
project(simplePrintf LANGUAGES C CXX CUDA)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/CudaSampleCommon.cmake")
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 89 90 100 110 120)
|
||||
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 simplePrintf
|
||||
add_executable(simplePrintf simplePrintf.cu)
|
||||
|
||||
target_compile_options(simplePrintf PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(simplePrintf PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(simplePrintf PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
include(InstallSamples)
|
||||
setup_samples_install()
|
||||
|
||||
@@ -1,32 +1,77 @@
|
||||
# simplePrintf - simplePrintf
|
||||
# Sample: Device-Side printf (simplePrintf)
|
||||
|
||||
## Description
|
||||
|
||||
This basic CUDA Runtime API sample demonstrates how to use the printf function in the device code.
|
||||
A first-look CUDA sample: call C's `printf` **from inside a CUDA kernel**. Every thread in a 2D grid of 3D blocks prints its own block and thread identifiers along with a shared value passed from the host. The CUDA runtime buffers the device-side output and flushes it to the host's standard output when the kernel completes.
|
||||
|
||||
This sample is the canonical demonstration of device-side `printf`, the simplest debugging and tracing tool available inside kernel code. It also shows how multidimensional `blockIdx`/`threadIdx` coordinates are flattened into linear indices.
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
- Calling `printf` directly from device code (`__global__` kernel)
|
||||
- Launching a kernel with a **2D grid** (`dim3 dimGrid(2, 2)`) of **3D blocks** (`dim3 dimBlock(2, 2, 2)`)
|
||||
- Flattening multidimensional block and thread indices into a single linear index
|
||||
- Flushing device-side `printf` output to the host with `cudaDeviceSynchronize`
|
||||
- Querying the active device with `cudaGetDevice` / `cudaGetDeviceProperties`
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Debugging
|
||||
- **Device-side `printf`** — formatted output from within a kernel; output is buffered per launch and flushed at a synchronization point
|
||||
- **Multidimensional launch geometry** — `gridDim`, `blockIdx`, `blockDim`, and `threadIdx` are `dim3` values with `.x`, `.y`, `.z` components
|
||||
- **Index linearization** — converting `(x, y, z)` coordinates into a flat index:
|
||||
- block: `blockIdx.y * gridDim.x + blockIdx.x`
|
||||
- thread: `threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x`
|
||||
|
||||
## Supported SM Architectures
|
||||
## Key APIs
|
||||
|
||||
[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)
|
||||
### CUDA Runtime
|
||||
- `cudaSetDevice` — select the active GPU
|
||||
- `cudaDeviceGetAttribute` — query compute capability (major, minor) and SM count
|
||||
- `cudaDeviceSynchronize` — block the host until the kernel finishes, which also flushes the device `printf` buffer
|
||||
|
||||
## Supported OSes
|
||||
### Device
|
||||
- `printf` — standard C formatted output, callable from `__global__`/`__device__` code (requires Compute Capability 2.0 or higher)
|
||||
|
||||
Linux, Windows
|
||||
## Requirements
|
||||
|
||||
## Supported CPU Architecture
|
||||
### Hardware
|
||||
- NVIDIA GPU with Compute Capability 2.0 or higher (device-side `printf` is unavailable on earlier architectures)
|
||||
|
||||
x86_64, armv7l
|
||||
### Software
|
||||
- CMake 3.20 or newer
|
||||
- A C++17-capable host compiler
|
||||
|
||||
## CUDA APIs involved
|
||||
## How to Build
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaGetDeviceProperties, cudaDeviceSynchronize, cudaGetDevice
|
||||
See the [top-level README](../../../README.md#building-cuda-samples) for full build instructions, including how to build all samples or a single sample standalone.
|
||||
|
||||
## Prerequisites
|
||||
## How to Run
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
```bash
|
||||
./simplePrintf
|
||||
```
|
||||
|
||||
## References (for more details)
|
||||
## Expected Output
|
||||
|
||||
The launch uses a 2×2 grid of 2×2×2 blocks = 4 blocks × 8 threads = 32 lines. Each thread prints its linear block index, its linear thread index, and the value `10`. Because blocks and threads run concurrently, **the order of the lines will vary between runs**:
|
||||
|
||||
```text
|
||||
GPU Device 0: with compute capability X.Y and Number of SMs <smCount>
|
||||
printf() is called. Output:
|
||||
|
||||
[0, 0]: Value is:10
|
||||
[0, 1]: Value is:10
|
||||
[0, 2]: Value is:10
|
||||
...
|
||||
[3, 7]: Value is:10
|
||||
```
|
||||
|
||||
## Files
|
||||
|
||||
- `simplePrintf.cu` — device-side `printf` kernel + host driver
|
||||
- `README.md` — this file
|
||||
- `CMakeLists.txt` — build configuration
|
||||
|
||||
## See Also
|
||||
|
||||
- [CUDA C++ Programming Guide — printf()](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/cpp-language-support.html#printf)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -32,34 +32,35 @@
|
||||
// CUDA runtime
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
// helper functions and utilities to work with CUDA
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_functions.h>
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a, b) (a > b ? a : b)
|
||||
#endif
|
||||
|
||||
__global__ void testKernel(int val)
|
||||
// Each thread calls device-side printf, printing its linear block index, its
|
||||
// linear thread index, and val. Output is flushed on host sync; line order is
|
||||
// not deterministic since blocks/threads run concurrently.
|
||||
__global__ void simplePrintfKernel(int val)
|
||||
{
|
||||
printf("[%d, %d]:\t\tValue is:%d\n",
|
||||
blockIdx.y * gridDim.x + blockIdx.x,
|
||||
threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x,
|
||||
blockIdx.y * gridDim.x + blockIdx.x, // linear block index
|
||||
threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x, // linear thread index
|
||||
val);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int devID;
|
||||
cudaDeviceProp props;
|
||||
// Select device 0 as the active GPU
|
||||
int devID = 0;
|
||||
cudaSetDevice(devID);
|
||||
|
||||
// This will pick the best possible CUDA capable device
|
||||
devID = findCudaDevice(argc, (const char **)argv);
|
||||
// Query compute capability (major.minor) and number of SMs on the device
|
||||
int major = 0, minor = 0, smCount = 0;
|
||||
cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, devID);
|
||||
cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, devID);
|
||||
cudaDeviceGetAttribute(&smCount, cudaDevAttrMultiProcessorCount, devID);
|
||||
|
||||
// Get GPU information
|
||||
checkCudaErrors(cudaGetDevice(&devID));
|
||||
checkCudaErrors(cudaGetDeviceProperties(&props, devID));
|
||||
printf("Device %d: \"%s\" with Compute capability %d.%d\n", devID, props.name, props.major, props.minor);
|
||||
// Print device info
|
||||
printf("GPU Device %d: with compute capability %d.%d and Number of SMs %d\n\n", devID, major, minor, smCount);
|
||||
|
||||
printf("printf() is called. Output:\n\n");
|
||||
|
||||
@@ -67,7 +68,7 @@ int main(int argc, char **argv)
|
||||
// three-dimensional blocks are configured.
|
||||
dim3 dimGrid(2, 2);
|
||||
dim3 dimBlock(2, 2, 2);
|
||||
testKernel<<<dimGrid, dimBlock>>>(10);
|
||||
simplePrintfKernel<<<dimGrid, dimBlock>>>(10);
|
||||
cudaDeviceSynchronize();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
Reference in New Issue
Block a user