1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #if GOOGLE_CUDA && GOOGLE_TENSORRT
16
17 #include "tensorflow/compiler/tf2tensorrt/convert/logger_registry.h"
18
19 #include <unordered_map>
20
21 #include "tensorflow/core/lib/core/errors.h"
22 #include "tensorflow/core/platform/mutex.h"
23
24 namespace tensorflow {
25 namespace tensorrt {
26
27 class LoggerRegistryImpl : public LoggerRegistry {
Register(const string & name,nvinfer1::ILogger * logger)28 Status Register(const string& name, nvinfer1::ILogger* logger) override {
29 mutex_lock lock(mu_);
30 if (!registry_.emplace(name, std::unique_ptr<nvinfer1::ILogger>(logger))
31 .second) {
32 return errors::AlreadyExists("Logger ", name, " already registered");
33 }
34 return Status::OK();
35 }
36
LookUp(const string & name)37 nvinfer1::ILogger* LookUp(const string& name) override {
38 mutex_lock lock(mu_);
39 const auto found = registry_.find(name);
40 if (found == registry_.end()) {
41 return nullptr;
42 }
43 return found->second.get();
44 }
45
46 private:
47 mutable mutex mu_;
48 mutable std::unordered_map<string, std::unique_ptr<nvinfer1::ILogger>>
49 registry_ TF_GUARDED_BY(mu_);
50 };
51
GetLoggerRegistry()52 LoggerRegistry* GetLoggerRegistry() {
53 static LoggerRegistryImpl* registry = new LoggerRegistryImpl;
54 return registry;
55 }
56
57 } // namespace tensorrt
58 } // namespace tensorflow
59
60 #endif // GOOGLE_CUDA && GOOGLE_TENSORRT
61