mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-11-18 01:48:16 +08:00
Sync v13.5-public from internal repo
This commit is contained in:
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]);
|
||||
}
|
||||
Reference in New Issue
Block a user