NVIDIA DeepStream SDK API Reference

8.0 Release
memory.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #ifndef MEMORY_HPP
14 #define MEMORY_HPP
15 
16 #include <cuda_runtime.h>
17 #include <stdio.h>
18 
19 #define checkRuntime(call) check_runtime(call, #call, __LINE__, __FILE__)
20 
21 #define CUOSD_PRINT_E(f_, ...) \
22  fprintf(stderr, "[cuOSD Error] at %s:%d : " f_, (const char*)__FILE__, __LINE__, ##__VA_ARGS__)
23 
24 #define CUOSD_PRINT_W(f_, ...) \
25  printf("[cuOSD Warning] at %s:%d : " f_, (const char*)__FILE__, __LINE__, ##__VA_ARGS__)
26 
27 static bool inline check_runtime(cudaError_t e, const char* call, int line, const char *file) {
28  if (e != cudaSuccess) {
29  fprintf(stderr, "CUDA Runtime error %s # %s, code = %s [ %d ] in file %s:%d\n", call, cudaGetErrorString(e), cudaGetErrorName(e), e, file, line);
30  return false;
31  }
32  return true;
33 }
34 
35 template<typename T>
36 class Memory{
37 public:
38  T* host() const{return host_;}
39  T* device() const{return device_;}
40  size_t size() const{return size_;}
41  size_t bytes() const{return size_ * sizeof(T);}
42 
43  virtual ~Memory() {
44  free_memory();
45  }
46 
47  void copy_host_to_device(cudaStream_t stream=nullptr) {
48  checkRuntime(cudaMemcpyAsync(device_, host_, bytes(), cudaMemcpyHostToDevice, stream));
49  }
50  void copy_device_to_host(cudaStream_t stream=nullptr) {
51  checkRuntime(cudaMemcpyAsync(host_, device_, bytes(), cudaMemcpyDeviceToHost, stream));
52  }
53 
54  void alloc_or_resize_to(size_t size) {
55  if (capacity_ < size) {
56  free_memory();
57 
58  checkRuntime(cudaMallocHost(&host_, size * sizeof(T)));
59  checkRuntime(cudaMalloc(&device_, size * sizeof(T)));
60  capacity_ = size;
61  }
62  size_ = size;
63  }
64 
65  void free_memory() {
66  if (host_ || device_) {
67  checkRuntime(cudaFreeHost(host_));
68  checkRuntime(cudaFree(device_));
69  host_ = nullptr;
70  device_ = nullptr;
71  capacity_ = 0;
72  size_ = 0;
73  }
74  }
75 
76 private:
77  T* host_ = nullptr;
78  T* device_ = nullptr;
79  size_t size_ = 0;
80  size_t capacity_ = 0;
81 };
82 
83 
84 
85 #endif // MEMORY_HPP
Memory::size
size_t size() const
Definition: memory.hpp:40
Memory::copy_device_to_host
void copy_device_to_host(cudaStream_t stream=nullptr)
Definition: memory.hpp:50
checkRuntime
#define checkRuntime(call)
Definition: memory.hpp:19
Memory::alloc_or_resize_to
void alloc_or_resize_to(size_t size)
Definition: memory.hpp:54
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: nvbufsurftransform.h:35
check_runtime
static bool check_runtime(cudaError_t e, const char *call, int line, const char *file)
Definition: memory.hpp:27
Memory::bytes
size_t bytes() const
Definition: memory.hpp:41
Memory::host
T * host() const
Definition: memory.hpp:38
Memory::free_memory
void free_memory()
Definition: memory.hpp:65
Memory::device
T * device() const
Definition: memory.hpp:39
Memory::~Memory
virtual ~Memory()
Definition: memory.hpp:43
Memory
Definition: memory.hpp:36
Memory::copy_host_to_device
void copy_host_to_device(cudaStream_t stream=nullptr)
Definition: memory.hpp:47