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_CORE_COMMON_RUNTIME_DEVICE_DEVICE_ID_UTILS_H_ 17 #define TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_DEVICE_ID_UTILS_H_ 18 19 #include <numeric> 20 21 #include "tensorflow/core/common_runtime/device/device_id.h" 22 #include "tensorflow/core/common_runtime/device/device_id_manager.h" 23 #include "tensorflow/core/lib/gtl/int_type.h" 24 #include "tensorflow/core/platform/types.h" 25 #include "tensorflow/stream_executor/platform.h" 26 #include "tensorflow/stream_executor/stream_executor.h" 27 28 namespace tensorflow { 29 30 // Utility methods for translation between TensorFlow device ids and platform 31 // device ids. 32 class DeviceIdUtil { 33 public: 34 // Convenient methods for getting the associated executor given a TfDeviceId 35 // or PlatformDeviceId. ExecutorForPlatformDeviceId(se::Platform * device_manager,PlatformDeviceId platform_device_id)36 static se::port::StatusOr<se::StreamExecutor*> ExecutorForPlatformDeviceId( 37 se::Platform* device_manager, PlatformDeviceId platform_device_id) { 38 return device_manager->ExecutorForDevice(platform_device_id.value()); 39 } ExecutorForTfDeviceId(const DeviceType & type,se::Platform * device_manager,TfDeviceId tf_device_id)40 static se::port::StatusOr<se::StreamExecutor*> ExecutorForTfDeviceId( 41 const DeviceType& type, se::Platform* device_manager, 42 TfDeviceId tf_device_id) { 43 PlatformDeviceId platform_device_id; 44 TF_RETURN_IF_ERROR(DeviceIdManager::TfToPlatformDeviceId( 45 type, tf_device_id, &platform_device_id)); 46 return ExecutorForPlatformDeviceId(device_manager, platform_device_id); 47 } 48 49 // Verify that the platform_device_id associated with a TfDeviceId is 50 // legitimate. CheckValidTfDeviceId(const DeviceType & type,se::Platform * device_manager,TfDeviceId tf_device_id)51 static void CheckValidTfDeviceId(const DeviceType& type, 52 se::Platform* device_manager, 53 TfDeviceId tf_device_id) { 54 PlatformDeviceId platform_device_id; 55 TF_CHECK_OK(DeviceIdManager::TfToPlatformDeviceId(type, tf_device_id, 56 &platform_device_id)); 57 const int visible_device_count = device_manager->VisibleDeviceCount(); 58 CHECK_LT(platform_device_id.value(), visible_device_count) 59 << "platform_device_id is outside discovered device range." 60 << " TF " << type << " id: " << tf_device_id << ", platform " << type 61 << " id: " << platform_device_id 62 << ", visible device count: " << visible_device_count; 63 } 64 65 // Parse `visible_device_list` into a list of platform Device ids. ParseVisibleDeviceList(const string & visible_device_list,const int visible_device_count,std::vector<PlatformDeviceId> * visible_device_order)66 static Status ParseVisibleDeviceList( 67 const string& visible_device_list, const int visible_device_count, 68 std::vector<PlatformDeviceId>* visible_device_order) { 69 visible_device_order->clear(); 70 71 // If the user wants to remap the visible to virtual Device mapping, 72 // check for that here. 73 if (visible_device_list.empty()) { 74 visible_device_order->resize(visible_device_count); 75 // By default, visible to virtual mapping is unchanged. 76 std::iota(visible_device_order->begin(), visible_device_order->end(), 0); 77 } else { 78 const std::vector<string> order_str = 79 str_util::Split(visible_device_list, ','); 80 for (const string& platform_device_id_str : order_str) { 81 int32_t platform_device_id; 82 if (!strings::safe_strto32(platform_device_id_str, 83 &platform_device_id)) { 84 return errors::InvalidArgument( 85 "Could not parse entry in 'visible_device_list': '", 86 platform_device_id_str, 87 "'. visible_device_list = ", visible_device_list); 88 } 89 if (platform_device_id < 0 || 90 platform_device_id >= visible_device_count) { 91 return errors::InvalidArgument( 92 "'visible_device_list' listed an invalid Device id '", 93 platform_device_id, "' but visible device count is ", 94 visible_device_count); 95 } 96 visible_device_order->push_back(PlatformDeviceId(platform_device_id)); 97 } 98 } 99 100 // Validate no repeats. 101 std::set<PlatformDeviceId> visible_device_set(visible_device_order->begin(), 102 visible_device_order->end()); 103 if (visible_device_set.size() != visible_device_order->size()) { 104 return errors::InvalidArgument( 105 "visible_device_list contained a duplicate entry: ", 106 visible_device_list); 107 } 108 return OkStatus(); 109 } 110 }; 111 112 } // namespace tensorflow 113 114 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_DEVICE_ID_UTILS_H_ 115