NVIDIA DeepStream SDK API Reference

9.1 Release
sources/libs/nvll_osd/memory.hpp
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 MEMORY_HPP
19 #define MEMORY_HPP
20 
21 #include <cuda_runtime.h>
22 #include <stdio.h>
23 
24 #define checkRuntime(call) check_runtime(call, #call, __LINE__, __FILE__)
25 
26 #define CUOSD_PRINT_E(f_, ...) \
27  fprintf(stderr, "[cuOSD Error] at %s:%d : " f_, (const char*)__FILE__, __LINE__, ##__VA_ARGS__)
28 
29 #define CUOSD_PRINT_W(f_, ...) \
30  printf("[cuOSD Warning] at %s:%d : " f_, (const char*)__FILE__, __LINE__, ##__VA_ARGS__)
31 
32 static bool inline check_runtime(cudaError_t e, const char* call, int line, const char *file) {
33  if (e != cudaSuccess) {
34  fprintf(stderr, "CUDA Runtime error %s # %s, code = %s [ %d ] in file %s:%d\n", call, cudaGetErrorString(e), cudaGetErrorName(e), e, file, line);
35  return false;
36  }
37  return true;
38 }
39 
40 template<typename T>
41 class Memory{
42 public:
43  T* host() const{return host_;}
44  T* device() const{return device_;}
45  size_t size() const{return size_;}
46  size_t bytes() const{return size_ * sizeof(T);}
47 
48  virtual ~Memory() {
49  free_memory();
50  }
51 
52  void copy_host_to_device(cudaStream_t stream=nullptr) {
53  checkRuntime(cudaMemcpyAsync(device_, host_, bytes(), cudaMemcpyHostToDevice, stream));
54  }
55  void copy_device_to_host(cudaStream_t stream=nullptr) {
56  checkRuntime(cudaMemcpyAsync(host_, device_, bytes(), cudaMemcpyDeviceToHost, stream));
57  }
58 
59  void alloc_or_resize_to(size_t size) {
60  if (capacity_ < size) {
61  free_memory();
62 
63  checkRuntime(cudaMallocHost(&host_, size * sizeof(T)));
64  checkRuntime(cudaMalloc(&device_, size * sizeof(T)));
65  capacity_ = size;
66  }
67  size_ = size;
68  }
69 
70  void free_memory() {
71  if (host_ || device_) {
72  checkRuntime(cudaFreeHost(host_));
73  checkRuntime(cudaFree(device_));
74  host_ = nullptr;
75  device_ = nullptr;
76  capacity_ = 0;
77  size_ = 0;
78  }
79  }
80 
81 private:
82  T* host_ = nullptr;
83  T* device_ = nullptr;
84  size_t size_ = 0;
85  size_t capacity_ = 0;
86 };
87 
88 
89 
90 #endif // MEMORY_HPP
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: sources/includes/nvbufsurftransform.h:35
Memory::size
size_t size() const
Definition: sources/libs/nvll_osd/memory.hpp:45
Memory::copy_device_to_host
void copy_device_to_host(cudaStream_t stream=nullptr)
Definition: sources/libs/nvll_osd/memory.hpp:55
check_runtime
static bool check_runtime(cudaError_t e, const char *call, int line, const char *file)
Definition: sources/libs/nvll_osd/memory.hpp:32
checkRuntime
#define checkRuntime(call)
Definition: sources/libs/nvll_osd/memory.hpp:24
Memory::alloc_or_resize_to
void alloc_or_resize_to(size_t size)
Definition: sources/libs/nvll_osd/memory.hpp:59
Memory::bytes
size_t bytes() const
Definition: sources/libs/nvll_osd/memory.hpp:46
Memory::host
T * host() const
Definition: sources/libs/nvll_osd/memory.hpp:43
Memory::free_memory
void free_memory()
Definition: sources/libs/nvll_osd/memory.hpp:70
Memory::device
T * device() const
Definition: sources/libs/nvll_osd/memory.hpp:44
Memory::~Memory
virtual ~Memory()
Definition: sources/libs/nvll_osd/memory.hpp:48
Memory
Definition: sources/libs/nvll_osd/memory.hpp:41
Memory::copy_host_to_device
void copy_host_to_device(cudaStream_t stream=nullptr)
Definition: sources/libs/nvll_osd/memory.hpp:52