• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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_PROFILER_UTILS_STEP_INTERSECTION_H_
17 #define TENSORFLOW_CORE_PROFILER_UTILS_STEP_INTERSECTION_H_
18 
19 #include <algorithm>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "tensorflow/core/platform/types.h"
23 #include "tensorflow/core/profiler/protobuf/steps_db.pb.h"
24 
25 namespace tensorflow {
26 namespace profiler {
27 
28 // Description of how two step sequences are aligned.
29 struct StepsAlignment {
30   uint32 begin_subordinate_idx;  // where the alignment begins on the
31                                  // subordinate steps.
32   uint32 begin_chief_idx;  // where the alignment begins on the chief steps.
33   uint32 num_steps;        // aligned for how many steps.
34 };
35 
36 class StepIntersection {
37  public:
38   StepIntersection(
39       uint32 max_steps,
40       const absl::flat_hash_map</*host_id=*/uint32, const StepDatabaseResult*>&
41           perhost_stepdb);
42 
43   // Returns the number of steps in the intersection.
NumSteps()44   uint32 NumSteps() const { return end_chief_idx_ - begin_chief_idx_; }
45 
46   // Returns the step numbers for the destination (i.e. the intersection
47   // result).
48   std::vector<uint32> DstStepNumbers() const;
49 
50   // Returns the index to the step in the given host that corresponds to the
51   // first step in the intersection.
52   uint32 FirstStepIndex(uint32 host_id) const;
53 
54   // Returns the number of steps dropped due to the max_steps constraint
55   // specified in the constructor.
StepsDropped()56   uint32 StepsDropped() const { return steps_dropped_; }
57 
58   std::string DebugString() const;
59 
60  private:
61   absl::flat_hash_map</*host_id=*/uint32, StepsAlignment> perhost_alignment_;
62   uint32
63       chief_host_id_;  // the host whose step sequence is selected as the chief.
64   uint32 steps_dropped_;  // number of steps dropped.
65   // The begin and end indices to the chief step sequence for this step
66   // intersection. Note that the begin index is inclusive but the end index is
67   // exclusive.
68   uint32 begin_chief_idx_;
69   uint32 end_chief_idx_;
70 };
71 
72 }  // namespace profiler
73 }  // namespace tensorflow
74 
75 #endif  // TENSORFLOW_CORE_PROFILER_UTILS_STEP_INTERSECTION_H_
76