aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/gpu/infeed_manager.cc
blob: ae310beefad0c81c17fd4140b441b3a19a002e2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/gpu/infeed_manager.h"

#include "tensorflow/compiler/xla/map_util.h"
#include "tensorflow/compiler/xla/ptr_util.h"
#include "tensorflow/core/platform/logging.h"

namespace xla {
namespace gpu {

InfeedManager::InfeedManager() : host_to_device_executor_(nullptr) {}

void InfeedManager::Reset() {
  tensorflow::mutex_lock l(mu_);
  CHECK(dequeued_buffer_.empty());
  for (auto buffer : enqueued_buffer_) {
    buffer->Done();
  }
  enqueued_buffer_.clear();
}

void InfeedManager::EnqueueBuffers(const std::vector<InfeedBuffer*>& buffers) {
  tensorflow::mutex_lock l(mu_);
  bool was_empty = enqueued_buffer_.empty();
  for (gpu::InfeedBuffer* b : buffers) {
    enqueued_buffer_.push_back(b);
  }
  if (was_empty) {
    // This has the potential to suffer from the notified thread
    // immediately trying and failing to acquire mu_, but seems
    // preferable to the alternative of notifying outside the lock
    // on every enqueue.
    cv_.notify_one();
  }
}

InfeedBuffer* InfeedManager::BlockingDequeueBuffer() {
  bool became_empty = false;
  InfeedBuffer* current_buffer;
  {
    tensorflow::mutex_lock l(mu_);
    while (enqueued_buffer_.empty()) {
      cv_.wait(l);
    }
    current_buffer = enqueued_buffer_.front();
    enqueued_buffer_.pop_front();
    dequeued_buffer_.insert(current_buffer);
    if (enqueued_buffer_.empty()) {
      became_empty = true;
    }
  }
  if (became_empty) {
    for (const auto& callback : on_empty_callbacks_) {
      callback();
    }
  }
  return current_buffer;
}

void InfeedManager::ReleaseBuffers(const std::vector<InfeedBuffer*>& buffers) {
  {
    tensorflow::mutex_lock l(mu_);
    for (gpu::InfeedBuffer* b : buffers) {
      CHECK(ContainsKey(dequeued_buffer_, b));
      dequeued_buffer_.erase(b);
    }
  }
  for (gpu::InfeedBuffer* b : buffers) {
    b->Done();
  }
}

se::Stream* InfeedManager::GetStream(se::StreamExecutor* executor) {
  if (host_to_device_executor_ == nullptr) {
    host_to_device_executor_ = executor;
    host_to_device_stream_ = MakeUnique<se::Stream>(executor);
    host_to_device_stream_->Init();
  }

  if (executor != host_to_device_executor_) {
    // The requested executor must be the same as the one for which
    // the stream is cached.
    return nullptr;
  }

  return host_to_device_stream_.get();
}

void InfeedManager::RegisterOnEmptyCallback(std::function<void()> callback) {
  on_empty_callbacks_.push_back(std::move(callback));
}

InfeedManager* GetOrCreateInfeedManager() {
  static InfeedManager* manager = new InfeedManager;
  return manager;
}

}  // namespace gpu
}  // namespace xla