Home | History | Annotate | Download | only in framework
      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_FRAMEWORK_OP_SEGMENT_H_
     17 #define TENSORFLOW_FRAMEWORK_OP_SEGMENT_H_
     18 
     19 #include <string>
     20 #include <unordered_map>
     21 
     22 #include "tensorflow/core/framework/op_kernel.h"
     23 #include "tensorflow/core/lib/core/status.h"
     24 #include "tensorflow/core/platform/macros.h"
     25 #include "tensorflow/core/platform/mutex.h"
     26 #include "tensorflow/core/platform/thread_annotations.h"
     27 #include "tensorflow/core/platform/types.h"
     28 
     29 namespace tensorflow {
     30 
     31 // OpSegment keeps track of OpKernels registered for sessions running
     32 // on a device.
     33 //
     34 // The implementation maintains a two-level map. The 1st level maps
     35 // session handle to the map of registered OpKernels. The 2nd level
     36 // map maps node names to instantiated OpKernel objects.
     37 //
     38 // Each 2-nd level map is reference-counted and the caller can call
     39 // AddHold to obtain a reference on all kernels of a session and
     40 // ensure these kernels are alive until a corresponding RemoveHold is
     41 // called on the same session.
     42 class OpSegment {
     43  public:
     44   OpSegment();
     45   ~OpSegment();
     46 
     47   // A hold can be placed on a session, preventing all its kernels
     48   // from being deleted.
     49   void AddHold(const string& session_handle);
     50   void RemoveHold(const string& session_handle);
     51 
     52   // If the kernel for "node_name" has been created in the
     53   // "session_handle", returns the existing op kernel in "*kernel".
     54   // Otherwise, creates the kernel by calling create_fn(), cache it,
     55   // and returns it in "*kernel". If create_fn() fails, returns the
     56   // error.
     57   //
     58   // OpSegment keeps the ownership of the returned "*kernel".
     59   typedef std::function<Status(OpKernel**)> CreateKernelFn;
     60   Status FindOrCreate(const string& session_handle, const string& node_name,
     61                       OpKernel** kernel, CreateKernelFn create_fn);
     62 
     63  private:
     64   // op name -> OpKernel
     65   typedef std::unordered_map<string, OpKernel*> KernelMap;
     66   struct Item {
     67     int num_holds = 1;      // Num of holds put on the session.
     68     KernelMap name_kernel;  // op name -> kernel.
     69     ~Item();
     70   };
     71 
     72   // session handle -> item.
     73   // Session handles are produced by strings::FpToString()
     74   typedef std::unordered_map<string, Item*> SessionMap;
     75 
     76   mutable mutex mu_;
     77   SessionMap sessions_ GUARDED_BY(mu_);
     78 
     79   TF_DISALLOW_COPY_AND_ASSIGN(OpSegment);
     80 };
     81 
     82 }  // end namespace tensorflow
     83 
     84 #endif  // TENSORFLOW_FRAMEWORK_OP_SEGMENT_H_
     85