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 "absl/memory/memory.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
AsyncExecution(Backend * backend,std::vector<StreamPool::Ptr> streams,const ExecutionProfile & profile,GlobalDataHandle result)27 AsyncExecution::AsyncExecution(Backend* backend,
28 std::vector<StreamPool::Ptr> 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
BlockUntilDone() const40 Status AsyncExecution::BlockUntilDone() const {
41 for (auto& stream : streams_) {
42 TF_RETURN_IF_ERROR(stream->BlockHostUntilDone());
43 }
44 return Status::OK();
45 }
46
ExecutionTracker()47 ExecutionTracker::ExecutionTracker() : next_handle_(1) {}
48
Register(Backend * backend,std::vector<StreamPool::Ptr> streams,const ExecutionProfile & profile,GlobalDataHandle result)49 ExecutionHandle ExecutionTracker::Register(Backend* backend,
50 std::vector<StreamPool::Ptr> streams,
51 const ExecutionProfile& profile,
52 GlobalDataHandle result) {
53 tensorflow::mutex_lock lock(execution_mutex_);
54 int64 handle = next_handle_++;
55 auto inserted = handle_to_execution_.emplace(
56 handle, absl::make_unique<AsyncExecution>(backend, std::move(streams),
57 profile, result));
58 CHECK(inserted.second);
59
60 ExecutionHandle execution_handle;
61 execution_handle.set_handle(handle);
62 return execution_handle;
63 }
64
Unregister(const ExecutionHandle & handle)65 Status ExecutionTracker::Unregister(const ExecutionHandle& handle) {
66 tensorflow::mutex_lock lock(execution_mutex_);
67 auto it = handle_to_execution_.find(handle.handle());
68 if (it == handle_to_execution_.end()) {
69 return NotFound("no execution record for execution handle: %d",
70 handle.handle());
71 }
72 handle_to_execution_.erase(handle.handle());
73 return Status::OK();
74 }
75
Resolve(const ExecutionHandle & handle)76 StatusOr<const AsyncExecution*> ExecutionTracker::Resolve(
77 const ExecutionHandle& handle) {
78 tensorflow::mutex_lock lock(execution_mutex_);
79 auto it = handle_to_execution_.find(handle.handle());
80 if (it == handle_to_execution_.end()) {
81 return NotFound("no execution record for execution handle: %d",
82 handle.handle());
83 }
84 return it->second.get();
85 }
86
87 } // namespace xla
88