NVIDIA DeepStream SDK API Reference

9.1 Release
sources/includes/ds3d/common/helper/cuda_utils.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef DS3D_COMMON_HELPER_CUDA_UTILS_H
19 #define DS3D_COMMON_HELPER_CUDA_UTILS_H
20 
21 #include "ds3d/common/common.h"
22 #include "ds3d/common/helper/memdata.h"
23 
24 #include <cuda.h>
25 #include <cuda_runtime_api.h>
26 
27 namespace ds3d {
28 
29 class CudaStream {
30  cudaStream_t _stream = nullptr;
31  int _gpuId = 0;
32  DS3D_DISABLE_CLASS_COPY(CudaStream);
33 
34 public:
35  explicit CudaStream(uint flag = cudaStreamDefault, int gpuId = 0, int priority = 0)
36  {
37  cudaSetDevice(_gpuId);
39  cudaStreamCreateWithPriority(&_stream, flag, priority), , "cudaStreamCreate failed");
40  }
42  {
43  if (_stream != nullptr) {
45  cudaSetDevice(_gpuId), , "cudaStreamDestroy failed to set gpu-id:%d", _gpuId);
46  DS3D_CHECK_CUDA_ERROR(cudaStreamDestroy(_stream), , "cudaStreamDestroy failed");
47  }
48  }
50  {
51  if (!_stream) {
52  return ErrCode::kGood;
53  }
55  cudaSetDevice(_gpuId), return ErrCode::kCuda,
56  "cudaStreamSynchronize failed to set gpu-id:%d", _gpuId);
58  cudaStreamSynchronize(_stream), return ErrCode::kCuda, "cudaStreamSynchronize failed");
59  return ErrCode::kGood;
60  }
61  int gpuId() const { return _gpuId; }
62  cudaStream_t& get() { return _stream; }
63 };
64 
65 class GpuCudaMemBuf : public MemData {
66 public:
68  static std::unique_ptr<GpuCudaMemBuf> CreateBuf(size_t size, int gpuId)
69  {
70  auto mem = std::make_unique<GpuCudaMemBuf>();
71 
73  cudaSetDevice(gpuId), return nullptr,
74  "cudaSetDevice: %d failed when allocate cuda memory", gpuId);
75  size = DS3D_ROUND_UP(size, 256);
76  void* data = nullptr;
78  cudaMalloc(&data, size), return nullptr, "cudaMalloc size: %d failed", (int)size);
79  DS_ASSERT(data);
80 
81  mem->data = data;
82  mem->byteSize = size;
83  mem->devId = 0;
84  mem->type = MemType::kGpuCuda;
85  return mem;
86  }
88  {
89  if (data) {
90  cudaSetDevice(devId);
91  cudaFree(data);
92  data = nullptr;
93  byteSize = 0;
94  }
95  }
96 };
97 
98 }; // namespace ds3d
99 #endif
DS3D_ROUND_UP
#define DS3D_ROUND_UP(value, align)
Definition: sources/includes/ds3d/common/defines.h:140
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: sources/includes/nvbufsurftransform.h:35
ds3d::CudaStream
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:29
ds3d::CudaStream::CudaStream
CudaStream(uint flag=cudaStreamDefault, int gpuId=0, int priority=0)
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:35
ds3d::MemData
Definition: sources/includes/ds3d/common/helper/memdata.h:25
ds3d::ErrCode::kGood
@ kGood
ds3d::ErrCode::kCuda
@ kCuda
DS_ASSERT
#define DS_ASSERT(...)
Definition: sources/includes/ds3d/common/defines.h:36
ds3d::ErrCode
ErrCode
Definition: sources/includes/ds3d/common/common.h:47
ds3d::MemData::data
void * data
Definition: sources/includes/ds3d/common/helper/memdata.h:26
ds3d::MemData::byteSize
size_t byteSize
Definition: sources/includes/ds3d/common/helper/memdata.h:27
DS3D_CHECK_CUDA_ERROR
#define DS3D_CHECK_CUDA_ERROR(err, action, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:95
ds3d::GpuCudaMemBuf
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:65
ds3d::MemType::kGpuCuda
@ kGpuCuda
ds3d::MemData::devId
int devId
Definition: sources/includes/ds3d/common/helper/memdata.h:28
ds3d::GpuCudaMemBuf::~GpuCudaMemBuf
~GpuCudaMemBuf()
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:87
ds3d::GpuCudaMemBuf::CreateBuf
static std::unique_ptr< GpuCudaMemBuf > CreateBuf(size_t size, int gpuId)
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:68
ds3d::CudaStream::gpuId
int gpuId() const
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:61
ds3d::CudaStream::~CudaStream
~CudaStream()
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:41
ds3d::CudaStream::get
cudaStream_t & get()
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:62
ds3d
Definition: sources/includes/ds3d/common/abi_dataprocess.h:25
ds3d::GpuCudaMemBuf::GpuCudaMemBuf
GpuCudaMemBuf()
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:67
ds3d::CudaStream::sync
ErrCode sync()
Definition: sources/includes/ds3d/common/helper/cuda_utils.h:49