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
16 #if GOOGLE_TENSORRT
17
18 #include "tensorflow/compiler/tf2tensorrt/convert/logger_registry.h"
19
20 #include <unordered_map>
21
22 #include "tensorflow/core/lib/core/errors.h"
23 #include "tensorflow/core/platform/mutex.h"
24
25 namespace tensorflow {
26 namespace tensorrt {
27
28 class LoggerRegistryImpl : public LoggerRegistry {
Register(const string & name,nvinfer1::ILogger * logger)29 Status Register(const string& name, nvinfer1::ILogger* logger) override {
30 mutex_lock lock(mu_);
31 if (!registry_.emplace(name, std::unique_ptr<nvinfer1::ILogger>(logger))
32 .second) {
33 return errors::AlreadyExists("Logger ", name, " already registered");
34 }
35 return Status::OK();
36 }
37
LookUp(const string & name)38 nvinfer1::ILogger* LookUp(const string& name) override {
39 mutex_lock lock(mu_);
40 const auto found = registry_.find(name);
41 if (found == registry_.end()) {
42 return nullptr;
43 }
44 return found->second.get();
45 }
46
47 private:
48 mutable mutex mu_;
49 mutable std::unordered_map<string, std::unique_ptr<nvinfer1::ILogger>>
50 registry_ GUARDED_BY(mu_);
51 };
52
GetLoggerRegistry()53 LoggerRegistry* GetLoggerRegistry() {
54 static LoggerRegistryImpl* registry = new LoggerRegistryImpl;
55 return registry;
56 }
57
58 } // namespace tensorrt
59 } // namespace tensorflow
60
61 #endif // GOOGLE_TENSORRT
62 #endif // GOOGLE_CUDA
63