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 #ifdef TENSORFLOW_USE_MPI 17 18 #include "tensorflow/contrib/mpi/mpi_server_lib.h" 19 20 #include <string> 21 #include <utility> 22 23 #include "grpc/support/alloc.h" 24 25 #include "tensorflow/core/distributed_runtime/rpc/rpc_rendezvous_mgr.h" 26 #include "tensorflow/core/distributed_runtime/server_lib.h" 27 #include "tensorflow/core/lib/core/status.h" 28 #include "tensorflow/core/platform/env.h" 29 30 namespace tensorflow { 31 32 namespace { 33 // static utility function 34 RendezvousMgrInterface* NewMPIRendezvousMgr(const WorkerEnv* env) { 35 // Runtime check to disable the MPI path 36 const char* mpienv = getenv("MPI_DISABLED"); 37 if (mpienv && mpienv[0] == '1') { 38 LOG(INFO) << "MPI path disabled by environment variable\n"; 39 return new RpcRendezvousMgr(env); 40 } else { 41 return new MPIRendezvousMgr(env); 42 } 43 } 44 45 } // namespace 46 47 MPIServer::MPIServer(const ServerDef& server_def, Env* env) 48 : GrpcServer(server_def, env) {} 49 50 MPIServer::~MPIServer() { 51 TF_CHECK_OK(Stop()); 52 TF_CHECK_OK(Join()); 53 } 54 55 Status MPIServer::Init(ServiceInitFunction service_func, 56 RendezvousMgrCreationFunction rendezvous_mgr_func) { 57 Status s = GrpcServer::Init(service_func, rendezvous_mgr_func); 58 return s; 59 } 60 61 Status MPIServer::Start() { 62 Status s = GrpcServer::Start(); 63 return s; 64 } 65 66 Status MPIServer::Join() { 67 Status s = GrpcServer::Join(); 68 return s; 69 } 70 71 /* static */ 72 Status MPIServer::Create(const ServerDef& server_def, Env* env, 73 std::unique_ptr<ServerInterface>* out_server) { 74 std::unique_ptr<MPIServer> ret(new MPIServer(server_def, Env::Default())); 75 ServiceInitFunction service_func = nullptr; 76 TF_RETURN_IF_ERROR(ret->Init(service_func, NewMPIRendezvousMgr)); 77 *out_server = std::move(ret); 78 return Status::OK(); 79 } 80 81 namespace { 82 83 class MPIServerFactory : public ServerFactory { 84 public: 85 bool AcceptsOptions(const ServerDef& server_def) override { 86 return server_def.protocol() == "grpc+mpi"; 87 } 88 89 Status NewServer(const ServerDef& server_def, 90 std::unique_ptr<ServerInterface>* out_server) override { 91 return MPIServer::Create(server_def, Env::Default(), out_server); 92 } 93 }; 94 95 // Registers a `ServerFactory` for `MPIServer` instances. 96 class MPIServerRegistrar { 97 public: 98 MPIServerRegistrar() { 99 gpr_allocation_functions alloc_fns; 100 alloc_fns.malloc_fn = port::Malloc; 101 alloc_fns.realloc_fn = port::Realloc; 102 alloc_fns.free_fn = port::Free; 103 gpr_set_allocation_functions(alloc_fns); 104 ServerFactory::Register("MPI_SERVER", new MPIServerFactory()); 105 } 106 }; 107 static MPIServerRegistrar registrar; 108 109 } // namespace 110 } // namespace tensorflow 111 112 #endif // TENSORFLOW_USE_MPI 113