linker_topo.cpp 6.08 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
5
6
7
#include <LightGBM/network.h>
#include <LightGBM/utils/common.h>
#include <LightGBM/utils/log.h>
Guolin Ke's avatar
Guolin Ke committed
8
9
10

#include <string>
#include <unordered_map>
11
#include <vector>
Guolin Ke's avatar
Guolin Ke committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

namespace LightGBM {


BruckMap::BruckMap() {
  k = 0;
}

BruckMap::BruckMap(int n) {
  k = n;
  // default set to -1
  for (int i = 0; i < n; ++i) {
    in_ranks.push_back(-1);
    out_ranks.push_back(-1);
  }
}

BruckMap BruckMap::Construct(int rank, int num_machines) {
  // distance at k-th communication, distance[k] = 2^k
  std::vector<int> distance;
  int k = 0;
Guolin Ke's avatar
Guolin Ke committed
33
  for (k = 0; (1 << k) < num_machines; ++k) {
Guolin Ke's avatar
Guolin Ke committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    distance.push_back(1 << k);
  }
  BruckMap bruckMap(k);
  for (int j = 0; j < k; ++j) {
    // set incoming rank at k-th commuication
    const int in_rank = (rank + distance[j]) % num_machines;
    bruckMap.in_ranks[j] = in_rank;
    // set outgoing rank at k-th commuication
    const int out_rank = (rank - distance[j] + num_machines) % num_machines;
    bruckMap.out_ranks[j] = out_rank;
  }
  return bruckMap;
}

RecursiveHalvingMap::RecursiveHalvingMap() {
  k = 0;
}
Guolin Ke's avatar
Guolin Ke committed
51

Guolin Ke's avatar
Guolin Ke committed
52
53
RecursiveHalvingMap::RecursiveHalvingMap(int in_k, RecursiveHalvingNodeType _type, bool _is_power_of_2) {
  type = _type;
Guolin Ke's avatar
Guolin Ke committed
54
  k = in_k;
Guolin Ke's avatar
Guolin Ke committed
55
56
57
  is_power_of_2 = _is_power_of_2;
  if (type != RecursiveHalvingNodeType::Other) {
    for (int i = 0; i < k; ++i) {
Andrew Ziem's avatar
Andrew Ziem committed
58
      // default set as -1
Guolin Ke's avatar
Guolin Ke committed
59
60
61
62
63
64
      ranks.push_back(-1);
      send_block_start.push_back(-1);
      send_block_len.push_back(-1);
      recv_block_start.push_back(-1);
      recv_block_len.push_back(-1);
    }
Guolin Ke's avatar
Guolin Ke committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  }
}

RecursiveHalvingMap RecursiveHalvingMap::Construct(int rank, int num_machines) {
  // construct all recursive halving map for all machines
  int k = 0;
  while ((1 << k) <= num_machines) { ++k; }
  // let 1 << k <= num_machines
  --k;
  // distance of each communication
  std::vector<int> distance;
  for (int i = 0; i < k; ++i) {
    distance.push_back(1 << (k - 1 - i));
  }

Guolin Ke's avatar
Guolin Ke committed
80
81
82
  if ((1 << k) == num_machines) {
    RecursiveHalvingMap rec_map(k, RecursiveHalvingNodeType::Normal, true);
    // if num_machines = 2^k, don't need to group machines
Guolin Ke's avatar
Guolin Ke committed
83
84
    for (int i = 0; i < k; ++i) {
      // communication direction, %2 == 0 is positive
Guolin Ke's avatar
Guolin Ke committed
85
      const int dir = ((rank / distance[i]) % 2 == 0) ? 1 : -1;
Guolin Ke's avatar
Guolin Ke committed
86
      // neighbor at k-th communication
Guolin Ke's avatar
Guolin Ke committed
87
      const int next_node_idx = rank + dir * distance[i];
Guolin Ke's avatar
Guolin Ke committed
88
89
      rec_map.ranks[i] = next_node_idx;
      // receive data block at k-th communication
Guolin Ke's avatar
Guolin Ke committed
90
      const int recv_block_start = rank / distance[i];
Guolin Ke's avatar
Guolin Ke committed
91
92
93
94
95
96
97
      rec_map.recv_block_start[i] = recv_block_start * distance[i];
      rec_map.recv_block_len[i] = distance[i];
      // send data block at k-th communication
      const int send_block_start = next_node_idx / distance[i];
      rec_map.send_block_start[i] = send_block_start * distance[i];
      rec_map.send_block_len[i] = distance[i];
    }
Guolin Ke's avatar
Guolin Ke committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    return rec_map;
  } else {
    // if num_machines != 2^k, need to group machines

    int lower_power_of_2 = 1 << k;

    int rest = num_machines - lower_power_of_2;

    std::vector<RecursiveHalvingNodeType> node_type(num_machines);
    for (int i = 0; i < num_machines; ++i) {
      node_type[i] = RecursiveHalvingNodeType::Normal;
    }
    // group, two machine in one group, total "rest" groups will have 2 machines.
    for (int i = 0; i < rest; ++i) {
      int right = num_machines - i * 2 - 1;
      int left = num_machines - i * 2 - 2;
      // let left machine as group leader
      node_type[left] = RecursiveHalvingNodeType::GroupLeader;
      node_type[right] = RecursiveHalvingNodeType::Other;
    }
    int group_cnt = 0;
    // cache block information for groups, group with 2 machines will have double block size
    std::vector<int> group_block_start(lower_power_of_2);
    std::vector<int> group_block_len(lower_power_of_2, 0);
    // convert from group to node leader
    std::vector<int> group_to_node(lower_power_of_2);
    // convert from node to group
    std::vector<int> node_to_group(num_machines);

    for (int i = 0; i < num_machines; ++i) {
      // meet new group
      if (node_type[i] == RecursiveHalvingNodeType::Normal || node_type[i] == RecursiveHalvingNodeType::GroupLeader) {
        group_to_node[group_cnt++] = i;
      }
      node_to_group[i] = group_cnt - 1;
      // add block len for this group
      group_block_len[group_cnt - 1]++;
    }
    // calculate the group block start
    group_block_start[0] = 0;
    for (int i = 1; i < lower_power_of_2; ++i) {
      group_block_start[i] = group_block_start[i - 1] + group_block_len[i - 1];
    }

    RecursiveHalvingMap rec_map(k, node_type[rank], false);
    if (node_type[rank] == RecursiveHalvingNodeType::Other) {
      rec_map.neighbor = rank - 1;
      // not need to construct
      return rec_map;
    }
    if (node_type[rank] == RecursiveHalvingNodeType::GroupLeader) {
      rec_map.neighbor = rank + 1;
    }
    const int cur_group_idx = node_to_group[rank];
    for (int i = 0; i < k; ++i) {
      const int dir = ((cur_group_idx / distance[i]) % 2 == 0) ? 1 : -1;
      const int next_node_idx = group_to_node[(cur_group_idx + dir * distance[i])];
      rec_map.ranks[i] = next_node_idx;
Andrew Ziem's avatar
Andrew Ziem committed
156
      // get receive block information
Guolin Ke's avatar
Guolin Ke committed
157
158
159
160
161
162
163
164
      const int recv_block_start = cur_group_idx / distance[i];
      rec_map.recv_block_start[i] = group_block_start[recv_block_start * distance[i]];
      int recv_block_len = 0;
      // accumulate block len
      for (int j = 0; j < distance[i]; ++j) {
        recv_block_len += group_block_len[recv_block_start * distance[i] + j];
      }
      rec_map.recv_block_len[i] = recv_block_len;
Andrew Ziem's avatar
Andrew Ziem committed
165
      // get send block information
Guolin Ke's avatar
Guolin Ke committed
166
167
168
169
170
171
172
173
174
175
      const int send_block_start = (cur_group_idx + dir * distance[i]) / distance[i];
      rec_map.send_block_start[i] = group_block_start[send_block_start * distance[i]];
      int send_block_len = 0;
      // accumulate block len
      for (int j = 0; j < distance[i]; ++j) {
        send_block_len += group_block_len[send_block_start * distance[i] + j];
      }
      rec_map.send_block_len[i] = send_block_len;
    }
    return rec_map;
Guolin Ke's avatar
Guolin Ke committed
176
177
178
179
  }
}

}  // namespace LightGBM