Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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_QUEUE_OP_H_
     17 #define TENSORFLOW_KERNELS_QUEUE_OP_H_
     18 
     19 #include <deque>
     20 
     21 #include "tensorflow/core/framework/op_kernel.h"
     22 #include "tensorflow/core/framework/resource_op_kernel.h"
     23 #include "tensorflow/core/framework/tensor.h"
     24 #include "tensorflow/core/framework/types.h"
     25 #include "tensorflow/core/kernels/queue_base.h"
     26 #include "tensorflow/core/lib/core/errors.h"
     27 #include "tensorflow/core/platform/macros.h"
     28 #include "tensorflow/core/platform/types.h"
     29 
     30 namespace tensorflow {
     31 
     32 // Defines a QueueOp, an abstract class for Queue construction ops.
     33 class QueueOp : public ResourceOpKernel<QueueInterface> {
     34  public:
     35   QueueOp(OpKernelConstruction* context) : ResourceOpKernel(context) {
     36     OP_REQUIRES_OK(context, context->GetAttr("capacity", &capacity_));
     37     if (capacity_ < 0) {
     38       capacity_ = QueueBase::kUnbounded;
     39     }
     40     OP_REQUIRES_OK(context,
     41                    context->GetAttr("component_types", &component_types_));
     42   }
     43 
     44   void Compute(OpKernelContext* context) override {
     45     ResourceOpKernel<QueueInterface>::Compute(context);
     46     if (resource_ && context->track_allocations()) {
     47       context->record_persistent_memory_allocation(resource_->MemoryUsed());
     48     }
     49   }
     50 
     51  protected:
     52   // Variables accessible by subclasses
     53   int32 capacity_;
     54   DataTypeVector component_types_;
     55 
     56  private:
     57   Status VerifyResource(QueueInterface* queue) override {
     58     return queue->MatchesNodeDef(def());
     59   }
     60 };
     61 
     62 class TypedQueueOp : public QueueOp {
     63  public:
     64   using QueueOp::QueueOp;
     65 
     66  protected:
     67   template <typename TypedQueue>
     68   Status CreateTypedQueue(TypedQueue* queue, QueueInterface** ret) {
     69     if (queue == nullptr) {
     70       return errors::ResourceExhausted("Failed to allocate queue.");
     71     }
     72     *ret = queue;
     73     return queue->Initialize();
     74   }
     75 };
     76 
     77 }  // namespace tensorflow
     78 
     79 #endif  // TENSORFLOW_KERNELS_QUEUE_OP_H_
     80