Home | History | Annotate | Download | only in gpu
      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/gpu/infeed_manager.h"
     17 
     18 #include "tensorflow/compiler/xla/map_util.h"
     19 #include "tensorflow/compiler/xla/ptr_util.h"
     20 #include "tensorflow/core/platform/logging.h"
     21 
     22 namespace se = ::perftools::gputools;
     23 
     24 namespace xla {
     25 namespace gpu {
     26 
     27 InfeedManager::InfeedManager() : host_to_device_executor_(nullptr) {}
     28 
     29 void InfeedManager::Reset() {
     30   tensorflow::mutex_lock l(mu_);
     31   CHECK(dequeued_buffer_.empty());
     32   for (auto buffer : enqueued_buffer_) {
     33     buffer->Done();
     34   }
     35   enqueued_buffer_.clear();
     36 }
     37 
     38 void InfeedManager::EnqueueBuffers(const std::vector<InfeedBuffer*>& buffers) {
     39   tensorflow::mutex_lock l(mu_);
     40   bool was_empty = enqueued_buffer_.empty();
     41   for (gpu::InfeedBuffer* b : buffers) {
     42     enqueued_buffer_.push_back(b);
     43   }
     44   if (was_empty) {
     45     // This has the potential to suffer from the notified thread
     46     // immediately trying and failing to acquire mu_, but seems
     47     // preferable to the alternative of notifying outside the lock
     48     // on every enqueue.
     49     cv_.notify_one();
     50   }
     51 }
     52 
     53 InfeedBuffer* InfeedManager::BlockingDequeueBuffer() {
     54   tensorflow::mutex_lock l(mu_);
     55   while (enqueued_buffer_.empty()) {
     56     cv_.wait(l);
     57   }
     58   InfeedBuffer* current_buffer = enqueued_buffer_.front();
     59   enqueued_buffer_.pop_front();
     60   dequeued_buffer_.insert(current_buffer);
     61   return current_buffer;
     62 }
     63 
     64 void InfeedManager::ReleaseBuffers(const std::vector<InfeedBuffer*>& buffers) {
     65   {
     66     tensorflow::mutex_lock l(mu_);
     67     for (gpu::InfeedBuffer* b : buffers) {
     68       CHECK(ContainsKey(dequeued_buffer_, b));
     69       dequeued_buffer_.erase(b);
     70     }
     71   }
     72   for (gpu::InfeedBuffer* b : buffers) {
     73     b->Done();
     74   }
     75 }
     76 
     77 se::Stream* InfeedManager::GetStream(se::StreamExecutor* executor) {
     78   if (host_to_device_executor_ == nullptr) {
     79     host_to_device_executor_ = executor;
     80     host_to_device_stream_ = MakeUnique<se::Stream>(executor);
     81     host_to_device_stream_->Init();
     82   }
     83 
     84   if (executor != host_to_device_executor_) {
     85     // The requested executor must be the same as the one for which
     86     // the stream is cached.
     87     return nullptr;
     88   }
     89 
     90   return host_to_device_stream_.get();
     91 }
     92 
     93 InfeedManager* GetOrCreateInfeedManager() {
     94   static InfeedManager* manager = new InfeedManager;
     95   return manager;
     96 }
     97 
     98 }  // namespace gpu
     99 }  // namespace xla
    100