1 /* Copyright 2017 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_COMPILER_XLA_SERVICE_PLATFORM_UTIL_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_PLATFORM_UTIL_H_ 18 19 #include <set> 20 #include <string> 21 #include <vector> 22 23 #include "tensorflow/compiler/xla/statusor.h" 24 #include "tensorflow/compiler/xla/types.h" 25 #include "tensorflow/core/platform/macros.h" 26 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 27 #include "tensorflow/core/platform/types.h" 28 29 namespace xla { 30 31 // Utilities for querying platforms and devices used by XLA. 32 class PlatformUtil { 33 public: 34 // Returns the platforms present on the system and supported by XLA. 35 // 36 // Note that, even if a platform is present with zero devices, if we *do* have 37 // compilation support for it, it will be returned in this sequence. 38 static StatusOr<std::vector<se::Platform*>> GetSupportedPlatforms(); 39 40 // Convenience function which returns the default supported platform for 41 // tests. If exactly one supported platform is present, then this platform is 42 // the default platform. If exactly two platforms are present and one of them 43 // is the interpreter platform, then the other platform is the default 44 // platform. Otherwise returns an error. 45 static StatusOr<se::Platform*> GetDefaultPlatform(); 46 47 // Convenience function which returns the sole supported platform. If 48 // exactly one supported platform is present, then this platform is the 49 // default platform. Otherwise returns an error. 50 static StatusOr<se::Platform*> GetSolePlatform(); 51 52 // Returns the platform according to the given name. Returns error if there is 53 // no such platform. 54 static StatusOr<se::Platform*> GetPlatform(const string& platform_name); 55 56 // Returns exactly one platform that does not have given name. Returns error 57 // if there is no such platform, or there are multiple such platforms. 58 static StatusOr<se::Platform*> GetPlatformExceptFor( 59 const string& platform_name); 60 61 // Returns a vector of StreamExecutors for the given platform. The vector is 62 // indexed by device ordinal (device numbering used by StreamExecutor). If an 63 // element is nullptr, then the device is present by not supported by XLA. 64 // If populated, only the devices in allowed_devices will have 65 // their StreamExecutors initialized, otherwise all StreamExecutors will be 66 // initialized and returned. 67 // 68 // If the platform has no visible devices, a not-found error is returned. 69 static StatusOr<std::vector<se::StreamExecutor*>> GetStreamExecutors( 70 se::Platform* platform, 71 const absl::optional<std::set<int>>& allowed_devices = absl::nullopt); 72 73 private: 74 TF_DISALLOW_COPY_AND_ASSIGN(PlatformUtil); 75 }; 76 77 } // namespace xla 78 79 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_PLATFORM_UTIL_H_ 80