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/0_Introduction/simpleCooperativeGroups/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/0_Introduction/simpleCooperativeGroups/.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/0_Introduction/simpleCooperativeGroups/.vscode/extensions.json
vendored
Normal file
7
cpp/0_Introduction/simpleCooperativeGroups/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
34
cpp/0_Introduction/simpleCooperativeGroups/CMakeLists.txt
Normal file
34
cpp/0_Introduction/simpleCooperativeGroups/CMakeLists.txt
Normal file
@@ -0,0 +1,34 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(simpleCooperativeGroups LANGUAGES C CXX CUDA)
|
||||
|
||||
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 simpleCooperativeGroups
|
||||
add_executable(simpleCooperativeGroups simpleCooperativeGroups.cu)
|
||||
|
||||
target_compile_options(simpleCooperativeGroups PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(simpleCooperativeGroups PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(simpleCooperativeGroups PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
32
cpp/0_Introduction/simpleCooperativeGroups/README.md
Normal file
32
cpp/0_Introduction/simpleCooperativeGroups/README.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# simpleCooperativeGroups - Simple Cooperative Groups
|
||||
|
||||
## Description
|
||||
|
||||
This sample is a simple code that illustrates basic usage of cooperative groups within the thread block.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Cooperative Groups
|
||||
|
||||
## 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)
|
||||
cudaDeviceSynchronize, cudaGetErrorString
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
|
||||
## References (for more details)
|
||||
@@ -0,0 +1,177 @@
|
||||
/* 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 is a simple code that illustrates basic usage of
|
||||
* cooperative groups within the thread block. The code launches a single
|
||||
* thread block, creates a cooperative group of all threads in the block,
|
||||
* and a set of tiled partition cooperative groups. For each, it uses a
|
||||
* generic reduction function to calculate the sum of all the ranks in
|
||||
* that group. In each case the result is printed, together with the
|
||||
* expected answer (which is calculated using the analytical formula
|
||||
* (n-1)*n)/2, noting that the ranks start at zero).
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cooperative_groups.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace cooperative_groups;
|
||||
|
||||
/**
|
||||
* CUDA device function
|
||||
*
|
||||
* calculates the sum of val across the group g. The workspace array, x,
|
||||
* must be large enough to contain g.size() integers.
|
||||
*/
|
||||
__device__ int sumReduction(thread_group g, int *x, int val)
|
||||
{
|
||||
// rank of this thread in the group
|
||||
int lane = g.thread_rank();
|
||||
|
||||
// for each iteration of this loop, the number of threads active in the
|
||||
// reduction, i, is halved, and each active thread (with index [lane])
|
||||
// performs a single summation of it's own value with that
|
||||
// of a "partner" (with index [lane+i]).
|
||||
for (int i = g.size() / 2; i > 0; i /= 2) {
|
||||
// store value for this thread in temporary array
|
||||
x[lane] = val;
|
||||
|
||||
// synchronize all threads in group
|
||||
g.sync();
|
||||
|
||||
if (lane < i)
|
||||
// active threads perform summation of their value with
|
||||
// their partner's value
|
||||
val += x[lane + i];
|
||||
|
||||
// synchronize all threads in group
|
||||
g.sync();
|
||||
}
|
||||
|
||||
// master thread in group returns result, and others return -1.
|
||||
if (g.thread_rank() == 0)
|
||||
return val;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* CUDA kernel device code
|
||||
*
|
||||
* Creates cooperative groups and performs reductions
|
||||
*/
|
||||
__global__ void cgkernel()
|
||||
{
|
||||
// threadBlockGroup includes all threads in the block
|
||||
thread_block threadBlockGroup = this_thread_block();
|
||||
int threadBlockGroupSize = threadBlockGroup.size();
|
||||
|
||||
// workspace array in shared memory required for reduction
|
||||
extern __shared__ int workspace[];
|
||||
|
||||
int input, output, expectedOutput;
|
||||
|
||||
// input to reduction, for each thread, is its' rank in the group
|
||||
input = threadBlockGroup.thread_rank();
|
||||
|
||||
// expected output from analytical formula (n-1)(n)/2
|
||||
// (noting that indexing starts at 0 rather than 1)
|
||||
expectedOutput = (threadBlockGroupSize - 1) * threadBlockGroupSize / 2;
|
||||
|
||||
// perform reduction
|
||||
output = sumReduction(threadBlockGroup, workspace, input);
|
||||
|
||||
// master thread in group prints out result
|
||||
if (threadBlockGroup.thread_rank() == 0) {
|
||||
printf(" Sum of all ranks 0..%d in threadBlockGroup is %d (expected %d)\n\n",
|
||||
(int)threadBlockGroup.size() - 1,
|
||||
output,
|
||||
expectedOutput);
|
||||
|
||||
printf(" Now creating %d groups, each of size 16 threads:\n\n", (int)threadBlockGroup.size() / 16);
|
||||
}
|
||||
|
||||
threadBlockGroup.sync();
|
||||
|
||||
// each tiledPartition16 group includes 16 threads
|
||||
thread_block_tile<16> tiledPartition16 = tiled_partition<16>(threadBlockGroup);
|
||||
|
||||
// This offset allows each group to have its own unique area in the workspace
|
||||
// array
|
||||
int workspaceOffset = threadBlockGroup.thread_rank() - tiledPartition16.thread_rank();
|
||||
|
||||
// input to reduction, for each thread, is its' rank in the group
|
||||
input = tiledPartition16.thread_rank();
|
||||
|
||||
// expected output from analytical formula (n-1)(n)/2
|
||||
// (noting that indexing starts at 0 rather than 1)
|
||||
expectedOutput = 15 * 16 / 2;
|
||||
|
||||
// Perform reduction
|
||||
output = sumReduction(tiledPartition16, workspace + workspaceOffset, input);
|
||||
|
||||
// each master thread prints out result
|
||||
if (tiledPartition16.thread_rank() == 0)
|
||||
printf(" Sum of all ranks 0..15 in this tiledPartition16 group is %d "
|
||||
"(expected %d)\n",
|
||||
output,
|
||||
expectedOutput);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Host main routine
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
// Error code to check return values for CUDA calls
|
||||
cudaError_t err;
|
||||
|
||||
// Launch the kernel
|
||||
|
||||
int blocksPerGrid = 1;
|
||||
int threadsPerBlock = 64;
|
||||
|
||||
printf("\nLaunching a single block with %d threads...\n\n", threadsPerBlock);
|
||||
|
||||
// we use the optional third argument to specify the size
|
||||
// of shared memory required in the kernel
|
||||
cgkernel<<<blocksPerGrid, threadsPerBlock, threadsPerBlock * sizeof(int)>>>();
|
||||
err = cudaDeviceSynchronize();
|
||||
|
||||
if (err != cudaSuccess) {
|
||||
fprintf(stderr, "Failed to launch kernel (error code %s)!\n", cudaGetErrorString(err));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("\n...Done.\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user