grpc_testlib_server.cc revision 00986d48bb646daab659503ad3a713919865f32d
1/* Copyright 2016 Google Inc. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "external/grpc/include/grpc++/grpc++.h"
17#include "external/grpc/include/grpc++/security/credentials.h"
18#include "external/grpc/include/grpc++/server_builder.h"
19
20#include "tensorflow/core/distributed_runtime/rpc/grpc_server_lib.h"
21
22#include "tensorflow/core/lib/core/errors.h"
23#include "tensorflow/core/lib/core/status.h"
24#include "tensorflow/core/lib/strings/str_util.h"
25#include "tensorflow/core/lib/strings/strcat.h"
26#include "tensorflow/core/platform/init_main.h"
27#include "tensorflow/core/public/session_options.h"
28#include "tensorflow/core/util/command_line_flags.h"
29
30// This binary starts a TensorFlow server (master and worker) for test purposes.
31namespace tensorflow {
32namespace {
33
34Status ParseFlagsForTask(int argc, char* argv[], GrpcServerOptions* options) {
35  string job_spec;
36  int num_cpus = 1;
37  int num_gpus = 0;
38  const bool parse_result =
39      ParseFlags(&argc, argv, {Flag("tf_jobs", &job_spec),             //
40                               Flag("tf_job", &options->job_name),     //
41                               Flag("tf_task", &options->task_index),  //
42                               Flag("num_cpus", &num_cpus),            //
43                               Flag("num_gpus", &num_gpus)});
44  if (!parse_result) {
45    return errors::InvalidArgument("Error parsing command-line flags");
46  }
47
48  uint32 my_tasks_per_replica = 0;
49  for (const string& job_str : str_util::Split(job_spec, ',')) {
50    // Split each entry in the flag into 3 pieces, separated by "|".
51    const std::vector<string> job_pieces = str_util::Split(job_str, '|');
52    CHECK_EQ(2, job_pieces.size()) << job_str;
53    const string& job = job_pieces[0];
54    // Does a bit more validation of the tasks_per_replica.
55    const StringPiece spec = job_pieces[1];
56    // job_str is of form <job_name>|<host_ports>.
57    const std::vector<string> host_ports = str_util::Split(spec, ';');
58    uint32 tasks_per_replica = host_ports.size();
59    if (job == options->job_name) {
60      my_tasks_per_replica = tasks_per_replica;
61    }
62    TF_RETURN_IF_ERROR(options->channel_spec.AddHostPortsJob(
63        job, host_ports, tasks_per_replica));
64    LOG(INFO) << "Peer " << job << " " << tasks_per_replica << " {"
65              << str_util::Join(host_ports, ", ") << "}";
66  }
67  if (my_tasks_per_replica == 0) {
68    return errors::InvalidArgument("Invalid job specification");
69  }
70
71  (*options->default_session_options.config.mutable_device_count())["CPU"] =
72      num_cpus;
73  (*options->default_session_options.config.mutable_device_count())["GPU"] =
74      num_gpus;
75  return Status::OK();
76}
77
78}  // namespace
79}  // namespace tensorflow
80
81int main(int argc, char* argv[]) {
82  tensorflow::port::InitMain(argv[0], &argc, &argv);
83  tensorflow::GrpcServerOptions options;
84  tensorflow::Status s = tensorflow::ParseFlagsForTask(argc, argv, &options);
85  if (!s.ok()) {
86    LOG(ERROR) << "Could not parse flags: " << s.error_message();
87    return -1;
88  }
89  tensorflow::StartTensorFlowServer(options);
90  // NOTE(mrry): Unreachable code.
91  return 0;
92}
93