| 21 | namespace LightGBM { |
| 22 | |
| 23 | Linkers::Linkers(Config config) { |
| 24 | is_init_ = false; |
| 25 | // start up socket |
| 26 | TcpSocket::Startup(); |
| 27 | network_time_ = std::chrono::duration<double, std::milli>(0); |
| 28 | num_machines_ = config.num_machines; |
| 29 | local_listen_port_ = config.local_listen_port; |
| 30 | socket_timeout_ = config.time_out; |
| 31 | rank_ = -1; |
| 32 | // parse clients from file |
| 33 | ParseMachineList(config.machines, config.machine_list_filename); |
| 34 | |
| 35 | if (rank_ == -1) { |
| 36 | // get ip list of local machine |
| 37 | std::unordered_set<std::string> local_ip_list = TcpSocket::GetLocalIpList(); |
| 38 | // get local rank |
| 39 | for (size_t i = 0; i < client_ips_.size(); ++i) { |
| 40 | if (local_ip_list.count(client_ips_[i]) > 0 && client_ports_[i] == local_listen_port_) { |
| 41 | rank_ = static_cast<int>(i); |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | if (rank_ == -1) { |
| 47 | Log::Fatal("Machine list file doesn't contain the local machine"); |
| 48 | } |
| 49 | // construct listener |
| 50 | listener_ = std::unique_ptr<TcpSocket>(new TcpSocket()); |
| 51 | TryBind(local_listen_port_); |
| 52 | |
| 53 | for (int i = 0; i < num_machines_; ++i) { |
| 54 | linkers_.push_back(nullptr); |
| 55 | } |
| 56 | |
| 57 | // construct communication topo |
| 58 | bruck_map_ = BruckMap::Construct(rank_, num_machines_); |
| 59 | recursive_halving_map_ = RecursiveHalvingMap::Construct(rank_, num_machines_); |
| 60 | |
| 61 | // construct linkers |
| 62 | Construct(); |
| 63 | // free listener |
| 64 | listener_->Close(); |
| 65 | is_init_ = true; |
| 66 | } |
| 67 | |
| 68 | Linkers::~Linkers() { |
| 69 | if (is_init_) { |