• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/core/platform/macros.h"
29 #include "tensorflow/core/platform/mutex.h"
30 #include "tensorflow/core/platform/thread_annotations.h"
31 #include "tensorflow/core/platform/types.h"
32 
33 namespace xla {
34 
35 // Tracks channels between computations in the XLA service. Channels
36 // are associated with a unique handle and can be resolved from the handle for
37 // later use.
38 //
39 // TODO(b/34027823): Destruct channels when all the associated computations that
40 // communicate via each channel are destructed.
41 class ChannelTracker {
42  public:
43   ChannelTracker();
44 
45   // A struct that keeps the current status of each channel. has_sender and
46   // receiver_count fields are initialized with false and 0 respectively when
47   // the struct is created and are updated by RegisterSend() and RegisterRecev()
48   // as Send or Recv instructions using the channel are requested.
49   struct Channel {
50     bool has_sender;
51     int64 receiver_count;
52     ChannelHandle::ChannelType type;
53   };
54 
55   // Creates a new Channel object and returns the corresponding
56   // ChannelHandle for it.
57   StatusOr<ChannelHandle> NewChannel(ChannelHandle::ChannelType type);
58 
59   // Informs that the given channel handle is used for a Send operation.
60   // Returns an error status if the handle is already used by another Send.
61   Status RegisterSend(const ChannelHandle& handle);
62 
63   // Informs that the given channel handle is used for a Recv operation.
64   // Returns an error status if the handle is already used by another Recv.
65   Status RegisterRecv(const ChannelHandle& handle);
66 
67  private:
68   // Bumps the next_channel_ number and returns the allocated number
69   // wrapped in a ChannelHandle.
70   ChannelHandle AllocateHandle(ChannelHandle::ChannelType type)
71       TF_EXCLUSIVE_LOCKS_REQUIRED(channel_mutex_);
72 
73   Status RegisterSendInternal(const ChannelHandle& handle)
74       TF_EXCLUSIVE_LOCKS_REQUIRED(channel_mutex_);
75 
76   Status RegisterRecvInternal(const ChannelHandle& handle)
77       TF_EXCLUSIVE_LOCKS_REQUIRED(channel_mutex_);
78 
79   // Guards the channel mapping.
80   tensorflow::mutex channel_mutex_;
81 
82   // The next sequence number to assign to a channel.
83   int64 next_channel_ TF_GUARDED_BY(channel_mutex_);
84 
85   // Mapping from ChannelHandle value to the corresponding registered
86   // Channel object.
87   absl::flat_hash_map<int64, Channel> opaque_to_channel_
88       TF_GUARDED_BY(channel_mutex_);
89 
90   TF_DISALLOW_COPY_AND_ASSIGN(ChannelTracker);
91 };
92 
93 }  // namespace xla
94 
95 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CHANNEL_TRACKER_H_
96