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/cpu/xfeed_manager.h"
17
18#include "tensorflow/compiler/xla/shape_util.h"
19#include "tensorflow/core/platform/logging.h"
20
21namespace xla {
22namespace cpu {
23namespace runtime {
24
25void XfeedManager::Reset() {
26  infeed()->Reset();
27  outfeed()->Reset();
28}
29
30void XfeedQueueManager::Reset() {
31  tensorflow::mutex_lock l(mu_);
32  CHECK(current_buffer_ == nullptr);
33  for (auto buffer : enqueued_buffers_) {
34    buffer->Done(ShapeUtil::MakeNil());
35  }
36  enqueued_buffers_.clear();
37}
38
39void XfeedQueueManager::EnqueueBuffersAtomically(
40    tensorflow::gtl::ArraySlice<XfeedBuffer*> buffers) {
41  tensorflow::mutex_lock l(mu_);
42  bool was_empty = enqueued_buffers_.empty();
43  for (XfeedBuffer* b : buffers) {
44    VLOG(3) << "Enqueueing " << queue_name_ << " buffer (of " << buffers.size()
45            << " buffers) with length: " << b->length();
46    enqueued_buffers_.push_back(b);
47  }
48  if (was_empty && !buffers.empty()) {
49    // This has the potential to suffer from the notified thread
50    // immediately trying and failing to acquire mu_, but seems
51    // preferable to the alternative of notifying outside the lock
52    // on every enqueue.
53    cv_.notify_one();
54  }
55}
56
57XfeedBuffer* XfeedQueueManager::BlockingDequeueBuffer() {
58  tensorflow::mutex_lock l(mu_);
59  VLOG(3) << "Waiting for an available buffer.";
60  while (enqueued_buffers_.empty()) {
61    cv_.wait(l);
62  }
63  VLOG(3) << "A buffer is available!";
64  CHECK(current_buffer_ == nullptr);
65  current_buffer_ = enqueued_buffers_.front();
66  enqueued_buffers_.pop_front();
67  return current_buffer_;
68}
69
70void XfeedQueueManager::ReleaseCurrentBuffer(int32 length, void* data,
71                                             StatusOr<Shape> shape) {
72  VLOG(3) << "Releasing buffer with shape: "
73          << (shape.ok() ? ShapeUtil::HumanString(shape.ValueOrDie())
74                         : "<error status>");
75  tensorflow::mutex_lock l(mu_);
76  CHECK(current_buffer_ != nullptr);
77  CHECK_EQ(length, current_buffer_->length());
78  CHECK_EQ(data, current_buffer_->data());
79  current_buffer_->Done(std::move(shape));
80  current_buffer_ = nullptr;
81}
82
83}  // namespace runtime
84}  // namespace cpu
85}  // namespace xla
86