• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 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 // Class method definitions for HostStream, the Stream implementation for
17 // the HostExecutor implementation.
18 #include "tensorflow/stream_executor/host/host_stream.h"
19 
20 #include "absl/synchronization/notification.h"
21 #include "tensorflow/core/platform/denormal.h"
22 #include "tensorflow/core/platform/setround.h"
23 
24 namespace stream_executor {
25 namespace host {
26 
27 namespace {
28 
GetThreadOptions(size_t stack_size_in_bytes)29 port::ThreadOptions GetThreadOptions(size_t stack_size_in_bytes) {
30   port::ThreadOptions options;
31   options.stack_size = stack_size_in_bytes;
32   return options;
33 }
34 
35 }  // namespace
36 
HostStream(size_t stack_size_in_bytes)37 HostStream::HostStream(size_t stack_size_in_bytes)
38     : thread_(port::Env::Default()->StartThread(
39           GetThreadOptions(stack_size_in_bytes), "host_executor",
40           [this]() { WorkLoop(); })) {}
41 
~HostStream()42 HostStream::~HostStream() {
43   {
44     absl::MutexLock lock(&mu_);
45     work_queue_.push(nullptr);
46   }
47   // thread_'s destructor blocks until the thread finishes running.
48   thread_.reset();
49 }
50 
EnqueueTask(std::function<void ()> fn)51 bool HostStream::EnqueueTask(std::function<void()> fn) {
52   CHECK(fn != nullptr);
53   absl::MutexLock lock(&mu_);
54   work_queue_.push(std::move(fn));
55   return true;
56 }
57 
WorkAvailable()58 bool HostStream::WorkAvailable() { return !work_queue_.empty(); }
59 
WorkLoop()60 void HostStream::WorkLoop() {
61   // Set denormal and rounding behavior to match the default TF ThreadPool
62   // behavior.
63   // TODO(phawkins, jlebar): it's not clear this is the best place to set this.
64   tensorflow::port::ScopedFlushDenormal flush;
65   tensorflow::port::ScopedSetRound round(FE_TONEAREST);
66   while (true) {
67     std::function<void()> fn;
68     {
69       absl::MutexLock lock(&mu_);
70       mu_.Await(absl::Condition(this, &HostStream::WorkAvailable));
71       fn = std::move(work_queue_.front());
72       work_queue_.pop();
73     }
74     if (!fn) {
75       return;
76     }
77     fn();
78   }
79 }
80 
BlockUntilDone()81 void HostStream::BlockUntilDone() {
82   absl::Notification done;
83   EnqueueTask([&done]() { done.Notify(); });
84   done.WaitForNotification();
85 }
86 
87 }  // namespace host
88 
89 }  // namespace stream_executor
90