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,37 +1,28 @@
|
||||
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(simpleAssert 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()
|
||||
|
||||
# Removes -DNDEBUG For Print specific logs in this sample.
|
||||
string(REPLACE "-DNDEBUG" "" CMAKE_CUDA_FLAGS_RELEASE "${CMAKE_CUDA_FLAGS_RELEASE}")
|
||||
|
||||
# Include directories and libraries
|
||||
include_directories(../../../Common)
|
||||
|
||||
# Source file
|
||||
# Add target for simpleAssert
|
||||
add_executable(simpleAssert simpleAssert.cu)
|
||||
|
||||
target_compile_options(simpleAssert PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(simpleAssert PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(simpleAssert PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
include(InstallSamples)
|
||||
setup_samples_install()
|
||||
|
||||
@@ -1,32 +1,83 @@
|
||||
# simpleAssert - simpleAssert
|
||||
# Sample: Device-Side assert (simpleAssert)
|
||||
|
||||
## Description
|
||||
|
||||
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 .
|
||||
Use the standard C `assert` macro **inside a CUDA kernel**. The sample launches threads that each assert `gtid < N`; threads whose global index reaches `N` trip the assertion, print a diagnostic message to the host, and cause the kernel to fail. The host detects this through the `cudaErrorAssert` status returned by `cudaDeviceSynchronize` and reports it as the expected outcome.
|
||||
|
||||
This sample demonstrates device-side `assert`, a debugging aid for catching invalid conditions in kernel code, and shows how an assertion failure surfaces on the host as an asynchronous CUDA error.
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
- Calling `assert` directly from device code (`__global__` kernel)
|
||||
- How a failed device assertion is reported to the host as `cudaErrorAssert`
|
||||
- Detecting that error after the launch via `cudaDeviceSynchronize`
|
||||
- Computing a global thread index from `blockIdx`, `blockDim`, and `threadIdx`
|
||||
- Turning an error code into a human-readable message with `cudaGetErrorString`
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Assert
|
||||
- **Device-side `assert`** — `assert(condition)` in a kernel; a false condition halts the kernel, prints `file:line: function: block: ... Assertion ... failed`, and flags the launch as failed
|
||||
- **Asynchronous error reporting** — the assertion failure is not seen at launch time; it is surfaced at the next synchronization point as `cudaErrorAssert`
|
||||
- **Global thread indexing** — `blockIdx.x * 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; flushes assert output and returns `cudaErrorAssert` if an assertion failed
|
||||
- `cudaGetErrorString` — convert a `cudaError_t` into a readable description
|
||||
|
||||
## Supported OSes
|
||||
## Requirements
|
||||
|
||||
Linux, Windows
|
||||
### Hardware
|
||||
- NVIDIA GPU with Compute Capability 7.5 or higher
|
||||
|
||||
## Supported CPU Architecture
|
||||
### Software
|
||||
- CMake 3.20 or newer
|
||||
- A C++17-capable host compiler
|
||||
|
||||
x86_64, armv7l
|
||||
## How to Build
|
||||
|
||||
## CUDA APIs involved
|
||||
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.
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaDeviceSynchronize, cudaGetErrorString
|
||||
## How to Run
|
||||
|
||||
## Prerequisites
|
||||
```bash
|
||||
./simpleAssert
|
||||
```
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
## Expected Output
|
||||
|
||||
## References (for more details)
|
||||
The kernel launches 2 blocks × 32 threads = 64 threads and asserts `gtid < 60`, so the 4 threads with global indices 60–63 fail the assertion. The assertion failures are **expected** — the sample reports `OK` because it successfully detected `cudaErrorAssert`:
|
||||
|
||||
```text
|
||||
simpleAssert starting...
|
||||
|
||||
GPU Device 0: with compute capability X.Y and Number of SMs <smCount>
|
||||
|
||||
Launch kernel to generate assertion failures
|
||||
|
||||
-- Begin assert output
|
||||
|
||||
simpleAssert.cu:50: void simpleAssertKernel(int): block: [1,0,0], thread: [28,0,0] Assertion `gtid < N` failed.
|
||||
...
|
||||
|
||||
-- End assert output
|
||||
|
||||
Device assert failed as expected, CUDA error message is: device-side assert triggered
|
||||
|
||||
simpleAssert completed, returned OK
|
||||
```
|
||||
|
||||
The order and exact set of failing-thread lines may vary between runs.
|
||||
|
||||
## Files
|
||||
|
||||
- `simpleAssert.cu` — device-side `assert` kernel + host driver
|
||||
- `README.md` — this file
|
||||
- `CMakeLists.txt` — build configuration
|
||||
|
||||
## See Also
|
||||
|
||||
- [CUDA C++ Programming Guide - Assertion](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/cpp-language-extensions.html#assertion)
|
||||
|
||||
@@ -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
|
||||
@@ -25,86 +25,47 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
#define WINDOWS_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/utsname.h>
|
||||
#endif
|
||||
|
||||
// Includes, system
|
||||
// System includes
|
||||
#include <cassert>
|
||||
#include <stdio.h>
|
||||
|
||||
// Includes CUDA
|
||||
// CUDA runtime
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
// Utilities and timing functions
|
||||
#include <helper_functions.h> // includes cuda.h and cuda_runtime_api.h
|
||||
|
||||
// CUDA helper functions
|
||||
#include <helper_cuda.h> // helper functions for CUDA error check
|
||||
|
||||
const char *sampleName = "simpleAssert";
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Auto-Verification Code
|
||||
bool testResult = true;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Kernels
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//! Tests assert function.
|
||||
//! Thread whose id > N will print assertion failed error message.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void testKernel(int N)
|
||||
// Each thread computes its global index and asserts it is below N. Threads with
|
||||
// gtid >= N trip the assertion, which aborts the kernel and surfaces on the host
|
||||
// as cudaErrorAssert at the next synchronization point.
|
||||
__global__ void simpleAssertKernel(int N)
|
||||
{
|
||||
int gtid = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
assert(gtid < N);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Declaration, forward
|
||||
void runTest(int argc, char **argv);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Program main
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
printf("%s starting...\n", sampleName);
|
||||
|
||||
runTest(argc, argv);
|
||||
|
||||
printf("%s completed, returned %s\n", sampleName, testResult ? "OK" : "ERROR!");
|
||||
exit(testResult ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void runTest(int argc, char **argv)
|
||||
{
|
||||
int Nblocks = 2;
|
||||
int Nthreads = 32;
|
||||
cudaError_t error;
|
||||
|
||||
#ifndef _WIN32
|
||||
utsname OS_System_Type;
|
||||
uname(&OS_System_Type);
|
||||
printf("%s starting...\n\n", sampleName);
|
||||
|
||||
printf("OS_System_Type.release = %s\n", OS_System_Type.release);
|
||||
// Select device 0 as the active GPU
|
||||
int devID = 0;
|
||||
cudaSetDevice(devID);
|
||||
|
||||
if (!strcasecmp(OS_System_Type.sysname, "Darwin")) {
|
||||
printf("simpleAssert is not current supported on Mac OSX\n\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else {
|
||||
printf("OS Info: <%s>\n\n", OS_System_Type.version);
|
||||
}
|
||||
// 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);
|
||||
|
||||
#endif
|
||||
|
||||
// This will pick the best possible CUDA capable device
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
// Print device info
|
||||
printf("GPU Device %d: with compute capability %d.%d and Number of SMs %d\n\n", devID, major, minor, smCount);
|
||||
|
||||
// Kernel configuration, where a one-dimensional
|
||||
// grid and one-dimensional blocks are configured.
|
||||
@@ -112,7 +73,7 @@ void runTest(int argc, char **argv)
|
||||
dim3 dimBlock(Nthreads);
|
||||
|
||||
printf("Launch kernel to generate assertion failures\n");
|
||||
testKernel<<<dimGrid, dimBlock>>>(60);
|
||||
simpleAssertKernel<<<dimGrid, dimBlock>>>(60);
|
||||
|
||||
// Synchronize (flushes assert output).
|
||||
printf("\n-- Begin assert output\n\n");
|
||||
@@ -127,4 +88,7 @@ void runTest(int argc, char **argv)
|
||||
}
|
||||
|
||||
testResult = error == cudaErrorAssert;
|
||||
|
||||
printf("%s completed, returned %s\n", sampleName, testResult ? "OK" : "ERROR!");
|
||||
exit(testResult ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user