triton_cpp v1.2.1
Header-only C++ wrapper for NVIDIA Triton Inference Server clients
Loading...
Searching...
No Matches
cuda_shm.hpp
Go to the documentation of this file.
1// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <cstdint>
7#include <stdexcept>
8#include <string>
9
10#if defined(TRITON_CPP_ENABLE_CUDA_SHM)
11#include <cuda_runtime_api.h>
12#endif
13
14namespace triton_cpp {
15
16#if defined(TRITON_CPP_ENABLE_CUDA_SHM)
17
24inline void throw_on_cuda_error(cudaError_t status, const char* operation) {
25 if (status == cudaSuccess) {
26 return;
27 }
28 throw std::runtime_error(std::string(operation) + " failed: " + cudaGetErrorString(status));
29}
30
36inline bool LocalCudaSharedMemorySupported(std::string* reason = nullptr) {
37 int device_count = 0;
38 const auto status = cudaGetDeviceCount(&device_count);
39 if (status != cudaSuccess) {
40 if (reason != nullptr) {
41 *reason = cudaGetErrorString(status);
42 }
43 cudaGetLastError();
44 return false;
45 }
46 if (device_count <= 0) {
47 if (reason != nullptr) {
48 *reason = "no CUDA-capable device is visible to the client";
49 }
50 return false;
51 }
52 return true;
53}
54
62 public:
69 CudaSharedMemoryRegion(const std::string& name, std::int64_t size) : name_{name}, size_{size} {
70 throw_on_cuda_error(cudaGetDevice(&device_id_), "cudaGetDevice");
71 throw_on_cuda_error(cudaMalloc(reinterpret_cast<void**>(&device_ptr_), static_cast<std::size_t>(size_)), "cudaMalloc");
72 throw_on_cuda_error(cudaIpcGetMemHandle(&ipc_handle_, device_ptr_), "cudaIpcGetMemHandle");
73 }
74
77 if (device_ptr_ != nullptr) {
78 cudaFree(device_ptr_);
79 }
80 }
81
86
88 uint8_t* getDeviceAddress() const { return device_ptr_; }
90 const cudaIpcMemHandle_t& getIpcHandle() const { return ipc_handle_; }
92 std::size_t getDeviceId() const { return static_cast<std::size_t>(device_id_); }
94 std::int64_t getSize() const { return size_; }
96 const std::string& getName() const { return name_; }
97
98 private:
99 std::string name_;
100 std::int64_t size_ = 0;
101 int device_id_ = 0;
102 uint8_t* device_ptr_ = nullptr;
103 cudaIpcMemHandle_t ipc_handle_{};
104};
105
106#endif
107
108} // namespace triton_cpp
Own a CUDA device allocation exportable through a CUDA IPC handle.
Definition cuda_shm.hpp:61
const std::string & getName() const
Definition cuda_shm.hpp:96
CudaSharedMemoryRegion(const CudaSharedMemoryRegion &)=delete
~CudaSharedMemoryRegion()
Release the owned CUDA device allocation.
Definition cuda_shm.hpp:76
CudaSharedMemoryRegion & operator=(const CudaSharedMemoryRegion &)=delete
CudaSharedMemoryRegion(CudaSharedMemoryRegion &&)=delete
std::int64_t getSize() const
Definition cuda_shm.hpp:94
uint8_t * getDeviceAddress() const
Definition cuda_shm.hpp:88
std::size_t getDeviceId() const
Definition cuda_shm.hpp:92
const cudaIpcMemHandle_t & getIpcHandle() const
Definition cuda_shm.hpp:90
CudaSharedMemoryRegion & operator=(CudaSharedMemoryRegion &&)=delete
CudaSharedMemoryRegion(const std::string &name, std::int64_t size)
Allocate a CUDA shared-memory region on the current device.
Definition cuda_shm.hpp:69
void throw_on_cuda_error(cudaError_t status, const char *operation)
Throw a descriptive exception when a CUDA Runtime API call fails.
Definition cuda_shm.hpp:24
bool LocalCudaSharedMemorySupported(std::string *reason=nullptr)
Check whether this process can allocate CUDA IPC shared memory.
Definition cuda_shm.hpp:36