aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/mpi/mpi_server_lib.cc
blob: 3b2fba97a9929269461694928e7f6ca17b87c983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifdef TENSORFLOW_USE_MPI

#include "tensorflow/contrib/mpi/mpi_server_lib.h"

#include <string>
#include <utility>

#include "tensorflow/core/distributed_runtime/server_lib.h"
#include "tensorflow/core/distributed_runtime/rpc/rpc_rendezvous_mgr.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/env.h"

namespace tensorflow {

namespace {
// static utility function
RendezvousMgrInterface* NewMPIRendezvousMgr(const WorkerEnv* env) {
  // Runtime check to disable the MPI path
  const char* mpienv = getenv("MPI_DISABLED");
  if (mpienv && mpienv[0] == '1') {
    LOG(INFO) << "MPI path disabled by environment variable\n";
    return new RpcRendezvousMgr(env);
  } else {
    return new MPIRendezvousMgr(env);
  }
}

}  // namespace

MPIServer::MPIServer(const ServerDef& server_def, Env* env)
    : GrpcServer(server_def, env) {}

MPIServer::~MPIServer() {
  TF_CHECK_OK(Stop());
  TF_CHECK_OK(Join());
}

Status MPIServer::Init(ServiceInitFunction service_func,
                       RendezvousMgrCreationFunction rendezvous_mgr_func) {
  Status s = GrpcServer::Init(service_func, rendezvous_mgr_func);
  return s;
}

Status MPIServer::Start() {
  Status s = GrpcServer::Start();
  return s;
}

Status MPIServer::Join() {
  Status s = GrpcServer::Join();
  return s;
}

/* static */
Status MPIServer::Create(const ServerDef& server_def, Env* env,
                         std::unique_ptr<ServerInterface>* out_server) {
  std::unique_ptr<MPIServer> ret(new MPIServer(server_def, Env::Default()));
  ServiceInitFunction service_func = nullptr;
  TF_RETURN_IF_ERROR(ret->Init(service_func, NewMPIRendezvousMgr));
  *out_server = std::move(ret);
  return Status::OK();
}

namespace {

class MPIServerFactory : public ServerFactory {
 public:
  bool AcceptsOptions(const ServerDef& server_def) override {
    return server_def.protocol() == "grpc+mpi";
  }

  Status NewServer(const ServerDef& server_def,
                   std::unique_ptr<ServerInterface>* out_server) override {
    return MPIServer::Create(server_def, Env::Default(), out_server);
  }
};

// Registers a `ServerFactory` for `MPIServer` instances.
class MPIServerRegistrar {
 public:
  MPIServerRegistrar() {
    gpr_allocation_functions alloc_fns;
    alloc_fns.malloc_fn = port::Malloc;
    alloc_fns.realloc_fn = port::Realloc;
    alloc_fns.free_fn = port::Free;
    gpr_set_allocation_functions(alloc_fns);
    ServerFactory::Register("MPI_SERVER", new MPIServerFactory());
  }
};
static MPIServerRegistrar registrar;

}  // namespace
}  // namespace tensorflow

#endif  // TENSORFLOW_USE_MPI