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/cpu/xfeed_manager.h"
17
18 #include "tensorflow/compiler/xla/shape_util.h"
19 #include "tensorflow/core/platform/logging.h"
20
21 namespace xla {
22 namespace cpu {
23 namespace runtime {
24
Reset()25 void XfeedManager::Reset() {
26 infeed()->Reset();
27 outfeed()->Reset();
28 }
29
Reset()30 void XfeedQueueManager::Reset() {
31 absl::MutexLock 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
EnqueueBuffersAtomically(absl::Span<XfeedBuffer * const> buffers)39 void XfeedQueueManager::EnqueueBuffersAtomically(
40 absl::Span<XfeedBuffer* const> buffers) {
41 absl::MutexLock 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_.Signal();
54 }
55 }
56
BlockingDequeueBuffer()57 XfeedBuffer* XfeedQueueManager::BlockingDequeueBuffer() {
58 absl::MutexLock l(&mu_);
59 VLOG(3) << "Waiting for an available buffer.";
60 while (enqueued_buffers_.empty()) {
61 cv_.Wait(&mu_);
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
ReleaseCurrentBuffer(int32_t length,void * data,StatusOr<Shape> shape)70 void XfeedQueueManager::ReleaseCurrentBuffer(int32_t 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 absl::MutexLock 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
GetByteSizeRequirement(const Shape & shape,int64_t pointer_size)83 int64_t GetByteSizeRequirement(const Shape& shape, int64_t pointer_size) {
84 if (shape.is_static() || shape.IsTuple()) {
85 return ShapeUtil::ByteSizeOf(shape, pointer_size);
86 }
87 int64_t metadata_size = sizeof(int32_t) * shape.dimensions_size();
88 return ShapeUtil::ByteSizeOf(shape, pointer_size) + metadata_size;
89 }
90
91 } // namespace runtime
92 } // namespace cpu
93 } // namespace xla
94