NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
ZlibResourceAggregator.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_ZLIBRESOURCEAGGREGATOR_H
27 #define NVNEURAL_ZLIBRESOURCEAGGREGATOR_H
28 
29 #include <nvneural/CodeGenTypes.h>
30 #include <nvneural/RefObject.h>
31 #include <string>
32 #include <unordered_map>
33 #include <vector>
34 
35 namespace nvneural {
36 
40 class ZlibResourceAggregator : public refobj::RefObjectBase<refobj::Implements<IResourceAggregator>>
41 {
42 public:
44  NeuralResult addResource(IResourceAggregator::ResourceId* pResourceIdOut, const void* pBuffer, std::uint32_t bufferBytes) noexcept override;
45 
47  NeuralResult finalize() noexcept override;
48 
50  std::size_t resourceIdCount() const noexcept override;
52  IResourceAggregator::ResourceId resourceIdByIndex(std::size_t resourceIndex) const noexcept override;
54  std::size_t resourceBufferSizeByIndex(std::size_t resourceIndex) const noexcept override;
56  const void* resourceBufferByIndex(std::size_t resourceIndex) const noexcept override;
57 
58 private:
59  using ByteBuffer = std::string; // Note: Using std::string as a hashable std::vector<uint8_t>
60  std::unordered_map<ByteBuffer, ResourceId> m_resources; // buffer -> ID, prevents duplication
61 
62  std::vector<std::pair<std::vector<uint8_t>, ResourceId>> m_finalizedResources; // only filled in after finalize()
63 
64  IResourceAggregator::ResourceId generateNewId() const;
65 };
66 
67 } // namespace nvneural
68 
69 #endif // NVNEURAL_ZLIBRESOURCEAGGREGATOR_H
Definitions of C++ code generation interfaces exported by tools and plugins.
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
Standard implementation for IRefObject-derived objects.
IResourceDictionary::ResourceId ResourceId
Unique identifier for a resource.
Definition: CodeGenTypes.h:65
Resource aggregator that compresses payloads using Zlib.
Definition: ZlibResourceAggregator.h:41
const void * resourceBufferByIndex(std::size_t resourceIndex) const noexcept
Returns a pointer to the Nth finalized resource buffer in the object.
Definition: ZlibResourceAggregator.cpp:143
NeuralResult finalize() noexcept
Finalizes the resource collection, allowing for compression and serialization to take place.
Definition: ZlibResourceAggregator.cpp:81
NeuralResult addResource(IResourceAggregator::ResourceId *pResourceIdOut, const void *pBuffer, std::uint32_t bufferBytes) noexcept
Adds a resource to the dictionary, avoiding duplicates.
Definition: ZlibResourceAggregator.cpp:31
std::size_t resourceBufferSizeByIndex(std::size_t resourceIndex) const noexcept
Returns the byte count associated with the Nth finalized resource buffer in the object.
Definition: ZlibResourceAggregator.cpp:134
IResourceAggregator::ResourceId resourceIdByIndex(std::size_t resourceIndex) const noexcept
Returns the Nth resource ID in the object.
Definition: ZlibResourceAggregator.cpp:125
std::size_t resourceIdCount() const noexcept
Returns the number of finalized resources in the object.
Definition: ZlibResourceAggregator.cpp:120
Parameterized base class implementing common IRefObject operations.
Definition: RefObject.h:336