common_graph_table.cc 93.3 KB
Newer Older
S
seemingwang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2021 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.

15
#include "paddle/fluid/distributed/ps/table/common_graph_table.h"
16

S
seemingwang 已提交
17
#include <time.h>
18

S
seemingwang 已提交
19
#include <algorithm>
20
#include <chrono>
S
seemingwang 已提交
21 22
#include <set>
#include <sstream>
23

D
danleifeng 已提交
24
#include "gflags/gflags.h"
S
seemingwang 已提交
25
#include "paddle/fluid/distributed/common/utils.h"
26
#include "paddle/fluid/distributed/ps/table/graph/graph_node.h"
L
lxsbupt 已提交
27 28
#include "paddle/fluid/framework/fleet/fleet_wrapper.h"
#include "paddle/fluid/framework/fleet/heter_ps/graph_gpu_wrapper.h"
29
#include "paddle/fluid/framework/generator.h"
D
danleifeng 已提交
30 31
#include "paddle/fluid/framework/io/fs.h"
#include "paddle/fluid/platform/timer.h"
S
seemingwang 已提交
32 33
#include "paddle/fluid/string/printf.h"
#include "paddle/fluid/string/string_helper.h"
34

D
danleifeng 已提交
35
DECLARE_bool(graph_load_in_parallel);
L
lxsbupt 已提交
36 37 38
DECLARE_bool(graph_get_neighbor_id);
DECLARE_int32(gpugraph_storage_mode);
DECLARE_uint64(gpugraph_slot_feasign_max_num);
39
DECLARE_bool(graph_metapath_split_opt);
D
danleifeng 已提交
40

S
seemingwang 已提交
41 42 43
namespace paddle {
namespace distributed {

44
#ifdef PADDLE_WITH_HETERPS
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
int32_t GraphTable::Load_to_ssd(const std::string &path,
                                const std::string &param) {
  bool load_edge = (param[0] == 'e');
  bool load_node = (param[0] == 'n');
  if (load_edge) {
    bool reverse_edge = (param[1] == '<');
    std::string edge_type = param.substr(2);
    return this->load_edges_to_ssd(path, reverse_edge, edge_type);
  }
  if (load_node) {
    std::string node_type = param.substr(1);
    return this->load_nodes(path, node_type);
  }
  return 0;
}

D
danleifeng 已提交
61
paddle::framework::GpuPsCommGraphFea GraphTable::make_gpu_ps_graph_fea(
L
lxsbupt 已提交
62 63 64 65 66 67 68 69 70
    int gpu_id, std::vector<uint64_t> &node_ids, int slot_num) {
  size_t shard_num = 64;
  std::vector<std::vector<uint64_t>> bags(shard_num);
  std::vector<uint64_t> feature_array[shard_num];
  std::vector<uint8_t> slot_id_array[shard_num];
  std::vector<uint64_t> node_id_array[shard_num];
  std::vector<paddle::framework::GpuPsFeaInfo> node_fea_info_array[shard_num];
  for (size_t i = 0; i < shard_num; i++) {
    auto predsize = node_ids.size() / shard_num;
D
danleifeng 已提交
71
    bags[i].reserve(predsize * 1.2);
L
lxsbupt 已提交
72 73 74 75
    feature_array[i].reserve(predsize * 1.2 * slot_num);
    slot_id_array[i].reserve(predsize * 1.2 * slot_num);
    node_id_array[i].reserve(predsize * 1.2);
    node_fea_info_array[i].reserve(predsize * 1.2);
D
danleifeng 已提交
76 77 78
  }

  for (auto x : node_ids) {
L
lxsbupt 已提交
79
    int location = x % shard_num;
80 81
    bags[location].push_back(x);
  }
D
danleifeng 已提交
82

83
  std::vector<std::future<int>> tasks;
L
lxsbupt 已提交
84 85 86 87 88 89 90
  if (slot_feature_num_map_.size() == 0) {
    slot_feature_num_map_.resize(slot_num);
    for (int k = 0; k < slot_num; ++k) {
      slot_feature_num_map_[k] = 0;
    }
  }

91
  for (size_t i = 0; i < bags.size(); i++) {
92
    if (bags[i].size() > 0) {
L
lxsbupt 已提交
93
      tasks.push_back(_cpu_worker_pool[gpu_id]->enqueue([&, i, this]() -> int {
D
danleifeng 已提交
94 95 96
        uint64_t node_id;
        paddle::framework::GpuPsFeaInfo x;
        std::vector<uint64_t> feature_ids;
97
        for (size_t j = 0; j < bags[i].size(); j++) {
98
          Node *v = find_node(GraphTableType::FEATURE_TABLE, bags[i][j]);
D
danleifeng 已提交
99
          node_id = bags[i][j];
100
          if (v == NULL) {
D
danleifeng 已提交
101 102 103
            x.feature_size = 0;
            x.feature_offset = 0;
            node_fea_info_array[i].push_back(x);
104
          } else {
D
danleifeng 已提交
105 106 107 108
            // x <- v
            x.feature_offset = feature_array[i].size();
            int total_feature_size = 0;
            for (int k = 0; k < slot_num; ++k) {
L
lxsbupt 已提交
109 110 111 112
              auto feature_ids_size =
                  v->get_feature_ids(k, feature_array[i], slot_id_array[i]);
              if (slot_feature_num_map_[k] < feature_ids_size) {
                slot_feature_num_map_[k] = feature_ids_size;
D
danleifeng 已提交
113
              }
L
lxsbupt 已提交
114
              total_feature_size += feature_ids_size;
D
danleifeng 已提交
115 116 117 118 119 120 121 122 123 124
            }
            x.feature_size = total_feature_size;
            node_fea_info_array[i].push_back(x);
          }
          node_id_array[i].push_back(node_id);
        }
        return 0;
      }));
    }
  }
125
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
L
lxsbupt 已提交
126

127 128 129 130 131 132
  if (FLAGS_v > 0) {
    std::stringstream ss;
    for (int k = 0; k < slot_num; ++k) {
      ss << slot_feature_num_map_[k] << " ";
    }
    VLOG(1) << "slot_feature_num_map: " << ss.str();
L
lxsbupt 已提交
133 134 135 136
  }

  tasks.clear();

D
danleifeng 已提交
137 138
  paddle::framework::GpuPsCommGraphFea res;
  uint64_t tot_len = 0;
L
lxsbupt 已提交
139
  for (size_t i = 0; i < shard_num; i++) {
D
danleifeng 已提交
140 141
    tot_len += feature_array[i].size();
  }
142
  VLOG(1) << "Loaded feature table on cpu, feature_list_size[" << tot_len
D
danleifeng 已提交
143 144 145
          << "] node_ids_size[" << node_ids.size() << "]";
  res.init_on_cpu(tot_len, (unsigned int)node_ids.size(), slot_num);
  unsigned int offset = 0, ind = 0;
L
lxsbupt 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  for (size_t i = 0; i < shard_num; i++) {
    tasks.push_back(
        _cpu_worker_pool[gpu_id]->enqueue([&, i, ind, offset, this]() -> int {
          auto start = ind;
          for (size_t j = 0; j < node_id_array[i].size(); j++) {
            res.node_list[start] = node_id_array[i][j];
            res.fea_info_list[start] = node_fea_info_array[i][j];
            res.fea_info_list[start++].feature_offset += offset;
          }
          for (size_t j = 0; j < feature_array[i].size(); j++) {
            res.feature_list[offset + j] = feature_array[i][j];
            res.slot_id_list[offset + j] = slot_id_array[i][j];
          }
          return 0;
        }));
D
danleifeng 已提交
161
    offset += feature_array[i].size();
L
lxsbupt 已提交
162
    ind += node_id_array[i].size();
D
danleifeng 已提交
163
  }
L
lxsbupt 已提交
164
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
D
danleifeng 已提交
165 166 167 168
  return res;
}

paddle::framework::GpuPsCommGraph GraphTable::make_gpu_ps_graph(
L
lxsbupt 已提交
169
    int idx, const std::vector<uint64_t> &ids) {
D
danleifeng 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  std::vector<std::vector<uint64_t>> bags(task_pool_size_);
  for (int i = 0; i < task_pool_size_; i++) {
    auto predsize = ids.size() / task_pool_size_;
    bags[i].reserve(predsize * 1.2);
  }
  for (auto x : ids) {
    int location = x % shard_num % task_pool_size_;
    bags[location].push_back(x);
  }

  std::vector<std::future<int>> tasks;
  std::vector<uint64_t> node_array[task_pool_size_];  // node id list
  std::vector<paddle::framework::GpuPsNodeInfo> info_array[task_pool_size_];
  std::vector<uint64_t> edge_array[task_pool_size_];  // edge id list

  for (size_t i = 0; i < bags.size(); i++) {
    if (bags[i].size() > 0) {
      tasks.push_back(_shards_task_pool[i]->enqueue([&, i, this]() -> int {
        node_array[i].resize(bags[i].size());
        info_array[i].resize(bags[i].size());
        edge_array[i].reserve(bags[i].size());

        for (size_t j = 0; j < bags[i].size(); j++) {
          auto node_id = bags[i][j];
          node_array[i][j] = node_id;
195
          Node *v = find_node(GraphTableType::EDGE_TABLE, idx, node_id);
D
danleifeng 已提交
196 197 198 199
          if (v != nullptr) {
            info_array[i][j].neighbor_offset = edge_array[i].size();
            info_array[i][j].neighbor_size = v->get_neighbor_size();
            for (size_t k = 0; k < v->get_neighbor_size(); k++) {
200 201
              edge_array[i].push_back(v->get_neighbor_id(k));
            }
D
danleifeng 已提交
202 203 204
          } else {
            info_array[i][j].neighbor_offset = 0;
            info_array[i][j].neighbor_size = 0;
205 206 207 208 209 210
          }
        }
        return 0;
      }));
    }
  }
211
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
D
danleifeng 已提交
212

S
seemingwang 已提交
213
  int64_t tot_len = 0;
214
  for (int i = 0; i < task_pool_size_; i++) {
215 216
    tot_len += edge_array[i].size();
  }
D
danleifeng 已提交
217 218

  paddle::framework::GpuPsCommGraph res;
S
seemingwang 已提交
219 220
  res.init_on_cpu(tot_len, ids.size());
  int64_t offset = 0, ind = 0;
221
  for (int i = 0; i < task_pool_size_; i++) {
222
    for (size_t j = 0; j < node_array[i].size(); j++) {
223
      res.node_list[ind] = node_array[i][j];
D
danleifeng 已提交
224 225
      res.node_info_list[ind] = info_array[i][j];
      res.node_info_list[ind++].neighbor_offset += offset;
226
    }
227
    for (size_t j = 0; j < edge_array[i].size(); j++) {
228 229 230 231 232 233
      res.neighbor_list[offset + j] = edge_array[i][j];
    }
    offset += edge_array[i].size();
  }
  return res;
}
234

235
int32_t GraphTable::add_node_to_ssd(
D
danleifeng 已提交
236
    int type_id, int idx, uint64_t src_id, char *data, int len) {
237
  if (_db != NULL) {
D
danleifeng 已提交
238
    char ch[sizeof(int) * 2 + sizeof(uint64_t)];
239 240
    memcpy(ch, &type_id, sizeof(int));
    memcpy(ch + sizeof(int), &idx, sizeof(int));
D
danleifeng 已提交
241
    memcpy(ch + sizeof(int) * 2, &src_id, sizeof(uint64_t));
242
    std::string str;
243 244
    if (_db->get(src_id % shard_num % task_pool_size_,
                 ch,
D
danleifeng 已提交
245
                 sizeof(int) * 2 + sizeof(uint64_t),
246
                 str) == 0) {
L
lxsbupt 已提交
247 248
      const uint64_t *stored_data =
          reinterpret_cast<const uint64_t *>(str.c_str());  // NOLINT
D
danleifeng 已提交
249 250 251 252
      int n = str.size() / sizeof(uint64_t);
      char *new_data = new char[n * sizeof(uint64_t) + len];
      memcpy(new_data, stored_data, n * sizeof(uint64_t));
      memcpy(new_data + n * sizeof(uint64_t), data, len);
253 254
      _db->put(src_id % shard_num % task_pool_size_,
               ch,
D
danleifeng 已提交
255
               sizeof(int) * 2 + sizeof(uint64_t),
L
lxsbupt 已提交
256
               reinterpret_cast<char *>(new_data),
D
danleifeng 已提交
257
               n * sizeof(uint64_t) + len);
258 259
      delete[] new_data;
    } else {
260 261
      _db->put(src_id % shard_num % task_pool_size_,
               ch,
D
danleifeng 已提交
262
               sizeof(int) * 2 + sizeof(uint64_t),
L
lxsbupt 已提交
263
               reinterpret_cast<char *>(data),
264
               len);
265
    }
266
  }
267 268 269
  return 0;
}
char *GraphTable::random_sample_neighbor_from_ssd(
270
    int idx,
D
danleifeng 已提交
271
    uint64_t id,
272 273 274
    int sample_size,
    const std::shared_ptr<std::mt19937_64> rng,
    int &actual_size) {
275 276 277 278 279
  if (_db == NULL) {
    actual_size = 0;
    return NULL;
  }
  std::string str;
S
seemingwang 已提交
280
  VLOG(2) << "sample ssd for key " << id;
D
danleifeng 已提交
281
  char ch[sizeof(int) * 2 + sizeof(uint64_t)];
282 283
  memset(ch, 0, sizeof(int));
  memcpy(ch + sizeof(int), &idx, sizeof(int));
D
danleifeng 已提交
284
  memcpy(ch + sizeof(int) * 2, &id, sizeof(uint64_t));
285 286
  if (_db->get(id % shard_num % task_pool_size_,
               ch,
D
danleifeng 已提交
287
               sizeof(int) * 2 + sizeof(uint64_t),
288
               str) == 0) {
L
lxsbupt 已提交
289
    const uint64_t *data = reinterpret_cast<const uint64_t *>(str.c_str());
D
danleifeng 已提交
290
    int n = str.size() / sizeof(uint64_t);
291
    std::unordered_map<int, int> m;
D
danleifeng 已提交
292
    // std::vector<uint64_t> res;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    int sm_size = std::min(n, sample_size);
    actual_size = sm_size * Node::id_size;
    char *buff = new char[actual_size];
    for (int i = 0; i < sm_size; i++) {
      std::uniform_int_distribution<int> distrib(0, n - i - 1);
      int t = distrib(*rng);
      // int t = rand() % (n-i);
      int pos = 0;
      auto iter = m.find(t);
      if (iter != m.end()) {
        pos = iter->second;
      } else {
        pos = t;
      }
      auto iter2 = m.find(n - i - 1);
308

309 310 311 312 313 314
      int key2 = iter2 == m.end() ? n - i - 1 : iter2->second;
      m[t] = key2;
      m.erase(n - i - 1);
      memcpy(buff + i * Node::id_size, &data[pos], Node::id_size);
      // res.push_back(data[pos]);
    }
S
seemingwang 已提交
315
    for (int i = 0; i < actual_size; i += 8) {
L
lxsbupt 已提交
316 317
      VLOG(2) << "sampled an neighbor "
              << *reinterpret_cast<uint64_t *>(&buff[i]);
S
seemingwang 已提交
318
    }
319 320 321 322 323
    return buff;
  }
  actual_size = 0;
  return NULL;
}
324 325

int64_t GraphTable::load_graph_to_memory_from_ssd(int idx,
D
danleifeng 已提交
326 327
                                                  std::vector<uint64_t> &ids) {
  std::vector<std::vector<uint64_t>> bags(task_pool_size_);
328 329 330 331 332 333 334 335 336
  for (auto x : ids) {
    int location = x % shard_num % task_pool_size_;
    bags[location].push_back(x);
  }
  std::vector<std::future<int>> tasks;
  std::vector<int64_t> count(task_pool_size_, 0);
  for (size_t i = 0; i < bags.size(); i++) {
    if (bags[i].size() > 0) {
      tasks.push_back(_shards_task_pool[i]->enqueue([&, i, idx, this]() -> int {
D
danleifeng 已提交
337
        char ch[sizeof(int) * 2 + sizeof(uint64_t)];
338 339 340 341
        memset(ch, 0, sizeof(int));
        memcpy(ch + sizeof(int), &idx, sizeof(int));
        for (size_t k = 0; k < bags[i].size(); k++) {
          auto v = bags[i][k];
D
danleifeng 已提交
342
          memcpy(ch + sizeof(int) * 2, &v, sizeof(uint64_t));
343
          std::string str;
D
danleifeng 已提交
344
          if (_db->get(i, ch, sizeof(int) * 2 + sizeof(uint64_t), str) == 0) {
345
            count[i] += (int64_t)str.size();
346
            for (size_t j = 0; j < str.size(); j += sizeof(uint64_t)) {
L
lxsbupt 已提交
347 348
              uint64_t id =
                  *reinterpret_cast<const uint64_t *>(str.c_str() + j);
349 350 351 352 353 354 355 356 357
              add_comm_edge(idx, v, id);
            }
          }
        }
        return 0;
      }));
    }
  }

358
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
359 360 361 362 363 364 365 366 367 368 369 370
  int64_t tot = 0;
  for (auto x : count) tot += x;
  return tot;
}

void GraphTable::make_partitions(int idx, int64_t byte_size, int device_len) {
  VLOG(2) << "start to make graph partitions , byte_size = " << byte_size
          << " total memory cost = " << total_memory_cost;
  if (total_memory_cost == 0) {
    VLOG(0) << "no edges are detected,make partitions exits";
    return;
  }
371 372
  auto &weight_map = node_weight[0][idx];
  const double a = 2.0, y = 1.25, weight_param = 1.0;
373 374 375 376 377 378 379 380 381
  int64_t gb_size_by_discount = byte_size * 0.8 * device_len;
  if (gb_size_by_discount <= 0) gb_size_by_discount = 1;
  int part_len = total_memory_cost / gb_size_by_discount;
  if (part_len == 0) part_len = 1;

  VLOG(2) << "part_len = " << part_len
          << " byte size = " << gb_size_by_discount;
  partitions[idx].clear();
  partitions[idx].resize(part_len);
382
  std::vector<double> weight_cost(part_len, 0);
383
  std::vector<int64_t> memory_remaining(part_len, gb_size_by_discount);
384
  std::vector<double> score(part_len, 0);
D
danleifeng 已提交
385
  std::unordered_map<uint64_t, int> id_map;
386 387 388 389 390
  std::vector<rocksdb::Iterator *> iters;
  for (int i = 0; i < task_pool_size_; i++) {
    iters.push_back(_db->get_iterator(i));
    iters[i]->SeekToFirst();
  }
391
  size_t next = 0;
392
  while (iters.size()) {
393
    if (next >= iters.size()) {
394 395 396 397 398 399 400
      next = 0;
    }
    if (!iters[next]->Valid()) {
      iters.erase(iters.begin() + next);
      continue;
    }
    std::string key = iters[next]->key().ToString();
L
lxsbupt 已提交
401 402
    int type_idx = *(reinterpret_cast<const int *>(key.c_str()));
    int temp_idx = *(reinterpret_cast<const int *>(key.c_str() + sizeof(int)));
403
    if (type_idx != 0 || temp_idx != idx) {
404 405 406 407 408
      iters[next]->Next();
      next++;
      continue;
    }
    std::string value = iters[next]->value().ToString();
409
    std::uint64_t i_key =
L
lxsbupt 已提交
410
        *reinterpret_cast<const uint64_t *>(key.c_str() + sizeof(int) * 2);
411 412 413 414 415 416 417
    for (int i = 0; i < part_len; i++) {
      if (memory_remaining[i] < (int64_t)value.size()) {
        score[i] = -100000.0;
      } else {
        score[i] = 0;
      }
    }
418
    for (size_t j = 0; j < value.size(); j += sizeof(uint64_t)) {
L
lxsbupt 已提交
419
      uint64_t v = *(reinterpret_cast<const uint64_t *>(value.c_str() + j));
420 421 422 423 424 425
      int index = -1;
      if (id_map.find(v) != id_map.end()) {
        index = id_map[v];
        score[index]++;
      }
    }
426 427 428 429 430 431 432
    double base, weight_base = 0;
    double w = 0;
    bool has_weight = false;
    if (weight_map.find(i_key) != weight_map.end()) {
      w = weight_map[i_key];
      has_weight = true;
    }
433 434
    int index = 0;
    for (int i = 0; i < part_len; i++) {
435
      base = gb_size_by_discount - memory_remaining[i] + value.size();
436
      if (has_weight) {
437
        weight_base = weight_cost[i] + w * weight_param;
438
      } else {
439 440 441
        weight_base = 0;
      }
      score[i] -= a * y * std::pow(1.0 * base, y - 1) + weight_base;
442 443 444 445 446 447 448
      if (score[i] > score[index]) index = i;
      VLOG(2) << "score" << i << " = " << score[i] << " memory left "
              << memory_remaining[i];
    }
    id_map[i_key] = index;
    partitions[idx][index].push_back(i_key);
    memory_remaining[index] -= (int64_t)value.size();
449
    if (has_weight) weight_cost[index] += w;
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    iters[next]->Next();
    next++;
  }
  for (int i = 0; i < part_len; i++) {
    if (partitions[idx][i].size() == 0) {
      partitions[idx].erase(partitions[idx].begin() + i);
      i--;
      part_len--;
      continue;
    }
    VLOG(2) << " partition " << i << " size = " << partitions[idx][i].size();
    for (auto x : partitions[idx][i]) {
      VLOG(2) << "find a id " << x;
    }
  }
  next_partition = 0;
}

468 469 470 471
void GraphTable::export_partition_files(int idx, std::string file_path) {
  int part_len = partitions[idx].size();
  if (part_len == 0) return;
  if (file_path == "") file_path = ".";
472
  if (file_path[file_path.size() - 1] != '/') {
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    file_path += "/";
  }
  std::vector<std::future<int>> tasks;
  for (int i = 0; i < part_len; i++) {
    tasks.push_back(_shards_task_pool[i % task_pool_size_]->enqueue(
        [&, i, idx, this]() -> int {
          std::string output_path =
              file_path + "partition_" + std::to_string(i);

          std::ofstream ofs(output_path);
          if (ofs.fail()) {
            VLOG(0) << "creating " << output_path << " failed";
            return 0;
          }
          for (auto x : partitions[idx][i]) {
            auto str = std::to_string(x);
            ofs.write(str.c_str(), str.size());
            ofs.write("\n", 1);
          }
          ofs.close();
          return 0;
        }));
  }

497
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
498
}
499 500
#endif

501 502
void GraphTable::clear_graph(int idx) {
  for (auto p : edge_shards[idx]) {
L
lxsbupt 已提交
503
    p->clear();
504 505 506 507 508 509 510 511
    delete p;
  }

  edge_shards[idx].clear();
  for (size_t i = 0; i < shard_num_per_server; i++) {
    edge_shards[idx].push_back(new GraphShard());
  }
}
L
lxsbupt 已提交
512

513
#ifdef PADDLE_WITH_HETERPS
L
lxsbupt 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
void GraphTable::release_graph() {
  // Before releasing graph, prepare for sampling ids and embedding keys.
  build_graph_type_keys();

  if (FLAGS_gpugraph_storage_mode ==
      paddle::framework::GpuGraphStorageMode::WHOLE_HBM) {
    build_graph_total_keys();
  }
  // clear graph
  if (FLAGS_gpugraph_storage_mode == paddle::framework::GpuGraphStorageMode::
                                         MEM_EMB_FEATURE_AND_GPU_GRAPH ||
      FLAGS_gpugraph_storage_mode == paddle::framework::GpuGraphStorageMode::
                                         SSD_EMB_AND_MEM_FEATURE_GPU_GRAPH) {
    clear_edge_shard();
  } else {
    clear_graph();
  }
}

void GraphTable::release_graph_edge() {
  if (FLAGS_gpugraph_storage_mode ==
      paddle::framework::GpuGraphStorageMode::WHOLE_HBM) {
    build_graph_total_keys();
  }
  clear_edge_shard();
}

void GraphTable::release_graph_node() {
  build_graph_type_keys();
543
  if (FLAGS_graph_metapath_split_opt) {
L
lxsbupt 已提交
544 545
    clear_feature_shard();
  } else {
546 547 548 549 550 551 552 553 554
    if (FLAGS_gpugraph_storage_mode != paddle::framework::GpuGraphStorageMode::
                                           MEM_EMB_FEATURE_AND_GPU_GRAPH &&
        FLAGS_gpugraph_storage_mode != paddle::framework::GpuGraphStorageMode::
                                           SSD_EMB_AND_MEM_FEATURE_GPU_GRAPH) {
      clear_feature_shard();
    } else {
      merge_feature_shard();
      feature_shrink_to_fit();
    }
L
lxsbupt 已提交
555 556
  }
}
557
#endif
L
lxsbupt 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

void GraphTable::clear_edge_shard() {
  VLOG(0) << "begin clear edge shard";
  std::vector<std::future<int>> tasks;
  for (auto &type_shards : edge_shards) {
    for (auto &shard : type_shards) {
      tasks.push_back(
          load_node_edge_task_pool->enqueue([&shard, this]() -> int {
            delete shard;
            return 0;
          }));
    }
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
  for (auto &shards : edge_shards) {
    shards.clear();
    for (size_t i = 0; i < shard_num_per_server; i++) {
      shards.push_back(new GraphShard());
    }
  }
  VLOG(0) << "finish clear edge shard";
}

void GraphTable::clear_feature_shard() {
  VLOG(0) << "begin clear feature shard";
  std::vector<std::future<int>> tasks;
  for (auto &type_shards : feature_shards) {
    for (auto &shard : type_shards) {
      tasks.push_back(
          load_node_edge_task_pool->enqueue([&shard, this]() -> int {
            delete shard;
            return 0;
          }));
    }
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
  for (auto &shards : feature_shards) {
    shards.clear();
    for (size_t i = 0; i < shard_num_per_server; i++) {
      shards.push_back(new GraphShard());
    }
  }
  VLOG(0) << "finish clear feature shard";
}

603
#ifdef PADDLE_WITH_HETERPS
L
lxsbupt 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
void GraphTable::feature_shrink_to_fit() {
  std::vector<std::future<int>> tasks;
  for (auto &type_shards : feature_shards) {
    for (auto &shard : type_shards) {
      tasks.push_back(
          load_node_edge_task_pool->enqueue([&shard, this]() -> int {
            shard->shrink_to_fit();
            return 0;
          }));
    }
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
}

void GraphTable::merge_feature_shard() {
  VLOG(0) << "begin merge_feature_shard";
  std::vector<std::future<int>> tasks;
  for (size_t i = 0; i < feature_shards[0].size(); i++) {
    tasks.push_back(load_node_edge_task_pool->enqueue([i, this]() -> int {
      for (size_t j = 1; j < feature_shards.size(); j++) {
        feature_shards[0][i]->merge_shard(feature_shards[j][i]);
      }
      return 0;
    }));
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
  feature_shards.resize(1);
}

633 634
#endif

L
lxsbupt 已提交
635 636 637 638 639 640 641
void GraphTable::clear_graph() {
  VLOG(0) << "begin clear_graph";
  clear_edge_shard();
  clear_feature_shard();
  VLOG(0) << "finish clear_graph";
}

642
#ifdef PADDLE_WITH_HETERPS
643
int32_t GraphTable::load_next_partition(int idx) {
644
  if (next_partition >= static_cast<int>(partitions[idx].size())) {
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    VLOG(0) << "partition iteration is done";
    return -1;
  }
  clear_graph(idx);
  load_graph_to_memory_from_ssd(idx, partitions[idx][next_partition]);
  next_partition++;
  return 0;
}
int32_t GraphTable::load_edges_to_ssd(const std::string &path,
                                      bool reverse_edge,
                                      const std::string &edge_type) {
  int idx = 0;
  if (edge_type == "") {
    VLOG(0) << "edge_type not specified, loading edges to " << id_to_edge[0]
            << " part";
  } else {
    if (edge_to_id.find(edge_type) == edge_to_id.end()) {
      VLOG(0) << "edge_type " << edge_type
              << " is not defined, nothing will be loaded";
      return 0;
    }
    idx = edge_to_id[edge_type];
  }
  total_memory_cost = 0;
  auto paths = paddle::string::split_string<std::string>(path, ";");
  int64_t count = 0;
  std::string sample_type = "random";
  for (auto path : paths) {
    std::ifstream file(path);
    std::string line;
    while (std::getline(file, line)) {
      VLOG(0) << "get a line from file " << line;
      auto values = paddle::string::split_string<std::string>(line, "\t");
      count++;
      if (values.size() < 2) continue;
      auto src_id = std::stoll(values[0]);
      auto dist_ids = paddle::string::split_string<std::string>(values[1], ";");
D
danleifeng 已提交
682
      std::vector<uint64_t> dist_data;
683 684
      for (auto x : dist_ids) {
        dist_data.push_back(std::stoll(x));
D
danleifeng 已提交
685
        total_memory_cost += sizeof(uint64_t);
686
      }
687 688 689
      add_node_to_ssd(0,
                      idx,
                      src_id,
L
lxsbupt 已提交
690
                      reinterpret_cast<char *>(dist_data.data()),
691
                      static_cast<int>(dist_data.size() * sizeof(uint64_t)));
692 693 694 695 696 697 698
    }
  }
  VLOG(0) << "total memory cost = " << total_memory_cost << " bytes";
  return 0;
}

int32_t GraphTable::dump_edges_to_ssd(int idx) {
S
seemingwang 已提交
699
  VLOG(2) << "calling dump edges to ssd";
700 701 702 703 704 705 706 707
  std::vector<std::future<int64_t>> tasks;
  auto &shards = edge_shards[idx];
  for (size_t i = 0; i < shards.size(); ++i) {
    tasks.push_back(_shards_task_pool[i % task_pool_size_]->enqueue(
        [&, i, this]() -> int64_t {
          int64_t cost = 0;
          std::vector<Node *> &v = shards[i]->get_bucket();
          for (size_t j = 0; j < v.size(); j++) {
D
danleifeng 已提交
708
            std::vector<uint64_t> s;
709
            for (size_t k = 0; k < v[j]->get_neighbor_size(); k++) {
710 711
              s.push_back(v[j]->get_neighbor_id(k));
            }
D
danleifeng 已提交
712
            cost += v[j]->get_neighbor_size() * sizeof(uint64_t);
713 714 715
            add_node_to_ssd(0,
                            idx,
                            v[j]->get_id(),
L
lxsbupt 已提交
716
                            (char *)(s.data()),  // NOLINT
D
danleifeng 已提交
717
                            s.size() * sizeof(uint64_t));
718 719 720 721 722 723 724 725 726
          }
          return cost;
        }));
  }
  for (size_t i = 0; i < tasks.size(); i++) total_memory_cost += tasks[i].get();
  return 0;
}
int32_t GraphTable::make_complementary_graph(int idx, int64_t byte_size) {
  VLOG(0) << "make_complementary_graph";
727
  const size_t fixed_size = byte_size / 8;
D
danleifeng 已提交
728
  std::vector<std::unordered_map<uint64_t, int>> count(task_pool_size_);
729 730 731 732 733 734 735 736
  std::vector<std::future<int>> tasks;
  auto &shards = edge_shards[idx];
  for (size_t i = 0; i < shards.size(); ++i) {
    tasks.push_back(
        _shards_task_pool[i % task_pool_size_]->enqueue([&, i, this]() -> int {
          std::vector<Node *> &v = shards[i]->get_bucket();
          size_t ind = i % this->task_pool_size_;
          for (size_t j = 0; j < v.size(); j++) {
S
seemingwang 已提交
737
            // size_t location = v[j]->get_id();
D
danleifeng 已提交
738
            for (size_t k = 0; k < v[j]->get_neighbor_size(); k++) {
739 740 741 742 743 744
              count[ind][v[j]->get_neighbor_id(k)]++;
            }
          }
          return 0;
        }));
  }
S
seemingwang 已提交
745
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
D
danleifeng 已提交
746 747 748
  std::unordered_map<uint64_t, int> final_count;
  std::map<int, std::vector<uint64_t>> count_to_id;
  std::vector<uint64_t> buffer;
S
seemingwang 已提交
749
  clear_graph(idx);
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766

  for (int i = 0; i < task_pool_size_; i++) {
    for (auto &p : count[i]) {
      final_count[p.first] = final_count[p.first] + p.second;
    }
    count[i].clear();
  }
  for (auto &p : final_count) {
    count_to_id[p.second].push_back(p.first);
    VLOG(2) << p.first << " appear " << p.second << " times";
  }
  auto iter = count_to_id.rbegin();
  while (iter != count_to_id.rend() && byte_size > 0) {
    for (auto x : iter->second) {
      buffer.push_back(x);
      if (buffer.size() >= fixed_size) {
        int64_t res = load_graph_to_memory_from_ssd(idx, buffer);
S
seemingwang 已提交
767
        buffer.clear();
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
        byte_size -= res;
      }
      if (byte_size <= 0) break;
    }
    iter++;
  }
  if (byte_size > 0 && buffer.size() > 0) {
    int64_t res = load_graph_to_memory_from_ssd(idx, buffer);
    byte_size -= res;
  }
  std::string sample_type = "random";
  for (auto &shard : edge_shards[idx]) {
    auto bucket = shard->get_bucket();
    for (size_t i = 0; i < bucket.size(); i++) {
      bucket[i]->build_sampler(sample_type);
    }
  }
D
danleifeng 已提交
785

786 787
  return 0;
}
788
#endif
789

790
/*
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
int CompleteGraphSampler::run_graph_sampling() {
  pthread_rwlock_t *rw_lock = graph_table->rw_lock.get();
  pthread_rwlock_rdlock(rw_lock);
  std::cout << "in graph sampling" << std::endl;
  sample_nodes.clear();
  sample_neighbors.clear();
  sample_res.clear();
  sample_nodes.resize(gpu_num);
  sample_neighbors.resize(gpu_num);
  sample_res.resize(gpu_num);
  std::vector<std::vector<std::vector<paddle::framework::GpuPsGraphNode>>>
      sample_nodes_ex(graph_table->task_pool_size_);
  std::vector<std::vector<std::vector<int64_t>>> sample_neighbors_ex(
      graph_table->task_pool_size_);
  for (int i = 0; i < graph_table->task_pool_size_; i++) {
    sample_nodes_ex[i].resize(gpu_num);
    sample_neighbors_ex[i].resize(gpu_num);
  }
  std::vector<std::future<int>> tasks;
  for (size_t i = 0; i < graph_table->shards.size(); ++i) {
    tasks.push_back(
        graph_table->_shards_task_pool[i % graph_table->task_pool_size_]
            ->enqueue([&, i, this]() -> int {
              if (this->status == GraphSamplerStatus::terminating) return 0;
              paddle::framework::GpuPsGraphNode node;
              std::vector<Node *> &v =
                  this->graph_table->shards[i]->get_bucket();
              size_t ind = i % this->graph_table->task_pool_size_;
              for (size_t j = 0; j < v.size(); j++) {
                size_t location = v[j]->get_id() % this->gpu_num;
                node.node_id = v[j]->get_id();
                node.neighbor_size = v[j]->get_neighbor_size();
                node.neighbor_offset =
L
lxsbupt 已提交
824
                    static_cast<int>sample_neighbors_ex[ind][location].size();
825 826 827 828 829 830 831 832 833 834
                sample_nodes_ex[ind][location].emplace_back(node);
                for (int k = 0; k < node.neighbor_size; k++)
                  sample_neighbors_ex[ind][location].push_back(
                      v[j]->get_neighbor_id(k));
              }
              return 0;
            }));
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
  tasks.clear();
835
  for (int i = 0; i < gpu_num; i++) {
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    tasks.push_back(
        graph_table->_shards_task_pool[i % graph_table->task_pool_size_]
            ->enqueue([&, i, this]() -> int {
              if (this->status == GraphSamplerStatus::terminating) return 0;
              int total_offset = 0;
              size_t ind = i % this->graph_table->task_pool_size_;
              for (int j = 0; j < this->graph_table->task_pool_size_; j++) {
                for (size_t k = 0; k < sample_nodes_ex[j][ind].size(); k++) {
                  sample_nodes[ind].push_back(sample_nodes_ex[j][ind][k]);
                  sample_nodes[ind].back().neighbor_offset += total_offset;
                }
                size_t neighbor_size = sample_neighbors_ex[j][ind].size();
                total_offset += neighbor_size;
                for (size_t k = 0; k < neighbor_size; k++) {
                  sample_neighbors[ind].push_back(
                      sample_neighbors_ex[j][ind][k]);
                }
              }
              return 0;
            }));
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();

  if (this->status == GraphSamplerStatus::terminating) {
    pthread_rwlock_unlock(rw_lock);
    return 0;
  }
863
  for (int i = 0; i < gpu_num; i++) {
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
    sample_res[i].node_list = sample_nodes[i].data();
    sample_res[i].neighbor_list = sample_neighbors[i].data();
    sample_res[i].node_size = sample_nodes[i].size();
    sample_res[i].neighbor_size = sample_neighbors[i].size();
  }
  pthread_rwlock_unlock(rw_lock);
  if (this->status == GraphSamplerStatus::terminating) {
    return 0;
  }
  callback(sample_res);
  return 0;
}
void CompleteGraphSampler::init(size_t gpu_num, GraphTable *graph_table,
                                std::vector<std::string> args) {
  this->gpu_num = gpu_num;
  this->graph_table = graph_table;
}

int BasicBfsGraphSampler::run_graph_sampling() {
  pthread_rwlock_t *rw_lock = graph_table->rw_lock.get();
  pthread_rwlock_rdlock(rw_lock);
  while (rounds > 0 && status == GraphSamplerStatus::running) {
    for (size_t i = 0; i < sample_neighbors_map.size(); i++) {
      sample_neighbors_map[i].clear();
    }
    sample_neighbors_map.clear();
    std::vector<int> nodes_left(graph_table->shards.size(),
                                node_num_for_each_shard);
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();
    sample_neighbors_map.resize(graph_table->task_pool_size_);
    int task_size = 0;
    std::vector<std::future<int>> tasks;
    int init_size = 0;
898 899
    //__sync_fetch_and_add
    std::function<int(int, int64_t)> bfs = [&, this](int i, int id) -> int {
900 901 902 903 904 905 906 907 908 909 910 911 912
      if (this->status == GraphSamplerStatus::terminating) {
        int task_left = __sync_sub_and_fetch(&task_size, 1);
        if (task_left == 0) {
          prom.set_value(0);
        }
        return 0;
      }
      size_t ind = i % this->graph_table->task_pool_size_;
      if (nodes_left[i] > 0) {
        auto iter = sample_neighbors_map[ind].find(id);
        if (iter == sample_neighbors_map[ind].end()) {
          Node *node = graph_table->shards[i]->find_node(id);
          if (node != NULL) {
913 914 915
            nodes_left[i]--;
            sample_neighbors_map[ind][id] = std::vector<int64_t>();
            iter = sample_neighbors_map[ind].find(id);
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
            size_t edge_fetch_size =
                std::min((size_t) this->edge_num_for_each_node,
                         node->get_neighbor_size());
            for (size_t k = 0; k < edge_fetch_size; k++) {
              int64_t neighbor_id = node->get_neighbor_id(k);
              int node_location = neighbor_id % this->graph_table->shard_num %
                                  this->graph_table->task_pool_size_;
              __sync_add_and_fetch(&task_size, 1);
              graph_table->_shards_task_pool[node_location]->enqueue(
                  bfs, neighbor_id % this->graph_table->shard_num, neighbor_id);
              iter->second.push_back(neighbor_id);
            }
          }
        }
      }
      int task_left = __sync_sub_and_fetch(&task_size, 1);
      if (task_left == 0) {
        prom.set_value(0);
      }
      return 0;
    };
    for (size_t i = 0; i < graph_table->shards.size(); ++i) {
      std::vector<Node *> &v = graph_table->shards[i]->get_bucket();
      if (v.size() > 0) {
L
lxsbupt 已提交
940
        int search_size = std::min(init_search_size, static_cast<int>v.size());
941 942 943 944 945 946 947
        for (int k = 0; k < search_size; k++) {
          init_size++;
          __sync_add_and_fetch(&task_size, 1);
          int64_t id = v[k]->get_id();
          graph_table->_shards_task_pool[i % graph_table->task_pool_size_]
              ->enqueue(bfs, i, id);
        }
948 949 950 951 952 953 954 955 956 957
      }  // if
    }
    if (init_size == 0) {
      prom.set_value(0);
    }
    fut.get();
    if (this->status == GraphSamplerStatus::terminating) {
      pthread_rwlock_unlock(rw_lock);
      return 0;
    }
958
    VLOG(0) << "BasicBfsGraphSampler finishes the graph searching task";
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
    sample_nodes.clear();
    sample_neighbors.clear();
    sample_res.clear();
    sample_nodes.resize(gpu_num);
    sample_neighbors.resize(gpu_num);
    sample_res.resize(gpu_num);
    std::vector<std::vector<std::vector<paddle::framework::GpuPsGraphNode>>>
        sample_nodes_ex(graph_table->task_pool_size_);
    std::vector<std::vector<std::vector<int64_t>>> sample_neighbors_ex(
        graph_table->task_pool_size_);
    for (int i = 0; i < graph_table->task_pool_size_; i++) {
      sample_nodes_ex[i].resize(gpu_num);
      sample_neighbors_ex[i].resize(gpu_num);
    }
    tasks.clear();
    for (size_t i = 0; i < (size_t)graph_table->task_pool_size_; ++i) {
      tasks.push_back(
          graph_table->_shards_task_pool[i]->enqueue([&, i, this]() -> int {
            if (this->status == GraphSamplerStatus::terminating) {
              return 0;
            }
            paddle::framework::GpuPsGraphNode node;
            auto iter = sample_neighbors_map[i].begin();
            size_t ind = i;
            for (; iter != sample_neighbors_map[i].end(); iter++) {
              size_t location = iter->first % this->gpu_num;
              node.node_id = iter->first;
              node.neighbor_size = iter->second.size();
              node.neighbor_offset =
L
lxsbupt 已提交
988
                  static_cast<int>sample_neighbors_ex[ind][location].size();
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
              sample_nodes_ex[ind][location].emplace_back(node);
              for (auto k : iter->second)
                sample_neighbors_ex[ind][location].push_back(k);
            }
            return 0;
          }));
    }

    for (size_t i = 0; i < tasks.size(); i++) {
      tasks[i].get();
      sample_neighbors_map[i].clear();
    }
    tasks.clear();
    if (this->status == GraphSamplerStatus::terminating) {
      pthread_rwlock_unlock(rw_lock);
      return 0;
    }
1006
    for (size_t i = 0; i < (size_t)gpu_num; i++) {
1007 1008 1009 1010 1011 1012 1013 1014 1015
      tasks.push_back(
          graph_table->_shards_task_pool[i % graph_table->task_pool_size_]
              ->enqueue([&, i, this]() -> int {
                if (this->status == GraphSamplerStatus::terminating) {
                  pthread_rwlock_unlock(rw_lock);
                  return 0;
                }
                int total_offset = 0;
                for (int j = 0; j < this->graph_table->task_pool_size_; j++) {
1016 1017
                  for (size_t k = 0; k < sample_nodes_ex[j][i].size(); k++) {
                    sample_nodes[i].push_back(sample_nodes_ex[j][i][k]);
1018 1019
                    sample_nodes[i].back().neighbor_offset += total_offset;
                  }
1020
                  size_t neighbor_size = sample_neighbors_ex[j][i].size();
1021 1022
                  total_offset += neighbor_size;
                  for (size_t k = 0; k < neighbor_size; k++) {
1023
                    sample_neighbors[i].push_back(sample_neighbors_ex[j][i][k]);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
                  }
                }
                return 0;
              }));
    }
    for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
    if (this->status == GraphSamplerStatus::terminating) {
      pthread_rwlock_unlock(rw_lock);
      return 0;
    }
1034
    for (int i = 0; i < gpu_num; i++) {
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
      sample_res[i].node_list = sample_nodes[i].data();
      sample_res[i].neighbor_list = sample_neighbors[i].data();
      sample_res[i].node_size = sample_nodes[i].size();
      sample_res[i].neighbor_size = sample_neighbors[i].size();
    }
    pthread_rwlock_unlock(rw_lock);
    if (this->status == GraphSamplerStatus::terminating) {
      return 0;
    }
    callback(sample_res);
    rounds--;
    if (rounds > 0) {
      for (int i = 0;
           i < interval && this->status == GraphSamplerStatus::running; i++) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
      }
    }
1052
    VLOG(0)<<"bfs returning";
1053 1054 1055 1056 1057 1058 1059
  }
  return 0;
}
void BasicBfsGraphSampler::init(size_t gpu_num, GraphTable *graph_table,
                                std::vector<std::string> args) {
  this->gpu_num = gpu_num;
  this->graph_table = graph_table;
1060 1061 1062 1063 1064
  init_search_size = args.size() > 0 ? std::stoi(args[0]) : 10;
  node_num_for_each_shard = args.size() > 1 ? std::stoi(args[1]) : 10;
  edge_num_for_each_node = args.size() > 2 ? std::stoi(args[2]) : 10;
  rounds = args.size() > 3 ? std::stoi(args[3]) : 1;
  interval = args.size() > 4 ? std::stoi(args[4]) : 60;
1065 1066 1067
}

#endif
1068
*/
S
seemingwang 已提交
1069 1070 1071
std::vector<Node *> GraphShard::get_batch(int start, int end, int step) {
  if (start < 0) start = 0;
  std::vector<Node *> res;
L
lxsbupt 已提交
1072
  for (int pos = start; pos < std::min(end, static_cast<int>(bucket.size()));
1073
       pos += step) {
S
seemingwang 已提交
1074 1075 1076 1077 1078 1079 1080
    res.push_back(bucket[pos]);
  }
  return res;
}

size_t GraphShard::get_size() { return bucket.size(); }

D
danleifeng 已提交
1081
int32_t GraphTable::add_comm_edge(int idx, uint64_t src_id, uint64_t dst_id) {
1082 1083 1084 1085 1086 1087
  size_t src_shard_id = src_id % shard_num;

  if (src_shard_id >= shard_end || src_shard_id < shard_start) {
    return -1;
  }
  size_t index = src_shard_id - shard_start;
1088 1089
  edge_shards[idx][index]->add_graph_node(src_id)->build_edges(false);
  edge_shards[idx][index]->add_neighbor(src_id, dst_id, 1.0);
1090 1091
  return 0;
}
1092
int32_t GraphTable::add_graph_node(int idx,
D
danleifeng 已提交
1093
                                   std::vector<uint64_t> &id_list,
1094
                                   std::vector<bool> &is_weight_list) {
1095
  auto &shards = edge_shards[idx];
1096
  size_t node_size = id_list.size();
D
danleifeng 已提交
1097
  std::vector<std::vector<std::pair<uint64_t, bool>>> batch(task_pool_size_);
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
  for (size_t i = 0; i < node_size; i++) {
    size_t shard_id = id_list[i] % shard_num;
    if (shard_id >= shard_end || shard_id < shard_start) {
      continue;
    }
    batch[get_thread_pool_index(id_list[i])].push_back(
        {id_list[i], i < is_weight_list.size() ? is_weight_list[i] : false});
  }
  std::vector<std::future<int>> tasks;
  for (size_t i = 0; i < batch.size(); ++i) {
    if (!batch[i].size()) continue;
1109 1110 1111 1112 1113 1114 1115 1116
    tasks.push_back(
        _shards_task_pool[i]->enqueue([&shards, &batch, i, this]() -> int {
          for (auto &p : batch[i]) {
            size_t index = p.first % this->shard_num - this->shard_start;
            shards[index]->add_graph_node(p.first)->build_edges(p.second);
          }
          return 0;
        }));
1117 1118 1119 1120 1121
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
  return 0;
}

D
danleifeng 已提交
1122
int32_t GraphTable::remove_graph_node(int idx, std::vector<uint64_t> &id_list) {
1123
  size_t node_size = id_list.size();
D
danleifeng 已提交
1124
  std::vector<std::vector<uint64_t>> batch(task_pool_size_);
1125 1126 1127 1128 1129
  for (size_t i = 0; i < node_size; i++) {
    size_t shard_id = id_list[i] % shard_num;
    if (shard_id >= shard_end || shard_id < shard_start) continue;
    batch[get_thread_pool_index(id_list[i])].push_back(id_list[i]);
  }
1130
  auto &shards = edge_shards[idx];
1131 1132 1133
  std::vector<std::future<int>> tasks;
  for (size_t i = 0; i < batch.size(); ++i) {
    if (!batch[i].size()) continue;
1134 1135 1136 1137 1138 1139 1140 1141
    tasks.push_back(
        _shards_task_pool[i]->enqueue([&shards, &batch, i, this]() -> int {
          for (auto &p : batch[i]) {
            size_t index = p % this->shard_num - this->shard_start;
            shards[index]->delete_node(p);
          }
          return 0;
        }));
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
  return 0;
}

void GraphShard::clear() {
  for (size_t i = 0; i < bucket.size(); i++) {
    delete bucket[i];
  }
  bucket.clear();
  node_location.clear();
}

GraphShard::~GraphShard() { clear(); }
1156

D
danleifeng 已提交
1157
void GraphShard::delete_node(uint64_t id) {
1158 1159 1160 1161
  auto iter = node_location.find(id);
  if (iter == node_location.end()) return;
  int pos = iter->second;
  delete bucket[pos];
1162
  if (pos != static_cast<int>(bucket.size()) - 1) {
1163 1164 1165 1166 1167 1168
    bucket[pos] = bucket.back();
    node_location[bucket.back()->get_id()] = pos;
  }
  node_location.erase(id);
  bucket.pop_back();
}
D
danleifeng 已提交
1169
GraphNode *GraphShard::add_graph_node(uint64_t id) {
S
seemingwang 已提交
1170 1171 1172 1173
  if (node_location.find(id) == node_location.end()) {
    node_location[id] = bucket.size();
    bucket.push_back(new GraphNode(id));
  }
L
lxsbupt 已提交
1174
  return reinterpret_cast<GraphNode *>(bucket[node_location[id]]);
S
seemingwang 已提交
1175 1176
}

1177 1178 1179 1180 1181 1182
GraphNode *GraphShard::add_graph_node(Node *node) {
  auto id = node->get_id();
  if (node_location.find(id) == node_location.end()) {
    node_location[id] = bucket.size();
    bucket.push_back(node);
  }
L
lxsbupt 已提交
1183
  return reinterpret_cast<GraphNode *>(bucket[node_location[id]]);
1184
}
D
danleifeng 已提交
1185 1186

FeatureNode *GraphShard::add_feature_node(uint64_t id, bool is_overlap) {
S
seemingwang 已提交
1187 1188 1189
  if (node_location.find(id) == node_location.end()) {
    node_location[id] = bucket.size();
    bucket.push_back(new FeatureNode(id));
L
lxsbupt 已提交
1190
    return reinterpret_cast<FeatureNode *>(bucket[node_location[id]]);
D
danleifeng 已提交
1191 1192
  }
  if (is_overlap) {
L
lxsbupt 已提交
1193
    return reinterpret_cast<FeatureNode *>(bucket[node_location[id]]);
S
seemingwang 已提交
1194
  }
D
danleifeng 已提交
1195 1196

  return NULL;
S
seemingwang 已提交
1197 1198
}

D
danleifeng 已提交
1199
void GraphShard::add_neighbor(uint64_t id, uint64_t dst_id, float weight) {
S
seemingwang 已提交
1200 1201 1202
  find_node(id)->add_edge(dst_id, weight);
}

D
danleifeng 已提交
1203
Node *GraphShard::find_node(uint64_t id) {
S
seemingwang 已提交
1204 1205 1206 1207
  auto iter = node_location.find(id);
  return iter == node_location.end() ? nullptr : bucket[iter->second];
}

1208
GraphTable::~GraphTable() {
L
lxsbupt 已提交
1209 1210 1211
#ifdef PADDLE_WITH_GPU_GRAPH
  clear_graph();
#endif
1212 1213
}

Z
zhaocaibei123 已提交
1214
int32_t GraphTable::Load(const std::string &path, const std::string &param) {
S
seemingwang 已提交
1215 1216 1217 1218
  bool load_edge = (param[0] == 'e');
  bool load_node = (param[0] == 'n');
  if (load_edge) {
    bool reverse_edge = (param[1] == '<');
1219
    std::string edge_type = param.substr(2);
1220 1221 1222 1223 1224 1225
    int ret = this->load_edges(path, reverse_edge, edge_type);
    if (ret != 0) {
      VLOG(0) << "Fail to load edges, path[" << path << "] edge_type["
              << edge_type << "]";
      return -1;
    }
S
seemingwang 已提交
1226 1227 1228
  }
  if (load_node) {
    std::string node_type = param.substr(1);
1229 1230 1231 1232 1233 1234
    int ret = this->load_nodes(path, node_type);
    if (ret != 0) {
      VLOG(0) << "Fail to load nodes, path[" << path << "] node_type["
              << node_type << "]";
      return -1;
    }
S
seemingwang 已提交
1235 1236 1237 1238
  }
  return 0;
}

D
danleifeng 已提交
1239 1240 1241
std::string GraphTable::get_inverse_etype(std::string &etype) {
  auto etype_split = paddle::string::split_string<std::string>(etype, "2");
  std::string res;
1242
  if (etype_split.size() == 3) {
D
danleifeng 已提交
1243 1244 1245 1246 1247 1248 1249
    res = etype_split[2] + "2" + etype_split[1] + "2" + etype_split[0];
  } else {
    res = etype_split[1] + "2" + etype_split[0];
  }
  return res;
}

L
lxsbupt 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
int32_t GraphTable::parse_type_to_typepath(
    std::string &type2files,
    std::string graph_data_local_path,
    std::vector<std::string> &res_type,
    std::unordered_map<std::string, std::string> &res_type2path) {
  auto type2files_split =
      paddle::string::split_string<std::string>(type2files, ",");
  if (type2files_split.size() == 0) {
    return -1;
  }
  for (auto one_type2file : type2files_split) {
    auto one_type2file_split =
        paddle::string::split_string<std::string>(one_type2file, ":");
    auto type = one_type2file_split[0];
    auto type_dir = one_type2file_split[1];
    res_type.push_back(type);
    res_type2path[type] = graph_data_local_path + "/" + type_dir;
  }
  return 0;
}

1271 1272 1273 1274 1275 1276
int32_t GraphTable::parse_edge_and_load(
    std::string etype2files,
    std::string graph_data_local_path,
    int part_num,
    bool reverse,
    const std::vector<bool> &is_reverse_edge_map) {
L
lxsbupt 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
  std::vector<std::string> etypes;
  std::unordered_map<std::string, std::string> edge_to_edgedir;
  int res = parse_type_to_typepath(
      etype2files, graph_data_local_path, etypes, edge_to_edgedir);
  if (res != 0) {
    VLOG(0) << "parse edge type and edgedir failed!";
    return -1;
  }
  VLOG(0) << "etypes size: " << etypes.size();
  VLOG(0) << "whether reverse: " << reverse;
  is_load_reverse_edge = reverse;
  std::string delim = ";";
  size_t total_len = etypes.size();

  std::vector<std::future<int>> tasks;
  for (size_t i = 0; i < total_len; i++) {
    tasks.push_back(
        _shards_task_pool[i % task_pool_size_]->enqueue([&, i, this]() -> int {
          std::string etype_path = edge_to_edgedir[etypes[i]];
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
          bool only_load_reverse_edge = false;
          if (!reverse) {
            only_load_reverse_edge = is_reverse_edge_map[i];
          }
          if (only_load_reverse_edge) {
            VLOG(1) << "only_load_reverse_edge is True, etype[" << etypes[i]
                    << "], file_path[" << etype_path << "]";
          } else {
            VLOG(1) << "only_load_reverse_edge is False, etype[" << etypes[i]
                    << "], file_path[" << etype_path << "]";
          }
L
lxsbupt 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
          auto etype_path_list = paddle::framework::localfs_list(etype_path);
          std::string etype_path_str;
          if (part_num > 0 &&
              part_num < static_cast<int>(etype_path_list.size())) {
            std::vector<std::string> sub_etype_path_list(
                etype_path_list.begin(), etype_path_list.begin() + part_num);
            etype_path_str =
                paddle::string::join_strings(sub_etype_path_list, delim);
          } else {
            etype_path_str =
                paddle::string::join_strings(etype_path_list, delim);
          }
1319 1320 1321 1322 1323 1324 1325 1326
          if (!only_load_reverse_edge) {
            this->load_edges(etype_path_str, false, etypes[i]);
            if (reverse) {
              std::string r_etype = get_inverse_etype(etypes[i]);
              this->load_edges(etype_path_str, true, r_etype);
            }
          } else {
            this->load_edges(etype_path_str, true, etypes[i]);
L
lxsbupt 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
          }
          return 0;
        }));
  }
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
  return 0;
}

int32_t GraphTable::parse_node_and_load(std::string ntype2files,
                                        std::string graph_data_local_path,
                                        int part_num) {
  std::vector<std::string> ntypes;
  std::unordered_map<std::string, std::string> node_to_nodedir;
  int res = parse_type_to_typepath(
      ntype2files, graph_data_local_path, ntypes, node_to_nodedir);
  if (res != 0) {
    VLOG(0) << "parse node type and nodedir failed!";
    return -1;
  }
  std::string delim = ";";
  std::string npath = node_to_nodedir[ntypes[0]];
  auto npath_list = paddle::framework::localfs_list(npath);
  std::string npath_str;
  if (part_num > 0 && part_num < static_cast<int>(npath_list.size())) {
    std::vector<std::string> sub_npath_list(npath_list.begin(),
                                            npath_list.begin() + part_num);
    npath_str = paddle::string::join_strings(sub_npath_list, delim);
  } else {
    npath_str = paddle::string::join_strings(npath_list, delim);
  }

  if (ntypes.size() == 0) {
    VLOG(0) << "node_type not specified, nothing will be loaded ";
    return 0;
  }
  if (FLAGS_graph_load_in_parallel) {
1363 1364 1365 1366 1367
    int ret = this->load_nodes(npath_str, "");
    if (ret != 0) {
      VLOG(0) << "Fail to load nodes, path[" << npath << "]";
      return -1;
    }
L
lxsbupt 已提交
1368 1369
  } else {
    for (size_t j = 0; j < ntypes.size(); j++) {
1370 1371 1372 1373 1374 1375
      int ret = this->load_nodes(npath_str, ntypes[j]);
      if (ret != 0) {
        VLOG(0) << "Fail to load nodes, path[" << npath << "], ntypes["
                << ntypes[j] << "]";
        return -1;
      }
L
lxsbupt 已提交
1376 1377 1378 1379 1380
    }
  }
  return 0;
}

1381 1382 1383 1384 1385 1386 1387
int32_t GraphTable::load_node_and_edge_file(
    std::string etype2files,
    std::string ntype2files,
    std::string graph_data_local_path,
    int part_num,
    bool reverse,
    const std::vector<bool> &is_reverse_edge_map) {
L
lxsbupt 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
  std::vector<std::string> etypes;
  std::unordered_map<std::string, std::string> edge_to_edgedir;
  int res = parse_type_to_typepath(
      etype2files, graph_data_local_path, etypes, edge_to_edgedir);
  if (res != 0) {
    VLOG(0) << "parse edge type and edgedir failed!";
    return -1;
  }
  std::vector<std::string> ntypes;
  std::unordered_map<std::string, std::string> node_to_nodedir;
  res = parse_type_to_typepath(
      ntype2files, graph_data_local_path, ntypes, node_to_nodedir);
  if (res != 0) {
    VLOG(0) << "parse node type and nodedir failed!";
    return -1;
  }

D
danleifeng 已提交
1405 1406
  VLOG(0) << "etypes size: " << etypes.size();
  VLOG(0) << "whether reverse: " << reverse;
L
lxsbupt 已提交
1407
  is_load_reverse_edge = reverse;
D
danleifeng 已提交
1408 1409 1410 1411 1412 1413 1414 1415
  std::string delim = ";";
  size_t total_len = etypes.size() + 1;  // 1 is for node

  std::vector<std::future<int>> tasks;
  for (size_t i = 0; i < total_len; i++) {
    tasks.push_back(
        _shards_task_pool[i % task_pool_size_]->enqueue([&, i, this]() -> int {
          if (i < etypes.size()) {
L
lxsbupt 已提交
1416
            std::string etype_path = edge_to_edgedir[etypes[i]];
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
            bool only_load_reverse_edge = false;
            if (!reverse) {
              only_load_reverse_edge = is_reverse_edge_map[i];
            }
            if (only_load_reverse_edge) {
              VLOG(1) << "only_load_reverse_edge is True, etype[" << etypes[i]
                      << "], file_path[" << etype_path << "]";
            } else {
              VLOG(1) << "only_load_reverse_edge is False, etype[" << etypes[i]
                      << "], file_path[" << etype_path << "]";
            }
D
danleifeng 已提交
1428 1429
            auto etype_path_list = paddle::framework::localfs_list(etype_path);
            std::string etype_path_str;
1430
            if (part_num > 0 &&
L
lxsbupt 已提交
1431
                part_num < static_cast<int>(etype_path_list.size())) {
D
danleifeng 已提交
1432 1433 1434 1435 1436 1437 1438 1439
              std::vector<std::string> sub_etype_path_list(
                  etype_path_list.begin(), etype_path_list.begin() + part_num);
              etype_path_str =
                  paddle::string::join_strings(sub_etype_path_list, delim);
            } else {
              etype_path_str =
                  paddle::string::join_strings(etype_path_list, delim);
            }
1440 1441 1442 1443 1444 1445 1446 1447
            if (!only_load_reverse_edge) {
              this->load_edges(etype_path_str, false, etypes[i]);
              if (reverse) {
                std::string r_etype = get_inverse_etype(etypes[i]);
                this->load_edges(etype_path_str, true, r_etype);
              }
            } else {
              this->load_edges(etype_path_str, true, etypes[i]);
D
danleifeng 已提交
1448 1449
            }
          } else {
L
lxsbupt 已提交
1450
            std::string npath = node_to_nodedir[ntypes[0]];
D
danleifeng 已提交
1451 1452
            auto npath_list = paddle::framework::localfs_list(npath);
            std::string npath_str;
L
lxsbupt 已提交
1453 1454
            if (part_num > 0 &&
                part_num < static_cast<int>(npath_list.size())) {
D
danleifeng 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
              std::vector<std::string> sub_npath_list(
                  npath_list.begin(), npath_list.begin() + part_num);
              npath_str = paddle::string::join_strings(sub_npath_list, delim);
            } else {
              npath_str = paddle::string::join_strings(npath_list, delim);
            }

            if (ntypes.size() == 0) {
              VLOG(0) << "node_type not specified, nothing will be loaded ";
              return 0;
            }
            if (FLAGS_graph_load_in_parallel) {
1467 1468 1469 1470 1471
              int ret = this->load_nodes(npath_str, "");
              if (ret != 0) {
                VLOG(0) << "Fail to load nodes, path[" << npath_str << "]";
                return -1;
              }
D
danleifeng 已提交
1472 1473
            } else {
              for (size_t j = 0; j < ntypes.size(); j++) {
1474 1475 1476 1477 1478 1479
                int ret = this->load_nodes(npath_str, ntypes[j]);
                if (ret != 0) {
                  VLOG(0) << "Fail to load nodes, path[" << npath_str
                          << "], ntypes[" << ntypes[j] << "]";
                  return -1;
                }
D
danleifeng 已提交
1480 1481 1482 1483 1484 1485
              }
            }
          }
          return 0;
        }));
  }
1486
  for (size_t i = 0; i < tasks.size(); i++) tasks[i].get();
1487 1488 1489 1490
  if (is_parse_node_fail_) {
    VLOG(0) << "Fail to load node_and_edge_file";
    return -1;
  }
D
danleifeng 已提交
1491 1492 1493
  return 0;
}

S
seemingwang 已提交
1494
int32_t GraphTable::get_nodes_ids_by_ranges(
1495
    GraphTableType table_type,
1496 1497
    int idx,
    std::vector<std::pair<int, int>> ranges,
D
danleifeng 已提交
1498 1499
    std::vector<uint64_t> &res) {
  std::mutex mutex;
S
seemingwang 已提交
1500 1501
  int start = 0, end, index = 0, total_size = 0;
  res.clear();
1502 1503
  auto &shards = table_type == GraphTableType::EDGE_TABLE ? edge_shards[idx]
                                                          : feature_shards[idx];
D
danleifeng 已提交
1504
  std::vector<std::future<size_t>> tasks;
L
lxsbupt 已提交
1505 1506
  for (size_t i = 0;
       i < shards.size() && index < static_cast<int>(ranges.size());
1507
       i++) {
1508
    end = total_size + shards[i]->get_size();
S
seemingwang 已提交
1509
    start = total_size;
1510 1511
    while (start < end && index < static_cast<int>(ranges.size())) {
      if (ranges[index].second <= start) {
S
seemingwang 已提交
1512
        index++;
1513
      } else if (ranges[index].first >= end) {
S
seemingwang 已提交
1514 1515 1516 1517 1518 1519 1520 1521
        break;
      } else {
        int first = std::max(ranges[index].first, start);
        int second = std::min(ranges[index].second, end);
        start = second;
        first -= total_size;
        second -= total_size;
        tasks.push_back(_shards_task_pool[i % task_pool_size_]->enqueue(
D
danleifeng 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530
            [&shards, this, first, second, i, &res, &mutex]() -> size_t {
              std::vector<uint64_t> keys;
              shards[i]->get_ids_by_range(first, second, &keys);

              size_t num = keys.size();
              mutex.lock();
              res.reserve(res.size() + num);
              for (auto &id : keys) {
                res.push_back(id);
1531
                std::swap(res[rand() % res.size()],
L
lxsbupt 已提交
1532
                          res[static_cast<int>(res.size()) - 1]);
D
danleifeng 已提交
1533 1534 1535 1536
              }
              mutex.unlock();

              return num;
S
seemingwang 已提交
1537 1538 1539
            }));
      }
    }
1540
    total_size += shards[i]->get_size();
S
seemingwang 已提交
1541
  }
1542
  for (size_t i = 0; i < tasks.size(); i++) {
D
danleifeng 已提交
1543
    tasks[i].get();
S
seemingwang 已提交
1544 1545 1546 1547
  }
  return 0;
}

D
danleifeng 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
std::pair<uint64_t, uint64_t> GraphTable::parse_node_file(
    const std::string &path, const std::string &node_type, int idx) {
  std::ifstream file(path);
  std::string line;
  uint64_t local_count = 0;
  uint64_t local_valid_count = 0;

  int num = 0;
  std::vector<paddle::string::str_ptr> vals;
  size_t n = node_type.length();
  while (std::getline(file, line)) {
    if (strncmp(line.c_str(), node_type.c_str(), n) != 0) {
      continue;
1561
    }
D
danleifeng 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
    vals.clear();
    num = paddle::string::split_string_ptr(
        line.c_str() + n + 1, line.length() - n - 1, '\t', &vals);
    if (num == 0) {
      continue;
    }
    uint64_t id = std::strtoul(vals[0].ptr, NULL, 10);
    size_t shard_id = id % shard_num;
    if (shard_id >= shard_end || shard_id < shard_start) {
      VLOG(4) << "will not load " << id << " from " << path
              << ", please check id distribution";
      continue;
    }
    local_count++;
S
seemingwang 已提交
1576

D
danleifeng 已提交
1577 1578 1579 1580 1581 1582
    size_t index = shard_id - shard_start;
    auto node = feature_shards[idx][index]->add_feature_node(id, false);
    if (node != NULL) {
      node->set_feature_size(feat_name[idx].size());
      for (int i = 1; i < num; ++i) {
        auto &v = vals[i];
1583 1584 1585 1586 1587 1588
        int ret = parse_feature(idx, v.ptr, v.len, node);
        if (ret != 0) {
          VLOG(0) << "Fail to parse feature, node_id[" << id << "]";
          is_parse_node_fail_ = true;
          return {0, 0};
        }
S
seemingwang 已提交
1589
      }
D
danleifeng 已提交
1590 1591 1592 1593 1594 1595 1596
    }
    local_valid_count++;
  }
  VLOG(2) << "node_type[" << node_type << "] loads " << local_count
          << " nodes from filepath->" << path;
  return {local_count, local_valid_count};
}
S
seemingwang 已提交
1597

D
danleifeng 已提交
1598 1599 1600 1601 1602 1603 1604
std::pair<uint64_t, uint64_t> GraphTable::parse_node_file(
    const std::string &path) {
  std::ifstream file(path);
  std::string line;
  uint64_t local_count = 0;
  uint64_t local_valid_count = 0;
  int idx = 0;
S
seemingwang 已提交
1605

D
danleifeng 已提交
1606 1607
  auto path_split = paddle::string::split_string<std::string>(path, "/");
  auto path_name = path_split[path_split.size() - 1];
S
seemingwang 已提交
1608

D
danleifeng 已提交
1609 1610
  int num = 0;
  std::vector<paddle::string::str_ptr> vals;
S
seemingwang 已提交
1611

D
danleifeng 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
  while (std::getline(file, line)) {
    vals.clear();
    num = paddle::string::split_string_ptr(
        line.c_str(), line.length(), '\t', &vals);
    if (vals.empty()) {
      continue;
    }
    std::string parse_node_type = vals[0].to_string();
    auto it = feature_to_id.find(parse_node_type);
    if (it == feature_to_id.end()) {
      VLOG(0) << parse_node_type << "type error, please check";
      continue;
    }
    idx = it->second;
    uint64_t id = std::strtoul(vals[1].ptr, NULL, 10);
    size_t shard_id = id % shard_num;
    if (shard_id >= shard_end || shard_id < shard_start) {
      VLOG(4) << "will not load " << id << " from " << path
              << ", please check id distribution";
      continue;
    }
    local_count++;

    size_t index = shard_id - shard_start;
    auto node = feature_shards[idx][index]->add_feature_node(id, false);
    if (node != NULL) {
      for (int i = 2; i < num; ++i) {
        auto &v = vals[i];
1640 1641 1642 1643 1644 1645
        int ret = parse_feature(idx, v.ptr, v.len, node);
        if (ret != 0) {
          VLOG(0) << "Fail to parse feature, node_id[" << id << "]";
          is_parse_node_fail_ = true;
          return {0, 0};
        }
D
danleifeng 已提交
1646 1647 1648 1649 1650 1651 1652 1653
      }
    }
    local_valid_count++;
  }
  VLOG(2) << local_valid_count << "/" << local_count << " nodes from filepath->"
          << path;
  return {local_count, local_valid_count};
}
S
seemingwang 已提交
1654

1655
// // TODO(danleifeng): opt load all node_types in once reading
D
danleifeng 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
int32_t GraphTable::load_nodes(const std::string &path, std::string node_type) {
  auto paths = paddle::string::split_string<std::string>(path, ";");
  uint64_t count = 0;
  uint64_t valid_count = 0;
  int idx = 0;
  if (FLAGS_graph_load_in_parallel) {
    if (node_type == "") {
      VLOG(0) << "Begin GraphTable::load_nodes(), will load all node_type once";
    }
    std::vector<std::future<std::pair<uint64_t, uint64_t>>> tasks;
    for (size_t i = 0; i < paths.size(); i++) {
      tasks.push_back(load_node_edge_task_pool->enqueue(
          [&, i, this]() -> std::pair<uint64_t, uint64_t> {
            return parse_node_file(paths[i]);
          }));
    }
1672
    for (size_t i = 0; i < tasks.size(); i++) {
D
danleifeng 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
      auto res = tasks[i].get();
      count += res.first;
      valid_count += res.second;
    }
  } else {
    VLOG(0) << "Begin GraphTable::load_nodes() node_type[" << node_type << "]";
    if (node_type == "") {
      VLOG(0) << "node_type not specified, loading edges to "
              << id_to_feature[0] << " part";
    } else {
      if (feature_to_id.find(node_type) == feature_to_id.end()) {
        VLOG(0) << "node_type " << node_type
                << " is not defined, nothing will be loaded";
        return 0;
S
seemingwang 已提交
1687
      }
D
danleifeng 已提交
1688 1689 1690 1691 1692 1693 1694
      idx = feature_to_id[node_type];
    }
    for (auto path : paths) {
      VLOG(2) << "Begin GraphTable::load_nodes(), path[" << path << "]";
      auto res = parse_node_file(path, node_type, idx);
      count += res.first;
      valid_count += res.second;
S
seemingwang 已提交
1695 1696
    }
  }
1697 1698 1699 1700 1701
  if (is_parse_node_fail_) {
    VLOG(0) << "Fail to load nodes, path[" << paths[0] << ".."
            << paths[paths.size() - 1] << "] node_type[" << node_type << "]";
    return -1;
  }
S
seemingwang 已提交
1702

D
danleifeng 已提交
1703 1704
  VLOG(0) << valid_count << "/" << count << " nodes in node_type[ " << node_type
          << "] are loaded successfully!";
S
seemingwang 已提交
1705 1706 1707
  return 0;
}

1708 1709 1710 1711 1712 1713 1714 1715 1716
int32_t GraphTable::build_sampler(int idx, std::string sample_type) {
  for (auto &shard : edge_shards[idx]) {
    auto bucket = shard->get_bucket();
    for (size_t i = 0; i < bucket.size(); i++) {
      bucket[i]->build_sampler(sample_type);
    }
  }
  return 0;
}
D
danleifeng 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771

std::pair<uint64_t, uint64_t> GraphTable::parse_edge_file(
    const std::string &path, int idx, bool reverse) {
  std::string sample_type = "random";
  bool is_weighted = false;
  std::ifstream file(path);
  std::string line;
  uint64_t local_count = 0;
  uint64_t local_valid_count = 0;
  uint64_t part_num = 0;
  if (FLAGS_graph_load_in_parallel) {
    auto path_split = paddle::string::split_string<std::string>(path, "/");
    auto part_name_split = paddle::string::split_string<std::string>(
        path_split[path_split.size() - 1], "-");
    part_num = std::stoull(part_name_split[part_name_split.size() - 1]);
  }

  while (std::getline(file, line)) {
    size_t start = line.find_first_of('\t');
    if (start == std::string::npos) continue;
    local_count++;
    uint64_t src_id = std::stoull(&line[0]);
    uint64_t dst_id = std::stoull(&line[start + 1]);
    if (reverse) {
      std::swap(src_id, dst_id);
    }
    size_t src_shard_id = src_id % shard_num;
    if (FLAGS_graph_load_in_parallel) {
      if (src_shard_id != (part_num % shard_num)) {
        continue;
      }
    }

    float weight = 1;
    size_t last = line.find_last_of('\t');
    if (start != last) {
      weight = std::stof(&line[last + 1]);
      sample_type = "weighted";
      is_weighted = true;
    }

    if (src_shard_id >= shard_end || src_shard_id < shard_start) {
      VLOG(4) << "will not load " << src_id << " from " << path
              << ", please check id distribution";
      continue;
    }
    size_t index = src_shard_id - shard_start;
    auto node = edge_shards[idx][index]->add_graph_node(src_id);
    if (node != NULL) {
      node->build_edges(is_weighted);
      node->add_edge(dst_id, weight);
    }

    local_valid_count++;
  }
1772 1773
  VLOG(2) << local_valid_count << "/" << local_count
          << " edges are loaded from filepath->" << path;
D
danleifeng 已提交
1774 1775 1776
  return {local_count, local_valid_count};
}

1777 1778
int32_t GraphTable::load_edges(const std::string &path,
                               bool reverse_edge,
1779
                               const std::string &edge_type) {
1780 1781 1782
#ifdef PADDLE_WITH_HETERPS
  if (search_level == 2) total_memory_cost = 0;
#endif
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
  int idx = 0;
  if (edge_type == "") {
    VLOG(0) << "edge_type not specified, loading edges to " << id_to_edge[0]
            << " part";
  } else {
    if (edge_to_id.find(edge_type) == edge_to_id.end()) {
      VLOG(0) << "edge_type " << edge_type
              << " is not defined, nothing will be loaded";
      return 0;
    }
    idx = edge_to_id[edge_type];
  }
1795

S
seemingwang 已提交
1796
  auto paths = paddle::string::split_string<std::string>(path, ";");
D
danleifeng 已提交
1797 1798 1799 1800 1801 1802
  uint64_t count = 0;
  uint64_t valid_count = 0;

  VLOG(0) << "Begin GraphTable::load_edges() edge_type[" << edge_type << "]";
  if (FLAGS_graph_load_in_parallel) {
    std::vector<std::future<std::pair<uint64_t, uint64_t>>> tasks;
1803
    for (size_t i = 0; i < paths.size(); i++) {
D
danleifeng 已提交
1804 1805 1806 1807 1808
      tasks.push_back(load_node_edge_task_pool->enqueue(
          [&, i, idx, this]() -> std::pair<uint64_t, uint64_t> {
            return parse_edge_file(paths[i], idx, reverse_edge);
          }));
    }
1809
    for (size_t j = 0; j < tasks.size(); j++) {
D
danleifeng 已提交
1810 1811 1812 1813 1814 1815 1816 1817 1818
      auto res = tasks[j].get();
      count += res.first;
      valid_count += res.second;
    }
  } else {
    for (auto path : paths) {
      auto res = parse_edge_file(path, idx, reverse_edge);
      count += res.first;
      valid_count += res.second;
S
seemingwang 已提交
1819 1820
    }
  }
D
danleifeng 已提交
1821 1822
  VLOG(0) << valid_count << "/" << count << " edge_type[" << edge_type
          << "] edges are loaded successfully";
L
lxsbupt 已提交
1823 1824
  std::string edge_size = edge_type + ":" + std::to_string(valid_count);
  edge_type_size.push_back(edge_size);
S
seemingwang 已提交
1825

1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
#ifdef PADDLE_WITH_HETERPS
  if (search_level == 2) {
    if (count > 0) {
      dump_edges_to_ssd(idx);
      VLOG(0) << "dumping edges to ssd, edge count is reset to 0";
      clear_graph(idx);
      count = 0;
    }
    return 0;
  }
#endif
D
danleifeng 已提交
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850

  if (!build_sampler_on_cpu) {
    // To reduce memory overhead, CPU samplers won't be created in gpugraph.
    // In order not to affect the sampler function of other scenario,
    // this optimization is only performed in load_edges function.
    VLOG(0) << "run in gpugraph mode!";
  } else {
    std::string sample_type = "random";
    VLOG(0) << "build sampler ... ";
    for (auto &shard : edge_shards[idx]) {
      auto bucket = shard->get_bucket();
      for (size_t i = 0; i < bucket.size(); i++) {
        bucket[i]->build_sampler(sample_type);
      }
1851 1852 1853
    }
  }

S
seemingwang 已提交
1854 1855 1856
  return 0;
}

1857
Node *GraphTable::find_node(GraphTableType table_type, uint64_t id) {
D
danleifeng 已提交
1858 1859 1860 1861 1862 1863
  size_t shard_id = id % shard_num;
  if (shard_id >= shard_end || shard_id < shard_start) {
    return nullptr;
  }
  Node *node = nullptr;
  size_t index = shard_id - shard_start;
1864 1865
  auto &search_shards =
      table_type == GraphTableType::EDGE_TABLE ? edge_shards : feature_shards;
D
danleifeng 已提交
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
  for (auto &search_shard : search_shards) {
    PADDLE_ENFORCE_NOT_NULL(search_shard[index],
                            paddle::platform::errors::InvalidArgument(
                                "search_shard[%d] should not be null.", index));
    node = search_shard[index]->find_node(id);
    if (node != nullptr) {
      break;
    }
  }
  return node;
}

1878
Node *GraphTable::find_node(GraphTableType table_type, int idx, uint64_t id) {
S
seemingwang 已提交
1879 1880
  size_t shard_id = id % shard_num;
  if (shard_id >= shard_end || shard_id < shard_start) {
1881
    return nullptr;
S
seemingwang 已提交
1882 1883
  }
  size_t index = shard_id - shard_start;
1884 1885 1886
  auto &search_shards = table_type == GraphTableType::EDGE_TABLE
                            ? edge_shards[idx]
                            : feature_shards[idx];
D
danleifeng 已提交
1887 1888 1889
  PADDLE_ENFORCE_NOT_NULL(search_shards[index],
                          paddle::platform::errors::InvalidArgument(
                              "search_shard[%d] should not be null.", index));
1890
  Node *node = search_shards[index]->find_node(id);
S
seemingwang 已提交
1891 1892
  return node;
}
D
danleifeng 已提交
1893
uint32_t GraphTable::get_thread_pool_index(uint64_t node_id) {
1894
  return node_id % shard_num % shard_num_per_server % task_pool_size_;
S
seemingwang 已提交
1895
}
1896

D
danleifeng 已提交
1897 1898
uint32_t GraphTable::get_thread_pool_index_by_shard_index(
    uint64_t shard_index) {
S
seemingwang 已提交
1899
  return shard_index % shard_num_per_server % task_pool_size_;
1900 1901
}

1902 1903 1904 1905
int32_t GraphTable::clear_nodes(GraphTableType table_type, int idx) {
  auto &search_shards = table_type == GraphTableType::EDGE_TABLE
                            ? edge_shards[idx]
                            : feature_shards[idx];
Z
zhangchunle 已提交
1906
  for (size_t i = 0; i < search_shards.size(); i++) {
1907
    search_shards[i]->clear();
1908 1909 1910 1911
  }
  return 0;
}

1912
int32_t GraphTable::random_sample_nodes(GraphTableType table_type,
1913 1914
                                        int idx,
                                        int sample_size,
S
seemingwang 已提交
1915 1916 1917
                                        std::unique_ptr<char[]> &buffer,
                                        int &actual_size) {
  int total_size = 0;
1918 1919 1920
  auto &shards = table_type == GraphTableType::EDGE_TABLE ? edge_shards[idx]
                                                          : feature_shards[idx];
  for (int i = 0; i < (int)shards.size(); i++) {
1921
    total_size += shards[i]->get_size();
S
seemingwang 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930
  }
  if (sample_size > total_size) sample_size = total_size;
  int range_num = random_sample_nodes_ranges;
  if (range_num > sample_size) range_num = sample_size;
  if (sample_size == 0 || range_num == 0) return 0;
  std::vector<int> ranges_len, ranges_pos;
  int remain = sample_size, last_pos = -1, num;
  std::set<int> separator_set;
  for (int i = 0; i < range_num - 1; i++) {
L
lxsbupt 已提交
1931
    while (separator_set.find(num = rand() % (sample_size - 1)) !=  // NOLINT
1932 1933
           separator_set.end()) {
    }
S
seemingwang 已提交
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
    separator_set.insert(num);
  }
  for (auto p : separator_set) {
    ranges_len.push_back(p - last_pos);
    last_pos = p;
  }
  ranges_len.push_back(sample_size - 1 - last_pos);
  remain = total_size - sample_size + range_num;
  separator_set.clear();
  for (int i = 0; i < range_num; i++) {
L
lxsbupt 已提交
1944
    while (separator_set.find(num = rand() % remain) !=  // NOLINT
1945 1946
           separator_set.end()) {
    }
S
seemingwang 已提交
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
    separator_set.insert(num);
  }
  int used = 0, index = 0;
  last_pos = -1;
  for (auto p : separator_set) {
    used += p - last_pos - 1;
    last_pos = p;
    ranges_pos.push_back(used);
    used += ranges_len[index++];
  }
  std::vector<std::pair<int, int>> first_half, second_half;
L
lxsbupt 已提交
1958
  int start_index = rand() % total_size;  // NOLINT
1959
  for (size_t i = 0; i < ranges_len.size() && i < ranges_pos.size(); i++) {
1960
    if (ranges_pos[i] + ranges_len[i] - 1 + start_index < total_size) {
S
seemingwang 已提交
1961 1962
      first_half.push_back({ranges_pos[i] + start_index,
                            ranges_pos[i] + ranges_len[i] + start_index});
L
lxsbupt 已提交
1963
    } else if ((ranges_pos[i] + start_index) >= total_size) {
S
seemingwang 已提交
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
      second_half.push_back(
          {ranges_pos[i] + start_index - total_size,
           ranges_pos[i] + ranges_len[i] + start_index - total_size});
    } else {
      first_half.push_back({ranges_pos[i] + start_index, total_size});
      second_half.push_back(
          {0, ranges_pos[i] + ranges_len[i] + start_index - total_size});
    }
  }
  for (auto &pair : first_half) second_half.push_back(pair);
D
danleifeng 已提交
1974
  std::vector<uint64_t> res;
1975
  get_nodes_ids_by_ranges(table_type, idx, second_half, res);
D
danleifeng 已提交
1976
  actual_size = res.size() * sizeof(uint64_t);
S
seemingwang 已提交
1977 1978 1979 1980 1981
  buffer.reset(new char[actual_size]);
  char *pointer = buffer.get();
  memcpy(pointer, res.data(), actual_size);
  return 0;
}
1982
int32_t GraphTable::random_sample_neighbors(
1983
    int idx,
D
danleifeng 已提交
1984
    uint64_t *node_ids,
1985 1986 1987
    int sample_size,
    std::vector<std::shared_ptr<char>> &buffers,
    std::vector<int> &actual_sizes,
1988
    bool need_weight) {
S
seemingwang 已提交
1989
  size_t node_num = buffers.size();
1990
  std::function<void(char *)> char_del = [](char *c) { delete[] c; };
S
seemingwang 已提交
1991
  std::vector<std::future<int>> tasks;
1992 1993
  std::vector<std::vector<uint32_t>> seq_id(task_pool_size_);
  std::vector<std::vector<SampleKey>> id_list(task_pool_size_);
S
seemingwang 已提交
1994
  size_t index;
1995 1996 1997 1998
  for (size_t idy = 0; idy < node_num; ++idy) {
    index = get_thread_pool_index(node_ids[idy]);
    seq_id[index].emplace_back(idy);
    id_list[index].emplace_back(idx, node_ids[idy], sample_size, need_weight);
S
seemingwang 已提交
1999
  }
2000

2001
  for (size_t i = 0; i < seq_id.size(); i++) {
S
seemingwang 已提交
2002 2003
    if (seq_id[i].size() == 0) continue;
    tasks.push_back(_shards_task_pool[i]->enqueue([&, i, this]() -> int {
D
danleifeng 已提交
2004
      uint64_t node_id;
S
seemingwang 已提交
2005 2006 2007 2008
      std::vector<std::pair<SampleKey, SampleResult>> r;
      LRUResponse response = LRUResponse::blocked;
      if (use_cache) {
        response =
2009
            scaled_lru->query(i, id_list[i].data(), id_list[i].size(), r);
S
seemingwang 已提交
2010
      }
2011
      size_t index = 0;
S
seemingwang 已提交
2012 2013 2014 2015
      std::vector<SampleResult> sample_res;
      std::vector<SampleKey> sample_keys;
      auto &rng = _shards_task_rng_pool[i];
      for (size_t k = 0; k < id_list[i].size(); k++) {
2016
        if (index < r.size() &&
S
seemingwang 已提交
2017
            r[index].first.node_key == id_list[i][k].node_key) {
2018 2019 2020
          int idy = seq_id[i][k];
          actual_sizes[idy] = r[index].second.actual_size;
          buffers[idy] = r[index].second.buffer;
S
seemingwang 已提交
2021 2022 2023
          index++;
        } else {
          node_id = id_list[i][k].node_key;
2024
          Node *node = find_node(GraphTableType::EDGE_TABLE, idx, node_id);
2025 2026
          int idy = seq_id[i][k];
          int &actual_size = actual_sizes[idy];
S
seemingwang 已提交
2027
          if (node == nullptr) {
2028 2029
#ifdef PADDLE_WITH_HETERPS
            if (search_level == 2) {
S
seemingwang 已提交
2030
              VLOG(2) << "enter sample from ssd for node_id " << node_id;
2031
              char *buffer_addr = random_sample_neighbor_from_ssd(
2032
                  idx, node_id, sample_size, rng, actual_size);
2033
              if (actual_size != 0) {
S
seemingwang 已提交
2034
                std::shared_ptr<char> &buffer = buffers[idy];
2035 2036
                buffer.reset(buffer_addr, char_del);
              }
S
seemingwang 已提交
2037
              VLOG(2) << "actual sampled size from ssd = " << actual_sizes[idy];
2038 2039 2040
              continue;
            }
#endif
S
seemingwang 已提交
2041 2042 2043
            actual_size = 0;
            continue;
          }
2044
          std::shared_ptr<char> &buffer = buffers[idy];
S
seemingwang 已提交
2045
          std::vector<int> res = node->sample_k(sample_size, rng);
2046 2047 2048
          actual_size =
              res.size() * (need_weight ? (Node::id_size + Node::weight_size)
                                        : Node::id_size);
S
seemingwang 已提交
2049
          int offset = 0;
D
danleifeng 已提交
2050
          uint64_t id;
S
seemingwang 已提交
2051 2052 2053
          float weight;
          char *buffer_addr = new char[actual_size];
          if (response == LRUResponse::ok) {
2054
            sample_keys.emplace_back(idx, node_id, sample_size, need_weight);
S
seemingwang 已提交
2055 2056
            sample_res.emplace_back(actual_size, buffer_addr);
            buffer = sample_res.back().buffer;
2057
          } else {
S
seemingwang 已提交
2058 2059 2060 2061 2062 2063
            buffer.reset(buffer_addr, char_del);
          }
          for (int &x : res) {
            id = node->get_neighbor_id(x);
            memcpy(buffer_addr + offset, &id, Node::id_size);
            offset += Node::id_size;
2064 2065 2066 2067 2068
            if (need_weight) {
              weight = node->get_neighbor_weight(x);
              memcpy(buffer_addr + offset, &weight, Node::weight_size);
              offset += Node::weight_size;
            }
2069 2070
          }
        }
2071
      }
S
seemingwang 已提交
2072
      if (sample_res.size()) {
2073 2074
        scaled_lru->insert(
            i, sample_keys.data(), sample_res.data(), sample_keys.size());
2075 2076 2077
      }
      return 0;
    }));
S
seemingwang 已提交
2078
  }
S
seemingwang 已提交
2079 2080
  for (auto &t : tasks) {
    t.get();
S
seemingwang 已提交
2081 2082 2083 2084
  }
  return 0;
}

2085
int32_t GraphTable::get_node_feat(int idx,
D
danleifeng 已提交
2086
                                  const std::vector<uint64_t> &node_ids,
S
seemingwang 已提交
2087 2088 2089 2090
                                  const std::vector<std::string> &feature_names,
                                  std::vector<std::vector<std::string>> &res) {
  size_t node_num = node_ids.size();
  std::vector<std::future<int>> tasks;
2091
  for (size_t idy = 0; idy < node_num; ++idy) {
D
danleifeng 已提交
2092
    uint64_t node_id = node_ids[idy];
S
seemingwang 已提交
2093
    tasks.push_back(_shards_task_pool[get_thread_pool_index(node_id)]->enqueue(
2094
        [&, idx, idy, node_id]() -> int {
2095
          Node *node = find_node(GraphTableType::FEATURE_TABLE, idx, node_id);
S
seemingwang 已提交
2096 2097 2098 2099

          if (node == nullptr) {
            return 0;
          }
2100
          for (size_t feat_idx = 0; feat_idx < feature_names.size();
2101
               ++feat_idx) {
S
seemingwang 已提交
2102
            const std::string &feature_name = feature_names[feat_idx];
2103
            if (feat_id_map[idx].find(feature_name) != feat_id_map[idx].end()) {
S
seemingwang 已提交
2104 2105
              // res[feat_idx][idx] =
              // node->get_feature(feat_id_map[feature_name]);
2106 2107
              auto feat = node->get_feature(feat_id_map[idx][feature_name]);
              res[feat_idx][idy] = feat;
S
seemingwang 已提交
2108 2109 2110 2111
            }
          }
          return 0;
        }));
S
seemingwang 已提交
2112
  }
2113 2114
  for (size_t idy = 0; idy < node_num; ++idy) {
    tasks[idy].get();
S
seemingwang 已提交
2115 2116 2117 2118 2119
  }
  return 0;
}

int32_t GraphTable::set_node_feat(
2120
    int idx,
D
danleifeng 已提交
2121
    const std::vector<uint64_t> &node_ids,
S
seemingwang 已提交
2122 2123 2124 2125
    const std::vector<std::string> &feature_names,
    const std::vector<std::vector<std::string>> &res) {
  size_t node_num = node_ids.size();
  std::vector<std::future<int>> tasks;
2126
  for (size_t idy = 0; idy < node_num; ++idy) {
D
danleifeng 已提交
2127
    uint64_t node_id = node_ids[idy];
S
seemingwang 已提交
2128
    tasks.push_back(_shards_task_pool[get_thread_pool_index(node_id)]->enqueue(
2129
        [&, idx, idy, node_id]() -> int {
S
seemingwang 已提交
2130
          size_t index = node_id % this->shard_num - this->shard_start;
2131 2132
          auto node = feature_shards[idx][index]->add_feature_node(node_id);
          node->set_feature_size(this->feat_name[idx].size());
2133
          for (size_t feat_idx = 0; feat_idx < feature_names.size();
2134
               ++feat_idx) {
S
seemingwang 已提交
2135
            const std::string &feature_name = feature_names[feat_idx];
2136 2137 2138
            if (feat_id_map[idx].find(feature_name) != feat_id_map[idx].end()) {
              node->set_feature(feat_id_map[idx][feature_name],
                                res[feat_idx][idy]);
S
seemingwang 已提交
2139 2140 2141 2142
            }
          }
          return 0;
        }));
S
seemingwang 已提交
2143
  }
2144 2145
  for (size_t idy = 0; idy < node_num; ++idy) {
    tasks[idy].get();
S
seemingwang 已提交
2146 2147 2148 2149
  }
  return 0;
}

D
danleifeng 已提交
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
void string_vector_2_string(std::vector<std::string>::iterator strs_begin,
                            std::vector<std::string>::iterator strs_end,
                            char delim,
                            std::string *output) {
  size_t i = 0;
  for (std::vector<std::string>::iterator iter = strs_begin; iter != strs_end;
       ++iter) {
    if (i > 0) {
      *output += delim;
    }

    *output += *iter;
    ++i;
  }
}

void string_vector_2_string(
    std::vector<paddle::string::str_ptr>::iterator strs_begin,
    std::vector<paddle::string::str_ptr>::iterator strs_end,
    char delim,
    std::string *output) {
  size_t i = 0;
  for (auto iter = strs_begin; iter != strs_end; ++iter) {
    if (i > 0) {
      output->append(&delim, 1);
    }
    output->append((*iter).ptr, (*iter).len);
    ++i;
  }
}

int GraphTable::parse_feature(int idx,
                              const char *feat_str,
                              size_t len,
                              FeatureNode *node) {
S
seemingwang 已提交
2185 2186
  // Return (feat_id, btyes) if name are in this->feat_name, else return (-1,
  // "")
D
danleifeng 已提交
2187 2188
  thread_local std::vector<paddle::string::str_ptr> fields;
  fields.clear();
L
lxsbupt 已提交
2189
  char c = slot_feature_separator_.at(0);
D
danleifeng 已提交
2190 2191
  paddle::string::split_string_ptr(feat_str, len, c, &fields);

L
lxsbupt 已提交
2192 2193 2194 2195 2196 2197 2198 2199
  thread_local std::vector<paddle::string::str_ptr> fea_fields;
  fea_fields.clear();
  c = feature_separator_.at(0);
  paddle::string::split_string_ptr(fields[1].ptr,
                                   fields[1].len,
                                   c,
                                   &fea_fields,
                                   FLAGS_gpugraph_slot_feasign_max_num);
D
danleifeng 已提交
2200 2201 2202 2203 2204
  std::string name = fields[0].to_string();
  auto it = feat_id_map[idx].find(name);
  if (it != feat_id_map[idx].end()) {
    int32_t id = it->second;
    std::string *fea_ptr = node->mutable_feature(id);
2205
    std::string dtype = this->feat_dtype[idx][id];
S
seemingwang 已提交
2206
    if (dtype == "feasign") {
D
danleifeng 已提交
2207 2208
      //      string_vector_2_string(fields.begin() + 1, fields.end(), ' ',
      //      fea_ptr);
2209
      int ret = FeatureNode::parse_value_to_bytes<uint64_t>(
L
lxsbupt 已提交
2210
          fea_fields.begin(), fea_fields.end(), fea_ptr);
2211 2212 2213 2214
      if (ret != 0) {
        VLOG(0) << "Fail to parse value";
        return -1;
      }
D
danleifeng 已提交
2215
      return 0;
S
seemingwang 已提交
2216
    } else if (dtype == "string") {
L
lxsbupt 已提交
2217 2218
      string_vector_2_string(
          fea_fields.begin(), fea_fields.end(), ' ', fea_ptr);
D
danleifeng 已提交
2219
      return 0;
S
seemingwang 已提交
2220
    } else if (dtype == "float32") {
2221
      int ret = FeatureNode::parse_value_to_bytes<float>(
L
lxsbupt 已提交
2222
          fea_fields.begin(), fea_fields.end(), fea_ptr);
2223 2224 2225 2226
      if (ret != 0) {
        VLOG(0) << "Fail to parse value";
        return -1;
      }
D
danleifeng 已提交
2227
      return 0;
S
seemingwang 已提交
2228
    } else if (dtype == "float64") {
2229
      int ret = FeatureNode::parse_value_to_bytes<double>(
L
lxsbupt 已提交
2230
          fea_fields.begin(), fea_fields.end(), fea_ptr);
2231 2232 2233 2234
      if (ret != 0) {
        VLOG(0) << "Fail to parse value";
        return -1;
      }
D
danleifeng 已提交
2235
      return 0;
S
seemingwang 已提交
2236
    } else if (dtype == "int32") {
2237
      int ret = FeatureNode::parse_value_to_bytes<int32_t>(
L
lxsbupt 已提交
2238
          fea_fields.begin(), fea_fields.end(), fea_ptr);
2239 2240 2241 2242
      if (ret != 0) {
        VLOG(0) << "Fail to parse value";
        return -1;
      }
D
danleifeng 已提交
2243
      return 0;
S
seemingwang 已提交
2244
    } else if (dtype == "int64") {
2245
      int ret = FeatureNode::parse_value_to_bytes<uint64_t>(
L
lxsbupt 已提交
2246
          fea_fields.begin(), fea_fields.end(), fea_ptr);
2247 2248 2249 2250
      if (ret != 0) {
        VLOG(0) << "Fail to parse value";
        return -1;
      }
D
danleifeng 已提交
2251 2252 2253
      return 0;
    }
  } else {
2254
    VLOG(4) << "feature_name[" << name << "] is not in feat_id_map, ntype_id["
D
danleifeng 已提交
2255 2256 2257
            << idx << "] feat_id_map_size[" << feat_id_map.size() << "]";
  }

2258
  return 0;
D
danleifeng 已提交
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
}
// thread safe shard vector merge
class MergeShardVector {
 public:
  MergeShardVector(std::vector<std::vector<uint64_t>> *output, int slice_num) {
    _slice_num = slice_num;
    _shard_keys = output;
    _shard_keys->resize(slice_num);
    _mutexs = new std::mutex[slice_num];
  }
  ~MergeShardVector() {
    if (_mutexs != nullptr) {
      delete[] _mutexs;
      _mutexs = nullptr;
    }
  }
  // merge shard keys
  void merge(const std::vector<std::vector<uint64_t>> &shard_keys) {
    // add to shard
    for (int shard_id = 0; shard_id < _slice_num; ++shard_id) {
      auto &dest = (*_shard_keys)[shard_id];
      auto &src = shard_keys[shard_id];

      _mutexs[shard_id].lock();
      dest.insert(dest.end(), src.begin(), src.end());
      _mutexs[shard_id].unlock();
    }
  }

 private:
  int _slice_num = 0;
  std::mutex *_mutexs = nullptr;
  std::vector<std::vector<uint64_t>> *_shard_keys;
};

2294
int GraphTable::get_all_id(GraphTableType table_type,
D
danleifeng 已提交
2295 2296 2297
                           int slice_num,
                           std::vector<std::vector<uint64_t>> *output) {
  MergeShardVector shard_merge(output, slice_num);
2298 2299
  auto &search_shards =
      table_type == GraphTableType::EDGE_TABLE ? edge_shards : feature_shards;
D
danleifeng 已提交
2300
  std::vector<std::future<size_t>> tasks;
2301 2302
  for (size_t idx = 0; idx < search_shards.size(); idx++) {
    for (size_t j = 0; j < search_shards[idx].size(); j++) {
D
danleifeng 已提交
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
      tasks.push_back(_shards_task_pool[j % task_pool_size_]->enqueue(
          [&search_shards, idx, j, slice_num, &shard_merge]() -> size_t {
            std::vector<std::vector<uint64_t>> shard_keys;
            size_t num =
                search_shards[idx][j]->get_all_id(&shard_keys, slice_num);
            // add to shard
            shard_merge.merge(shard_keys);
            return num;
          }));
    }
  }
  for (size_t i = 0; i < tasks.size(); ++i) {
    tasks[i].wait();
  }
  return 0;
}

int GraphTable::get_all_neighbor_id(
2321 2322 2323
    GraphTableType table_type,
    int slice_num,
    std::vector<std::vector<uint64_t>> *output) {
D
danleifeng 已提交
2324
  MergeShardVector shard_merge(output, slice_num);
2325 2326
  auto &search_shards =
      table_type == GraphTableType::EDGE_TABLE ? edge_shards : feature_shards;
D
danleifeng 已提交
2327
  std::vector<std::future<size_t>> tasks;
2328 2329
  for (size_t idx = 0; idx < search_shards.size(); idx++) {
    for (size_t j = 0; j < search_shards[idx].size(); j++) {
D
danleifeng 已提交
2330 2331 2332 2333 2334 2335 2336 2337 2338
      tasks.push_back(_shards_task_pool[j % task_pool_size_]->enqueue(
          [&search_shards, idx, j, slice_num, &shard_merge]() -> size_t {
            std::vector<std::vector<uint64_t>> shard_keys;
            size_t num = search_shards[idx][j]->get_all_neighbor_id(&shard_keys,
                                                                    slice_num);
            // add to shard
            shard_merge.merge(shard_keys);
            return num;
          }));
S
seemingwang 已提交
2339 2340
    }
  }
D
danleifeng 已提交
2341 2342 2343 2344
  for (size_t i = 0; i < tasks.size(); ++i) {
    tasks[i].wait();
  }
  return 0;
S
seemingwang 已提交
2345 2346
}

2347
int GraphTable::get_all_id(GraphTableType table_type,
D
danleifeng 已提交
2348 2349 2350 2351
                           int idx,
                           int slice_num,
                           std::vector<std::vector<uint64_t>> *output) {
  MergeShardVector shard_merge(output, slice_num);
2352 2353 2354
  auto &search_shards = table_type == GraphTableType::EDGE_TABLE
                            ? edge_shards[idx]
                            : feature_shards[idx];
D
danleifeng 已提交
2355 2356
  std::vector<std::future<size_t>> tasks;
  VLOG(3) << "begin task, task_pool_size_[" << task_pool_size_ << "]";
Z
zhangchunle 已提交
2357
  for (size_t i = 0; i < search_shards.size(); i++) {
2358
    tasks.push_back(_shards_task_pool[i % task_pool_size_]->enqueue(
D
danleifeng 已提交
2359 2360 2361 2362 2363 2364
        [&search_shards, i, slice_num, &shard_merge]() -> size_t {
          std::vector<std::vector<uint64_t>> shard_keys;
          size_t num = search_shards[i]->get_all_id(&shard_keys, slice_num);
          // add to shard
          shard_merge.merge(shard_keys);
          return num;
2365 2366 2367 2368 2369
        }));
  }
  for (size_t i = 0; i < tasks.size(); ++i) {
    tasks[i].wait();
  }
D
danleifeng 已提交
2370 2371 2372 2373 2374
  VLOG(3) << "end task, task_pool_size_[" << task_pool_size_ << "]";
  return 0;
}

int GraphTable::get_all_neighbor_id(
2375
    GraphTableType table_type,
D
danleifeng 已提交
2376 2377 2378 2379
    int idx,
    int slice_num,
    std::vector<std::vector<uint64_t>> *output) {
  MergeShardVector shard_merge(output, slice_num);
2380 2381 2382
  auto &search_shards = table_type == GraphTableType::EDGE_TABLE
                            ? edge_shards[idx]
                            : feature_shards[idx];
D
danleifeng 已提交
2383 2384
  std::vector<std::future<size_t>> tasks;
  VLOG(3) << "begin task, task_pool_size_[" << task_pool_size_ << "]";
2385
  for (size_t i = 0; i < search_shards.size(); i++) {
D
danleifeng 已提交
2386 2387 2388 2389 2390 2391 2392 2393 2394
    tasks.push_back(_shards_task_pool[i % task_pool_size_]->enqueue(
        [&search_shards, i, slice_num, &shard_merge]() -> size_t {
          std::vector<std::vector<uint64_t>> shard_keys;
          size_t num =
              search_shards[i]->get_all_neighbor_id(&shard_keys, slice_num);
          // add to shard
          shard_merge.merge(shard_keys);
          return num;
        }));
2395
  }
D
danleifeng 已提交
2396 2397 2398 2399 2400
  for (size_t i = 0; i < tasks.size(); ++i) {
    tasks[i].wait();
  }
  VLOG(3) << "end task, task_pool_size_[" << task_pool_size_ << "]";
  return 0;
2401
}
D
danleifeng 已提交
2402 2403

int GraphTable::get_all_feature_ids(
2404
    GraphTableType table_type,
D
danleifeng 已提交
2405 2406 2407 2408
    int idx,
    int slice_num,
    std::vector<std::vector<uint64_t>> *output) {
  MergeShardVector shard_merge(output, slice_num);
2409 2410 2411
  auto &search_shards = table_type == GraphTableType::EDGE_TABLE
                            ? edge_shards[idx]
                            : feature_shards[idx];
D
danleifeng 已提交
2412
  std::vector<std::future<size_t>> tasks;
2413
  for (size_t i = 0; i < search_shards.size(); i++) {
D
danleifeng 已提交
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
    tasks.push_back(_shards_task_pool[i % task_pool_size_]->enqueue(
        [&search_shards, i, slice_num, &shard_merge]() -> size_t {
          std::vector<std::vector<uint64_t>> shard_keys;
          size_t num =
              search_shards[i]->get_all_feature_ids(&shard_keys, slice_num);
          // add to shard
          shard_merge.merge(shard_keys);
          return num;
        }));
  }
  for (size_t i = 0; i < tasks.size(); ++i) {
    tasks[i].wait();
  }
  return 0;
}

L
lxsbupt 已提交
2430 2431
int GraphTable::get_node_embedding_ids(
    int slice_num, std::vector<std::vector<uint64_t>> *output) {
2432 2433
  if (is_load_reverse_edge and !FLAGS_graph_get_neighbor_id) {
    return get_all_id(GraphTableType::EDGE_TABLE, slice_num, output);
L
lxsbupt 已提交
2434
  } else {
2435 2436
    get_all_id(GraphTableType::EDGE_TABLE, slice_num, output);
    return get_all_neighbor_id(GraphTableType::EDGE_TABLE, slice_num, output);
L
lxsbupt 已提交
2437 2438 2439
  }
}

2440
int32_t GraphTable::pull_graph_list(GraphTableType table_type,
2441 2442
                                    int idx,
                                    int start,
2443
                                    int total_size,
S
seemingwang 已提交
2444
                                    std::unique_ptr<char[]> &buffer,
2445 2446
                                    int &actual_size,
                                    bool need_feature,
S
seemingwang 已提交
2447 2448 2449
                                    int step) {
  if (start < 0) start = 0;
  int size = 0, cur_size;
2450 2451 2452
  auto &search_shards = table_type == GraphTableType::EDGE_TABLE
                            ? edge_shards[idx]
                            : feature_shards[idx];
S
seemingwang 已提交
2453
  std::vector<std::future<std::vector<Node *>>> tasks;
2454 2455
  for (size_t i = 0; i < search_shards.size() && total_size > 0; i++) {
    cur_size = search_shards[i]->get_size();
S
seemingwang 已提交
2456 2457 2458 2459 2460 2461 2462
    if (size + cur_size <= start) {
      size += cur_size;
      continue;
    }
    int count = std::min(1 + (size + cur_size - start - 1) / step, total_size);
    int end = start + (count - 1) * step + 1;
    tasks.push_back(_shards_task_pool[i % task_pool_size_]->enqueue(
2463 2464
        [&search_shards, this, i, start, end, step, size]()
            -> std::vector<Node *> {
2465
          return search_shards[i]->get_batch(start - size, end - size, step);
S
seemingwang 已提交
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
        }));
    start += count * step;
    total_size -= count;
    size += cur_size;
  }
  for (size_t i = 0; i < tasks.size(); ++i) {
    tasks[i].wait();
  }
  size = 0;
  std::vector<std::vector<Node *>> res;
  for (size_t i = 0; i < tasks.size(); i++) {
    res.push_back(tasks[i].get());
    for (size_t j = 0; j < res.back().size(); j++) {
      size += res.back()[j]->get_size(need_feature);
    }
  }
  char *buffer_addr = new char[size];
  buffer.reset(buffer_addr);
  int index = 0;
  for (size_t i = 0; i < res.size(); i++) {
    for (size_t j = 0; j < res[i].size(); j++) {
      res[i][j]->to_buffer(buffer_addr + index, need_feature);
      index += res[i][j]->get_size(need_feature);
    }
  }
  actual_size = size;
  return 0;
}
S
seemingwang 已提交
2494

D
danleifeng 已提交
2495 2496 2497 2498
void GraphTable::set_feature_separator(const std::string &ch) {
  feature_separator_ = ch;
}

L
lxsbupt 已提交
2499 2500 2501 2502
void GraphTable::set_slot_feature_separator(const std::string &ch) {
  slot_feature_separator_ = ch;
}

D
danleifeng 已提交
2503
int32_t GraphTable::get_server_index_by_id(uint64_t id) {
S
seemingwang 已提交
2504 2505
  return id % shard_num / shard_num_per_server;
}
Z
zhaocaibei123 已提交
2506
int32_t GraphTable::Initialize(const TableParameter &config,
2507 2508 2509
                               const FsClientParameter &fs_config) {
  LOG(INFO) << "in graphTable initialize";
  _config = config;
Z
zhaocaibei123 已提交
2510
  if (InitializeAccessor() != 0) {
2511 2512 2513
    LOG(WARNING) << "Table accessor initialize failed";
    return -1;
  }
S
seemingwang 已提交
2514

2515 2516 2517 2518 2519 2520 2521
  if (_afs_client.initialize(fs_config) != 0) {
    LOG(WARNING) << "Table fs_client initialize failed";
    // return -1;
  }
  auto graph = config.graph_parameter();
  shard_num = _config.shard_num();
  LOG(INFO) << "in graphTable initialize over";
Z
zhaocaibei123 已提交
2522
  return Initialize(graph);
2523
}
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541

void GraphTable::load_node_weight(int type_id, int idx, std::string path) {
  auto paths = paddle::string::split_string<std::string>(path, ";");
  int64_t count = 0;
  auto &weight_map = node_weight[type_id][idx];
  for (auto path : paths) {
    std::ifstream file(path);
    std::string line;
    while (std::getline(file, line)) {
      auto values = paddle::string::split_string<std::string>(line, "\t");
      count++;
      if (values.size() < 2) continue;
      auto src_id = std::stoull(values[0]);
      double weight = std::stod(values[1]);
      weight_map[src_id] = weight;
    }
  }
}
Z
zhaocaibei123 已提交
2542
int32_t GraphTable::Initialize(const GraphParameter &graph) {
2543
  task_pool_size_ = graph.task_pool_size();
D
danleifeng 已提交
2544
  build_sampler_on_cpu = graph.build_sampler_on_cpu();
2545

2546
#ifdef PADDLE_WITH_HETERPS
2547 2548 2549 2550 2551
  _db = NULL;
  search_level = graph.search_level();
  if (search_level >= 2) {
    _db = paddle::distributed::RocksDBHandler::GetInstance();
    _db->initialize("./temp_gpups_db", task_pool_size_);
2552
  }
2553 2554 2555 2556 2557 2558 2559 2560 2561
// gpups_mode = true;
// auto *sampler =
//     CREATE_PSCORE_CLASS(GraphSampler, graph.gpups_graph_sample_class());
// auto slices =
//     string::split_string<std::string>(graph.gpups_graph_sample_args(), ",");
// std::cout << "slices" << std::endl;
// for (auto x : slices) std::cout << x << std::endl;
// sampler->init(graph.gpu_num(), this, slices);
// graph_sampler.reset(sampler);
2562
#endif
2563 2564 2565 2566 2567 2568 2569 2570 2571
  if (shard_num == 0) {
    server_num = 1;
    _shard_idx = 0;
    shard_num = graph.shard_num();
  }
  use_cache = graph.use_cache();
  if (use_cache) {
    cache_size_limit = graph.cache_size_limit();
    cache_ttl = graph.cache_ttl();
L
lxsbupt 已提交
2572
    make_neighbor_sample_cache(cache_size_limit, cache_ttl);
2573
  }
S
seemingwang 已提交
2574 2575 2576
  _shards_task_pool.resize(task_pool_size_);
  for (size_t i = 0; i < _shards_task_pool.size(); ++i) {
    _shards_task_pool[i].reset(new ::ThreadPool(1));
2577
    _shards_task_rng_pool.push_back(paddle::framework::GetCPURandomEngine(0));
S
seemingwang 已提交
2578
  }
D
danleifeng 已提交
2579 2580
  load_node_edge_task_pool.reset(new ::ThreadPool(load_thread_num));

2581
  auto graph_feature = graph.graph_feature();
2582 2583
  auto node_types = graph.node_types();
  auto edge_types = graph.edge_types();
2584
  VLOG(0) << "got " << edge_types.size() << " edge types in total";
2585 2586 2587 2588 2589 2590 2591 2592 2593
  feat_id_map.resize(node_types.size());
  for (int k = 0; k < edge_types.size(); k++) {
    VLOG(0) << "in initialize: get a edge_type " << edge_types[k];
    edge_to_id[edge_types[k]] = k;
    id_to_edge.push_back(edge_types[k]);
  }
  feat_name.resize(node_types.size());
  feat_shape.resize(node_types.size());
  feat_dtype.resize(node_types.size());
L
lxsbupt 已提交
2594
  VLOG(0) << "got " << node_types.size() << " node types in total";
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616
  for (int k = 0; k < node_types.size(); k++) {
    feature_to_id[node_types[k]] = k;
    auto node_type = node_types[k];
    auto feature = graph_feature[k];
    id_to_feature.push_back(node_type);
    int feat_conf_size = static_cast<int>(feature.name().size());

    for (int i = 0; i < feat_conf_size; i++) {
      // auto &f_name = common.attributes()[i];
      // auto &f_shape = common.dims()[i];
      // auto &f_dtype = common.params()[i];
      auto &f_name = feature.name()[i];
      auto &f_shape = feature.shape()[i];
      auto &f_dtype = feature.dtype()[i];
      feat_name[k].push_back(f_name);
      feat_shape[k].push_back(f_shape);
      feat_dtype[k].push_back(f_dtype);
      feat_id_map[k][f_name] = i;
      VLOG(0) << "init graph table feat conf name:" << f_name
              << " shape:" << f_shape << " dtype:" << f_dtype;
    }
  }
2617 2618 2619 2620
  // this->table_name = common.table_name();
  // this->table_type = common.name();
  this->table_name = graph.table_name();
  this->table_type = graph.table_type();
S
seemingwang 已提交
2621 2622
  VLOG(0) << " init graph table type " << this->table_type << " table name "
          << this->table_name;
2623
  // int feat_conf_size = static_cast<int>(common.attributes().size());
2624
  // int feat_conf_size = static_cast<int>(graph_feature.name().size());
S
seemingwang 已提交
2625 2626
  VLOG(0) << "in init graph table shard num = " << shard_num << " shard_idx"
          << _shard_idx;
S
seemingwang 已提交
2627 2628 2629
  shard_num_per_server = sparse_local_shard_num(shard_num, server_num);
  shard_start = _shard_idx * shard_num_per_server;
  shard_end = shard_start + shard_num_per_server;
S
seemingwang 已提交
2630 2631
  VLOG(0) << "in init graph table shard idx = " << _shard_idx << " shard_start "
          << shard_start << " shard_end " << shard_end;
2632
  edge_shards.resize(id_to_edge.size());
2633 2634
  node_weight.resize(2);
  node_weight[0].resize(id_to_edge.size());
2635 2636 2637
#ifdef PADDLE_WITH_HETERPS
  partitions.resize(id_to_edge.size());
#endif
2638
  for (size_t k = 0; k < edge_shards.size(); k++) {
2639 2640 2641
    for (size_t i = 0; i < shard_num_per_server; i++) {
      edge_shards[k].push_back(new GraphShard());
    }
2642
  }
2643
  node_weight[1].resize(id_to_feature.size());
2644
  feature_shards.resize(id_to_feature.size());
2645
  for (size_t k = 0; k < feature_shards.size(); k++) {
2646 2647 2648
    for (size_t i = 0; i < shard_num_per_server; i++) {
      feature_shards[k].push_back(new GraphShard());
    }
2649 2650
  }

S
seemingwang 已提交
2651 2652
  return 0;
}
2653

L
lxsbupt 已提交
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
void GraphTable::init_worker_poll(int gpu_num) {
  _cpu_worker_pool.resize(gpu_num);
  for (int i = 0; i < gpu_num; i++) {
    _cpu_worker_pool[i].reset(new ::ThreadPool(16));
  }
}

void GraphTable::build_graph_total_keys() {
  VLOG(0) << "begin insert edge to graph_total_keys";
  // build node embedding id
  std::vector<std::vector<uint64_t>> keys;
  this->get_node_embedding_ids(1, &keys);
  graph_total_keys_.insert(
      graph_total_keys_.end(), keys[0].begin(), keys[0].end());

  VLOG(0) << "finish insert edge to graph_total_keys";
}

void GraphTable::build_graph_type_keys() {
  VLOG(0) << "begin build_graph_type_keys";
  graph_type_keys_.clear();
  graph_type_keys_.resize(this->feature_to_id.size());

  int cnt = 0;
  for (auto &it : this->feature_to_id) {
    auto node_idx = it.second;
    std::vector<std::vector<uint64_t>> keys;
2681
    this->get_all_id(GraphTableType::FEATURE_TABLE, node_idx, 1, &keys);
L
lxsbupt 已提交
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
    type_to_index_[node_idx] = cnt;
    graph_type_keys_[cnt++] = std::move(keys[0]);
  }
  VLOG(0) << "finish build_graph_type_keys";

  VLOG(0) << "begin insert feature into graph_total_keys";
  // build feature embedding id
  for (auto &it : this->feature_to_id) {
    auto node_idx = it.second;
    std::vector<std::vector<uint64_t>> keys;
2692 2693
    this->get_all_feature_ids(
        GraphTableType::FEATURE_TABLE, node_idx, 1, &keys);
L
lxsbupt 已提交
2694 2695 2696 2697 2698 2699
    graph_total_keys_.insert(
        graph_total_keys_.end(), keys[0].begin(), keys[0].end());
  }
  VLOG(0) << "finish insert feature into graph_total_keys";
}

2700 2701
}  // namespace distributed
};  // namespace paddle