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