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 // See docs in ../ops/data_flow_ops.cc.
     16 
     17 #include <deque>
     18 #include <vector>
     19 
     20 #include "tensorflow/core/framework/op_kernel.h"
     21 #include "tensorflow/core/framework/resource_mgr.h"
     22 #include "tensorflow/core/framework/tensor.h"
     23 #include "tensorflow/core/framework/tensor_shape.h"
     24 #include "tensorflow/core/framework/types.h"
     25 #include "tensorflow/core/kernels/priority_queue.h"
     26 #include "tensorflow/core/kernels/queue_base.h"
     27 #include "tensorflow/core/kernels/queue_op.h"
     28 #include "tensorflow/core/lib/core/errors.h"
     29 #include "tensorflow/core/platform/logging.h"
     30 #include "tensorflow/core/platform/macros.h"
     31 #include "tensorflow/core/platform/mutex.h"
     32 #include "tensorflow/core/platform/thread_annotations.h"
     33 #include "tensorflow/core/platform/types.h"
     34 
     35 namespace tensorflow {
     36 
     37 // Defines a PriorityQueueOp, which produces a Queue (specifically, one
     38 // backed by PriorityQueue) that persists across different graph
     39 // executions, and sessions. Running this op produces a single-element
     40 // tensor of handles to Queues in the corresponding device.
     41 class PriorityQueueOp : public TypedQueueOp {
     42  public:
     43   explicit PriorityQueueOp(OpKernelConstruction* context)
     44       : TypedQueueOp(context) {
     45     OP_REQUIRES_OK(context, context->GetAttr("shapes", &component_shapes_));
     46     component_types_.insert(component_types_.begin(), DT_INT64);
     47     if (!component_shapes_.empty()) {
     48       component_shapes_.insert(component_shapes_.begin(), TensorShape({}));
     49     }
     50   }
     51 
     52  private:
     53   Status CreateResource(QueueInterface** ret) override
     54       EXCLUSIVE_LOCKS_REQUIRED(mu_) {
     55     PriorityQueue* queue = new PriorityQueue(capacity_, component_types_,
     56                                              component_shapes_, cinfo_.name());
     57     return CreateTypedQueue(queue, ret);
     58   }
     59 
     60   std::vector<TensorShape> component_shapes_;
     61 
     62   TF_DISALLOW_COPY_AND_ASSIGN(PriorityQueueOp);
     63 };
     64 
     65 REGISTER_KERNEL_BUILDER(Name("PriorityQueue").Device(DEVICE_CPU),
     66                         PriorityQueueOp);
     67 REGISTER_KERNEL_BUILDER(Name("PriorityQueueV2").Device(DEVICE_CPU),
     68                         PriorityQueueOp);
     69 
     70 }  // namespace tensorflow
     71