|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 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 | +#include "PServerUtil.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | + |
| 19 | +ParameterServerConfig* PServerUtil::initConfig() { |
| 20 | + ParameterServerConfig* config = new ParameterServerConfig(); |
| 21 | + config->set_nics(FLAGS_nics); |
| 22 | + config->set_port(FLAGS_port); |
| 23 | + config->set_ports_num(FLAGS_ports_num); |
| 24 | + config->set_rdma_tcp(FLAGS_rdma_tcp); |
| 25 | + return config; |
| 26 | +} |
| 27 | + |
| 28 | +PServerUtil* PServerUtil::create() { |
| 29 | + auto& pServerConfig = *paddle::PServerUtil::initConfig(); |
| 30 | + return PServerUtil::create(pServerConfig); |
| 31 | +} |
| 32 | + |
| 33 | +PServerUtil* PServerUtil::create(const ParameterServerConfig& config) { |
| 34 | + return new PServerUtil(config); |
| 35 | +} |
| 36 | + |
| 37 | +PServerUtil::PServerUtil(const ParameterServerConfig& config) { |
| 38 | + // round robin to load balance RDMA server ENGINE |
| 39 | + std::vector<std::string> devices; |
| 40 | + int rdmaCpu = 0; |
| 41 | + int onlineCpus = rdma::numCpus(); |
| 42 | + ; |
| 43 | + int numPorts = config.ports_num() + config.ports_num_for_sparse(); |
| 44 | + |
| 45 | + if (FLAGS_nics.empty()) { |
| 46 | + pservers_.resize(numPorts); |
| 47 | + for (int i = 0; i < numPorts; ++i) { |
| 48 | + if (FLAGS_rdma_tcp == "rdma") { |
| 49 | + pservers_[i].reset( |
| 50 | + new ParameterServer2(std::string(), FLAGS_port + i, rdmaCpu++)); |
| 51 | + rdmaCpu = rdmaCpu % onlineCpus; |
| 52 | + } else { |
| 53 | + pservers_[i].reset(new ParameterServer2(std::string(), FLAGS_port + i)); |
| 54 | + } |
| 55 | + CHECK(pservers_[i]->init()) << "Fail to initialize parameter server" |
| 56 | + << FLAGS_port + i; |
| 57 | + } |
| 58 | + } else { |
| 59 | + str::split(FLAGS_nics, ',', &devices); |
| 60 | + pservers_.resize(devices.size() * numPorts); |
| 61 | + for (int i = 0; i < numPorts; ++i) { |
| 62 | + for (size_t j = 0; j < devices.size(); ++j) { |
| 63 | + if (FLAGS_rdma_tcp == "rdma") { |
| 64 | + pservers_[i * devices.size() + j].reset(new ParameterServer2( |
| 65 | + getIpAddr(devices[j]), FLAGS_port + i, rdmaCpu++)); |
| 66 | + rdmaCpu = rdmaCpu % onlineCpus; |
| 67 | + } else { |
| 68 | + pservers_[i * devices.size() + j].reset( |
| 69 | + new ParameterServer2(getIpAddr(devices[j]), FLAGS_port + i)); |
| 70 | + } |
| 71 | + CHECK(pservers_[i * devices.size() + j]->init()) |
| 72 | + << "Fail to initialize parameter server" << devices[j] |
| 73 | + << FLAGS_port + i; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +PServerUtil::~PServerUtil() { this->join(); } |
| 80 | + |
| 81 | +void PServerUtil::start() { |
| 82 | + LOG(INFO) << "pserver sizes : " << pservers_.size(); |
| 83 | + int i = 0; |
| 84 | + for (const auto& pserver : pservers_) { |
| 85 | + LOG(INFO) << "pserver started : " << i; |
| 86 | + pserver->start(); |
| 87 | + i++; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +void PServerUtil::join() { |
| 92 | + LOG(INFO) << "pserver sizes : " << pservers_.size(); |
| 93 | + int i = 0; |
| 94 | + for (const auto& pserver : pservers_) { |
| 95 | + LOG(INFO) << "pserver join : " << i; |
| 96 | + pserver->join(); |
| 97 | + i++; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace paddle |
0 commit comments