xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/channel_tracker.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2017 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_COMPILER_XLA_SERVICE_CHANNEL_TRACKER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_CHANNEL_TRACKER_H_
18 
19 #include <map>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/types/span.h"
23 #include "tensorflow/compiler/xla/service/hlo_module.h"
24 #include "tensorflow/compiler/xla/status.h"
25 #include "tensorflow/compiler/xla/statusor.h"
26 #include "tensorflow/compiler/xla/types.h"
27 #include "tensorflow/compiler/xla/xla_data.pb.h"
28 
29 namespace xla {
30 
31 // Tracks channels between computations in the XLA service. Channels
32 // are associated with a unique handle and can be resolved from the handle for
33 // later use.
34 //
35 // TODO(b/34027823): Destruct channels when all the associated computations that
36 // communicate via each channel are destructed.
37 class ChannelTracker {
38  public:
39   ChannelTracker();
40 
41   // A struct that keeps the current status of each channel. has_sender and
42   // receiver_count fields are initialized with false and 0 respectively when
43   // the struct is created and are updated by RegisterSend() and RegisterRecev()
44   // as Send or Recv instructions using the channel are requested.
45   struct Channel {
46     bool has_sender;
47     int64_t receiver_count;
48     ChannelHandle::ChannelType type;
49   };
50 
51   // Creates a new Channel object and returns the corresponding
52   // ChannelHandle for it.
53   StatusOr<ChannelHandle> NewChannel(ChannelHandle::ChannelType type);
54 
55   // Informs that the given channel handle is used for a Send operation.
56   // Returns an error status if the handle is already used by another Send.
57   Status RegisterSend(const ChannelHandle& handle);
58 
59   // Informs that the given channel handle is used for a Recv operation.
60   // Returns an error status if the handle is already used by another Recv.
61   Status RegisterRecv(const ChannelHandle& handle);
62 
63  private:
64   // Bumps the next_channel_ number and returns the allocated number
65   // wrapped in a ChannelHandle.
66   ChannelHandle AllocateHandle(ChannelHandle::ChannelType type)
67       ABSL_EXCLUSIVE_LOCKS_REQUIRED(channel_mutex_);
68 
69   Status RegisterSendInternal(const ChannelHandle& handle)
70       ABSL_EXCLUSIVE_LOCKS_REQUIRED(channel_mutex_);
71 
72   Status RegisterRecvInternal(const ChannelHandle& handle)
73       ABSL_EXCLUSIVE_LOCKS_REQUIRED(channel_mutex_);
74 
75   // Guards the channel mapping.
76   absl::Mutex channel_mutex_;
77 
78   // The next sequence number to assign to a channel.
79   int64_t next_channel_ ABSL_GUARDED_BY(channel_mutex_);
80 
81   // Mapping from ChannelHandle value to the corresponding registered
82   // Channel object.
83   absl::flat_hash_map<int64_t, Channel> opaque_to_channel_
84       ABSL_GUARDED_BY(channel_mutex_);
85 
86   ChannelTracker(const ChannelTracker&) = delete;
87   ChannelTracker& operator=(const ChannelTracker&) = delete;
88 };
89 
90 }  // namespace xla
91 
92 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CHANNEL_TRACKER_H_
93