Home | History | Annotate | Download | only in service
      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 #include "tensorflow/compiler/xla/service/compiler.h"
     17 
     18 #include <string>
     19 #include <utility>
     20 
     21 #include "tensorflow/compiler/xla/types.h"
     22 #include "tensorflow/compiler/xla/util.h"
     23 #include "tensorflow/core/platform/logging.h"
     24 #include "tensorflow/core/platform/macros.h"
     25 
     26 namespace se = ::perftools::gputools;
     27 
     28 namespace xla {
     29 
     30 /* static */ tensorflow::mutex Compiler::platform_compiler_mutex_(
     31     tensorflow::LINKER_INITIALIZED);
     32 
     33 /* static */ std::map<perftools::gputools::Platform::Id,
     34                       Compiler::CompilerFactory>*
     35 Compiler::GetPlatformCompilerFactories() {
     36   static auto* r =
     37       new std::map<perftools::gputools::Platform::Id, CompilerFactory>;
     38   return r;
     39 }
     40 
     41 /* static */
     42 std::map<perftools::gputools::Platform::Id, std::unique_ptr<Compiler>>*
     43 Compiler::GetPlatformCompilers() {
     44   static auto* r = new std::map<perftools::gputools::Platform::Id,
     45                                 std::unique_ptr<Compiler>>;
     46   return r;
     47 }
     48 
     49 /* static */ void Compiler::RegisterCompilerFactory(
     50     se::Platform::Id platform_id,
     51     std::function<std::unique_ptr<Compiler>()> compiler_factory) {
     52   tensorflow::mutex_lock lock(platform_compiler_mutex_);
     53   auto* factories = GetPlatformCompilerFactories();
     54   CHECK(factories->find(platform_id) == factories->end())
     55       << "Compiler factory already registered for platform";
     56   (*factories)[platform_id] = std::move(compiler_factory);
     57 }
     58 
     59 /* static */ StatusOr<Compiler*> Compiler::GetForPlatform(
     60     const se::Platform* platform) {
     61   tensorflow::mutex_lock lock(platform_compiler_mutex_);
     62 
     63   auto* compilers = GetPlatformCompilers();
     64   // See if we already instantiated a compiler for this platform.
     65   {
     66     auto it = compilers->find(platform->id());
     67     if (it != compilers->end()) {
     68       return it->second.get();
     69     }
     70 
     71     // If not, we just fall through to try to create one with a registered
     72     // factory.
     73   }
     74 
     75   auto* factories = GetPlatformCompilerFactories();
     76   auto it = factories->find(platform->id());
     77   if (it == factories->end()) {
     78     return NotFound(
     79         "could not find registered compiler for platform %s -- check "
     80         "target linkage",
     81         platform->Name().c_str());
     82   }
     83 
     84   // And then we invoke the factory, placing the result into the mapping.
     85   compilers->insert(std::make_pair(platform->id(), it->second()));
     86   return compilers->at(platform->id()).get();
     87 }
     88 
     89 }  // namespace xla
     90