1 /* Copyright 2018 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 // Classes for keeping track of on-device state. 17 18 #ifndef TENSORFLOW_COMPILER_XRT_XRT_DEVICE_H_ 19 #define TENSORFLOW_COMPILER_XRT_XRT_DEVICE_H_ 20 21 #include "tensorflow/compiler/xla/client/local_client.h" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/resource_mgr.h" 24 25 namespace tensorflow { 26 27 // This accessor is used for XLA CPU/GPU. It uses the device resource manager, 28 // so e.g., on multi-GPU setups the compilation cache will not be shared across 29 // devices. 30 class XRTGenericDeviceAccessor { 31 public: 32 static Status GetResourceManager(OpKernelContext* ctx, ResourceMgr** rm); 33 34 // We use a ScopedRef pattern here even though it's not strictly necessary, 35 // just so that templated uses of this and the TPU accessor class will be as 36 // similar as possible. 37 class ScopedRef { 38 public: ScopedRef()39 ScopedRef() {} ~ScopedRef()40 ~ScopedRef() {} 41 42 ScopedRef(const ScopedRef&) = delete; 43 ScopedRef& operator=(const ScopedRef&) = delete; 44 45 // Returns the XLA device protected by this ScopedRef. client()46 xla::LocalClient* client() { return client_; } backend()47 xla::Backend* backend() { return client_->mutable_backend(); } device_ordinal()48 int device_ordinal() { return 0; } 49 50 private: 51 // XRTGenericDeviceAccessor::InitScopedRef is the only way to initialize 52 // ScopedRef. 53 friend class XRTGenericDeviceAccessor; 54 Acquire(xla::LocalClient * client)55 void Acquire(xla::LocalClient* client) { client_ = client; } 56 57 xla::LocalClient* client_ = nullptr; 58 }; 59 60 static Status InitScopedRef(OpKernelContext* ctx, int device_ordinal, 61 ScopedRef* scoped_ref); 62 63 static Status InitScopedRef(OpKernelContext* ctx, ScopedRef* scoped_ref); 64 }; 65 66 } // namespace tensorflow 67 68 #endif // TENSORFLOW_COMPILER_XRT_XRT_DEVICE_H_ 69