aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/cc/training/queue_runner.cc
blob: 585ee15872ce4e3bdb276debe661f26f43aca1f3 (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
114
115
116
117
118
119
/* Copyright 2016 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/cc/training/queue_runner.h"
#include "tensorflow/core/platform/env.h"

namespace tensorflow {

QueueRunner::QueueRunner() : started_(false) {}

QueueRunner::QueueRunner(const QueueRunnerDef& queue_runner_def)
    : started_(false) {
  TF_CHECK_OK(Init(queue_runner_def));
}

Status QueueRunner::Init(const QueueRunnerDef& queue_runner_def) {
  if (started_.load()) {
    return Status(error::ALREADY_EXISTS, "QueueRunner is already running.");
  }
  queue_name_ = queue_runner_def.queue_name();
  enqueue_op_names_.insert(enqueue_op_names_.end(),
                           queue_runner_def.enqueue_op_name().begin(),
                           queue_runner_def.enqueue_op_name().end());
  runs_ = enqueue_op_names_.size();
  if (runs_ == 0) {
    return Status(error::INVALID_ARGUMENT, "Empty enqueue ops to run.");
  }
  close_op_name_ = queue_runner_def.close_op_name();
  cancel_op_name_ = queue_runner_def.cancel_op_name();
  if (queue_runner_def.queue_closed_exception_types_size() == 0) {
    queue_closed_exception_types_.insert(error::OUT_OF_RANGE);
  } else {
    for (const auto& code : queue_runner_def.queue_closed_exception_types()) {
      queue_closed_exception_types_.insert(static_cast<int>(code));
    }
  }

  thread_pool_.reset(
      new thread::ThreadPool(Env::Default(), queue_name_, runs_));
  should_stop_ = false;
  return Status::OK();
}

QueueRunner::~QueueRunner() {
  should_stop_ = true;
  Join();
}

Status QueueRunner::Start(Session* sess) {
  if (runs_ == 0) {
    return Status(
        error::INVALID_ARGUMENT,
        "No enqueue ops to run. You may want to Init the QueueRunner first.");
  }
  started_ = true;
  for (const string& enqueue_op : enqueue_op_names_) {
    thread_pool_->Schedule(
        std::bind(&QueueRunner::Run, this, sess, enqueue_op));
  }
  return Status::OK();
}

Status QueueRunner::Join() {
  thread_pool_.reset();
  started_ = false;
  return status_;
}

void QueueRunner::Run(Session* sess, const string& enqueue_op) {
  bool decremented = false;
  while (!should_stop_.load()) {
    std::vector<Tensor> outputs;
    auto status = sess->Run({}, {}, {enqueue_op}, &outputs);
    if (status.ok()) {
      continue;
    } else if (queue_closed_exception_types_.count(
                   static_cast<int>(status.code())) > 0) {
      mutex_lock l(mu_);
      runs_--;
      decremented = true;
      should_stop_ = true;

      // If all enqueue ops have finished, run the close op.
      if (runs_ == 0 && !close_op_name_.empty()) {
        std::vector<Tensor> outputs;
        auto s = sess->Run({}, {}, {close_op_name_}, &outputs);
        if (!s.ok()) {
          status_ = status;
        }
      }
    } else {
      mutex_lock l(mu_);
      should_stop_ = true;
      // Only record the first failure status.
      if (status_.ok()) {
        status_ = status;
      }
    }
  }

  if (!decremented) {
    mutex_lock l(mu_);
    runs_--;
  }
}

}  // namespace tensorflow