mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-11-17 17:38:18 +08:00
Post v13.5 (#470)
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
## Changelog
|
||||
|
||||
### CUDA 13.5
|
||||
* Added `sm_121` to the default set of GPU architectures the C++ samples build for.
|
||||
* Added `cpp/3_CUDA_Features/cftUnicastPut` - an MPI sample demonstrating unicast point-to-point data movement between GPUs with Compute Fabric Transport (CFT) logical endpoints, showing both plain fabric puts with an inter-GPU flag handshake and counted fabric puts with a byte-arrival counter for receive completion.
|
||||
* Added `cpp/10_Safety/sharedCtx`, a QNX Safety sample. Two processes each keep their own CUDA primary context and share one set of GPU scheduling resources through the CUDA Safe runtime. The sample takes no arguments and launches the second process itself.
|
||||
|
||||
### CUDA 13.4
|
||||
* Removed obsolete samples `simpleHyperQ`, `simpleOccupancy`, `mergeSort`, `simpleTemplates`, `template`, `p2pBandwidthLatencyTest`.
|
||||
* Removed redundant `_nvrtc` sample variants (`clock_nvrtc`, `matrixMul_nvrtc`, `simpleAssert_nvrtc`, `simpleAtomicIntrinsics_nvrtc`, `inlinePTX_nvrtc`, `binomialOptions_nvrtc`, `BlackScholes_nvrtc`, `quasirandomGenerator_nvrtc`); `vectorAdd_nvrtc` remains as the canonical NVRTC example.
|
||||
|
||||
62
README.md
62
README.md
@@ -1,6 +1,6 @@
|
||||
# CUDA Samples
|
||||
|
||||
Samples for CUDA Developers which demonstrates features in CUDA Toolkit. This version supports [CUDA Toolkit 13.4](https://developer.nvidia.com/cuda-downloads).
|
||||
Samples for CUDA Developers which demonstrates features in CUDA Toolkit. This version supports [CUDA Toolkit 13.5](https://developer.nvidia.com/cuda-downloads).
|
||||
|
||||
## Release Notes
|
||||
|
||||
@@ -28,7 +28,7 @@ Without using git the easiest way to use these samples is to download the zip fi
|
||||
|
||||
## Building CUDA Samples
|
||||
|
||||
The CUDA Samples are built using CMake. Follow the instructions below for building on Linux, Windows, and for cross-compilation to Tegra devices.
|
||||
The CUDA Samples are built using CMake. Follow the instructions below for building on Linux, Windows, natively on Tegra, and for cross-compilation to Automotive Linux and QNX.
|
||||
|
||||
### Linux
|
||||
|
||||
@@ -113,26 +113,30 @@ To build these samples, set the variables either on the command line or through
|
||||
cmake -DBUILD_TEGRA=True ..
|
||||
```
|
||||
|
||||
### Cross-Compilation for Tegra Platforms
|
||||
### Native Building on Tegra Platforms
|
||||
|
||||
Install the NVIDIA toolchain and cross-compilation environment for Tegra devices as described in the Tegra Development Guide.
|
||||
Build on the Tegra device itself (Jetson L4T or Drive OS Linux). Do not pass a CMake toolchain file; the compilers on the board already target aarch64 Linux. Cross compilation from x86 is covered in the Automotive Linux and QNX sections below.
|
||||
|
||||
Ensure that CMake (version 3.20 or later) is installed.
|
||||
Ensure that CMake (version 3.20 or later) and the CUDA toolkit are installed on the device. Then, from the repository root:
|
||||
|
||||
Navigate to the root of the cloned repository and create a build directory:
|
||||
```
|
||||
mkdir build && cd build
|
||||
```
|
||||
Configure the project with CMake, specifying the Tegra toolchain file. And you can use -DTARGET_FS to point to the target file system root path for necessary include and library files:
|
||||
```
|
||||
cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/toolchain-aarch64-linux.cmake -DTARGET_FS=/path/to/target/system/file/system
|
||||
```
|
||||
Build the samples:
|
||||
```
|
||||
cmake .. -DBUILD_TEGRA=True \
|
||||
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc \
|
||||
-DCMAKE_CUDA_ARCHITECTURES=<arch>
|
||||
make -j$(nproc)
|
||||
```
|
||||
Transfer the built binaries to the Tegra device and execute them there.
|
||||
|
||||
Replace `<arch>` with the GPU SM version (`87` for Orin, `110` for Thor).
|
||||
|
||||
On some Drive OS installs the toolkit keeps headers and libraries only under a chip-suffixed directory such as `targets/aarch64-linux-thor`, and nvcc's default target directory does not find them. In that case add the matching flag (use the directory name that exists under `/usr/local/cuda/targets/`):
|
||||
|
||||
```
|
||||
cmake .. -DBUILD_TEGRA=True \
|
||||
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc \
|
||||
-DCMAKE_CUDA_ARCHITECTURES=110 \
|
||||
-DCMAKE_CUDA_FLAGS="--target-directory aarch64-linux-thor"
|
||||
```
|
||||
|
||||
### Cross Building for Automotive Linux Platforms from the DriveOS Docker containers
|
||||
|
||||
@@ -152,7 +156,7 @@ Mount the filesystem by running the following command:
|
||||
$ mount /drive/drive-linux/filesystem/targetfs-images/dev_nsr_desktop_ubuntu-24.04_thor_rfs.img /drive/temp
|
||||
```
|
||||
|
||||
Configure the project by running the following cmake command:
|
||||
Configure the project by running the following cmake command. Replace `cuda-XX.X` with the CUDA version on the target filesystem, and `aarch64-linux-thor` with the directory name that exists under `targets/` (on both the host toolkit and the mounted RFS):
|
||||
|
||||
```
|
||||
$ mkdir build && cd build
|
||||
@@ -160,8 +164,9 @@ $ cmake .. -DBUILD_TEGRA=True \
|
||||
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc \
|
||||
-DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/toolchain-aarch64-linux.cmake \
|
||||
-DTARGET_FS=/drive/temp \
|
||||
-DCMAKE_LIBRARY_PATH=/drive/temp/usr/local/cuda-13.1/thor/lib64/ \
|
||||
-DCMAKE_INCLUDE_PATH=/drive/temp/usr/local/cuda-13.1/thor/include/
|
||||
-DCMAKE_CUDA_FLAGS="--target-directory aarch64-linux-thor" \
|
||||
-DCMAKE_LIBRARY_PATH=/drive/temp/usr/local/cuda-XX.X/targets/aarch64-linux-thor/lib/ \
|
||||
-DCMAKE_INCLUDE_PATH=/drive/temp/usr/local/cuda-XX.X/targets/aarch64-linux-thor/include/
|
||||
```
|
||||
|
||||
Please note that the following libraries are not pre-installed in the DriveOS dev-nsr target filesystem:
|
||||
@@ -177,12 +182,12 @@ $ make -j$(nproc) --ignore-errors # or --keep-going
|
||||
```
|
||||
|
||||
```
|
||||
# In cpp/5_Domain_Specific/CMakeList.txt
|
||||
# In cpp/5_Domain_Specific/CMakeLists.txt
|
||||
# add_subdirectory(simpleGL)
|
||||
# add_subdirectory(simpleVulkan)
|
||||
# add_subdirectory(simpleVulkanMMAP)
|
||||
|
||||
# In cpp/8_Platform_Specific/Tegra/CMakeList.txt
|
||||
# In cpp/8_Platform_Specific/Tegra/CMakeLists.txt
|
||||
# add_subdirectory(simpleGLES_EGLOutput)
|
||||
```
|
||||
|
||||
@@ -197,17 +202,18 @@ export QNX_HOST=/path/to/qnx/host
|
||||
export QNX_TARGET=/path/to/qnx/target
|
||||
```
|
||||
|
||||
Build the samples for the Tegra Thor QNX platform:
|
||||
Build the samples for the Tegra Thor QNX platform. Replace `aarch64-qnx-thor` with the directory name that exists under `/usr/local/cuda/targets/`:
|
||||
|
||||
```bash
|
||||
mkdir -p build && cd build
|
||||
cmake .. \
|
||||
-DBUILD_TEGRA=True \
|
||||
-DCMAKE_CUDA_COMPILER=/usr/local/cuda-13.3/bin/nvcc \
|
||||
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc \
|
||||
-DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/toolchain-aarch64-qnx.cmake \
|
||||
-DTARGET_FS=/path/to/qnx/targetfs \
|
||||
-DCMAKE_LIBRARY_PATH=/usr/local/cuda-13.3/thor/targets/aarch64-qnx/lib/stubs/ \
|
||||
-DCMAKE_INCLUDE_PATH=/usr/local/cuda-13.3/thor/targets/aarch64-qnx/include/
|
||||
-DCMAKE_CUDA_FLAGS="--target-directory aarch64-qnx-thor" \
|
||||
-DCMAKE_LIBRARY_PATH=/usr/local/cuda/targets/aarch64-qnx-thor/lib/stubs/ \
|
||||
-DCMAKE_INCLUDE_PATH=/usr/local/cuda/targets/aarch64-qnx-thor/include/
|
||||
cmake --build .
|
||||
```
|
||||
|
||||
@@ -219,14 +225,14 @@ For both QNX toolchains the build looks for `nvscibuf.h` in `<TARGET_FS>/include
|
||||
|
||||
### QNX Safety (CUDA Safe toolkit)
|
||||
|
||||
Cross-compilation for **QNX Safety** uses the CUDA Safe toolkit (for example `/usr/local/cuda-13.3-safe`).
|
||||
Cross-compilation for **QNX Safety** uses the CUDA Safe toolkit (for example `/usr/local/cuda-XX.X-safe`).
|
||||
|
||||
Set up the QNX SDP and Safe toolkit paths:
|
||||
|
||||
```bash
|
||||
export QNX_HOST=/path/to/qnx/host
|
||||
export QNX_TARGET=/path/to/qnx/target
|
||||
export CUDA_PATH=/usr/local/cuda-13.3-safe
|
||||
export CUDA_PATH=/usr/local/cuda-XX.X-safe
|
||||
export PATH=$CUDA_PATH/nvvm/bin:$PATH
|
||||
```
|
||||
|
||||
@@ -244,9 +250,9 @@ cmake --build .
|
||||
|
||||
The safe toolchain sets `-safety-compat`, links shared `libcudart` and `libcuda`, and uses QNX `q++` as the CUDA host compiler (`CMAKE_CUDA_HOST_COMPILER`). Set `CMAKE_CUDA_ARCHITECTURES` to match your GPU (`87` for Orin, `110` for Thor).
|
||||
|
||||
Supported QNX Safety samples: `matrixMul`, `cudaNvSci`.
|
||||
Supported QNX Safety samples: `matrixMul`, `cudaNvSci` and all samples in `cpp/10_Safety` directory.
|
||||
|
||||
`TARGET_FS` is only needed for `cudaNvSci`, which links the NvSci libraries from the safety target filesystem (`<NV_WORKSPACE>/drive-qnx-safety`, part of the DRIVE OS QNX Safety SDK). `matrixMul` builds without it. The NvSci search paths are the same as for the standard QNX build above.
|
||||
`TARGET_FS` is only needed for `cudaNvSci`, which links the NvSci libraries from the safety target filesystem (`<NV_WORKSPACE>/drive-qnx-safety`, part of the DRIVE OS QNX Safety SDK). `matrixMul` and `sharedCtx` build without it. The NvSci search paths are the same as for the standard QNX build above.
|
||||
|
||||
### Forward Compatibility
|
||||
|
||||
@@ -584,7 +590,7 @@ OpenGL ES is an embedded systems graphics library used for 2D and 3D rendering.
|
||||
|
||||
#### Freeglut
|
||||
|
||||
Freeglut is an open-source software library that serves as a replacement for the original OpenGL Utility Toolkit (GLUT). Its primary purpose is to make it easier for developers to create and manage windows containing OpenGL contexts, as well as handle input from devices like the mouse, keyboard, and joystick, across a wide range of platforms. To set up Freeglut on a Windowson on ARM system, you need to download the source from [Freeglut website](https://freeglut.sourceforge.net/), build freeglut on your system, and copy the freeglut.lib into the folder `./Common/lib/x64` and copy the freeglut.dll file into the `./bin/win64/${BUILD_TYPE}` execution folder.
|
||||
Freeglut is an open-source software library that serves as a replacement for the original OpenGL Utility Toolkit (GLUT). Its primary purpose is to make it easier for developers to create and manage windows containing OpenGL contexts, as well as handle input from devices like the mouse, keyboard, and joystick, across a wide range of platforms. To set up Freeglut on a Windows on ARM system, you need to download the source from [Freeglut website](https://freeglut.sourceforge.net/), build freeglut on your system, and copy the freeglut.lib into the folder `./Common/lib/x64` and copy the freeglut.dll file into the `./bin/win64/${BUILD_TYPE}` execution folder.
|
||||
|
||||
#### Vulkan
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
set(CUDA_SAMPLES_MASTER_ARCHS 75 80 86 87 89 90 100 103 107 110 120)
|
||||
set(CUDA_SAMPLES_MASTER_ARCHS 75 80 86 87 89 90 100 103 107 110 120 121)
|
||||
|
||||
@@ -10,8 +10,7 @@ set(CMAKE_RANLIB aarch64-linux-gnu-ranlib)
|
||||
# Indicate cross-compiling.
|
||||
set(CMAKE_CROSSCOMPILING TRUE)
|
||||
|
||||
# Set CUDA compiler flags
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -ccbin ${CMAKE_CXX_COMPILER}" CACHE STRING "" FORCE)
|
||||
string(APPEND CMAKE_CUDA_FLAGS " -ccbin ${CMAKE_CXX_COMPILER}")
|
||||
|
||||
# Use a local sysroot copy
|
||||
if(DEFINED TARGET_FS)
|
||||
@@ -30,7 +29,7 @@ if(DEFINED TARGET_FS)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler --sysroot=${TARGET_FS}")
|
||||
string(APPEND CMAKE_CUDA_FLAGS " -Xcompiler --sysroot=${TARGET_FS}")
|
||||
|
||||
set(LIB_PATHS
|
||||
"${TARGET_FS}/usr/lib/"
|
||||
|
||||
@@ -22,19 +22,18 @@ set(CMAKE_CXX_COMPILER ${QNX_QPLUS})
|
||||
set(CMAKE_C_COMPILER_TARGET aarch64)
|
||||
set(CMAKE_CXX_COMPILER_TARGET aarch64)
|
||||
|
||||
# Set compiler flags
|
||||
set(CMAKE_CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER} CACHE STRING "" FORCE)
|
||||
# Set compiler flags. Host compiler is a normal (non-cache) toolchain variable so it
|
||||
# is not FORCE-stamped into the cache.
|
||||
set(CMAKE_CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER})
|
||||
set(CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "-nodlink -L${CUDA_ROOT}/lib64 -L${CUDA_ROOT}/lib -I${CUDA_ROOT}/include")
|
||||
|
||||
set(CMAKE_C_FLAGS " \"-V${__qnx_gcc_ver},gcc_ntoaarch64le\"")
|
||||
set(CMAKE_CXX_FLAGS " \"-V${__qnx_gcc_ver},gcc_ntoaarch64le\"")
|
||||
set(CMAKE_CUDA_FLAGS " --qpp-config=${__qnx_gcc_ver},gcc_ntoaarch64le")
|
||||
string(APPEND CMAKE_C_FLAGS " \"-V${__qnx_gcc_ver},gcc_ntoaarch64le\"")
|
||||
string(APPEND CMAKE_CXX_FLAGS " \"-V${__qnx_gcc_ver},gcc_ntoaarch64le\" -D_QNX_SOURCE")
|
||||
string(APPEND CMAKE_CUDA_FLAGS " --qpp-config=${__qnx_gcc_ver},gcc_ntoaarch64le -D_QNX_SOURCE")
|
||||
set(AUTOMAGIC_NVCC_FLAGS --qpp-config=${__qnx_gcc_ver},gcc_ntoaarch64le CACHE STRING "automagic feature detection flags for cross build")
|
||||
add_link_options("-V${__qnx_gcc_ver},gcc_ntoaarch64le")
|
||||
|
||||
set(CROSS_COMPILE_FOR_QNX ON CACHE BOOL "Cross compiling for QNX platforms")
|
||||
string(APPEND CMAKE_CXX_FLAGS " -D_QNX_SOURCE")
|
||||
string(APPEND CMAKE_CUDA_FLAGS " -D_QNX_SOURCE")
|
||||
|
||||
# cudaNvSci needs NvSci headers/libs from the QNX rootfs; point TARGET_FS at it. This pre-seeds
|
||||
# FindNVSCI's cache vars and auto-detects the header/lib dirs across rootfs layouts.
|
||||
|
||||
@@ -38,12 +38,14 @@ foreach(arch ${CMAKE_CUDA_ARCHITECTURES})
|
||||
list(APPEND GENCODE_FLAGS "-gencode=arch=compute_${arch},code=sm_${arch}")
|
||||
endforeach()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
|
||||
set(INCLUDES_LIST)
|
||||
# This custom command does not inherit CMAKE_CUDA_FLAGS (e.g. --target-directory), so pass the
|
||||
# toolkit include dirs explicitly. On aarch64 (Tegra/QNX/SBSA) the toolkit may keep its headers
|
||||
# in a chip-suffixed targets/ dir that nvcc does not search by default.
|
||||
set(INCLUDES)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
|
||||
foreach(dir ${CUDAToolkit_INCLUDE_DIRS})
|
||||
list(APPEND INCLUDES_LIST "-I${dir}")
|
||||
list(APPEND INCLUDES "-I${dir}")
|
||||
endforeach()
|
||||
string(JOIN " " INCLUDES "${INCLUDES_LIST}")
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
|
||||
@@ -40,12 +40,14 @@ foreach(arch ${CMAKE_CUDA_ARCHITECTURES})
|
||||
list(APPEND GENCODE_FLAGS "-gencode=arch=compute_${arch},code=sm_${arch}")
|
||||
endforeach()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
|
||||
set(INCLUDES_LIST)
|
||||
# This custom command does not inherit CMAKE_CUDA_FLAGS (e.g. --target-directory), so pass the
|
||||
# toolkit include dirs explicitly. On aarch64 (Tegra/QNX/SBSA) the toolkit may keep its headers
|
||||
# in a chip-suffixed targets/ dir that nvcc does not search by default.
|
||||
set(INCLUDES)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
|
||||
foreach(dir ${CUDAToolkit_INCLUDE_DIRS})
|
||||
list(APPEND INCLUDES_LIST "-I${dir}")
|
||||
list(APPEND INCLUDES "-I${dir}")
|
||||
endforeach()
|
||||
string(JOIN " " INCLUDES "${INCLUDES_LIST}")
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
|
||||
@@ -38,12 +38,14 @@ foreach(arch ${CMAKE_CUDA_ARCHITECTURES})
|
||||
list(APPEND GENCODE_FLAGS "-gencode=arch=compute_${arch},code=sm_${arch}")
|
||||
endforeach()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
|
||||
set(INCLUDES_LIST)
|
||||
# This custom command does not inherit CMAKE_CUDA_FLAGS (e.g. --target-directory), so pass the
|
||||
# toolkit include dirs explicitly. On aarch64 (Tegra/QNX/SBSA) the toolkit may keep its headers
|
||||
# in a chip-suffixed targets/ dir that nvcc does not search by default.
|
||||
set(INCLUDES)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
|
||||
foreach(dir ${CUDAToolkit_INCLUDE_DIRS})
|
||||
list(APPEND INCLUDES_LIST "-I${dir}")
|
||||
list(APPEND INCLUDES "-I${dir}")
|
||||
endforeach()
|
||||
string(JOIN " " INCLUDES "${INCLUDES_LIST}")
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
|
||||
@@ -38,12 +38,14 @@ foreach(arch ${CMAKE_CUDA_ARCHITECTURES})
|
||||
list(APPEND GENCODE_FLAGS "-gencode=arch=compute_${arch},code=sm_${arch}")
|
||||
endforeach()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
|
||||
set(INCLUDES_LIST)
|
||||
# This custom command does not inherit CMAKE_CUDA_FLAGS (e.g. --target-directory), so pass the
|
||||
# toolkit include dirs explicitly. On aarch64 (Tegra/QNX/SBSA) the toolkit may keep its headers
|
||||
# in a chip-suffixed targets/ dir that nvcc does not search by default.
|
||||
set(INCLUDES)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
|
||||
foreach(dir ${CUDAToolkit_INCLUDE_DIRS})
|
||||
list(APPEND INCLUDES_LIST "-I${dir}")
|
||||
list(APPEND INCLUDES "-I${dir}")
|
||||
endforeach()
|
||||
string(JOIN " " INCLUDES "${INCLUDES_LIST}")
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
|
||||
1
cpp/10_Safety/CMakeLists.txt
Normal file
1
cpp/10_Safety/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
add_subdirectory(sharedCtx)
|
||||
16
cpp/10_Safety/README.md
Normal file
16
cpp/10_Safety/README.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# CUDA Safety Samples
|
||||
|
||||
Safety-specific samples live in this directory. They demonstrate CUDA safety workflows.
|
||||
|
||||
Build a sample from its directory:
|
||||
|
||||
```bash
|
||||
mkdir -p build && cd build
|
||||
cmake .. \
|
||||
-DCMAKE_TOOLCHAIN_FILE=../../../cmake/toolchains/toolchain-aarch64-qnx-safe.cmake \
|
||||
-DCMAKE_CUDA_COMPILER=$CUDA_PATH/bin/nvcc \
|
||||
-DCMAKE_CUDA_ARCHITECTURES=110
|
||||
cmake --build .
|
||||
```
|
||||
|
||||
Set `CMAKE_CUDA_ARCHITECTURES` to `110` for Thor. See the top-level [README](../../README.md#qnx-safety-cuda-safe-toolkit) for the QNX Safety toolchain setup.
|
||||
42
cpp/10_Safety/sharedCtx/CMakeLists.txt
Normal file
42
cpp/10_Safety/sharedCtx/CMakeLists.txt
Normal file
@@ -0,0 +1,42 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
# The shared-context integration test builds for SM 8.7 and later.
|
||||
set(SAMPLE_DISALLOW_ARCHS 75 80 86)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/DetectCudaArch.cmake")
|
||||
|
||||
if(SAMPLE_SKIP_BUILD)
|
||||
return()
|
||||
endif()
|
||||
|
||||
project(sharedCtx LANGUAGES C CXX CUDA)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/CudaSampleCommon.cmake")
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
# cudaSafeExQueryDeviceKey / Export / Import ship only in the CUDA Safe runtime.
|
||||
set(_safe_ex_header "")
|
||||
foreach(_inc ${CUDAToolkit_INCLUDE_DIRS})
|
||||
if(EXISTS "${_inc}/cuda_runtime_api_safe_ex.h")
|
||||
set(_safe_ex_header "${_inc}/cuda_runtime_api_safe_ex.h")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(NOT CMAKE_SYSTEM_NAME STREQUAL "QNX" OR _safe_ex_header STREQUAL "")
|
||||
message(STATUS "Will not build sample sharedCtx - requires the CUDA Safe toolkit on QNX")
|
||||
return()
|
||||
endif()
|
||||
|
||||
include_directories(../../../Common)
|
||||
|
||||
add_executable(sharedCtx sharedCtx.cu)
|
||||
|
||||
target_compile_options(sharedCtx PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(sharedCtx PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
target_link_libraries(sharedCtx PRIVATE socket)
|
||||
|
||||
include(InstallSamples)
|
||||
setup_samples_install()
|
||||
37
cpp/10_Safety/sharedCtx/README.md
Normal file
37
cpp/10_Safety/sharedCtx/README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# sharedCtx - Shared Device Context (CUDA Safe Toolkit)
|
||||
|
||||
## Description
|
||||
|
||||
This sample shows how two CUDA processes, each with its own CUDA primary context, can share one set of GPU scheduling resources. Because both primary contexts are scheduled through the same GPU context, the GPU does not swap scheduling resources on and off when it switches between work from the two processes. Kernels launched from the two processes can also execute concurrently. The sample takes no arguments and launches the second process itself.
|
||||
|
||||
1. Process A calls `cudaSafeExQueryDeviceKey()` before any CUDA context is created in it, and sends the resulting device key to process B.
|
||||
2. Process B creates its primary context, then calls `cudaSafeExSharedDeviceCtxKeyExport()` with that device key.
|
||||
3. After process B has exported, process A calls `cudaSafeExSharedDeviceCtxKeyImport()`, then initializes its primary context. This primary context is created on the same GPU scheduling resources as process B's primary context.
|
||||
4. Each process launches a small kernel on its own primary context.
|
||||
|
||||
`cudaSafeExQueryDeviceKey()` and `cudaSafeExSharedDeviceCtxKeyImport()` must be called before a CUDA context is created in the calling process, including the primary context created implicitly by the first CUDA Runtime API call that triggers context creation. `cudaSafeExSharedDeviceCtxKeyExport()` must be called after the calling process has created its primary context.
|
||||
|
||||
## Why this is safety-specific
|
||||
|
||||
These three calls are safety extensions of the CUDA runtime. They are declared in `cuda_runtime_api_safe_ex.h`, which ships with the CUDA Safe toolkit, and they are allowed only while CUDA is in the Initialization state.
|
||||
|
||||
In standard CUDA, this functionality is exposed through the CUDA MPS feature.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
CUDA Safe Toolkit, Shared Device Context, Multi-Process
|
||||
|
||||
## Supported SM Architectures
|
||||
|
||||
[SM 11.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||
|
||||
## Supported OSes
|
||||
|
||||
QNX Safety (CUDA Safe toolkit cross-build)
|
||||
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the CUDA Safe toolkit for QNX Safety. It is skipped when `cuda_runtime_api_safe_ex.h` is not in the toolkit include path. It does not need `TARGET_FS`.
|
||||
|
||||
Build it from `cpp/10_Safety/sharedCtx` with the QNX Safety toolchain. Set `CMAKE_CUDA_ARCHITECTURES` to `110` for Thor. See the [safety samples README](../README.md).
|
||||
318
cpp/10_Safety/sharedCtx/sharedCtx.cu
Normal file
318
cpp/10_Safety/sharedCtx/sharedCtx.cu
Normal file
@@ -0,0 +1,318 @@
|
||||
/* 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
|
||||
* 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.h>
|
||||
#include <cuda_runtime_api_safe_ex.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern char **environ;
|
||||
|
||||
static const unsigned int kDevice = 0;
|
||||
static const int kTimeoutMs = 30000;
|
||||
static const int kElements = 256;
|
||||
// Inherited by the process this sample spawns. A normal launch does not have it open.
|
||||
static const int kIpcFd = 100;
|
||||
|
||||
__global__ void incrementKernel(int *data, int n)
|
||||
{
|
||||
int i = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i < n) {
|
||||
data[i] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
static bool checkRt(cudaError_t err, const char *what)
|
||||
{
|
||||
if (err != cudaSuccess) {
|
||||
printf("sharedCtx: %s failed: %s\n", what, cudaGetErrorString(err));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool isIpcSocket(int fd)
|
||||
{
|
||||
int soType = 0;
|
||||
socklen_t len = sizeof(soType);
|
||||
return getsockopt(fd, SOL_SOCKET, SO_TYPE, &soType, &len) == 0 && soType == SOCK_STREAM;
|
||||
}
|
||||
|
||||
static bool setRecvTimeout(int fd)
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = kTimeoutMs / 1000;
|
||||
tv.tv_usec = (kTimeoutMs % 1000) * 1000;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
|
||||
printf("sharedCtx: setsockopt SO_RCVTIMEO failed: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool writeAll(int fd, const void *data, size_t bytes)
|
||||
{
|
||||
const char *pos = static_cast<const char *>(data);
|
||||
size_t left = bytes;
|
||||
while (left > 0) {
|
||||
ssize_t n = send(fd, pos, left, 0);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
printf("sharedCtx: send failed: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
if (n == 0) {
|
||||
printf("sharedCtx: socket closed during send\n");
|
||||
return false;
|
||||
}
|
||||
pos += n;
|
||||
left -= static_cast<size_t>(n);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool readAll(int fd, void *data, size_t bytes)
|
||||
{
|
||||
char *pos = static_cast<char *>(data);
|
||||
size_t left = bytes;
|
||||
while (left > 0) {
|
||||
ssize_t n = recv(fd, pos, left, 0);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
printf("sharedCtx: recv failed: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
if (n == 0) {
|
||||
printf("sharedCtx: socket closed during recv\n");
|
||||
return false;
|
||||
}
|
||||
pos += n;
|
||||
left -= static_cast<size_t>(n);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Launch a kernel on the current primary context and check the result.
|
||||
static bool runIncrement(const char *role)
|
||||
{
|
||||
int *host = static_cast<int *>(malloc(sizeof(int) * kElements));
|
||||
if (host == nullptr) {
|
||||
printf("sharedCtx: %s host allocation failed\n", role);
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < kElements; ++i) {
|
||||
host[i] = i;
|
||||
}
|
||||
|
||||
int *device = nullptr;
|
||||
cudaStream_t stream = nullptr;
|
||||
if (!checkRt(cudaMalloc(&device, sizeof(int) * kElements), "cudaMalloc")
|
||||
|| !checkRt(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking), "cudaStreamCreateWithFlags")
|
||||
|| !checkRt(cudaMemcpyAsync(device, host, sizeof(int) * kElements, cudaMemcpyHostToDevice, stream),
|
||||
"cudaMemcpyAsync HtoD")) {
|
||||
if (device != nullptr) {
|
||||
checkRt(cudaFree(device), "cudaFree");
|
||||
}
|
||||
if (stream != nullptr) {
|
||||
checkRt(cudaStreamDestroy(stream), "cudaStreamDestroy");
|
||||
}
|
||||
free(host);
|
||||
return false;
|
||||
}
|
||||
|
||||
incrementKernel<<<1, kElements, 0, stream>>>(device, kElements);
|
||||
if (!checkRt(cudaGetLastError(), "incrementKernel launch")
|
||||
|| !checkRt(cudaStreamSynchronize(stream), "cudaStreamSynchronize")
|
||||
|| !checkRt(cudaMemcpyAsync(host, device, sizeof(int) * kElements, cudaMemcpyDeviceToHost, stream),
|
||||
"cudaMemcpyAsync DtoH")
|
||||
|| !checkRt(cudaStreamSynchronize(stream), "cudaStreamSynchronize")) {
|
||||
checkRt(cudaStreamDestroy(stream), "cudaStreamDestroy");
|
||||
checkRt(cudaFree(device), "cudaFree");
|
||||
free(host);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool released = checkRt(cudaStreamDestroy(stream), "cudaStreamDestroy");
|
||||
released = checkRt(cudaFree(device), "cudaFree") && released;
|
||||
|
||||
bool ok = true;
|
||||
for (int i = 0; i < kElements; ++i) {
|
||||
if (host[i] != i + 1) {
|
||||
printf("sharedCtx: %s mismatch at %d (got %d)\n", role, i, host[i]);
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(host);
|
||||
if (ok && released) {
|
||||
printf("sharedCtx: %s kernel passed\n", role);
|
||||
}
|
||||
return ok && released;
|
||||
}
|
||||
|
||||
static int queryProcess(int fd)
|
||||
{
|
||||
int rc = EXIT_FAILURE;
|
||||
cudaSafeExSharedDeviceCtxDeviceKey deviceKey;
|
||||
cudaSafeExSharedDeviceCtxShareKey shareKey;
|
||||
|
||||
printf("sharedCtx: querying device key\n");
|
||||
if (!checkRt(cudaSafeExQueryDeviceKey(&deviceKey, kDevice), "cudaSafeExQueryDeviceKey")
|
||||
|| !writeAll(fd, &deviceKey, sizeof(deviceKey)) || !readAll(fd, &shareKey, sizeof(shareKey))) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
printf("sharedCtx: importing share key\n");
|
||||
if (!checkRt(cudaSafeExSharedDeviceCtxKeyImport(&shareKey, kDevice), "cudaSafeExSharedDeviceCtxKeyImport")
|
||||
|| !checkRt(cudaFree(0), "cudaFree(0) after import") || !runIncrement("import process")) {
|
||||
goto done;
|
||||
}
|
||||
rc = EXIT_SUCCESS;
|
||||
|
||||
done:
|
||||
close(fd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int exportProcess(pid_t peer, int fd)
|
||||
{
|
||||
int rc = EXIT_FAILURE;
|
||||
int status = 0;
|
||||
bool peerReaped = false;
|
||||
|
||||
cudaSafeExSharedDeviceCtxDeviceKey deviceKey;
|
||||
cudaSafeExSharedDeviceCtxShareKey shareKey;
|
||||
|
||||
if (!readAll(fd, &deviceKey, sizeof(deviceKey))) {
|
||||
goto done;
|
||||
}
|
||||
printf("sharedCtx: received device key\n");
|
||||
|
||||
if (!checkRt(cudaFree(0), "cudaFree(0) before export")
|
||||
|| !checkRt(cudaSafeExSharedDeviceCtxKeyExport(&shareKey, deviceKey, kDevice),
|
||||
"cudaSafeExSharedDeviceCtxKeyExport")) {
|
||||
goto done;
|
||||
}
|
||||
printf("sharedCtx: exported share key\n");
|
||||
|
||||
if (!runIncrement("export process") || !writeAll(fd, &shareKey, sizeof(shareKey))) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (waitpid(peer, &status, 0) < 0) {
|
||||
printf("sharedCtx: waitpid failed: %s\n", strerror(errno));
|
||||
peerReaped = true;
|
||||
goto done;
|
||||
}
|
||||
peerReaped = true;
|
||||
if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) {
|
||||
int code = WIFEXITED(status) ? WEXITSTATUS(status) : status;
|
||||
printf("sharedCtx: peer failed with status %d\n", code);
|
||||
goto done;
|
||||
}
|
||||
|
||||
printf("sharedCtx: Result = PASS\n");
|
||||
rc = EXIT_SUCCESS;
|
||||
|
||||
done:
|
||||
close(fd);
|
||||
if (!peerReaped) {
|
||||
kill(peer, SIGKILL);
|
||||
waitpid(peer, nullptr, 0);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
|
||||
// The spawned process inherits this socket. The program takes no arguments.
|
||||
if (isIpcSocket(kIpcFd)) {
|
||||
return queryProcess(kIpcFd);
|
||||
}
|
||||
|
||||
int sv[2] = {-1, -1};
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0) {
|
||||
printf("sharedCtx: socketpair failed: %s\n", strerror(errno));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (!setRecvTimeout(sv[0]) || !setRecvTimeout(sv[1])) {
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
posix_spawn_file_actions_t actions;
|
||||
if (posix_spawn_file_actions_init(&actions) != 0) {
|
||||
printf("sharedCtx: posix_spawn_file_actions_init failed: %s\n", strerror(errno));
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
// CUDA does not survive fork() without exec(). Spawn a new process image and
|
||||
// hand it one end of the socket. The other end stays in this process.
|
||||
int spawnErr = posix_spawn_file_actions_adddup2(&actions, sv[1], kIpcFd);
|
||||
if (spawnErr == 0 && sv[0] != kIpcFd) {
|
||||
spawnErr = posix_spawn_file_actions_addclose(&actions, sv[0]);
|
||||
}
|
||||
if (spawnErr == 0 && sv[1] != kIpcFd) {
|
||||
spawnErr = posix_spawn_file_actions_addclose(&actions, sv[1]);
|
||||
}
|
||||
if (spawnErr != 0) {
|
||||
printf("sharedCtx: posix_spawn file action failed: %s\n", strerror(spawnErr));
|
||||
posix_spawn_file_actions_destroy(&actions);
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
char *childArgv[] = {argv[0], nullptr};
|
||||
pid_t peer = 0;
|
||||
spawnErr = posix_spawnp(&peer, argv[0], &actions, nullptr, childArgv, environ);
|
||||
posix_spawn_file_actions_destroy(&actions);
|
||||
close(sv[1]);
|
||||
if (spawnErr != 0) {
|
||||
printf("sharedCtx: posix_spawnp failed: %s\n", strerror(spawnErr));
|
||||
close(sv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return exportProcess(peer, sv[0]);
|
||||
}
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
## Description
|
||||
|
||||
Demonstrates CUDA and EGL Streams interop, where consumer's EGL Stream is on one GPU and producer's on other and both consumer-producer are different processes.
|
||||
Demonstrates CUDA and EGL Streams interop between separate consumer and producer processes. By default, both processes use the same GPU. Use `-crossdev` to place the consumer on a discrete GPU and the producer on an integrated GPU.
|
||||
|
||||
The consumer adds its process ID to the Unix socket name and passes the path to the producer. Each process pair uses a separate socket, so multiple sample instances can run at the same time.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
|
||||
@@ -89,6 +89,7 @@ typedef struct _TestArgs
|
||||
{
|
||||
unsigned int charCnt;
|
||||
bool isProducer;
|
||||
const char *socketPath;
|
||||
} TestArgs;
|
||||
|
||||
extern int WIDTH, HEIGHT;
|
||||
|
||||
@@ -93,12 +93,25 @@ int parseCmdLine(int argc, char *argv[], TestArgs *args)
|
||||
else if (strcmp(argv[i], "-v") == 0) {
|
||||
verbose = 1;
|
||||
}
|
||||
else if (strcmp(argv[i], "-socket") == 0) {
|
||||
if (++i >= argc || argv[i][0] == '\0'
|
||||
|| strlen(argv[i]) >= sizeof(sockaddr_un::sun_path)) {
|
||||
printf("Invalid producer socket path\n");
|
||||
return -1;
|
||||
}
|
||||
args->socketPath = argv[i];
|
||||
}
|
||||
else {
|
||||
printf("Unknown option: %s\n", argv[i]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (args->isProducer && args->socketPath == NULL) {
|
||||
printf("Producer socket path is required\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (isCrossDevice) {
|
||||
int deviceCount = 0;
|
||||
|
||||
@@ -192,7 +205,8 @@ void launchProducer(TestArgs *args)
|
||||
char argsProducer[1024];
|
||||
char str[256];
|
||||
|
||||
strcpy(argsProducer, "./EGLStream_CUDA_CrossGPU -proctype prod ");
|
||||
snprintf(argsProducer, sizeof(argsProducer),
|
||||
"./EGLStream_CUDA_CrossGPU -proctype prod -socket %s ", args->socketPath);
|
||||
|
||||
if (isCrossDevice) {
|
||||
sprintf(str, "-crossdev ");
|
||||
@@ -225,4 +239,5 @@ void printUsage(void)
|
||||
printf(" -crossdev Run with producer on idgpu and consumer on dgpu\n");
|
||||
printf(" -dgpu (same as -crossdev, deprecated)\n");
|
||||
printf(" -v verbose output\n");
|
||||
printf(" -socket path Producer socket path, set by the consumer\n");
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ void DoneProd(int producerStatus, int connect_fd)
|
||||
int WIDTH = 8192, HEIGHT = 8192;
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
TestArgs args = {0, false};
|
||||
TestArgs args = {0, false, NULL};
|
||||
CUresult curesult = CUDA_SUCCESS;
|
||||
unsigned int j = 0;
|
||||
cudaError_t err = cudaSuccess;
|
||||
@@ -90,11 +90,21 @@ int main(int argc, char **argv)
|
||||
CUeglFrame cudaEgl1, cudaEgl2;
|
||||
int consumerStatus = 0;
|
||||
int send_fd = -1;
|
||||
char socketPath[sizeof(sockaddr_un::sun_path)];
|
||||
|
||||
if (parseCmdLine(argc, argv, &args) < 0) {
|
||||
printUsage();
|
||||
curesult = CUDA_ERROR_UNKNOWN;
|
||||
DoneCons(consumerStatus, send_fd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!args.isProducer) {
|
||||
// Use the consumer PID to keep concurrent process pairs separate.
|
||||
int written = snprintf(socketPath, sizeof(socketPath), "%s_%ld", SOCK_PATH, (long)getpid());
|
||||
if (written < 0 || (size_t)written >= sizeof(socketPath)) {
|
||||
printf("Socket path is too long\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
args.socketPath = socketPath;
|
||||
}
|
||||
|
||||
printf("Width : %u, height: %u and iterations: %u\n", WIDTH, HEIGHT, NUMTRIALS);
|
||||
@@ -137,9 +147,9 @@ int main(int argc, char **argv)
|
||||
|
||||
cuCtxPopCurrent(&cudaConsumer.context);
|
||||
|
||||
send_fd = UnixSocketConnect(SOCK_PATH);
|
||||
send_fd = UnixSocketConnect(args.socketPath);
|
||||
if (-1 == send_fd) {
|
||||
printf("%s: Cuda Consumer cannot create socket %s\n", __func__, SOCK_PATH);
|
||||
printf("%s: Cuda Consumer cannot create socket %s\n", __func__, args.socketPath);
|
||||
consumerStatus = -1;
|
||||
DoneCons(consumerStatus, send_fd);
|
||||
}
|
||||
@@ -242,9 +252,9 @@ int main(int argc, char **argv)
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
// Create connection to Consumer
|
||||
connect_fd = UnixSocketCreate(SOCK_PATH);
|
||||
connect_fd = UnixSocketCreate(args.socketPath);
|
||||
if (-1 == connect_fd) {
|
||||
printf("%s: Cuda Producer could not create socket: %s.\n", __func__, SOCK_PATH);
|
||||
printf("%s: Cuda Producer could not create socket: %s.\n", __func__, args.socketPath);
|
||||
producerStatus = -1;
|
||||
DoneProd(producerStatus, connect_fd);
|
||||
}
|
||||
|
||||
@@ -42,12 +42,14 @@ foreach(arch ${CMAKE_CUDA_ARCHITECTURES})
|
||||
list(APPEND GENCODE_FLAGS "-gencode=arch=compute_${arch},code=sm_${arch}")
|
||||
endforeach()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
|
||||
set(INCLUDES_LIST)
|
||||
# This custom command does not inherit CMAKE_CUDA_FLAGS (e.g. --target-directory), so pass the
|
||||
# toolkit include dirs explicitly. On aarch64 (Tegra/QNX/SBSA) the toolkit may keep its headers
|
||||
# in a chip-suffixed targets/ dir that nvcc does not search by default.
|
||||
set(INCLUDES)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
|
||||
foreach(dir ${CUDAToolkit_INCLUDE_DIRS})
|
||||
list(APPEND INCLUDES_LIST "-I${dir}")
|
||||
list(APPEND INCLUDES "-I${dir}")
|
||||
endforeach()
|
||||
string(JOIN " " INCLUDES "${INCLUDES_LIST}")
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
|
||||
@@ -7,6 +7,7 @@ add_subdirectory(cdpBezierTessellation)
|
||||
add_subdirectory(cdpQuadtree)
|
||||
add_subdirectory(cdpSimplePrint)
|
||||
add_subdirectory(cdpSimpleQuicksort)
|
||||
add_subdirectory(cftUnicastPut)
|
||||
add_subdirectory(cudaCompressibleMemory)
|
||||
add_subdirectory(cudaTensorCoreGemm)
|
||||
add_subdirectory(dmabufInterop)
|
||||
|
||||
@@ -25,6 +25,9 @@ This sample demonstrates simple printf implemented using CUDA Dynamic Parallelis
|
||||
### [cdpSimpleQuicksort](./cdpSimpleQuicksort)
|
||||
This sample demonstrates simple quicksort implemented using CUDA Dynamic Parallelism. This sample requires devices with compute capability 3.5 or higher.
|
||||
|
||||
### [cftUnicastPut](./cftUnicastPut)
|
||||
This MPI sample demonstrates unicast point-to-point data movement between GPUs with Compute Fabric Transport (CFT) logical endpoints, using either plain fabric puts with an inter-GPU completion signal or counted fabric puts with a byte-arrival counter for receive completion.
|
||||
|
||||
### [cudaCompressibleMemory](./cudaCompressibleMemory)
|
||||
This sample demonstrates the compressible memory allocation using cuMemMap API.
|
||||
|
||||
|
||||
@@ -4,10 +4,12 @@ cmake_minimum_required(VERSION 3.20)
|
||||
# toolkit ships only for the native Tegra archs; drop the desktop archs there.
|
||||
get_filename_component(_ctk_root "${CMAKE_CUDA_COMPILER}" DIRECTORY)
|
||||
get_filename_component(_ctk_root "${_ctk_root}" DIRECTORY)
|
||||
if(EXISTS "${_ctk_root}/targets/aarch64-linux" OR EXISTS "${_ctk_root}/targets/aarch64-qnx")
|
||||
file(GLOB _tegra_targets "${_ctk_root}/targets/aarch64-linux*" "${_ctk_root}/targets/aarch64-qnx*")
|
||||
if(_tegra_targets)
|
||||
set(SAMPLE_DISALLOW_ARCHS 75 80 86 89 90 100 103 107 120)
|
||||
endif()
|
||||
unset(_ctk_root)
|
||||
unset(_tegra_targets)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/DetectCudaArch.cmake")
|
||||
|
||||
|
||||
@@ -4,10 +4,12 @@ cmake_minimum_required(VERSION 3.20)
|
||||
# toolkit ships only for the native Tegra archs; drop the desktop archs there.
|
||||
get_filename_component(_ctk_root "${CMAKE_CUDA_COMPILER}" DIRECTORY)
|
||||
get_filename_component(_ctk_root "${_ctk_root}" DIRECTORY)
|
||||
if(EXISTS "${_ctk_root}/targets/aarch64-linux" OR EXISTS "${_ctk_root}/targets/aarch64-qnx")
|
||||
file(GLOB _tegra_targets "${_ctk_root}/targets/aarch64-linux*" "${_ctk_root}/targets/aarch64-qnx*")
|
||||
if(_tegra_targets)
|
||||
set(SAMPLE_DISALLOW_ARCHS 75 80 86 89 90 100 103 107 120)
|
||||
endif()
|
||||
unset(_ctk_root)
|
||||
unset(_tegra_targets)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/DetectCudaArch.cmake")
|
||||
|
||||
|
||||
@@ -4,10 +4,16 @@ cmake_minimum_required(VERSION 3.20)
|
||||
# toolkit ships only for the native Tegra archs; drop the desktop archs there.
|
||||
get_filename_component(_ctk_root "${CMAKE_CUDA_COMPILER}" DIRECTORY)
|
||||
get_filename_component(_ctk_root "${_ctk_root}" DIRECTORY)
|
||||
if(EXISTS "${_ctk_root}/targets/aarch64-linux" OR EXISTS "${_ctk_root}/targets/aarch64-qnx")
|
||||
file(GLOB _tegra_targets "${_ctk_root}/targets/aarch64-linux*" "${_ctk_root}/targets/aarch64-qnx*")
|
||||
if(_tegra_targets)
|
||||
set(SAMPLE_DISALLOW_ARCHS 75 80 86 89 90 100 103 107 120)
|
||||
else()
|
||||
# The sm_86 device runtime changes the linked PTX architecture of sm_87
|
||||
# code, so Thrust cannot select the matching compiled policy.
|
||||
set(SAMPLE_DISALLOW_ARCHS 87)
|
||||
endif()
|
||||
unset(_ctk_root)
|
||||
unset(_tegra_targets)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/DetectCudaArch.cmake")
|
||||
|
||||
|
||||
@@ -10,6 +10,11 @@ Cooperative Groups, CUDA Dynamic Parallelism
|
||||
|
||||
## Supported SM Architectures
|
||||
|
||||
SM 8.7 builds are disabled with the SBSA CUDA Toolkit. Linking its SM 8.6
|
||||
device runtime into SM 8.7 code changes the reported PTX architecture,
|
||||
which prevents Thrust from selecting the matching compiled policy.
|
||||
DriveOS toolkits retain native SM 8.7 support.
|
||||
|
||||
## Supported OSes
|
||||
|
||||
Linux, Windows
|
||||
|
||||
@@ -4,10 +4,12 @@ cmake_minimum_required(VERSION 3.20)
|
||||
# toolkit ships only for the native Tegra archs; drop the desktop archs there.
|
||||
get_filename_component(_ctk_root "${CMAKE_CUDA_COMPILER}" DIRECTORY)
|
||||
get_filename_component(_ctk_root "${_ctk_root}" DIRECTORY)
|
||||
if(EXISTS "${_ctk_root}/targets/aarch64-linux" OR EXISTS "${_ctk_root}/targets/aarch64-qnx")
|
||||
file(GLOB _tegra_targets "${_ctk_root}/targets/aarch64-linux*" "${_ctk_root}/targets/aarch64-qnx*")
|
||||
if(_tegra_targets)
|
||||
set(SAMPLE_DISALLOW_ARCHS 75 80 86 89 90 100 103 107 120)
|
||||
endif()
|
||||
unset(_ctk_root)
|
||||
unset(_tegra_targets)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/DetectCudaArch.cmake")
|
||||
|
||||
|
||||
@@ -4,10 +4,12 @@ cmake_minimum_required(VERSION 3.20)
|
||||
# toolkit ships only for the native Tegra archs; drop the desktop archs there.
|
||||
get_filename_component(_ctk_root "${CMAKE_CUDA_COMPILER}" DIRECTORY)
|
||||
get_filename_component(_ctk_root "${_ctk_root}" DIRECTORY)
|
||||
if(EXISTS "${_ctk_root}/targets/aarch64-linux" OR EXISTS "${_ctk_root}/targets/aarch64-qnx")
|
||||
file(GLOB _tegra_targets "${_ctk_root}/targets/aarch64-linux*" "${_ctk_root}/targets/aarch64-qnx*")
|
||||
if(_tegra_targets)
|
||||
set(SAMPLE_DISALLOW_ARCHS 75 80 86 89 90 100 103 107 120)
|
||||
endif()
|
||||
unset(_ctk_root)
|
||||
unset(_tegra_targets)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/DetectCudaArch.cmake")
|
||||
|
||||
|
||||
42
cpp/3_CUDA_Features/cftUnicastPut/CMakeLists.txt
Normal file
42
cpp/3_CUDA_Features/cftUnicastPut/CMakeLists.txt
Normal file
@@ -0,0 +1,42 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
# The fabric.try_put instructions and the mbarrier layout::v1 form used by this
|
||||
# sample require __CUDA_ARCH__ >= 1000 (Blackwell and later).
|
||||
set(SAMPLE_DISALLOW_ARCHS 75 80 86 87 89 90)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/DetectCudaArch.cmake")
|
||||
|
||||
if(SAMPLE_SKIP_BUILD)
|
||||
return()
|
||||
endif()
|
||||
|
||||
project(cftUnicastPut LANGUAGES C CXX CUDA)
|
||||
|
||||
include("${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/CudaSampleCommon.cmake")
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
find_package(MPI)
|
||||
|
||||
if(${MPI_FOUND})
|
||||
add_executable(cftUnicastPut cft_unicast_put.cu)
|
||||
|
||||
target_compile_options(cftUnicastPut PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(cftUnicastPut PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
target_include_directories(cftUnicastPut PUBLIC
|
||||
${MPI_INCLUDE_PATH}
|
||||
)
|
||||
|
||||
target_link_libraries(cftUnicastPut PUBLIC
|
||||
${MPI_C_LIBRARIES}
|
||||
${MPI_CXX_LIBRARIES}
|
||||
CUDA::cuda_driver
|
||||
)
|
||||
|
||||
include(InstallSamples)
|
||||
setup_samples_install()
|
||||
else()
|
||||
message(STATUS "MPI not found - will not build sample 'cftUnicastPut'")
|
||||
endif()
|
||||
115
cpp/3_CUDA_Features/cftUnicastPut/README.md
Normal file
115
cpp/3_CUDA_Features/cftUnicastPut/README.md
Normal file
@@ -0,0 +1,115 @@
|
||||
# cftUnicastPut - Unicast Point-to-Point Puts with Compute Fabric Transport
|
||||
|
||||
## Description
|
||||
|
||||
This sample moves data directly between GPUs with **Compute Fabric Transport (CFT)** unicast *logical endpoints*. A logical endpoint (LE) is a fabric-addressable name for a range of device memory: a rank creates an LE, binds VMM fabric memory to it, shares it with peers through a fabric IPC handle, and peers then write into it from device code with `fabric.try_put`, addressing the destination by `(endpoint id, offset)` rather than by a mapped peer pointer.
|
||||
|
||||
The ranks form a ring: rank `r` puts its payload into rank `(r + 1) % N` and expects the payload of rank `(r - 1 + N) % N` to land in its own buffer.
|
||||
|
||||
Two receive-completion schemes are shown, selected on the command line:
|
||||
|
||||
- **Default (plain puts)** — `cftTryPutKernel` only moves payload. A second, one-thread `cftSignalFlagKernel` then puts a 16-byte flag into the neighbor's sync region and spins on its own flag. Because the two kernels are ordered on the same stream, the flag cannot be observed before the payload puts of that rank have completed.
|
||||
- **`--counted` (counted puts)** — `cftTryPutCountedKernel` issues `fabric.try_put_counted`, which additionally increments a byte counter in the destination endpoint. The receiver simply waits until its own counter reaches the number of bytes it expects, so no separate signal kernel is needed.
|
||||
|
||||
After the transfer each rank copies its buffer back to the host and checks the rank-tagged pattern its receive neighbor sent, and rank 0 prints the combined result.
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
How to:
|
||||
- discover the unicast logical-endpoint clique a GPU belongs to and confirm all ranks agree on it
|
||||
- reserve logical-endpoint ids, create an endpoint, and share it across processes with a fabric IPC handle
|
||||
- bind VMM fabric memory to an endpoint and tear the endpoint down safely
|
||||
- issue `fabric.try_put` / `fabric.try_put_counted` from device code and complete them with an `mbarrier`
|
||||
- decode fabric operation errors from the `mbarrier.try_wait` status operand
|
||||
- pick a receive-completion scheme: an explicit inter-GPU signal flag put, or a counted put with a byte-arrival counter
|
||||
|
||||
## Key Concepts
|
||||
|
||||
- **Logical endpoint (LE)** — a fabric-addressable handle for a bound range of device memory; remote writes target `(endpoint id, offset)` instead of a mapped peer address
|
||||
- **Clique** — the set of devices that can address each other's logical endpoints; every participating rank must find the same `CU_CLIQUE_TYPE_UNICAST_LOGICAL_ENDPOINT` clique id
|
||||
- **Fabric put** — an asynchronous device-side write to a peer endpoint issued by a single thread, completed through an `mbarrier` with `complete_tx::16B` accounting
|
||||
- **Counted put** — a fabric put that also accumulates delivered bytes into a 256-byte-aligned counter in the destination endpoint, giving the receiver a completion signal without an extra message
|
||||
- **Fabric error reporting** — when the `mbarrier.try_wait` report predicate is set, `cudaFabricOpErrorStatus*` decodes failures from the `try_wait` status operand.
|
||||
|
||||
## Key APIs
|
||||
|
||||
### CUDA Driver API
|
||||
|
||||
- `cuDeviceGetCliqueCount` / `cuDeviceGetCliqueInfo` — find the unicast logical-endpoint clique of a device
|
||||
- `cuLogicalEndpointIdReserve` / `cuLogicalEndpointIdRelease` — reserve a contiguous range of local endpoint ids
|
||||
- `cuLogicalEndpointCreate` / `cuLogicalEndpointDestroy` — create and destroy an endpoint
|
||||
- `cuLogicalEndpointGetLimits` — query the bind alignment and the maximum endpoint size
|
||||
- `cuLogicalEndpointExport` / `cuLogicalEndpointImport` — share an endpoint through a fabric IPC handle
|
||||
- `cuLogicalEndpointQuery` — wait until a created or imported endpoint is ready to use
|
||||
- `cuLogicalEndpointBindMem` / `cuLogicalEndpointUnbind` — attach and detach the backing allocation
|
||||
- `cuMemCreate`, `cuMemAddressReserve`, `cuMemMap`, `cuMemSetAccess` — allocate the fabric-shareable backing memory
|
||||
|
||||
### Device-side PTX wrappers (`cuda::ptx`)
|
||||
|
||||
- `fabric_try_put` / `fabric_try_put_counted` — issue the remote write, optionally with byte counting
|
||||
- `fabric_submit` — submit the batch of issued fabric operations
|
||||
- `mbarrier_init` (`layout_v1`), `mbarrier_arrive_expect_tx`, `mbarrier_try_wait_parity` — track completion of the puts
|
||||
|
||||
### CUDA Runtime API
|
||||
|
||||
- `cudaFabricOpErrorStatusCount` / `cudaFabricOpErrorStatusGet` — decode fabric errors from the `mbarrier.try_wait` status field (`cudaFabricOpStatusSourceMbarrierV1`)
|
||||
|
||||
## Requirements
|
||||
|
||||
### Hardware
|
||||
|
||||
- Two or more NVIDIA GPUs of compute capability 10.0 or newer.
|
||||
- Two or more NVLink-capable datacenter class NVIDIA GPUs of compute capability 10.0 or newer (`sm_100` / `sm_103` / `sm_107`).
|
||||
- Each rank needs its own device, so a node must expose at least as many GPUs as it runs ranks.
|
||||
|
||||
### Software
|
||||
|
||||
- CUDA Toolkit 13.4 or newer and a driver with `cuLogicalEndpoint*` support
|
||||
- The NVIDIA IMEX daemon with IMEX channels configured, as required for any `CU_MEM_HANDLE_TYPE_FABRIC` sharing
|
||||
- [MPI](../../../README.md#message-passing-interface)
|
||||
- Linux on x86_64 or aarch64
|
||||
|
||||
## How to Build
|
||||
|
||||
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.
|
||||
|
||||
## How to Run
|
||||
|
||||
The sample must be launched under MPI with at least two ranks:
|
||||
|
||||
```bash
|
||||
mpirun -np 2 ./cftUnicastPut [chunkCnt] [--counted]
|
||||
```
|
||||
|
||||
`[chunkCnt]` is optional (default: 1000) and is the number of 8192-byte chunks each rank sends; it also becomes the grid size (one block per chunk). `--counted` switches from plain puts plus a flag handshake to counted puts plus a byte-arrival counter.
|
||||
|
||||
```bash
|
||||
mpirun -np 2 ./cftUnicastPut # defaults: 1000 chunks, plain puts
|
||||
mpirun -np 2 ./cftUnicastPut --counted # defaults: 1000 chunks, counted puts
|
||||
mpirun -np 2 ./cftUnicastPut 1000 # plain puts + inter-GPU flag
|
||||
mpirun -np 2 ./cftUnicastPut 1000 --counted # counted puts + byte counter
|
||||
```
|
||||
|
||||
Debug builds (`-DENABLE_CUDA_DEBUG=ON`, or any build without `NDEBUG`) cap every device-side spin-wait at 30 seconds and print which wait timed out, so a stalled fabric operation reports itself instead of hanging.
|
||||
|
||||
## Expected Output
|
||||
|
||||
```text
|
||||
mode = put, chunkCnt = 1000, totalBytes = 8192000, ranks = 2
|
||||
Result = PASS
|
||||
```
|
||||
|
||||
If any rank's received payload does not match what its neighbor sent, that rank prints the first mismatching element to `stderr`, rank 0 prints `Result = FAIL`, and the sample exits with a non-zero status.
|
||||
|
||||
## Files
|
||||
|
||||
- `cft_unicast_put.cu` — the complete sample: endpoint setup and teardown, both put kernels, the flag-signal kernel, and host-side verification
|
||||
- `README.md` — this file
|
||||
- `CMakeLists.txt` — build configuration
|
||||
|
||||
## See Also
|
||||
|
||||
- [CUDA Compute Fabric Transport](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/compute-fabric-transport.html)
|
||||
- [CUDA Virtual Memory Management](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/virtual-memory-management.html)
|
||||
- [CUDA Driver API](https://docs.nvidia.com/cuda/cuda-driver-api/)
|
||||
- [`cuda::ptx` PTX API](https://nvidia.github.io/cccl/unstable/libcudacxx/ptx_api.html)
|
||||
775
cpp/3_CUDA_Features/cftUnicastPut/cft_unicast_put.cu
Normal file
775
cpp/3_CUDA_Features/cftUnicastPut/cft_unicast_put.cu
Normal file
@@ -0,0 +1,775 @@
|
||||
/* 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
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Unicast point-to-point data movement with Compute Fabric Transport (CFT)
|
||||
* logical endpoints.
|
||||
*
|
||||
* MPI example on a ring: rank r sends to (r+1)%N and receives from (r-1+N)%N.
|
||||
* Default mode uses plain fabric puts; a separate one-thread kernel then flags the
|
||||
* send neighbor over the same endpoint and waits on the receive neighbor's flag.
|
||||
* Pass --counted for counted fabric puts and a counter which counts bytes arrival
|
||||
* for receive completion.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <cuda.h>
|
||||
#include <cuda/atomic>
|
||||
#include <cuda/ptx>
|
||||
#include <cuda_awbarrier_helpers.h>
|
||||
#include <cuda_runtime.h>
|
||||
#include <memory>
|
||||
#include <mpi.h>
|
||||
#include <numeric>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
|
||||
#ifndef EXIT_WAIVED
|
||||
#define EXIT_WAIVED 2
|
||||
#endif
|
||||
|
||||
#define MPI_CHECK(cmd) \
|
||||
do { \
|
||||
int e = cmd; \
|
||||
if (e != MPI_SUCCESS) { \
|
||||
printf("Failed: MPI error %s:%d '%d'\n", __FILE__, __LINE__, e); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static void checkCuda(CUresult result, const char *func, const char *file, int line)
|
||||
{
|
||||
if (result != CUDA_SUCCESS) {
|
||||
const char *name = nullptr;
|
||||
cuGetErrorName(result, &name);
|
||||
fprintf(stderr,
|
||||
"CUDA driver error at %s:%d code=%d(%s) \"%s\"\n",
|
||||
file,
|
||||
line,
|
||||
static_cast<int>(result),
|
||||
name ? name : "<unknown>",
|
||||
func);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
static void checkCuda(cudaError_t result, const char *func, const char *file, int line)
|
||||
{
|
||||
if (result != cudaSuccess) {
|
||||
fprintf(stderr,
|
||||
"CUDA runtime error at %s:%d code=%d(%s) \"%s\"\n",
|
||||
file,
|
||||
line,
|
||||
static_cast<int>(result),
|
||||
cudaGetErrorName(result),
|
||||
func);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
#define checkCudaErrors(val) checkCuda((val), #val, __FILE__, __LINE__)
|
||||
|
||||
// Round value up to the next multiple of alignment (alignment need not be a power of two).
|
||||
constexpr size_t alignUp(size_t value, size_t alignment)
|
||||
{
|
||||
return (value + alignment - 1) / alignment * alignment;
|
||||
}
|
||||
|
||||
void waitForEndpointReady(CUlogicalEndpointId leId)
|
||||
{
|
||||
while (true) {
|
||||
int ready = 0;
|
||||
checkCudaErrors(cuLogicalEndpointQuery(leId, 1, &ready));
|
||||
if (ready > 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr uint32_t FABRIC_CHUNK_SIZE = 8192;
|
||||
// A b128 fabric put moves 16 bytes at a time, and mbarrier complete_tx::16B
|
||||
// counts transferred bytes in 16-byte units. So an expected-tx count is always
|
||||
// the byte count divided by this granularity.
|
||||
static constexpr uint32_t FABRIC_TX_GRANULARITY_BYTES = 16;
|
||||
// Sync region placed after the payload on a 256-byte boundary, reused per mode:
|
||||
// default : a 16-byte inter-GPU flag that the receive neighbor sets with a
|
||||
// fabric put (b128 puts move 16 bytes at a time).
|
||||
// --counted : an 8-byte delivered-byte counter.
|
||||
static constexpr size_t INTER_GPU_FLAG_BYTES = 16;
|
||||
static constexpr size_t SYNC_REGION_BYTES = 16; // max of counter (8B) and flag (16B)
|
||||
// The counter targeted by counted fabric puts must be 256-byte aligned, so the
|
||||
// whole sync region is placed on a 256-byte boundary.
|
||||
static constexpr size_t SYNC_REGION_ALIGN_BYTES = 256;
|
||||
static constexpr int WARP_SIZE = 32;
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Debug builds: cap spin-waits at 30 s so a stuck fabric op prints instead of hanging.
|
||||
static constexpr uint64_t CFT_WAIT_TIMEOUT_NS = 30ULL * 1000 * 1000 * 1000;
|
||||
|
||||
inline __device__ uint64_t readGlobalTimerNs()
|
||||
{
|
||||
uint64_t t = 0;
|
||||
asm volatile("mov.u64 %0, %%globaltimer;" : "=l"(t));
|
||||
return t;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Spin while pred() is true. Returns true when pred() becomes false; in debug
|
||||
// builds returns false after CFT_WAIT_TIMEOUT_NS and prints `what` to identify
|
||||
// the stall. Release builds spin forever. Caller must handle false (e.g. __trap()).
|
||||
template <typename Predicate>
|
||||
inline __device__ bool waitWithDebugTimeout(Predicate &&pred, const char *what)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
uint64_t start = readGlobalTimerNs();
|
||||
while (pred()) {
|
||||
uint64_t now = readGlobalTimerNs();
|
||||
if (now - start > CFT_WAIT_TIMEOUT_NS) {
|
||||
printf("%s timeout after %llu ns (block=%u)\n", what, (unsigned long long)(now - start), blockIdx.x);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void)what;
|
||||
while (pred()) {
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
// Decode and print the fabric errors captured in the mbarrier's report value.
|
||||
// Decode it with cudaFabricOpStatusSourceMbarrierV1: first query the error count, then
|
||||
// retrieve each decoded error. Call this only when the report predicate is set.
|
||||
inline __device__ void reportFabricError(uint8_t *reportValue, uint64_t offset)
|
||||
{
|
||||
const unsigned long long off = offset;
|
||||
unsigned int errCount = 0;
|
||||
if (cudaFabricOpErrorStatusCount(reportValue, cudaFabricOpStatusSourceMbarrierV1, &errCount) != cudaSuccess) {
|
||||
printf("cudaFabricOpErrorStatusCount failed at offset %llu\n", off);
|
||||
return;
|
||||
}
|
||||
for (unsigned int i = 0; i < errCount; ++i) {
|
||||
cudaFabricOpStatusInfo info = cudaFabricOpStatusInfoSuccess;
|
||||
if (cudaFabricOpErrorStatusGet(reportValue, cudaFabricOpStatusSourceMbarrierV1, i, &info) == cudaSuccess) {
|
||||
printf("fabric put error[%u/%u] at offset %llu: status=%d\n", i, errCount, off, (int)info);
|
||||
}
|
||||
else {
|
||||
printf("cudaFabricOpErrorStatusGet failed for error[%u/%u] at offset %llu\n", i, errCount, off);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Each block sends one FABRIC_CHUNK_SIZE slice to the peer's logical endpoint (putLeId).
|
||||
// blockIdx.x selects the chunk: offset = blockIdx.x * FABRIC_CHUNK_SIZE. Launch
|
||||
// gridDim.x = totalBytes / FABRIC_CHUNK_SIZE so every chunk is covered by one block.
|
||||
//
|
||||
// expectedBytes is how many bytes this rank expects to receive (tracked in our
|
||||
// local counter by the peer's puts).
|
||||
__global__ void cftTryPutCountedKernel(uint32_t putLeId,
|
||||
uint64_t counterOffset,
|
||||
int myRank,
|
||||
uint64_t *counterPtr,
|
||||
uint64_t expectedBytes)
|
||||
{
|
||||
constexpr int elems = FABRIC_CHUNK_SIZE / sizeof(int);
|
||||
__shared__ alignas(16) int smemBuf[elems];
|
||||
__shared__ __mbarrier_t putBar;
|
||||
|
||||
if (threadIdx.x == 0) {
|
||||
cuda::ptx::mbarrier_init(cuda::ptx::layout_v1, &putBar, 1);
|
||||
}
|
||||
|
||||
// Fill shared memory with a rank-tagged pattern before streaming it to the peer.
|
||||
for (int i = threadIdx.x; i < elems; i += blockDim.x) {
|
||||
smemBuf[i] = myRank * elems + i;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// Thread 0 issues one counted fabric put for this block's chunk.
|
||||
if (threadIdx.x == 0) {
|
||||
const uint64_t offset = blockIdx.x * FABRIC_CHUNK_SIZE;
|
||||
// Make the staged shared-memory source visible to the async proxy before the put reads it.
|
||||
cuda::ptx::fence_proxy_async(cuda::ptx::space_shared);
|
||||
cuda::ptx::fabric_try_put_counted(cuda::ptx::space_shared,
|
||||
cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_sys,
|
||||
putLeId,
|
||||
offset,
|
||||
counterOffset,
|
||||
smemBuf,
|
||||
FABRIC_CHUNK_SIZE,
|
||||
&putBar);
|
||||
cuda::ptx::fabric_submit();
|
||||
cuda::ptx::mbarrier_arrive_expect_tx(cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_cta,
|
||||
cuda::ptx::space_shared,
|
||||
&putBar,
|
||||
(FABRIC_CHUNK_SIZE / FABRIC_TX_GRANULARITY_BYTES));
|
||||
// Spin while the barrier's phase hasn't flipped yet. The report outputs
|
||||
// are only valid once the barrier completes, so inspect them afterward.
|
||||
bool isReportSeen = false;
|
||||
uint8_t reportValue = 0;
|
||||
bool complete = waitWithDebugTimeout(
|
||||
[&]() {
|
||||
return !cuda::ptx::mbarrier_try_wait_parity(cuda::ptx::mbarrier_phase_primary,
|
||||
cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_cta,
|
||||
isReportSeen,
|
||||
reportValue,
|
||||
&putBar,
|
||||
/*phaseParity=*/0);
|
||||
},
|
||||
"cftTryPutCountedKernel: mbarrier try_wait parity");
|
||||
if (!complete) {
|
||||
__trap();
|
||||
}
|
||||
if (isReportSeen) {
|
||||
reportFabricError(&reportValue, offset);
|
||||
}
|
||||
}
|
||||
// Block 0 waits until our receive byte counter reaches expectedBytes. System
|
||||
// scope is required because the peer's counted fabric puts update this
|
||||
// counter in our fabric-bound memory, not via threads in this CTA.
|
||||
// A relaxed load suffices: the fabric-proxy is a strong proxy in the PTX
|
||||
// memory model, so fabric-delivered writes are eventually observable
|
||||
if (blockIdx.x == 0 && threadIdx.x == 0) {
|
||||
cuda::atomic_ref<uint64_t, cuda::thread_scope_system> ref(*counterPtr);
|
||||
if (!waitWithDebugTimeout([&]() { return ref.load(cuda::memory_order_relaxed) != expectedBytes; },
|
||||
"cftTryPutCountedKernel: receive byte counter")) {
|
||||
__trap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Non-counted variant: each block sends one FABRIC_CHUNK_SIZE chunk via plain
|
||||
// fabric.try_put (no byte counter). This kernel only moves payload; the inter-GPU
|
||||
// completion handshake happens in cftSignalFlagKernel, launched next on the same
|
||||
// stream. The kernel boundary is the cross-block sync point -- once this kernel
|
||||
// returns, every block's put has been issued and its source SMEM consumed.
|
||||
__global__ void cftTryPutKernel(uint32_t putLeId, int myRank)
|
||||
{
|
||||
constexpr int elems = FABRIC_CHUNK_SIZE / sizeof(int);
|
||||
__shared__ alignas(16) int smemBuf[elems];
|
||||
__shared__ __mbarrier_t putBar;
|
||||
|
||||
if (threadIdx.x == 0) {
|
||||
cuda::ptx::mbarrier_init(cuda::ptx::layout_v1, &putBar, 1);
|
||||
}
|
||||
|
||||
for (int i = threadIdx.x; i < elems; i += blockDim.x) {
|
||||
smemBuf[i] = myRank * elems + i;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
if (threadIdx.x == 0) {
|
||||
const uint64_t offset = blockIdx.x * FABRIC_CHUNK_SIZE;
|
||||
// Make the staged shared-memory source visible to the async proxy before the put reads it.
|
||||
cuda::ptx::fence_proxy_async(cuda::ptx::space_shared);
|
||||
cuda::ptx::fabric_try_put(cuda::ptx::space_shared,
|
||||
cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_sys,
|
||||
putLeId,
|
||||
offset,
|
||||
smemBuf,
|
||||
FABRIC_CHUNK_SIZE,
|
||||
&putBar);
|
||||
cuda::ptx::fabric_submit();
|
||||
cuda::ptx::mbarrier_arrive_expect_tx(cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_cta,
|
||||
cuda::ptx::space_shared,
|
||||
&putBar,
|
||||
(FABRIC_CHUNK_SIZE / FABRIC_TX_GRANULARITY_BYTES));
|
||||
// Spin while the barrier's phase hasn't flipped yet. The report outputs
|
||||
// are only valid once the barrier completes, so inspect them afterward.
|
||||
bool isReportSeen = false;
|
||||
uint8_t reportValue = 0;
|
||||
bool complete = waitWithDebugTimeout(
|
||||
[&]() {
|
||||
return !cuda::ptx::mbarrier_try_wait_parity(cuda::ptx::mbarrier_phase_primary,
|
||||
cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_cta,
|
||||
isReportSeen,
|
||||
reportValue,
|
||||
&putBar,
|
||||
/*phaseParity=*/0);
|
||||
},
|
||||
"cftTryPutKernel: mbarrier try_wait parity");
|
||||
if (!complete) {
|
||||
__trap();
|
||||
}
|
||||
if (isReportSeen) {
|
||||
reportFabricError(&reportValue, offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Inter-GPU completion signal for the default (non-counted) path, launched with one
|
||||
// after cftTryPutKernel. Because it is ordered on the same stream, the payload
|
||||
// kernel has already finished, so we issue a single flag put to signal completion.
|
||||
//
|
||||
// Note: this kernel could be overlapped with the tail of cftTryPutKernel using
|
||||
// Programmatic Dependent Launch (PDL) to hide its launch latency. The prologue
|
||||
// (barrier init, staging smemFlag) may run early, but the flag put must still be
|
||||
// gated on payload completion via cudaGridDependencySynchronize(), so we keep the
|
||||
// kernels plainly stream-ordered here for clarity.
|
||||
__global__ void cftSignalFlagKernel(uint32_t putLeId, uint64_t interGpuFlagOffset, uint32_t *interGpuFlag)
|
||||
{
|
||||
__shared__ alignas(16) uint32_t smemFlag[INTER_GPU_FLAG_BYTES / sizeof(uint32_t)];
|
||||
__shared__ __mbarrier_t putBar;
|
||||
|
||||
// This kernel is a single-thread, single-block signal: it is launched <<<1, 1>>>
|
||||
// and its one thread issues the flag put and waits on the neighbor's flag.
|
||||
assert(blockDim.x == 1 && gridDim.x == 1);
|
||||
|
||||
cuda::ptx::mbarrier_init(cuda::ptx::layout_v1, &putBar, 1);
|
||||
// A b128 fabric put moves 16 bytes. Initialize the full source even though
|
||||
// the receiver treats a non-zero first word as the signal.
|
||||
for (size_t i = 0; i < INTER_GPU_FLAG_BYTES / sizeof(uint32_t); ++i) {
|
||||
smemFlag[i] = 1;
|
||||
}
|
||||
cuda::ptx::fence_proxy_async(cuda::ptx::space_shared);
|
||||
cuda::ptx::fabric_try_put(cuda::ptx::space_shared,
|
||||
cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_sys,
|
||||
putLeId,
|
||||
interGpuFlagOffset,
|
||||
smemFlag,
|
||||
INTER_GPU_FLAG_BYTES,
|
||||
&putBar);
|
||||
cuda::ptx::fabric_submit();
|
||||
cuda::ptx::mbarrier_arrive_expect_tx(cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_cta,
|
||||
cuda::ptx::space_shared,
|
||||
&putBar,
|
||||
(INTER_GPU_FLAG_BYTES / FABRIC_TX_GRANULARITY_BYTES));
|
||||
// Spin while the barrier's phase hasn't flipped yet. The report outputs are
|
||||
// only valid once the barrier completes, so inspect them afterward.
|
||||
bool isReportSeen = false;
|
||||
uint8_t reportValue = 0;
|
||||
bool complete = waitWithDebugTimeout(
|
||||
[&]() {
|
||||
return !cuda::ptx::mbarrier_try_wait_parity(cuda::ptx::mbarrier_phase_primary,
|
||||
cuda::ptx::sem_relaxed,
|
||||
cuda::ptx::scope_cta,
|
||||
isReportSeen,
|
||||
reportValue,
|
||||
&putBar,
|
||||
/*phaseParity=*/0);
|
||||
},
|
||||
"cftSignalFlagKernel: mbarrier try_wait parity");
|
||||
if (!complete) {
|
||||
__trap();
|
||||
}
|
||||
if (isReportSeen) {
|
||||
reportFabricError(&reportValue, interGpuFlagOffset);
|
||||
}
|
||||
|
||||
// Wait for the receive neighbor to set our flag via a fabric put.
|
||||
cuda::atomic_ref<uint32_t, cuda::thread_scope_system> ref(*interGpuFlag);
|
||||
if (!waitWithDebugTimeout([&]() { return ref.load(cuda::memory_order_relaxed) == 0; },
|
||||
"cftSignalFlagKernel: inter-GPU flag")) {
|
||||
__trap();
|
||||
}
|
||||
}
|
||||
|
||||
// Confirm the device exposes the capabilities this example relies on: fabric
|
||||
// handles are always required; counted logical-endpoint ops only in --counted mode.
|
||||
// Returns false when the sample is not supported on this device (caller should
|
||||
// exit with EXIT_WAIVED, not EXIT_FAILURE).
|
||||
static bool requireDeviceCapabilities(CUdevice dev, bool needCounted)
|
||||
{
|
||||
int vmmSupported = 0;
|
||||
checkCudaErrors(cuDeviceGetAttribute(&vmmSupported, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, dev));
|
||||
if (!vmmSupported) {
|
||||
fprintf(stderr, "device does not support virtual memory management\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
int unicastSupported = 0;
|
||||
checkCudaErrors(cuDeviceGetAttribute(&unicastSupported, CU_DEVICE_ATTRIBUTE_LOGICAL_ENDPOINT_UNICAST_SUPPORTED, dev));
|
||||
if (!unicastSupported) {
|
||||
fprintf(stderr, "device does not support unicast logical endpoints\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
int fabricSupported = 0;
|
||||
checkCudaErrors(cuDeviceGetAttribute(&fabricSupported, CU_DEVICE_ATTRIBUTE_HANDLE_TYPE_FABRIC_SUPPORTED, dev));
|
||||
if (!fabricSupported) {
|
||||
fprintf(stderr, "device does not support fabric handles\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Bitmask of CUlogicalEndpointIpcHandleType.
|
||||
int leHandleTypes = 0;
|
||||
checkCudaErrors(cuDeviceGetAttribute(&leHandleTypes, CU_DEVICE_ATTRIBUTE_LOGICAL_ENDPOINT_SUPPORTED_HANDLE_TYPES, dev));
|
||||
if ((leHandleTypes & CU_LOGICAL_ENDPOINT_IPC_HANDLE_TYPE_FABRIC) == 0) {
|
||||
fprintf(stderr, "device does not support fabric IPC for logical endpoints\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (needCounted) {
|
||||
int countedSupported = 0;
|
||||
checkCudaErrors(
|
||||
cuDeviceGetAttribute(&countedSupported, CU_DEVICE_ATTRIBUTE_LOGICAL_ENDPOINT_COUNTED_OPS_SUPPORTED, dev));
|
||||
if (!countedSupported) {
|
||||
fprintf(stderr, "device does not support counted logical-endpoint ops\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static constexpr int DEFAULT_CHUNK_CNT = 100;
|
||||
|
||||
static void printUsage(const char *prog)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Usage: mpirun -np <N> %s [chunkCnt] [--counted]\n"
|
||||
" <N> number of MPI ranks (must be >= 2)\n"
|
||||
" [chunkCnt] number of %u-byte chunks each rank sends (default: %d)\n"
|
||||
" --counted use counted fabric puts with a byte-arrival counter\n"
|
||||
"Example: mpirun -np 2 %s\n",
|
||||
prog,
|
||||
FABRIC_CHUNK_SIZE,
|
||||
DEFAULT_CHUNK_CNT,
|
||||
prog);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int chunkCnt = DEFAULT_CHUNK_CNT;
|
||||
bool useCounted = false;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (strcmp(argv[i], "--counted") == 0) {
|
||||
useCounted = true;
|
||||
}
|
||||
else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
|
||||
printUsage(argv[0]);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
else {
|
||||
char *end = nullptr;
|
||||
const long parsed = strtol(argv[i], &end, 10);
|
||||
if (end == argv[i] || *end != '\0' || parsed <= 0 || parsed > INT_MAX) {
|
||||
fprintf(stderr, "error: invalid argument '%s'\n", argv[i]);
|
||||
printUsage(argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
chunkCnt = static_cast<int>(parsed);
|
||||
}
|
||||
}
|
||||
const size_t totalBytes = static_cast<size_t>(FABRIC_CHUNK_SIZE) * chunkCnt;
|
||||
|
||||
// ---- MPI setup: ring topology, rank i sends to (i+1)%N. ----
|
||||
int myRank = 0;
|
||||
int numRanks = 0;
|
||||
MPI_CHECK(MPI_Init(&argc, &argv));
|
||||
MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &myRank));
|
||||
MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &numRanks));
|
||||
if (numRanks < 2) {
|
||||
if (myRank == 0) {
|
||||
fprintf(stderr, "error: this example needs at least 2 MPI ranks (got %d)\n", numRanks);
|
||||
}
|
||||
MPI_CHECK(MPI_Finalize());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (myRank == 0) {
|
||||
printf("mode = %s, chunkCnt = %d, totalBytes = %zu, ranks = %d\n",
|
||||
useCounted ? "counted" : "put",
|
||||
chunkCnt,
|
||||
totalBytes,
|
||||
numRanks);
|
||||
}
|
||||
// Ring neighbors: put to sendRank; receive completion from recvRank.
|
||||
const int sendRank = (myRank + 1) % numRanks;
|
||||
const int recvRank = (myRank + numRanks - 1) % numRanks;
|
||||
|
||||
// ---- Device context and stream. ----
|
||||
// Device ordinals are node-local, so select a device using this process's
|
||||
// rank among the MPI processes on the same node.
|
||||
MPI_Comm localComm = MPI_COMM_NULL;
|
||||
MPI_CHECK(MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, myRank, MPI_INFO_NULL, &localComm));
|
||||
int localRank = 0;
|
||||
MPI_CHECK(MPI_Comm_rank(localComm, &localRank));
|
||||
|
||||
int deviceCount = 0;
|
||||
checkCudaErrors(cudaGetDeviceCount(&deviceCount));
|
||||
if (localRank >= deviceCount) {
|
||||
fprintf(stderr,
|
||||
"error: local MPI rank %d cannot be assigned a unique device; "
|
||||
"only %d device(s) are visible on this node\n",
|
||||
localRank,
|
||||
deviceCount);
|
||||
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
const int deviceOrdinal = localRank;
|
||||
|
||||
checkCudaErrors(cudaSetDevice(deviceOrdinal));
|
||||
CUdevice cuDevice;
|
||||
checkCudaErrors(cuInit(0));
|
||||
checkCudaErrors(cuDeviceGet(&cuDevice, deviceOrdinal));
|
||||
int localSupported = requireDeviceCapabilities(cuDevice, useCounted) ? 1 : 0;
|
||||
int allSupported = 0;
|
||||
MPI_CHECK(MPI_Allreduce(&localSupported, &allSupported, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD));
|
||||
if (!allSupported) {
|
||||
MPI_CHECK(MPI_Comm_free(&localComm));
|
||||
MPI_CHECK(MPI_Finalize());
|
||||
return EXIT_WAIVED;
|
||||
}
|
||||
CUstream stream;
|
||||
checkCudaErrors(cuStreamCreate(&stream, CU_STREAM_NON_BLOCKING));
|
||||
|
||||
// Query how many cliques this device is a member of:
|
||||
size_t cliqueCount = 0; // not modified if there are errors
|
||||
checkCudaErrors(cuDeviceGetCliqueCount(&cliqueCount, cuDevice));
|
||||
if (cliqueCount == 0) {
|
||||
fprintf(stderr, "This GPU is not a member of any clique.");
|
||||
}
|
||||
|
||||
// Query the clique info for all cliques
|
||||
std::vector<CUcliqueInfo> cliqueInfos(cliqueCount);
|
||||
if (cliqueCount > 0) {
|
||||
checkCudaErrors(cuDeviceGetCliqueInfo(cliqueInfos.data(), &cliqueCount, cuDevice));
|
||||
}
|
||||
unsigned int localCliqueId = 0;
|
||||
bool found = false;
|
||||
for (size_t idx = 0; idx < cliqueCount; ++idx) {
|
||||
if (cliqueInfos[idx].type == CU_CLIQUE_TYPE_UNICAST_LOGICAL_ENDPOINT) {
|
||||
localCliqueId = cliqueInfos[idx].id;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (cliqueCount != 0 && !found) {
|
||||
fprintf(stderr, "This GPU is not a member of a clique with unicast logical endpoint support.");
|
||||
}
|
||||
int localCliqueOk = found ? 1 : 0;
|
||||
int allCliqueOk = 0;
|
||||
MPI_CHECK(MPI_Allreduce(&localCliqueOk, &allCliqueOk, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD));
|
||||
if (!allCliqueOk) {
|
||||
checkCudaErrors(cuStreamDestroy(stream));
|
||||
MPI_CHECK(MPI_Comm_free(&localComm));
|
||||
MPI_CHECK(MPI_Finalize());
|
||||
return EXIT_WAIVED;
|
||||
}
|
||||
|
||||
// Verify that all MPI ranks found the same clique id:
|
||||
std::vector<unsigned int> allCliqueIds(numRanks);
|
||||
MPI_CHECK(MPI_Allgather(&localCliqueId,
|
||||
sizeof(unsigned int),
|
||||
MPI_BYTE,
|
||||
allCliqueIds.data(),
|
||||
sizeof(unsigned int),
|
||||
MPI_BYTE,
|
||||
MPI_COMM_WORLD));
|
||||
for (int i = 0; i < numRanks; i++) {
|
||||
if (allCliqueIds[i] != localCliqueId) {
|
||||
fprintf(stderr,
|
||||
"Rank %d: Clique id %u does not match expected %u\n",
|
||||
myRank,
|
||||
allCliqueIds[i],
|
||||
localCliqueId);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
// Reserve enough local ids for this rank's endpoint and all imported peers.
|
||||
CUlogicalEndpointId leId = 0;
|
||||
checkCudaErrors(cuLogicalEndpointIdReserve(&leId, (uint32_t)numRanks));
|
||||
|
||||
// ---- Describe the fabric-shareable allocation and query its granularity. ----
|
||||
CUmemAllocationProp exportAllocProp = {};
|
||||
exportAllocProp.type = CU_MEM_ALLOCATION_TYPE_PINNED;
|
||||
exportAllocProp.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
|
||||
exportAllocProp.location.id = cuDevice;
|
||||
exportAllocProp.requestedHandleTypes = CU_MEM_HANDLE_TYPE_FABRIC;
|
||||
size_t granularity = 0;
|
||||
checkCudaErrors(cuMemGetAllocationGranularity(&granularity, &exportAllocProp, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
|
||||
|
||||
// ---- Describe the logical endpoint (unicast; counted ops optional). ----
|
||||
CUlogicalEndpointProp endpointProp{};
|
||||
endpointProp.type = CU_LOGICAL_ENDPOINT_TYPE_UNICAST;
|
||||
endpointProp.unicast = {.device = cuDevice};
|
||||
endpointProp.ipcHandleTypes = CU_LOGICAL_ENDPOINT_IPC_HANDLE_TYPE_FABRIC;
|
||||
endpointProp.flags = useCounted ? CU_LOGICAL_ENDPOINT_FLAG_COUNTED_OPS : CU_LOGICAL_ENDPOINT_FLAG_NONE;
|
||||
cuuint64_t bindAlignment;
|
||||
cuuint64_t maxSize;
|
||||
checkCudaErrors(cuLogicalEndpointGetLimits(&bindAlignment, &maxSize, &endpointProp));
|
||||
// Make the allocation and endpoint size a multiple of both independent
|
||||
// alignment requirements.
|
||||
const size_t requiredAlignment = std::lcm(granularity, static_cast<size_t>(bindAlignment));
|
||||
|
||||
// ---- Allocate payload, sync region, and alignment padding. ----
|
||||
size_t exportSize = alignUp(totalBytes + SYNC_REGION_ALIGN_BYTES + SYNC_REGION_BYTES, requiredAlignment);
|
||||
if (exportSize > maxSize) {
|
||||
fprintf(stderr,
|
||||
"error: required endpoint size %zu exceeds maximum %llu\n",
|
||||
exportSize,
|
||||
static_cast<unsigned long long>(maxSize));
|
||||
MPI_CHECK(MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
endpointProp.size = exportSize;
|
||||
|
||||
// ---- Create the local endpoint, then exchange handles with send neighbor. ----
|
||||
checkCudaErrors(cuLogicalEndpointCreate(leId + myRank, &endpointProp));
|
||||
// Export our own endpoint to a fabric IPC handle, hand it to the peers that
|
||||
// need it, and import each peer's handle to a locally reserved id.
|
||||
CUlogicalEndpointFabricHandle localHandle;
|
||||
CUlogicalEndpointFabricHandle sendRankHandle;
|
||||
checkCudaErrors(cuLogicalEndpointExport(&localHandle, leId + myRank, CU_LOGICAL_ENDPOINT_IPC_HANDLE_TYPE_FABRIC));
|
||||
MPI_CHECK(MPI_Sendrecv(&localHandle,
|
||||
sizeof(CUlogicalEndpointFabricHandle),
|
||||
MPI_BYTE,
|
||||
recvRank,
|
||||
0,
|
||||
&sendRankHandle,
|
||||
sizeof(CUlogicalEndpointFabricHandle),
|
||||
MPI_BYTE,
|
||||
sendRank,
|
||||
0,
|
||||
MPI_COMM_WORLD,
|
||||
MPI_STATUS_IGNORE));
|
||||
checkCudaErrors(cuLogicalEndpointImport(leId + sendRank, &sendRankHandle, CU_LOGICAL_ENDPOINT_IPC_HANDLE_TYPE_FABRIC));
|
||||
// Create and import are non-blocking, so wait until both endpoints are ready.
|
||||
waitForEndpointReady(leId + myRank);
|
||||
waitForEndpointReady(leId + sendRank);
|
||||
|
||||
// ---- Allocate and bind memory after sharing the endpoint. ----
|
||||
CUmemGenericAllocationHandle exportHandle;
|
||||
CUdeviceptr exportPtr = 0;
|
||||
checkCudaErrors(cuMemCreate(&exportHandle, exportSize, &exportAllocProp, 0));
|
||||
checkCudaErrors(cuMemAddressReserve(&exportPtr, exportSize, granularity, 0, 0));
|
||||
checkCudaErrors(cuMemMap(exportPtr, exportSize, 0, exportHandle, 0));
|
||||
CUmemAccessDesc accessDesc = {};
|
||||
accessDesc.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
|
||||
accessDesc.location.id = exportAllocProp.location.id;
|
||||
accessDesc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
|
||||
checkCudaErrors(cuMemSetAccess(exportPtr, exportSize, &accessDesc, 1));
|
||||
// Zero the whole allocation before the first transfer or barrier handshake.
|
||||
checkCudaErrors(cuMemsetD8(exportPtr, 0, exportSize));
|
||||
// 256-byte-aligned sync region after the payload (required for counted fabric puts).
|
||||
std::byte *const basePtr = reinterpret_cast<std::byte *>(exportPtr);
|
||||
void *syncPtr = basePtr + totalBytes;
|
||||
size_t syncSpace = exportSize - totalBytes;
|
||||
syncPtr = std::align(SYNC_REGION_ALIGN_BYTES, SYNC_REGION_BYTES, syncPtr, syncSpace);
|
||||
assert(syncPtr != nullptr); // guaranteed by the SYNC_REGION_ALIGN_BYTES slack reserved above
|
||||
const size_t syncRegionOffset = static_cast<std::byte *>(syncPtr) - basePtr;
|
||||
// counted mode (--counted): local byte counter the peer's counted puts accumulate into.
|
||||
uint64_t *const counterPtr = reinterpret_cast<uint64_t *>(syncPtr);
|
||||
// default mode: local inter-GPU flag, set by the receive neighbor's fabric put.
|
||||
uint32_t *const interGpuFlag = reinterpret_cast<uint32_t *>(syncPtr);
|
||||
// Bind the whole backing allocation at endpoint offset 0, by allocation handle.
|
||||
checkCudaErrors(cuLogicalEndpointBindMem(leId + myRank, cuDevice, 0, exportHandle, 0, exportSize, 0));
|
||||
// No rank may issue an operation until every destination has been bound.
|
||||
MPI_CHECK(MPI_Barrier(MPI_COMM_WORLD));
|
||||
|
||||
// ---- Push data to sendRank. The kernel writes into the neighbor's endpoint
|
||||
// (leId + sendRank); the receive neighbor's puts land in our own buffer. ----
|
||||
const uint32_t sendLe = leId + sendRank;
|
||||
const uint64_t counterOffset = syncRegionOffset;
|
||||
const uint64_t interGpuFlagOffset = syncRegionOffset;
|
||||
|
||||
// Launch one block per chunk: gridDim.x == totalBytes / FABRIC_CHUNK_SIZE.
|
||||
if (useCounted) {
|
||||
cftTryPutCountedKernel<<<chunkCnt, WARP_SIZE, 0, stream>>>(sendLe,
|
||||
counterOffset,
|
||||
myRank,
|
||||
counterPtr,
|
||||
/*expectedBytes=*/totalBytes);
|
||||
}
|
||||
else {
|
||||
// Payload first, then a one-thread flag kernel; same-stream ordering guarantees
|
||||
// the payload puts are done before the inter-GPU flag handshake runs.
|
||||
cftTryPutKernel<<<chunkCnt, WARP_SIZE, 0, stream>>>(sendLe, myRank);
|
||||
cftSignalFlagKernel<<<1, 1, 0, stream>>>(sendLe, interGpuFlagOffset, interGpuFlag);
|
||||
}
|
||||
checkCudaErrors(cuStreamSynchronize(stream));
|
||||
|
||||
// ---- Check the payload the receive neighbor pushed into our endpoint. ----
|
||||
// Every sending block stages the same rank-tagged pattern, so the expected
|
||||
// value at int index i is recvRank * elemsPerChunk + (i % elemsPerChunk).
|
||||
constexpr size_t elemsPerChunk = FABRIC_CHUNK_SIZE / sizeof(int);
|
||||
const size_t totalElems = totalBytes / sizeof(int);
|
||||
std::vector<int> received(totalElems);
|
||||
checkCudaErrors(cuMemcpyDtoH(received.data(), exportPtr, totalBytes));
|
||||
|
||||
size_t mismatches = 0;
|
||||
for (size_t i = 0; i < totalElems; ++i) {
|
||||
const int expected = static_cast<int>(recvRank * elemsPerChunk + i % elemsPerChunk);
|
||||
if (received[i] != expected) {
|
||||
if (mismatches == 0) {
|
||||
fprintf(stderr,
|
||||
"rank %d: first mismatch at int index %zu: got %d, expected %d\n",
|
||||
myRank,
|
||||
i,
|
||||
received[i],
|
||||
expected);
|
||||
}
|
||||
++mismatches;
|
||||
}
|
||||
}
|
||||
|
||||
int localFailed = (mismatches != 0) ? 1 : 0;
|
||||
int anyFailed = 0;
|
||||
MPI_CHECK(MPI_Allreduce(&localFailed, &anyFailed, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD));
|
||||
if (myRank == 0) {
|
||||
printf("Result = %s\n", anyFailed ? "FAIL" : "PASS");
|
||||
}
|
||||
|
||||
// ---- Clean up endpoints and free the backing allocation. ----
|
||||
// Once every rank's stream has synchronized, all fabric puts
|
||||
// have completed and no rank will issue another operation. Making that global
|
||||
// with a barrier ensures no peer still targets an endpoint we clean up below.
|
||||
MPI_CHECK(MPI_Barrier(MPI_COMM_WORLD));
|
||||
|
||||
// Destroy the imported endpoint (the peer target our puts addressed). An import
|
||||
// is only a local reference to the peer's endpoint id and maps no peer memory,
|
||||
// so once our puts have drained this just drops that reference.
|
||||
checkCudaErrors(cuLogicalEndpointDestroy(leId + sendRank));
|
||||
|
||||
// Unbind and destroy our owned endpoint, then release the reserved id range.
|
||||
checkCudaErrors(cuLogicalEndpointUnbind(leId + myRank, cuDevice, 0, exportSize));
|
||||
checkCudaErrors(cuLogicalEndpointDestroy(leId + myRank));
|
||||
checkCudaErrors(cuLogicalEndpointIdRelease(leId, numRanks));
|
||||
|
||||
// Free the backing allocation in VMM cleanup order: unmap the range, release
|
||||
// the physical handle, then free the reserved virtual address range.
|
||||
checkCudaErrors(cuMemUnmap(exportPtr, exportSize));
|
||||
checkCudaErrors(cuMemRelease(exportHandle));
|
||||
checkCudaErrors(cuMemAddressFree(exportPtr, exportSize));
|
||||
|
||||
checkCudaErrors(cuStreamDestroy(stream));
|
||||
MPI_CHECK(MPI_Comm_free(&localComm));
|
||||
MPI_CHECK(MPI_Finalize());
|
||||
|
||||
return anyFailed ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
@@ -1,21 +1,19 @@
|
||||
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(localityDomains 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()
|
||||
|
||||
# Source file
|
||||
# Add target for localityDomains
|
||||
add_executable(localityDomains localityDomains.cu)
|
||||
@@ -27,5 +25,5 @@ target_compile_features(localityDomains PRIVATE cxx_std_17 cuda_std_17)
|
||||
set_target_properties(localityDomains PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
include(InstallSamples)
|
||||
setup_samples_install()
|
||||
|
||||
@@ -72,7 +72,7 @@ allocation 0x<address> is localized to locality domain 0
|
||||
...
|
||||
```
|
||||
|
||||
If device 0 does not support memory pools, the sample reports that it is waiving execution and exits with status EXIT_WAIVED.
|
||||
If device 0 does not support memory pools or reports no locality domains, the sample reports that it is waiving execution and exits with status 2 (waived). A successful run exits with status 0.
|
||||
|
||||
## Files
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ int main()
|
||||
|
||||
if (!memoryPoolsSupported) {
|
||||
std::cout << "Waiving execution as the device does not support memory pools." << std::endl;
|
||||
return 0;
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Device information
|
||||
@@ -65,6 +65,11 @@ int main()
|
||||
std::cout << "SMs per locality domain: " << localityDomainSmCount << std::endl;
|
||||
}
|
||||
|
||||
if (localityDomainCount == 0) {
|
||||
std::cout << "Waiving execution as the device does not support locality domains." << std::endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Construct localized green contexts and streams
|
||||
std::vector<cudaExecutionContext_t> greenContexts(localityDomainCount);
|
||||
std::vector<cudaStream_t> streams(localityDomainCount);
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
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(localityDomainsDrv 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()
|
||||
|
||||
# Source file
|
||||
# Add target for localityDomainsDrv
|
||||
add_executable(localityDomainsDrv localityDomainsDrv.cpp)
|
||||
@@ -35,5 +33,5 @@ target_link_libraries(localityDomainsDrv PUBLIC
|
||||
)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
include(InstallSamples)
|
||||
setup_samples_install()
|
||||
|
||||
@@ -38,7 +38,7 @@ How to:
|
||||
|
||||
### Software
|
||||
|
||||
- CUDA Toolkit and NVIDIA driver with locality domain support
|
||||
- CUDA Toolkit and NVIDIA driver with locality domain support (CUDA Driver API 13.1 or newer)
|
||||
- Linux or QNX on x86_64 or aarch64
|
||||
- CMake 3.20 or newer
|
||||
- A C++17-capable host compiler
|
||||
@@ -74,6 +74,8 @@ allocation 0x<address> is localized to locality domain 0
|
||||
...
|
||||
```
|
||||
|
||||
If device 0 reports no locality domains, the sample reports that it is waiving execution and exits with status 2 (waived). A successful run exits with status 0.
|
||||
|
||||
## Files
|
||||
|
||||
- `localityDomainsDrv.cpp` — Driver API example
|
||||
|
||||
@@ -62,6 +62,12 @@ int main()
|
||||
std::cout << "SMs per locality domain: " << localityDomainSmCount << std::endl;
|
||||
}
|
||||
|
||||
if (localityDomainCount == 0) {
|
||||
std::cout << "Waiving execution as the device does not support locality domains." << std::endl;
|
||||
cuCtxDestroy(context);
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Construct localized green contexts and streams
|
||||
std::vector<CUgreenCtx> greenContexts(localityDomainCount);
|
||||
std::vector<CUstream> streams(localityDomainCount);
|
||||
|
||||
@@ -40,12 +40,14 @@ endif()
|
||||
set(CUDA_PTX_FILE "${CMAKE_CURRENT_BINARY_DIR}/memMapIpc_kernel64.ptx")
|
||||
set(CUDA_KERNEL_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/memMapIpc_kernel.cu")
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
|
||||
set(INCLUDES_LIST)
|
||||
# This custom command does not inherit CMAKE_CUDA_FLAGS (e.g. --target-directory), so pass the
|
||||
# toolkit include dirs explicitly. On aarch64 (Tegra/QNX/SBSA) the toolkit may keep its headers
|
||||
# in a chip-suffixed targets/ dir that nvcc does not search by default.
|
||||
set(INCLUDES)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
|
||||
foreach(dir ${CUDAToolkit_INCLUDE_DIRS})
|
||||
list(APPEND INCLUDES_LIST "-I${dir}")
|
||||
list(APPEND INCLUDES "-I${dir}")
|
||||
endforeach()
|
||||
string(JOIN " " INCLUDES "${INCLUDES_LIST}")
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
|
||||
@@ -33,12 +33,14 @@ target_link_libraries(ptxjit PUBLIC
|
||||
set(CUDA_PTX_FILE "${CMAKE_CURRENT_BINARY_DIR}/ptxjit_kernel64.ptx")
|
||||
set(CUDA_KERNEL_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/ptxjit_kernel.cu")
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
|
||||
set(INCLUDES_LIST)
|
||||
# This custom command does not inherit CMAKE_CUDA_FLAGS (e.g. --target-directory), so pass the
|
||||
# toolkit include dirs explicitly. On aarch64 (Tegra/QNX/SBSA) the toolkit may keep its headers
|
||||
# in a chip-suffixed targets/ dir that nvcc does not search by default.
|
||||
set(INCLUDES)
|
||||
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
|
||||
foreach(dir ${CUDAToolkit_INCLUDE_DIRS})
|
||||
list(APPEND INCLUDES_LIST "-I${dir}")
|
||||
list(APPEND INCLUDES "-I${dir}")
|
||||
endforeach()
|
||||
string(JOIN " " INCLUDES "${INCLUDES_LIST}")
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
## Description
|
||||
|
||||
This sample demonstrates efficient all-pairs simulation of a gravitational n-body simulation in CUDA. This sample accompanies the GPU Gems 3 chapter "Fast N-Body Simulation with CUDA". With CUDA 5.5, performance on Tesla K20c has increased to over 1.8TFLOP/s single precision. Double Performance has also improved on all Kepler and Fermi GPU architectures as well. Starting in CUDA 4.0, the nBody sample has been updated to take advantage of new features to easily scale the n-body simulation across multiple GPUs in a single PC. Adding "-numbodies=<bodies>" to the command line will allow users to set # of bodies for simulation. Adding “-numdevices=<N>” to the command line option will cause the sample to use N devices (if available) for simulation. In this mode, the position and velocity data for all bodies are read from system memory using “zero copy” rather than from device memory. For a small number of devices (4 or fewer) and a large enough number of bodies, bandwidth is not a bottleneck so we can achieve strong scaling across these devices.
|
||||
This sample demonstrates efficient all-pairs simulation of a gravitational n-body simulation in CUDA. This sample accompanies the GPU Gems 3 chapter "Fast N-Body Simulation with CUDA". With CUDA 5.5, performance on Tesla K20c has increased to over 1.8TFLOP/s single precision. Double Performance has also improved on all Kepler and Fermi GPU architectures as well. Starting in CUDA 4.0, the nBody sample has been updated to take advantage of new features to easily scale the n-body simulation across multiple GPUs in a single PC. Adding "-numbodies=<bodies>" to the command line will allow users to set # of bodies for simulation. Adding “-numdevices=<N>” to the command line option will cause the sample to use N devices (if available) for simulation. In this mode, GPU 0 holds the authoritative position and velocity buffers. Each other device keeps a local scratch copy of the read-only position array and refreshes it with one peer-to-peer copy per simulation step, so the all-pairs kernel reads every tile from local memory instead of re-fetching it over the peer link on every tile access; each device still writes its own share of results directly back to GPU 0 over the peer link. Each device issues that copy and its kernel on its own non-blocking stream so the GPUs overlap. This keeps per-step peer traffic proportional to the number of devices instead of the number of thread blocks, so multi-GPU runs scale on both NVLink and PCIe-only (no NVLink) systems.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
@@ -23,7 +23,7 @@ x86_64, armv7l
|
||||
## CUDA APIs involved
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaGraphicsUnmapResources, cudaSetDeviceFlags, cudaGraphicsResourceSetMapFlags, cudaGraphicsResourceGetMappedPointer, cudaGraphicsMapResources, cudaSetDevice, cudaEventSynchronize, cudaGetDeviceCount, cudaGetDeviceProperties, cudaDeviceSynchronize, cudaEventRecord, cudaGetDevice, cudaMemcpyToSymbol, cudaStreamQuery, cudaEventDestroy, cudaEventElapsedTime, cudaDeviceCanAccessPeer, cudaEventCreate
|
||||
cudaGraphicsUnmapResources, cudaSetDeviceFlags, cudaGraphicsResourceSetMapFlags, cudaGraphicsResourceGetMappedPointer, cudaGraphicsMapResources, cudaSetDevice, cudaEventSynchronize, cudaGetDeviceCount, cudaGetDeviceProperties, cudaDeviceSynchronize, cudaEventRecord, cudaGetDevice, cudaMemcpyToSymbol, cudaEventDestroy, cudaEventElapsedTime, cudaDeviceCanAccessPeer, cudaDeviceEnablePeerAccess, cudaMemcpyPeerAsync, cudaStreamCreateWithFlags, cudaStreamDestroy, cudaStreamSynchronize, cudaEventCreate
|
||||
|
||||
## Dependencies needed to build/run
|
||||
[X11](../../../README.md#x11), [OpenGL](../../../README.md#opengl), [Freeglut](../../../README.md#freeglut), [GLEW](../../../README.md#glew)
|
||||
|
||||
@@ -89,8 +89,10 @@ template <> __device__ double getSofteningSquared<double>() { return soften
|
||||
template <typename T> struct DeviceData
|
||||
{
|
||||
T *dPos[2]; // mapped host pointers
|
||||
T *dReadScratch;
|
||||
T *dVel;
|
||||
cudaEvent_t event;
|
||||
cudaStream_t stream;
|
||||
unsigned int offset;
|
||||
unsigned int numBodies;
|
||||
};
|
||||
@@ -210,14 +212,22 @@ void integrateNbodySystem(DeviceData<T> *deviceData,
|
||||
bool bUsePBO)
|
||||
{
|
||||
if (bUsePBO) {
|
||||
if (numDevices > 1) {
|
||||
checkCudaErrors(cudaSetDevice(0));
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaGraphicsResourceSetMapFlags(pgres[currentRead], cudaGraphicsMapFlagsReadOnly));
|
||||
checkCudaErrors(cudaGraphicsResourceSetMapFlags(pgres[1 - currentRead], cudaGraphicsMapFlagsWriteDiscard));
|
||||
checkCudaErrors(cudaGraphicsMapResources(2, pgres, 0));
|
||||
checkCudaErrors(cudaGraphicsMapResources(2, pgres, deviceData[0].stream));
|
||||
size_t bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
|
||||
(void **)&(deviceData[0].dPos[currentRead]), &bytes, pgres[currentRead]));
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
|
||||
(void **)&(deviceData[0].dPos[1 - currentRead]), &bytes, pgres[1 - currentRead]));
|
||||
// Peer copies and non-owner writes use deviceData[0].dPos directly.
|
||||
// Those pointers are only valid after this map; other devices' streams
|
||||
// do not wait for it.
|
||||
checkCudaErrors(cudaStreamSynchronize(deviceData[0].stream));
|
||||
}
|
||||
|
||||
for (unsigned int dev = 0; dev != numDevices; dev++) {
|
||||
@@ -228,21 +238,33 @@ void integrateNbodySystem(DeviceData<T> *deviceData,
|
||||
int numBlocks = (deviceData[dev].numBodies + blockSize - 1) / blockSize;
|
||||
int numTiles = (numBodies + blockSize - 1) / blockSize;
|
||||
int sharedMemSize = blockSize * 4 * sizeof(T); // 4 floats for pos
|
||||
// GPU0 owns both ping-pong position buffers and velocity. Always pass
|
||||
// this map's GPU0 pointers into the kernel so a non-owner never writes
|
||||
// through a pre-map alias. Local scratch is the only per-device buffer.
|
||||
T *readPos = deviceData[0].dPos[currentRead];
|
||||
|
||||
integrateBodies<T>
|
||||
<<<numBlocks, blockSize, sharedMemSize>>>((typename vec4<T>::Type *)deviceData[dev].dPos[1 - currentRead],
|
||||
(typename vec4<T>::Type *)deviceData[dev].dPos[currentRead],
|
||||
(typename vec4<T>::Type *)deviceData[dev].dVel,
|
||||
deviceData[dev].offset,
|
||||
deviceData[dev].numBodies,
|
||||
deltaTime,
|
||||
damping,
|
||||
numTiles);
|
||||
if (deviceData[dev].dReadScratch != nullptr) {
|
||||
checkCudaErrors(cudaMemcpyPeerAsync(deviceData[dev].dReadScratch,
|
||||
dev,
|
||||
deviceData[0].dPos[currentRead],
|
||||
0,
|
||||
numBodies * 4 * sizeof(T),
|
||||
deviceData[dev].stream));
|
||||
readPos = deviceData[dev].dReadScratch;
|
||||
}
|
||||
|
||||
integrateBodies<T><<<numBlocks, blockSize, sharedMemSize, deviceData[dev].stream>>>(
|
||||
(typename vec4<T>::Type *)deviceData[0].dPos[1 - currentRead],
|
||||
(typename vec4<T>::Type *)readPos,
|
||||
(typename vec4<T>::Type *)deviceData[0].dVel,
|
||||
deviceData[dev].offset,
|
||||
deviceData[dev].numBodies,
|
||||
deltaTime,
|
||||
damping,
|
||||
numTiles);
|
||||
|
||||
if (numDevices > 1) {
|
||||
checkCudaErrors(cudaEventRecord(deviceData[dev].event));
|
||||
// MJH: Hack on older driver versions to force kernel launches to flush!
|
||||
cudaStreamQuery(0);
|
||||
checkCudaErrors(cudaEventRecord(deviceData[dev].event, deviceData[dev].stream));
|
||||
}
|
||||
|
||||
// check if kernel invocation generated an error
|
||||
@@ -250,13 +272,23 @@ void integrateNbodySystem(DeviceData<T> *deviceData,
|
||||
}
|
||||
|
||||
if (numDevices > 1) {
|
||||
// Single-GPU stays on the legacy default stream (see _initialize())
|
||||
// and keeps queuing kernels without a host wait here. Multi-GPU
|
||||
// kernels run on per-device non-blocking streams; wait so the next
|
||||
// step's peer copy and any PBO unmap see completed writes.
|
||||
for (unsigned int dev = 0; dev < numDevices; dev++) {
|
||||
checkCudaErrors(cudaEventSynchronize(deviceData[dev].event));
|
||||
}
|
||||
}
|
||||
|
||||
if (bUsePBO) {
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(2, pgres, 0));
|
||||
if (numDevices > 1) {
|
||||
checkCudaErrors(cudaSetDevice(0));
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(2, pgres, deviceData[0].stream));
|
||||
// GL (windowed display) may use the PBO as soon as update() returns.
|
||||
checkCudaErrors(cudaStreamSynchronize(deviceData[0].stream));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,10 @@
|
||||
template <typename T> struct DeviceData
|
||||
{
|
||||
T *dPos[2]; // mapped host pointers
|
||||
T *dReadScratch;
|
||||
T *dVel;
|
||||
cudaEvent_t event;
|
||||
cudaStream_t stream;
|
||||
unsigned int offset;
|
||||
unsigned int numBodies;
|
||||
};
|
||||
@@ -66,6 +68,12 @@ public:
|
||||
|
||||
virtual unsigned int getNumBodies() const { return m_numBodies; }
|
||||
|
||||
// Stream that GPU0 kernels (and PBO map/unmap) are issued on. Single-GPU
|
||||
// leaves this as the legacy default stream (0); multi-GPU uses a
|
||||
// non-blocking stream. Benchmark / FPS timing must record on this stream
|
||||
// rather than hard-coding 0.
|
||||
cudaStream_t getComputeStream() const { return m_deviceData[0].stream; }
|
||||
|
||||
protected: // methods
|
||||
BodySystemCUDA() {}
|
||||
|
||||
|
||||
@@ -116,6 +116,9 @@ template <typename T> void BodySystemCUDA<T>::_initialize(int numBodies)
|
||||
unsigned int remaining = m_numBodies;
|
||||
|
||||
for (unsigned int i = 0; i < m_numDevices; i++) {
|
||||
m_deviceData[i].dReadScratch = 0;
|
||||
m_deviceData[i].stream = 0;
|
||||
|
||||
unsigned int count = (int)((weights[i] / total) * m_numBodies);
|
||||
// Rounding up to numSms[i]*256 leads to better GPU utilization _per_ GPU
|
||||
// but when using multiple devices, it will lead to the last GPUs not having
|
||||
@@ -172,6 +175,13 @@ template <typename T> void BodySystemCUDA<T>::_initialize(int numBodies)
|
||||
checkCudaErrors(cudaSetDevice(m_devID));
|
||||
checkCudaErrors(cudaEventCreate(&m_deviceData[0].event));
|
||||
|
||||
// A non-blocking stream is only needed to let multiple devices' work
|
||||
// overlap. Single-GPU keeps m_deviceData[0].stream == 0 (set above)
|
||||
// and launches on the legacy default stream.
|
||||
if (m_numDevices > 1) {
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&m_deviceData[0].stream, cudaStreamNonBlocking));
|
||||
}
|
||||
|
||||
if (m_bUsePBO) {
|
||||
// create the position pixel buffer objects for rendering
|
||||
// we will actually compute directly from this memory in CUDA too
|
||||
@@ -217,12 +227,22 @@ template <typename T> void BodySystemCUDA<T>::_initialize(int numBodies)
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaEventCreate(&m_deviceData[i].event));
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&m_deviceData[i].stream, cudaStreamNonBlocking));
|
||||
|
||||
// Local scratch for this device's copy of the read-only position
|
||||
// array: integrateNbodySystem() refreshes it once per step so the
|
||||
// all-pairs kernel reads every tile from local memory instead of
|
||||
// re-fetching over the P2P link on every tile access.
|
||||
checkCudaErrors(cudaMalloc((void **)&m_deviceData[i].dReadScratch, memSize));
|
||||
|
||||
// Point all GPUs to the memory allocated on gpu0
|
||||
m_deviceData[i].dPos[0] = m_deviceData[0].dPos[0];
|
||||
m_deviceData[i].dPos[1] = m_deviceData[0].dPos[1];
|
||||
m_deviceData[i].dVel = m_deviceData[0].dVel;
|
||||
}
|
||||
|
||||
// Restore m_devID: callers create CUDA handles on whatever device is current right after this returns.
|
||||
checkCudaErrors(cudaSetDevice(m_devID));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,6 +267,13 @@ template <typename T> void BodySystemCUDA<T>::_finalize()
|
||||
delete[] m_hPos[1];
|
||||
delete[] m_hVel;
|
||||
|
||||
// integrateNbodySystem() can leave a non-zero device current when
|
||||
// m_numDevices > 1 and m_bUsePBO is false. The handles below were
|
||||
// created on m_devID, so restore it before touching them.
|
||||
if (m_numDevices > 1) {
|
||||
checkCudaErrors(cudaSetDevice(m_devID));
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaFree((void **)m_deviceData[0].dVel));
|
||||
|
||||
if (m_bUsePBO) {
|
||||
@@ -257,14 +284,21 @@ template <typename T> void BodySystemCUDA<T>::_finalize()
|
||||
else {
|
||||
checkCudaErrors(cudaFree((void **)m_deviceData[0].dPos[0]));
|
||||
checkCudaErrors(cudaFree((void **)m_deviceData[0].dPos[1]));
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaEventDestroy(m_deviceData[0].event));
|
||||
checkCudaErrors(cudaEventDestroy(m_deviceData[0].event));
|
||||
if (m_numDevices > 1) {
|
||||
checkCudaErrors(cudaStreamDestroy(m_deviceData[0].stream));
|
||||
}
|
||||
|
||||
if (m_bUseP2P) {
|
||||
for (unsigned int i = 1; i < m_numDevices; i++) {
|
||||
checkCudaErrors(cudaEventDestroy(m_deviceData[i].event));
|
||||
}
|
||||
if (m_bUseP2P) {
|
||||
for (unsigned int i = 1; i < m_numDevices; i++) {
|
||||
checkCudaErrors(cudaSetDevice(i));
|
||||
checkCudaErrors(cudaFree(m_deviceData[i].dReadScratch));
|
||||
checkCudaErrors(cudaEventDestroy(m_deviceData[i].event));
|
||||
checkCudaErrors(cudaStreamDestroy(m_deviceData[i].stream));
|
||||
}
|
||||
checkCudaErrors(cudaSetDevice(m_devID));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -304,6 +338,11 @@ template <typename T> void BodySystemCUDA<T>::setSoftening(T softening)
|
||||
|
||||
checkCudaErrors(setSofteningSquared(softeningSq));
|
||||
}
|
||||
|
||||
// Restore m_devID; the constructor calls this right after _initialize()'s own restore.
|
||||
if (m_numDevices > 1) {
|
||||
checkCudaErrors(cudaSetDevice(m_devID));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> void BodySystemCUDA<T>::setDamping(T damping) { m_damping = damping; }
|
||||
@@ -322,6 +361,14 @@ template <typename T> void BodySystemCUDA<T>::update(T deltaTime)
|
||||
m_blockSize,
|
||||
m_bUsePBO);
|
||||
|
||||
// The launch loop leaves the last device current. Host-memory multi-GPU
|
||||
// has no explicit stream, so a later cudaEventRecord(timingEvent, 0) uses
|
||||
// the current device's default stream. Timing events are created on
|
||||
// m_devID, and each device has its own default stream.
|
||||
if (m_numDevices > 1) {
|
||||
checkCudaErrors(cudaSetDevice(m_devID));
|
||||
}
|
||||
|
||||
std::swap(m_currentRead, m_currentWrite);
|
||||
}
|
||||
|
||||
@@ -357,15 +404,18 @@ template <typename T> T *BodySystemCUDA<T>::getArray(BodyArray array)
|
||||
if (!m_bUseSysMem) {
|
||||
if (pgres) {
|
||||
checkCudaErrors(cudaGraphicsResourceSetMapFlags(pgres, cudaGraphicsMapFlagsReadOnly));
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, &pgres, 0));
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, &pgres, m_deviceData[0].stream));
|
||||
size_t bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void **)&ddata, &bytes, pgres));
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaMemcpy(hdata, ddata, m_numBodies * 4 * sizeof(T), cudaMemcpyDeviceToHost));
|
||||
checkCudaErrors(cudaMemcpyAsync(
|
||||
hdata, ddata, m_numBodies * 4 * sizeof(T), cudaMemcpyDeviceToHost, m_deviceData[0].stream));
|
||||
checkCudaErrors(cudaStreamSynchronize(m_deviceData[0].stream));
|
||||
|
||||
if (pgres) {
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &pgres, 0));
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &pgres, m_deviceData[0].stream));
|
||||
checkCudaErrors(cudaStreamSynchronize(m_deviceData[0].stream));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -162,6 +162,8 @@ static int fpsLimit = 5;
|
||||
cudaEvent_t startEvent, stopEvent;
|
||||
cudaEvent_t hostMemSyncEvent;
|
||||
|
||||
cudaStream_t nbodyTimingStream();
|
||||
|
||||
template <typename T> class NBodyDemo
|
||||
{
|
||||
public:
|
||||
@@ -188,6 +190,11 @@ public:
|
||||
|
||||
static void runBenchmark(int iterations) { m_singleton->_runBenchmark(iterations); }
|
||||
|
||||
static cudaStream_t timingStream()
|
||||
{
|
||||
return (m_singleton && m_singleton->m_nbodyCuda) ? m_singleton->m_nbodyCuda->getComputeStream() : 0;
|
||||
}
|
||||
|
||||
static void updateParams()
|
||||
{
|
||||
m_singleton->m_nbody->setSoftening(activeParams.m_softening);
|
||||
@@ -440,7 +447,7 @@ private:
|
||||
sdkStartTimer(&timer);
|
||||
}
|
||||
else {
|
||||
checkCudaErrors(cudaEventRecord(startEvent, 0));
|
||||
checkCudaErrors(cudaEventRecord(startEvent, nbodyTimingStream()));
|
||||
}
|
||||
|
||||
for (int i = 0; i < iterations; ++i) {
|
||||
@@ -455,7 +462,7 @@ private:
|
||||
sdkStartTimer(&timer);
|
||||
}
|
||||
else {
|
||||
checkCudaErrors(cudaEventRecord(stopEvent, 0));
|
||||
checkCudaErrors(cudaEventRecord(stopEvent, nbodyTimingStream()));
|
||||
checkCudaErrors(cudaEventSynchronize(stopEvent));
|
||||
checkCudaErrors(cudaEventElapsedTime(&milliseconds, startEvent, stopEvent));
|
||||
}
|
||||
@@ -473,6 +480,19 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
cudaStream_t nbodyTimingStream()
|
||||
{
|
||||
if (useCpu) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fp64) {
|
||||
return NBodyDemo<double>::timingStream();
|
||||
}
|
||||
|
||||
return NBodyDemo<float>::timingStream();
|
||||
}
|
||||
|
||||
void finalize()
|
||||
{
|
||||
if (!useCpu) {
|
||||
@@ -642,7 +662,7 @@ void display()
|
||||
|
||||
if (!useCpu) {
|
||||
cudaEventRecord(hostMemSyncEvent,
|
||||
0); // insert an event to wait on before rendering
|
||||
nbodyTimingStream()); // insert an event to wait on before rendering
|
||||
}
|
||||
}
|
||||
|
||||
@@ -720,7 +740,7 @@ void display()
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
else {
|
||||
checkCudaErrors(cudaEventRecord(stopEvent, 0));
|
||||
checkCudaErrors(cudaEventRecord(stopEvent, nbodyTimingStream()));
|
||||
checkCudaErrors(cudaEventSynchronize(stopEvent));
|
||||
checkCudaErrors(cudaEventElapsedTime(&milliseconds, startEvent, stopEvent));
|
||||
}
|
||||
@@ -748,7 +768,7 @@ void display()
|
||||
|
||||
// restart timer
|
||||
if (!useCpu) {
|
||||
checkCudaErrors(cudaEventRecord(startEvent, 0));
|
||||
checkCudaErrors(cudaEventRecord(startEvent, nbodyTimingStream()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1086,26 +1106,13 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
if (!useCpu) {
|
||||
// Now choose the CUDA Device
|
||||
// Either without GL interop:
|
||||
if (benchmark || compareToCPU || useHostMem) {
|
||||
// Note if we are using host memory for the body system, we
|
||||
// don't use CUDA-GL interop.
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
|
||||
customGPU = true;
|
||||
}
|
||||
|
||||
devID = findCudaDevice(argc, (const char **)argv);
|
||||
// Device selection does not depend on GL interop. Benchmark, compare,
|
||||
// and host memory skip interop by leaving the PBO argument false below.
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
|
||||
customGPU = true;
|
||||
}
|
||||
else // or with GL interop:
|
||||
{
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
|
||||
customGPU = true;
|
||||
}
|
||||
|
||||
devID = findCudaDevice(argc, (const char **)argv);
|
||||
}
|
||||
devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
checkCudaErrors(cudaGetDevice(&devID));
|
||||
checkCudaErrors(cudaGetDeviceProperties(&props, devID));
|
||||
@@ -1147,10 +1154,7 @@ int main(int argc, char **argv)
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (numDevsRequested > 1) {
|
||||
checkCudaErrors(cudaSetDevice(i));
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaSetDevice(i));
|
||||
checkCudaErrors(cudaSetDeviceFlags(cudaDeviceMapHost));
|
||||
#else
|
||||
fprintf(stderr,
|
||||
@@ -1322,7 +1326,7 @@ int main(int argc, char **argv)
|
||||
glutIdleFunc(idle);
|
||||
|
||||
if (!useCpu) {
|
||||
checkCudaErrors(cudaEventRecord(startEvent, 0));
|
||||
checkCudaErrors(cudaEventRecord(startEvent, nbodyTimingStream()));
|
||||
}
|
||||
|
||||
glutMainLoop();
|
||||
@@ -1349,7 +1353,7 @@ int main(int argc, char **argv)
|
||||
glutIdleFunc(idle);
|
||||
|
||||
if (!useCpu) {
|
||||
checkCudaErrors(cudaEventRecord(startEvent, 0));
|
||||
checkCudaErrors(cudaEventRecord(startEvent, nbodyTimingStream()));
|
||||
}
|
||||
|
||||
glutMainLoop();
|
||||
|
||||
@@ -71,8 +71,17 @@ find_file(LIBNVVM_HOME nvvm PATHS "$ENV{LIBNVVM_HOME}" "${CUDA_HOME}")
|
||||
message(STATUS "Using LIBNVVM_HOME: ${LIBNVVM_HOME}")
|
||||
|
||||
# Find libNVVM and nvvm.h.
|
||||
# (Linux: nvvm/lib64, windows: nvvm/lib/x64, windows on arm:nvvm/lib/arm64)
|
||||
find_library(NVVM_LIB nvvm PATHS "${LIBNVVM_HOME}/lib64" "${LIBNVVM_HOME}/lib/x64" "${LIBNVVM_HOME}/lib/arm64")
|
||||
if (WIN32)
|
||||
# Select the target architecture, which can differ from the host architecture.
|
||||
if (CMAKE_C_COMPILER_ARCHITECTURE_ID STREQUAL "ARM64")
|
||||
set(NVVM_ARCH arm64)
|
||||
else ()
|
||||
set(NVVM_ARCH x64)
|
||||
endif ()
|
||||
find_library(NVVM_LIB nvvm PATHS "${LIBNVVM_HOME}/lib/${NVVM_ARCH}" NO_DEFAULT_PATH)
|
||||
else ()
|
||||
find_library(NVVM_LIB nvvm PATHS "${LIBNVVM_HOME}/lib64")
|
||||
endif ()
|
||||
find_file(NVVM_H nvvm.h PATH "${LIBNVVM_HOME}/include")
|
||||
get_filename_component(NVVM_INCLUDE_DIR ${NVVM_H} DIRECTORY)
|
||||
include_directories(${NVVM_INCLUDE_DIR})
|
||||
@@ -93,7 +102,9 @@ message(STATUS "Using rpath: ${CMAKE_INSTALL_RPATH}")
|
||||
|
||||
# On Windows, locate the nvvm.dll so we can install it later.
|
||||
if (WIN32)
|
||||
find_file(NVVM_DLL nvvm64_40_0.dll PATHS "${LIBNVVM_HOME}/bin" "${LIBNVVM_HOME}/bin/x64/")
|
||||
find_file(NVVM_DLL nvvm64_40_0.dll
|
||||
PATHS "${LIBNVVM_HOME}/bin/${NVVM_ARCH}" "${LIBNVVM_HOME}/bin"
|
||||
NO_DEFAULT_PATH)
|
||||
if (NOT NVVM_DLL)
|
||||
message(FATAL_ERROR "Found nvvm .h/.lib, but not .dll")
|
||||
endif()
|
||||
|
||||
@@ -34,3 +34,6 @@ endif()
|
||||
|
||||
set(CMAKE_FOLDER "9_CUDA_Tile")
|
||||
add_subdirectory(9_CUDA_Tile)
|
||||
|
||||
set(CMAKE_FOLDER "10_Safety")
|
||||
add_subdirectory(10_Safety)
|
||||
|
||||
@@ -23,7 +23,7 @@ Query and display detailed properties of all CUDA-capable devices in your system
|
||||
|
||||
- `Device.get_all_devices()` - Get tuple of all available Device instances
|
||||
- `Device(device_id)` - Get Device object for specific device ID
|
||||
- `system.get_driver_version()` - Query CUDA driver version
|
||||
- `system.get_user_mode_driver_version()` - Query CUDA driver version
|
||||
- `Device.set_current()` - Set the current device for API calls
|
||||
- `Device.properties` - Access comprehensive device properties
|
||||
- `Device.name` - Get device name string
|
||||
@@ -90,7 +90,7 @@ Query and display detailed properties of all CUDA-capable devices in your system
|
||||
- CUDA Toolkit 13.0 or newer (recommended; matches `cuda-python` 13.x)
|
||||
- Python 3.10 or newer
|
||||
- `cuda-python` package (>=13.0.0)
|
||||
- `cuda-core` package (>=1.0.0)
|
||||
- `cuda-core` package (>=1.2.0)
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -103,7 +103,7 @@ pip install -r requirements.txt
|
||||
|
||||
The requirements.txt installs:
|
||||
- `cuda-python` (>=13.0.0)
|
||||
- `cuda-core` (>=1.0.0)
|
||||
- `cuda-core` (>=1.2.0)
|
||||
|
||||
## How to Run
|
||||
|
||||
|
||||
@@ -87,7 +87,12 @@ def print_driver_info() -> None:
|
||||
"NVML bindings are not compatible with this driver; "
|
||||
"device info will be limited."
|
||||
)
|
||||
print(f"Current process: {system.get_process_name(os.getpid())}")
|
||||
try:
|
||||
# On WSL, NVML can only name a PID that already holds a CUDA
|
||||
# context, which this process doesn't have yet.
|
||||
print(f"Current process: {system.get_process_name(os.getpid())}")
|
||||
except system.NotFoundError as e:
|
||||
print(f"Current process: unavailable ({e})")
|
||||
|
||||
|
||||
def print_device_info(device: "system.Device") -> None:
|
||||
|
||||
@@ -147,14 +147,27 @@ def run_lifecycle(proc: checkpoint.Process, lock_timeout_ms: int) -> List[StepTi
|
||||
raise CheckpointUnsupportedError from exc
|
||||
timings.append(StepTiming("lock", ms, proc.state))
|
||||
|
||||
ms = _time_call(proc.checkpoint)
|
||||
timings.append(StepTiming("checkpoint", ms, proc.state))
|
||||
# Always unlock, even on failure, or the process stays permanently
|
||||
# "locked" and later cleanup calls deadlock against it. Keep the
|
||||
# original error primary if unlock() also fails.
|
||||
op_error: BaseException | None = None
|
||||
try:
|
||||
ms = _time_call(proc.checkpoint)
|
||||
timings.append(StepTiming("checkpoint", ms, proc.state))
|
||||
|
||||
ms = _time_call(proc.restore)
|
||||
timings.append(StepTiming("restore", ms, proc.state))
|
||||
ms = _time_call(proc.restore)
|
||||
timings.append(StepTiming("restore", ms, proc.state))
|
||||
except BaseException as exc:
|
||||
op_error = exc
|
||||
|
||||
ms = _time_call(proc.unlock)
|
||||
timings.append(StepTiming("unlock", ms, proc.state))
|
||||
try:
|
||||
ms = _time_call(proc.unlock)
|
||||
timings.append(StepTiming("unlock", ms, proc.state))
|
||||
except Exception:
|
||||
if op_error is None:
|
||||
raise
|
||||
if op_error is not None:
|
||||
raise op_error
|
||||
|
||||
return timings
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ This sample demonstrates how to add a custom GPU operation to PyTorch using the
|
||||
- cuda-python >= 13.0.0
|
||||
- cuda-core >=1.0.0
|
||||
|
||||
**PyTorch GPU compatibility:** Install a CUDA-enabled PyTorch build that supports your target GPU. GPU architecture support varies by platform and CUDA build. Check the [PyTorch CUDA Support Matrix](https://github.com/pytorch/pytorch/blob/main/RELEASE.md#pytorch-cuda-support-matrix) before installing PyTorch.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
|
||||
@@ -95,9 +95,9 @@ def check_ipc_support(device) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def child_worker(q_in, q_out, n_elements, parent_seed, child_seed):
|
||||
def child_worker(q_in, q_out, n_elements, parent_seed, child_seed, device_id):
|
||||
"""Runs in a separate process. Verifies and modifies the shared buffer."""
|
||||
device = Device(0)
|
||||
device = Device(device_id)
|
||||
device.set_current()
|
||||
pid = mp.current_process().pid
|
||||
|
||||
@@ -188,7 +188,7 @@ def main() -> int:
|
||||
q_from_child = mp.Queue()
|
||||
child = mp.Process(
|
||||
target=child_worker,
|
||||
args=(q_to_child, q_from_child, N, parent_seed, child_seed),
|
||||
args=(q_to_child, q_from_child, N, parent_seed, child_seed, args.device),
|
||||
)
|
||||
child.start()
|
||||
q_to_child.put(buffer)
|
||||
|
||||
@@ -6,7 +6,7 @@ This sample demonstrates peer-to-peer (P2P) memory access between multiple GPUs
|
||||
|
||||
## What you will learn
|
||||
|
||||
- How to detect multiple CUDA-capable GPUs using `system.get_num_devices()` and `Device(id)`
|
||||
- How to detect multiple CUDA-capable GPUs using `Device.get_all_devices()`
|
||||
- How to check P2P capability between GPU pairs using `device.can_access_peer()`
|
||||
- How to enable and disable peer access using `DeviceMemoryResource.peer_accessible_by`
|
||||
- How to allocate device memory on specific GPUs using `DeviceMemoryResource`
|
||||
@@ -26,9 +26,7 @@ This sample demonstrates peer-to-peer (P2P) memory access between multiple GPUs
|
||||
## Key APIs
|
||||
|
||||
**From cuda.core:**
|
||||
- `system` – Pre-instantiated singleton for system-level CUDA information
|
||||
- `system.get_num_devices()` – Get number of CUDA-capable devices
|
||||
- `Device(id)` – Get specific CUDA device handle
|
||||
- `Device.get_all_devices()` – Get handles for all CUDA-visible devices
|
||||
- `device.can_access_peer(peer)` – Check if this device can access peer device memory
|
||||
- `device.set_current()` – Set active device for subsequent operations
|
||||
- `device.create_stream()` – Create CUDA stream for kernel execution
|
||||
@@ -94,7 +92,7 @@ pip install -r requirements.txt
|
||||
|
||||
Or manually:
|
||||
```bash
|
||||
pip install numpy>=2.3.2 cuda-core>=1.0.0 cuda-python>=13.0.0
|
||||
pip install numpy>=2.3.2 cuda-core>=1.2.0 cuda-python>=13.0.0
|
||||
```
|
||||
|
||||
## How to run
|
||||
|
||||
@@ -39,7 +39,6 @@ try:
|
||||
Program,
|
||||
ProgramOptions,
|
||||
launch,
|
||||
system,
|
||||
)
|
||||
except ImportError as e:
|
||||
print(f"Error: Required package not found: {e}")
|
||||
@@ -91,7 +90,8 @@ def run(num_elements=1024 * 1024 * 16):
|
||||
|
||||
# Check for multiple GPUs
|
||||
print("\nChecking for multiple GPUs...")
|
||||
num_devices = system.get_num_devices()
|
||||
devices = Device.get_all_devices()
|
||||
num_devices = len(devices)
|
||||
print(f"CUDA-capable device count: {num_devices}")
|
||||
|
||||
if num_devices < 2:
|
||||
@@ -101,9 +101,6 @@ def run(num_elements=1024 * 1024 * 16):
|
||||
)
|
||||
return 2
|
||||
|
||||
# Get device properties
|
||||
devices = [Device(i) for i in range(num_devices)]
|
||||
|
||||
# Check for P2P capability
|
||||
print("\nChecking GPU(s) for support of peer to peer memory access...")
|
||||
|
||||
|
||||
@@ -334,6 +334,21 @@
|
||||
"EGLStream_CUDA_CrossGPU": {
|
||||
"min_gpus": 2
|
||||
},
|
||||
"cftUnicastPut": {
|
||||
"min_gpus": 2,
|
||||
"requires": ["mpi"],
|
||||
"launcher": ["mpirun", "--allow-run-as-root", "-np", "2"],
|
||||
"runs": [
|
||||
{
|
||||
"name": "put",
|
||||
"args": ["100"]
|
||||
},
|
||||
{
|
||||
"name": "counted",
|
||||
"args": ["100", "--counted"]
|
||||
}
|
||||
]
|
||||
},
|
||||
"reductionMultiBlockCG": {
|
||||
"python": {
|
||||
"args": ["--cuda-include-dir=$CUDA_HOME/include:$CUDA_HOME/include/cccl"]
|
||||
|
||||
Reference in New Issue
Block a user