1 /* Copyright 2020 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_CC_EXPERIMENTAL_BASE_PUBLIC_RUNTIME_H_ 17 #define TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_RUNTIME_H_ 18 19 #include <memory> 20 21 #include "tensorflow/c/eager/c_api_experimental.h" 22 23 namespace tensorflow { 24 namespace experimental { 25 namespace cc { 26 27 // Runtime represents an opaque instance of a Tensorflow runtime, with its own 28 // resources, threadpools, etc. Clients are expected to construct a Runtime 29 // object through tensorflow::cc::RuntimeBuilder::Build, after setting any 30 // relevant configuration options. Many Tensorflow functions take a reference to 31 // the runtime as an argument (eg: tensorflow::cc::SavedModelAPI::Load), and 32 // may have different implementations depending on the runtime. For many of 33 // these Runtime-attached objects (such as tensorflow::cc::TensorHandle), the 34 // Runtime must outlive these objects. 35 class Runtime { 36 public: 37 // Runtime is movable, but not copyable. 38 Runtime(Runtime&&) = default; 39 Runtime& operator=(Runtime&&) = default; 40 41 private: 42 friend class RuntimeBuilder; 43 friend class SavedModelAPI; 44 friend class TensorHandle; 45 46 // Wraps a TFE_Context. Takes ownership of ctx. Runtime(TFE_Context * ctx)47 explicit Runtime(TFE_Context* ctx) : ctx_(ctx) {} 48 49 // Deletes the currently wrapped TFE_Context, swaps it with ctx, 50 // and takes ownership of ctx. Reset(TFE_Context * ctx)51 void Reset(TFE_Context* ctx) { ctx_.reset(ctx); } 52 53 // Returns the TFE_Context that this object wraps. This object 54 // retains ownership of the pointer. GetTFEContext()55 TFE_Context* GetTFEContext() const { return ctx_.get(); } 56 57 // Runtime is not copyable 58 Runtime(const Runtime&) = delete; 59 Runtime& operator=(const Runtime&) = delete; 60 61 struct TFEContextDeleter { operatorTFEContextDeleter62 void operator()(TFE_Context* p) const { TFE_DeleteContext(p); } 63 }; 64 std::unique_ptr<TFE_Context, TFEContextDeleter> ctx_; 65 }; 66 67 } // namespace cc 68 } // namespace experimental 69 } // namespace tensorflow 70 71 #endif // TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_RUNTIME_H_ 72