Home | History | Annotate | Download | only in kernels
      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 #ifndef TENSORFLOW_KERNELS_PRIORITY_QUEUE_H_
     17 #define TENSORFLOW_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 
     32 namespace tensorflow {
     33 
     34 using PriorityTensorPair = std::pair<int64, PersistentTensor>;
     35 
     36 struct 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 
     46 class PriorityQueue
     47     : public TypedQueue<std::priority_queue<PriorityTensorPair,
     48                                             std::vector<PriorityTensorPair>,
     49                                             ComparePriorityTensorPair> > {
     50  public:
     51   PriorityQueue(int32 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() 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       EXCLUSIVE_LOCKS_REQUIRED(mu_);
     82 
     83   static Status GetElementComponentFromBatch(const Tuple& tuple, int index,
     84                                              int component,
     85                                              OpKernelContext* ctx,
     86                                              PersistentTensor* out_element);
     87 
     88   TF_DISALLOW_COPY_AND_ASSIGN(PriorityQueue);
     89 };
     90 
     91 }  // namespace tensorflow
     92 
     93 #endif  // TENSORFLOW_KERNELS_PRIORITY_QUEUE_H_
     94