1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_KERNELS_PRIORITY_QUEUE_H_
17#define TENSORFLOW_CORE_KERNELS_PRIORITY_QUEUE_H_
18
19#include <deque>
20#include <queue>
21#include <vector>
22
23#include "tensorflow/core/framework/op_kernel.h"
24#include "tensorflow/core/framework/tensor.h"
25#include "tensorflow/core/framework/tensor_shape.h"
26#include "tensorflow/core/framework/types.h"
27#include "tensorflow/core/kernels/typed_queue.h"
28#include "tensorflow/core/platform/macros.h"
29#include "tensorflow/core/platform/mutex.h"
30#include "tensorflow/core/platform/types.h"
31
32namespace tensorflow {
33
34using PriorityTensorPair = std::pair<int64_t, Tensor>;
35
36struct ComparePriorityTensorPair {
37 // 0 is a higher priority than 1, -MAX_LONG is a higher priority
38 // than MAX_LONG, etc. Values coming in with a smaller
39 // priority number will bubble to the front of the queue.
40 bool operator()(const PriorityTensorPair& lhs,
41 const PriorityTensorPair& rhs) const {
42 return lhs.first > rhs.first;
43 }
44};
45
46class PriorityQueue
47 : public TypedQueue<std::priority_queue<PriorityTensorPair,
48 std::vector<PriorityTensorPair>,
49 ComparePriorityTensorPair> > {
50 public:
51 PriorityQueue(int32_t capacity, const DataTypeVector& component_dtypes,
52 const std::vector<TensorShape>& component_shapes,
53 const string& name);
54
55 Status Initialize() override; // Must be called before any other method.
56
57 // Implementations of QueueInterface methods --------------------------------
58
59 void TryEnqueue(const Tuple& tuple, OpKernelContext* ctx,
60 DoneCallback callback) override;
61 void TryEnqueueMany(const Tuple& tuple, OpKernelContext* ctx,
62 DoneCallback callback) override;
63 void TryDequeue(OpKernelContext* ctx, CallbackWithTuple callback) override;
64 void TryDequeueMany(int num_elements, OpKernelContext* ctx,
65 bool allow_small_batch,
66 CallbackWithTuple callback) override;
67 Status MatchesNodeDef(const NodeDef& node_def) override;
68 Status MatchesPriorityNodeDefTypes(const NodeDef& node_def) const;
69 Status MatchesPriorityNodeDefShapes(const NodeDef& node_def) const;
70
71 int32 size() const override {
72 mutex_lock lock(mu_);
73 return queues_[0].size();
74 }
75
76 private:
77 ~PriorityQueue() override {}
78
79 // Helper for dequeuing a single element from queues_.
80 void DequeueLocked(OpKernelContext* ctx, Tuple* tuple)
81 TF_EXCLUSIVE_LOCKS_REQUIRED(mu_);
82
83 static Status GetElementComponentFromBatch(const Tuple& tuple, int index,
84 int component,
85 OpKernelContext* ctx,
86 Tensor* out_element);
87
88 TF_DISALLOW_COPY_AND_ASSIGN(PriorityQueue);
89};
90
91} // namespace tensorflow
92
93#endif // TENSORFLOW_CORE_KERNELS_PRIORITY_QUEUE_H_
94