collective_server.cc 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 PaddlePaddle 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. */

#include "paddle/fluid/operators/distributed/collective_server.h"
W
wanghuancoder 已提交
16
#include <memory>
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

DEFINE_int32(collective_get_thread_num, 5, "number of threads for rpc get");

namespace paddle {
namespace operators {
namespace distributed {

std::once_flag CollectiveServer::init_flag_;
std::shared_ptr<CollectiveServer> CollectiveServer::collective_server_(nullptr);

CollectiveServer::CollectiveServer(const std::string& end_point, int fan_in) {
  VLOG(1) << "Create colllective server:" << end_point << ", fan_in:" << fan_in;
  rpc_server_.reset(new RPCSERVER_T(end_point, fan_in));
}

void CollectiveServer::Stop() {
  rpc_server_->ShutDown();
  server_thread_->join();
  loop_thread_->join();
}

void CollectiveServer::StartServer() {
  get_monomer_handler_.reset(new GetMonomerHandler());
  get_monomer_handler_->SetRPCServer(rpc_server_.get());

  get_barrier_handler_.reset(new GetMonomerBarrierHandler());
  get_barrier_handler_->SetRPCServer(rpc_server_.get());

  rpc_server_->RegisterRPC(distributed::kRequestGetMonomerVariable,
                           get_monomer_handler_.get(),
                           FLAGS_collective_get_thread_num);
  rpc_server_->RegisterRPC(distributed::kRequestGetMonomerBarrier,
                           get_barrier_handler_.get(), 1);

  server_thread_.reset(new std::thread([&]() { rpc_server_->StartServer(); }));
  rpc_server_->WaitServerReady();

  loop_thread_.reset(new std::thread([&]() {
    while (true) {
      if (rpc_server_->IsExit()) {
        LOG(WARNING) << "get exit!rpc_processor break!";
        break;
      }
      sleep(1);
    }
    VLOG(1) << "CollectiveServer loop_thread end";
  }));
}

};  // namespace distributed
};  // namespace operators
};  // namespace paddle