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#include "tensorflow/core/framework/device.h"
17
18#include "tensorflow/core/framework/device_factory.h"
19#include "tensorflow/core/framework/op_segment.h"
20#include "tensorflow/core/platform/errors.h"
21#include "tensorflow/core/platform/logging.h"
22#include "tensorflow/core/platform/random.h"
23#include "tensorflow/core/platform/types.h"
24
25namespace tensorflow {
26
27Device::Device(Env* env, const DeviceAttributes& device_attributes)
28 : DeviceBase(env), device_attributes_(device_attributes) {
29 CHECK(DeviceNameUtils::ParseFullName(name(), &parsed_name_))
30 << "Invalid device name: " << name();
31 rmgr_ = new ResourceMgr(parsed_name_.job);
32}
33
34Device::~Device() {
35 if (rmgr_ != nullptr) {
36 DeleteResourceMgr();
37 }
38}
39
40void Device::Sync(const DoneCallback& done) { done(Sync()); }
41
42// static
43DeviceAttributes Device::BuildDeviceAttributes(
44 const string& name, DeviceType device, Bytes memory_limit,
45 const DeviceLocality& locality, const string& physical_device_desc) {
46 DeviceAttributes da;
47 da.set_name(name);
48 do {
49 da.set_incarnation(random::New64());
50 } while (da.incarnation() == 0); // This proto field must not be zero
51 da.set_device_type(device.type());
52 da.set_memory_limit(memory_limit.value());
53 *da.mutable_locality() = locality;
54 da.set_physical_device_desc(physical_device_desc);
55 da.set_xla_global_id(-1); // Unknown / not set
56 return da;
57}
58
59bool Device::IsRemoteCallAllowed() const {
60 auto& type = parsed_name_.type;
61 if (type == "TPU") {
62 return true;
63 }
64 if (type == "TPU_SYSTEM") {
65 return true;
66 }
67 if (type == "CPU") {
68 return true;
69 }
70 if (type == "GPU") {
71 return true;
72 }
73 if (DeviceFactory::IsPluggableDevice(type)) {
74 return true;
75 }
76 return false;
77}
78
79} // namespace tensorflow
80