Home | History | Annotate | Download | only in service
      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/execution_tracker.h"
     17 
     18 #include <utility>
     19 
     20 #include "tensorflow/compiler/xla/ptr_util.h"
     21 #include "tensorflow/compiler/xla/util.h"
     22 #include "tensorflow/core/platform/logging.h"
     23 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
     24 
     25 namespace xla {
     26 
     27 AsyncExecution::AsyncExecution(Backend* backend,
     28                                std::vector<Backend::StreamPtr> streams,
     29                                const ExecutionProfile& profile,
     30                                GlobalDataHandle result)
     31     : backend_(CHECK_NOTNULL(backend)),
     32       streams_(std::move(streams)),
     33       profile_(profile),
     34       result_(std::move(result)) {
     35   for (const auto& stream : streams_) {
     36     CHECK(stream != nullptr);
     37   }
     38 }
     39 
     40 tensorflow::Status AsyncExecution::BlockUntilDone() const {
     41   for (auto& stream : streams_) {
     42     TF_RETURN_IF_ERROR(stream->BlockHostUntilDone());
     43   }
     44   return tensorflow::Status::OK();
     45 }
     46 
     47 ExecutionTracker::ExecutionTracker() : next_handle_(1) {}
     48 
     49 ExecutionHandle ExecutionTracker::Register(
     50     Backend* backend, std::vector<Backend::StreamPtr> streams,
     51     const ExecutionProfile& profile, GlobalDataHandle result) {
     52   tensorflow::mutex_lock lock(execution_mutex_);
     53   int64 handle = next_handle_++;
     54   auto inserted = handle_to_execution_.emplace(
     55       handle,
     56       MakeUnique<AsyncExecution>(backend, std::move(streams), profile, result));
     57   CHECK(inserted.second);
     58 
     59   ExecutionHandle execution_handle;
     60   execution_handle.set_handle(handle);
     61   return execution_handle;
     62 }
     63 
     64 tensorflow::Status ExecutionTracker::Unregister(const ExecutionHandle& handle) {
     65   tensorflow::mutex_lock lock(execution_mutex_);
     66   auto it = handle_to_execution_.find(handle.handle());
     67   if (it == handle_to_execution_.end()) {
     68     return NotFound("no execution record for execution handle: %lld",
     69                     handle.handle());
     70   }
     71   handle_to_execution_.erase(handle.handle());
     72   return tensorflow::Status::OK();
     73 }
     74 
     75 StatusOr<const AsyncExecution*> ExecutionTracker::Resolve(
     76     const ExecutionHandle& handle) {
     77   tensorflow::mutex_lock lock(execution_mutex_);
     78   auto it = handle_to_execution_.find(handle.handle());
     79   if (it == handle_to_execution_.end()) {
     80     return NotFound("no execution record for execution handle: %lld",
     81                     handle.handle());
     82   }
     83   return it->second.get();
     84 }
     85 
     86 }  // namespace xla
     87