Compute Graph Framework SDK Reference  5.22
Buffer.hpp
Go to the documentation of this file.
1
2// This code contains NVIDIA Confidential Information and is disclosed
3// under the Mutual Non-Disclosure Agreement.
4//
5// Notice
6// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
7// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
8// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
9// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10//
11// NVIDIA Corporation assumes no responsibility for the consequences of use of such
12// information or for any infringement of patents or other rights of third parties that may
13// result from its use. No license is granted by implication or otherwise under any patent
14// or patent rights of NVIDIA Corporation. No third party distribution is allowed unless
15// expressly authorized by NVIDIA. Details are subject to change without notice.
16// This code supersedes and replaces all information previously supplied.
17// NVIDIA Corporation products are not authorized for use as critical
18// components in life support devices or systems without express written approval of
19// NVIDIA Corporation.
20//
21// Copyright (c) 2021-2024 NVIDIA Corporation. All rights reserved.
22//
23// NVIDIA Corporation and its licensors retain all intellectual property and proprietary
24// rights in and to this software and related documentation and any modifications thereto.
25// Any use, reproduction, disclosure or distribution of this software and related
26// documentation without an express license agreement from NVIDIA Corporation is
27// strictly prohibited.
28//
30
31#ifndef DW_FRAMEWORK_BUFFER_HPP_
32#define DW_FRAMEWORK_BUFFER_HPP_
33
34#include <cstddef>
35#include <nvscibuf.h>
36#include <dwshared/dwfoundation/dw/core/language/Optional.hpp>
37#include <dwshared/dwfoundation/dw/core/logger/Logger.hpp>
38#include <dwshared/dwfoundation/dw/cuda/misc/DevicePtr.hpp>
39
40namespace dw
41{
42namespace framework
43{
44
45enum class BufferBackendType : uint32_t
46{
47 CPU = 0,
48 CUDA = 1,
49 // Add others as appropriate
50};
51
52using BufferFlags = uint32_t;
53
55{
56 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
57 // coverity[cert_int34_c_violation]
58 return 0U != (static_cast<uint32_t>(flags) & (1U << static_cast<uint32_t>(type)));
59}
60
62{
63 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
64 // coverity[cert_int34_c_violation]
65 flags |= (1U << static_cast<uint32_t>(type));
66}
67
69{
71 size_t byteSize;
72};
73
74// coverity[autosar_cpp14_m3_4_1_violation] RFD Pending: TID-2586
76{
77public:
78 // coverity[autosar_cpp14_a0_1_1_violation]
79 // coverity[autosar_cpp14_a2_10_5_violation]
80 static constexpr char LOG_TAG[]{"Buffer"};
81
82 explicit BufferBase(BufferProperties properties);
83
84 virtual ~BufferBase() = default;
85
86 // coverity[autosar_cpp14_a2_10_5_violation]
88
89 virtual void bindNvSciBufObj(NvSciBufObj bufObj);
90
91 virtual void fillNvSciBufAttrs(NvSciBufAttrList attrList) const;
92
93 NvSciBufObj getNvSci();
94
95protected:
97 NvSciBufObj m_bufObj{};
98};
99
100class BufferCPU final : public BufferBase
101{
102public:
104
105 void fillSpecificAttributes(NvSciBufAttrList attrList) const;
106
107 void bindNvSciBufObj(NvSciBufObj bufObj) override;
108
109 void* getCpuPtr(size_t offset);
110
111private:
112 void* m_ptr{nullptr};
113};
114
115// BufferCUDA class encapsulates API mapping operations on single nvscibuf for cuda.
116// @note - If a buffer needs to be mapped for multiple cuda devices, multiple instances of this class
117// should be used and the appropriate device must be set as current before calling into each instance.
118class BufferCUDA final : public BufferBase
119{
120public:
121#ifdef DW_IS_SAFETY
122 explicit BufferCUDA(BufferProperties properties);
123#else
125#endif
126
127 ~BufferCUDA() final;
128
129 void fillSpecificAttributes(NvSciBufAttrList attrList) const;
130
131 void bindNvSciBufObj(NvSciBufObj bufObj) override;
132
133 core::DevicePtr<void> getCudaPtr(size_t offset);
134
135private:
136 cudaExternalMemory_t m_cudaHandle{};
137 void* m_ptr{nullptr};
138};
139
140// Buffer class encapsulates API mapping operations on single nvscibuf.
141// @note - If a buffer needs to be mapped for multiple cuda devices, multiple instances of this class
142// should be used and the appropriate device must be set as current before calling into each instance.
143// coverity[autosar_cpp14_a0_1_6_violation]
144// coverity[autosar_cpp14_a12_1_6_violation] FP: nvbugs/4016780
145class Buffer final : public BufferBase
146{
147
148public:
149 explicit Buffer(BufferProperties properties);
150
151 void bindNvSciBufObj(NvSciBufObj bufObj) override;
152
153 void fillNvSciBufAttrs(NvSciBufAttrList attrList) const override;
154
155 void* getCpuPtr(size_t offset);
156
157 core::DevicePtr<void> getCudaPtr(size_t offset);
158
159 bool hasCpuPtr() const;
160
161 bool hasCudaPtr() const;
162
163private:
164 dw::core::Optional<BufferCPU> m_bufferCpu{};
165 dw::core::Optional<BufferCUDA> m_bufferCuda{};
166};
167
168// Debug logger stream overload for Buffer.
169// Usage from other namespaces:
170// auto logstream = LOGSTREAM_INFO(this);
171// dw::dnn::operator<<(logstream, buffer) << Logger::State::endl;
172dw::core::Logger::LoggerStream& operator<<(
173 dw::core::Logger::LoggerStream& out, Buffer& buffer);
174// Debug logger stream overload for BufferProperties.
175// Usage from other namespaces:
176// auto logstream = LOGSTREAM_INFO(this);
177// dw::dnn::operator<<(logstream, bufferProperties) << Logger::State::endl;
178dw::core::Logger::LoggerStream& operator<<(
179 dw::core::Logger::LoggerStream& out, BufferProperties const& bufferProperties);
180
181} // namespace framework
182} // namespace dw
183
184#endif // DW_FRAMEWORK_BUFFER_HPP_
const BufferProperties & getProperties() const
virtual ~BufferBase()=default
NvSciBufObj m_bufObj
Definition: Buffer.hpp:97
BufferBase(BufferProperties properties)
virtual void bindNvSciBufObj(NvSciBufObj bufObj)
virtual void fillNvSciBufAttrs(NvSciBufAttrList attrList) const
static constexpr char LOG_TAG[]
Definition: Buffer.hpp:80
BufferProperties m_properties
Definition: Buffer.hpp:96
void * getCpuPtr(size_t offset)
void bindNvSciBufObj(NvSciBufObj bufObj) override
void fillSpecificAttributes(NvSciBufAttrList attrList) const
void fillSpecificAttributes(NvSciBufAttrList attrList) const
core::DevicePtr< void > getCudaPtr(size_t offset)
void bindNvSciBufObj(NvSciBufObj bufObj) override
void * getCpuPtr(size_t offset)
void fillNvSciBufAttrs(NvSciBufAttrList attrList) const override
void bindNvSciBufObj(NvSciBufObj bufObj) override
bool hasCudaPtr() const
Buffer(BufferProperties properties)
bool hasCpuPtr() const
core::DevicePtr< void > getCudaPtr(size_t offset)
void BufferFlagsEnableBackend(BufferFlags &flags, BufferBackendType type)
Definition: Buffer.hpp:61
bool BufferFlagsBackendEnabled(BufferFlags flags, BufferBackendType type)
Definition: Buffer.hpp:54
uint32_t BufferFlags
Definition: Buffer.hpp:52
dw::core::Logger::LoggerStream & operator<<(dw::core::Logger::LoggerStream &out, Buffer &buffer)
Definition: Buffer.hpp:41