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 #include "tensorflow/compiler/xla/service/channel_tracker.h"
17
18 #include "absl/memory/memory.h"
19 #include "absl/strings/str_cat.h"
20 #include "tensorflow/compiler/xla/service/hlo_computation.h"
21 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
22 #include "tensorflow/compiler/xla/status.h"
23 #include "tensorflow/compiler/xla/status_macros.h"
24 #include "tensorflow/compiler/xla/types.h"
25 #include "tensorflow/compiler/xla/util.h"
26 #include "tensorflow/core/platform/logging.h"
27 #include "tensorflow/core/platform/mutex.h"
28 #include "tensorflow/core/platform/types.h"
29
30 namespace xla {
31
ChannelTracker()32 ChannelTracker::ChannelTracker() : next_channel_(1) {}
33
NewChannel(ChannelHandle::ChannelType type)34 StatusOr<ChannelHandle> ChannelTracker::NewChannel(
35 ChannelHandle::ChannelType type) {
36 if (type != ChannelHandle::DEVICE_TO_DEVICE &&
37 type != ChannelHandle::HOST_TO_DEVICE &&
38 type != ChannelHandle::DEVICE_TO_HOST) {
39 return InvalidArgument("Invalid channel type: %d", type);
40 }
41 tensorflow::mutex_lock lock(channel_mutex_);
42
43 // Create a new channel handle with a unique value.
44 ChannelHandle new_handle = AllocateHandle(type);
45
46 // Register a channel object associated with the handle.
47 Channel channel;
48 channel.has_sender = false;
49 channel.receiver_count = 0;
50 channel.type = type;
51 opaque_to_channel_[new_handle.handle()] = channel;
52
53 return new_handle;
54 }
55
RegisterSend(const ChannelHandle & handle)56 Status ChannelTracker::RegisterSend(const ChannelHandle& handle) {
57 tensorflow::mutex_lock lock(channel_mutex_);
58 return RegisterSendInternal(handle);
59 }
60
RegisterRecv(const ChannelHandle & handle)61 Status ChannelTracker::RegisterRecv(const ChannelHandle& handle) {
62 tensorflow::mutex_lock lock(channel_mutex_);
63 return RegisterRecvInternal(handle);
64 }
65
AllocateHandle(ChannelHandle::ChannelType type)66 ChannelHandle ChannelTracker::AllocateHandle(ChannelHandle::ChannelType type) {
67 int64 handle_value = next_channel_++;
68 ChannelHandle result;
69 result.set_handle(handle_value);
70 result.set_type(type);
71 return result;
72 }
73
RegisterSendInternal(const ChannelHandle & handle)74 Status ChannelTracker::RegisterSendInternal(const ChannelHandle& handle) {
75 if (!opaque_to_channel_.contains(handle.handle())) {
76 return NotFound("channel handle not found: %d", handle.handle());
77 }
78 Channel& channel = opaque_to_channel_[handle.handle()];
79 if (channel.type == ChannelHandle::HOST_TO_DEVICE) {
80 return FailedPrecondition(
81 "host-to-device channels cannot be used with a Send operation; "
82 "channel handle: %d",
83 handle.handle());
84 }
85
86 if (channel.has_sender) {
87 return FailedPrecondition(
88 "when registering send, passed a channel handle that is already used "
89 "by a sender: %d",
90 handle.handle());
91 }
92 channel.has_sender = true;
93 return Status::OK();
94 }
95
RegisterRecvInternal(const ChannelHandle & handle)96 Status ChannelTracker::RegisterRecvInternal(const ChannelHandle& handle) {
97 if (!opaque_to_channel_.contains(handle.handle())) {
98 return NotFound("channel handle not found: %d", handle.handle());
99 }
100 Channel& channel = opaque_to_channel_[handle.handle()];
101 if (channel.type == ChannelHandle::DEVICE_TO_HOST) {
102 return FailedPrecondition(
103 "device-to-host channels cannot be used with a Recv operation; "
104 "channel handle: %d",
105 handle.handle());
106 }
107
108 // TODO(b/33942691): Allow more than 1 receivers for broadcast.
109 if (channel.receiver_count >= 1) {
110 return FailedPrecondition(
111 "when registering recv, passed a channel handle that is already used "
112 "by a receiver: %d",
113 handle.handle());
114 }
115 channel.receiver_count += 1;
116 return Status::OK();
117 }
118
119 } // namespace xla
120