1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | #ifndef GLOW_ONNXIFI_GLOWONNXIFIMANAGER_H |
17 | #define GLOW_ONNXIFI_GLOWONNXIFIMANAGER_H |
18 | |
19 | #include "Base.h" |
20 | #include "HostManagerOnnxifi.h" |
21 | #include "InlineOnnxifi.h" |
22 | |
23 | #include <mutex> |
24 | #include <unordered_set> |
25 | |
26 | namespace glow { |
27 | namespace onnxifi { |
28 | /// Singleton class for creating and destroying objects for the ONNXIFI |
29 | /// interface. GlowOnnxifiManager tracks objects it has created and can be used |
30 | /// to check if an ONNXIFI interface object was created by glow or not. |
31 | class GlowOnnxifiManager final { |
32 | public: |
33 | /// Get a reference to the GlowOnnxifiManager singleton. There should only |
34 | /// ever be one GlowOnnxifiManager. |
35 | static GlowOnnxifiManager &get(); |
36 | |
37 | /// Disallow copying and moving GlowOnnxifiManager to help enforce singleton |
38 | /// pattern. |
39 | GlowOnnxifiManager(const GlowOnnxifiManager &) = delete; |
40 | GlowOnnxifiManager(GlowOnnxifiManager &&) = delete; |
41 | GlowOnnxifiManager &operator=(const GlowOnnxifiManager &) = delete; |
42 | GlowOnnxifiManager &operator=(GlowOnnxifiManager &&) = delete; |
43 | |
44 | /// Create a new glow Backend for backend \p backendName using onnx graphs |
45 | /// if \p useOnnx and caffe2 graphs otherwise. If \p forQuantization is true |
46 | /// then a Backend will be created otherwise a HostManagerBackend will be |
47 | /// be created. |
48 | /// Can be called safely by multiple threads concurrently. |
49 | BackendPtr createBackend(llvm::StringRef backendName, bool useOnnx, |
50 | bool forQuantization = false); |
51 | |
52 | /// Create a new glow Event. |
53 | /// Can be called safely by multiple threads concurrently. |
54 | EventPtr createEvent(); |
55 | |
56 | /// Create a new glow Graph associated with \p backend. |
57 | /// Can be called safely by multiple threads concurrently. |
58 | GraphPtr |
59 | createGraph(BackendPtr backend, |
60 | QuantizationMode quantizationMode = QuantizationMode::None); |
61 | |
62 | /// Check if \p backend is a Backend created and managed by glow. |
63 | /// Can be called safely by multiple threads concurrently. |
64 | bool isValid(BackendPtr backend) const; |
65 | |
66 | /// Check if \p event is a Event created and managed by glow. |
67 | /// Can be called safely by multiple threads concurrently. |
68 | bool isValid(EventPtr event) const; |
69 | |
70 | /// Check if \p graph is a Graph created and managed by glow. |
71 | /// Can be called safely by multiple threads concurrently. |
72 | bool isValid(GraphPtr graph) const; |
73 | |
74 | /// Free \p backend. |
75 | /// Can be called safely by multiple threads concurrently. |
76 | void release(BackendPtr backend); |
77 | |
78 | /// Free \p event. |
79 | /// Can be called safely by multiple threads concurrently. |
80 | void release(EventPtr event); |
81 | |
82 | /// Free \p graph. |
83 | /// Can be called safely by multiple threads concurrently. |
84 | void release(GraphPtr graph); |
85 | |
86 | private: |
87 | GlowOnnxifiManager() = default; |
88 | |
89 | /// Create a new HostManager managing backends of kind \p backendName or get |
90 | /// an existing HostManager for the backendName if one exists. |
91 | /// NOTE: This method is not thread safe, the caller should be holding the |
92 | /// mutex m_ when calling it! |
93 | std::shared_ptr<runtime::HostManager> |
94 | getOrCreateHostManager(llvm::StringRef backendName); |
95 | |
96 | /// The set of all valid glow Backends. |
97 | std::unordered_set<BackendPtr> backends_; |
98 | |
99 | /// The set of all valid glow Events. |
100 | std::unordered_set<EventPtr> events_; |
101 | |
102 | /// The set of all valid glow Graphs. |
103 | std::unordered_set<GraphPtr> graphs_; |
104 | |
105 | /// Map from backend name to HostManager managing devices of that backend that |
106 | /// is shared by all Backends using that HostManager. HostManager is stored |
107 | /// as weak_ptr here so that it will be destructed when the last Backend |
108 | /// using it is destroyed not when this singleton is destroyed. |
109 | std::map<std::string, std::weak_ptr<runtime::HostManager>> hostManagers_; |
110 | |
111 | /// Mutex that protects all members of GlowOnnxifiManager. |
112 | /// TODO: can use one mutex per set if performance becomes an issue. |
113 | mutable std::mutex m_; |
114 | }; |
115 | |
116 | } // namespace onnxifi |
117 | } // namespace glow |
118 | |
119 | #endif // GLOW_ONNXIFI_GLOWONNXIFIMANAGER_H |
120 | |