1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/compiler/xla/service/channel_tracker.h"
17
18#include "tensorflow/compiler/xla/ptr_util.h"
19#include "tensorflow/compiler/xla/service/hlo_computation.h"
20#include "tensorflow/compiler/xla/service/hlo_instruction.h"
21#include "tensorflow/compiler/xla/status.h"
22#include "tensorflow/compiler/xla/status_macros.h"
23#include "tensorflow/compiler/xla/types.h"
24#include "tensorflow/compiler/xla/util.h"
25#include "tensorflow/core/lib/strings/strcat.h"
26#include "tensorflow/core/platform/logging.h"
27#include "tensorflow/core/platform/mutex.h"
28#include "tensorflow/core/platform/types.h"
29
30namespace xla {
31
32ChannelTracker::ChannelTracker() : next_channel_(1) {}
33
34ChannelHandle ChannelTracker::NewChannel() {
35  tensorflow::mutex_lock lock(channel_mutex_);
36
37  // Create a new channel handle with a unique value.
38  const ChannelHandle new_handle = AllocateHandle();
39
40  // Register a channel object associated with the handle.
41  Channel channel;
42  channel.has_sender = false;
43  channel.receiver_count = 0;
44  opaque_to_channel_[new_handle.handle()] = channel;
45
46  return new_handle;
47}
48
49Status ChannelTracker::RegisterSend(const ChannelHandle& handle) {
50  tensorflow::mutex_lock lock(channel_mutex_);
51  return RegisterSendInternal(handle);
52}
53
54Status ChannelTracker::RegisterRecv(const ChannelHandle& handle) {
55  tensorflow::mutex_lock lock(channel_mutex_);
56  return RegisterRecvInternal(handle);
57}
58
59ChannelHandle ChannelTracker::AllocateHandle() {
60  int64 handle_value = next_channel_++;
61  ChannelHandle result;
62  result.set_handle(handle_value);
63  return result;
64}
65
66Status ChannelTracker::RegisterSendInternal(const ChannelHandle& handle) {
67  if (opaque_to_channel_.count(handle.handle()) == 0) {
68    return NotFound("channel handle not found: %lld", handle.handle());
69  }
70  Channel& channel = opaque_to_channel_[handle.handle()];
71  if (channel.has_sender) {
72    return FailedPrecondition(
73        "when registering send, passed a channel handle that is already used "
74        "by a sender: %lld",
75        handle.handle());
76  }
77  channel.has_sender = true;
78  return Status::OK();
79}
80
81Status ChannelTracker::RegisterRecvInternal(const ChannelHandle& handle) {
82  if (opaque_to_channel_.count(handle.handle()) == 0) {
83    return NotFound("channel handle not found: %lld", handle.handle());
84  }
85  Channel& channel = opaque_to_channel_[handle.handle()];
86  // TODO(b/33942691): Allow more than 1 receivers for broadcast.
87  if (channel.receiver_count >= 1) {
88    return FailedPrecondition(
89        "when registering recv, passed a channel handle that is already used "
90        "by a receiver: %lld",
91        handle.handle());
92  }
93  channel.receiver_count += 1;
94  return Status::OK();
95}
96
97}  // namespace xla
98