• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 
16 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_COST_MEASUREMENT_REGISTRY_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_COST_MEASUREMENT_REGISTRY_H_
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/strings/string_view.h"
25 #include "tensorflow/core/common_runtime/cost_measurement.h"
26 
27 namespace tensorflow {
28 
29 // CostMeasurementRegistry allows to
30 // - register a CostMeasurement type to the global map
31 // - create an instance of registered CostMeasurement.
32 class CostMeasurementRegistry {
33  public:
34   // Creates an instance of registered CostMeasurement by name. If the named
35   // CostMeasurement is not registered yet, returns nullptr. Any returned
36   // std::unique_ptr<CostMeasurement> should not be moved.
37   // TODO(b/185852990): create a non-moveable wrapper class for the returned
38   // unique_ptr<CostMeasurement>.
39   static std::unique_ptr<CostMeasurement> CreateByNameOrNull(
40       const std::string& name);
41 
42   using Creator = std::function<std::unique_ptr<CostMeasurement>()>;
43 
44   // Registers a CostMeasurement type to the global map. Registering different
45   // types of CostMeasurement with the same name is prohibited.
46   static void RegisterCostMeasurement(absl::string_view name, Creator creator);
47 };
48 
49 // Registers a CostMeasurement type to the global map. Registering different
50 // types of CostMeasurement with the same name is prohibited.
51 class CostMeasurementRegistrar {
52  public:
CostMeasurementRegistrar(absl::string_view name,CostMeasurementRegistry::Creator creator)53   explicit CostMeasurementRegistrar(absl::string_view name,
54                                     CostMeasurementRegistry::Creator creator) {
55     CostMeasurementRegistry::RegisterCostMeasurement(name, std::move(creator));
56   }
57 };
58 
59 #define REGISTER_COST_MEASUREMENT(name, MyCostMeasurementClass) \
60   namespace {                                                   \
61   static ::tensorflow::CostMeasurementRegistrar                 \
62       MyCostMeasurementClass##_registrar((name), [] {           \
63         return absl::make_unique<MyCostMeasurementClass>();     \
64       });                                                       \
65   }  // namespace
66 
67 }  // namespace tensorflow
68 
69 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_COST_MEASUREMENT_REGISTRY_H_
70