1/* Copyright 2015 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_FRAMEWORK_OP_SEGMENT_H_
17#define TENSORFLOW_CORE_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
29namespace 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.
42class 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 std::string& session_handle);
50 void RemoveHold(const std::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 std::string& session_handle,
61 const std::string& node_name, OpKernel** kernel,
62 CreateKernelFn create_fn);
63
64 // Returns true if OpSegment should own the kernel.
65 static bool ShouldOwnKernel(FunctionLibraryRuntime* lib,
66 const std::string& node_op);
67
68 private:
69 // op name -> OpKernel
70 typedef std::unordered_map<string, OpKernel*> KernelMap;
71 struct Item {
72 int num_holds = 1; // Num of holds put on the session.
73 KernelMap name_kernel; // op name -> kernel.
74 ~Item();
75 };
76
77 // session handle -> item.
78 // Session handles are produced by strings::FpToString()
79 typedef std::unordered_map<string, Item*> SessionMap;
80
81 mutable mutex mu_;
82 SessionMap sessions_ TF_GUARDED_BY(mu_);
83
84 TF_DISALLOW_COPY_AND_ASSIGN(OpSegment);
85};
86
87} // end namespace tensorflow
88
89#endif // TENSORFLOW_CORE_FRAMEWORK_OP_SEGMENT_H_
90