common_graph_table.h 21.3 KB
Newer Older
S
seemingwang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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.

#pragma once

#include <ThreadPool.h>
#include <assert.h>
#include <pthread.h>
20

S
seemingwang 已提交
21 22 23 24 25 26
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <ctime>
#include <functional>
#include <iostream>
S
seemingwang 已提交
27
#include <list>
S
seemingwang 已提交
28
#include <map>
S
seemingwang 已提交
29 30
#include <memory>
#include <mutex>  // NOLINT
S
seemingwang 已提交
31 32 33
#include <numeric>
#include <queue>
#include <set>
S
seemingwang 已提交
34
#include <string>
S
seemingwang 已提交
35
#include <thread>
S
seemingwang 已提交
36
#include <unordered_map>
S
seemingwang 已提交
37
#include <unordered_set>
S
seemingwang 已提交
38 39
#include <utility>
#include <vector>
40

41 42
#include "paddle/fluid/distributed/ps/table/accessor.h"
#include "paddle/fluid/distributed/ps/table/common_table.h"
43
#include "paddle/fluid/distributed/ps/table/graph/class_macro.h"
44
#include "paddle/fluid/distributed/ps/table/graph/graph_node.h"
S
seemingwang 已提交
45
#include "paddle/fluid/string/string_helper.h"
46
#include "paddle/phi/core/utils/rw_lock.h"
47

48
#ifdef PADDLE_WITH_HETERPS
Z
zhaocaibei123 已提交
49
#include "paddle/fluid/distributed/ps/table/depends/rocksdb_warpper.h"
50 51
#include "paddle/fluid/framework/fleet/heter_ps/gpu_graph_node.h"
#endif
S
seemingwang 已提交
52 53 54 55 56 57
namespace paddle {
namespace distributed {
class GraphShard {
 public:
  size_t get_size();
  GraphShard() {}
58
  ~GraphShard();
S
seemingwang 已提交
59 60
  std::vector<Node *> &get_bucket() { return bucket; }
  std::vector<Node *> get_batch(int start, int end, int step);
61 62
  std::vector<int64_t> get_ids_by_range(int start, int end) {
    std::vector<int64_t> res;
63
    for (int i = start; i < end && i < (int)bucket.size(); i++) {
S
seemingwang 已提交
64 65 66 67
      res.push_back(bucket[i]->get_id());
    }
    return res;
  }
68 69 70 71 72 73 74
  std::vector<int64_t> get_all_id() {
    std::vector<int64_t> res;
    for (int i = 0; i < (int)bucket.size(); i++) {
      res.push_back(bucket[i]->get_id());
    }
    return res;
  }
75
  GraphNode *add_graph_node(int64_t id);
76
  GraphNode *add_graph_node(Node *node);
77 78 79
  FeatureNode *add_feature_node(int64_t id);
  Node *find_node(int64_t id);
  void delete_node(int64_t id);
80
  void clear();
81 82
  void add_neighbor(int64_t id, int64_t dst_id, float weight);
  std::unordered_map<int64_t, int> &get_node_location() {
S
seemingwang 已提交
83 84 85 86
    return node_location;
  }

 private:
87
  std::unordered_map<int64_t, int> node_location;
S
seemingwang 已提交
88 89
  std::vector<Node *> bucket;
};
S
seemingwang 已提交
90 91 92 93

enum LRUResponse { ok = 0, blocked = 1, err = 2 };

struct SampleKey {
94
  int idx;
95
  int64_t node_key;
S
seemingwang 已提交
96
  size_t sample_size;
97
  bool is_weighted;
98 99 100 101 102 103 104
  SampleKey(int _idx, int64_t _node_key, size_t _sample_size,
            bool _is_weighted) {
    idx = _idx;
    node_key = _node_key;
    sample_size = _sample_size;
    is_weighted = _is_weighted;
  }
S
seemingwang 已提交
105
  bool operator==(const SampleKey &s) const {
106 107
    return idx == s.idx && node_key == s.node_key &&
           sample_size == s.sample_size && is_weighted == s.is_weighted;
S
seemingwang 已提交
108 109 110 111 112 113
  }
};

class SampleResult {
 public:
  size_t actual_size;
114 115 116 117 118 119 120
  std::shared_ptr<char> buffer;
  SampleResult(size_t _actual_size, std::shared_ptr<char> &_buffer)
      : actual_size(_actual_size), buffer(_buffer) {}
  SampleResult(size_t _actual_size, char *_buffer)
      : actual_size(_actual_size),
        buffer(_buffer, [](char *p) { delete[] p; }) {}
  ~SampleResult() {}
S
seemingwang 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134
};

template <typename K, typename V>
class LRUNode {
 public:
  LRUNode(K _key, V _data, size_t _ttl) : key(_key), data(_data), ttl(_ttl) {
    next = pre = NULL;
  }
  K key;
  V data;
  size_t ttl;
  // time to live
  LRUNode<K, V> *pre, *next;
};
135
template <typename K, typename V>
S
seemingwang 已提交
136 137
class ScaledLRU;

138
template <typename K, typename V>
S
seemingwang 已提交
139 140
class RandomSampleLRU {
 public:
141 142 143
  RandomSampleLRU(ScaledLRU<K, V> *_father) {
    father = _father;
    remove_count = 0;
S
seemingwang 已提交
144 145 146
    node_size = 0;
    node_head = node_end = NULL;
    global_ttl = father->ttl;
147
    total_diff = 0;
S
seemingwang 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160
  }

  ~RandomSampleLRU() {
    LRUNode<K, V> *p;
    while (node_head != NULL) {
      p = node_head->next;
      delete node_head;
      node_head = p;
    }
  }
  LRUResponse query(K *keys, size_t length, std::vector<std::pair<K, V>> &res) {
    if (pthread_rwlock_tryrdlock(&father->rwlock) != 0)
      return LRUResponse::blocked;
161 162 163 164 165 166 167 168 169 170 171 172 173 174
    // pthread_rwlock_rdlock(&father->rwlock);
    int init_size = node_size - remove_count;
    process_redundant(length * 3);

    for (size_t i = 0; i < length; i++) {
      auto iter = key_map.find(keys[i]);
      if (iter != key_map.end()) {
        res.emplace_back(keys[i], iter->second->data);
        iter->second->ttl--;
        if (iter->second->ttl == 0) {
          remove(iter->second);
          if (remove_count != 0) remove_count--;
        } else {
          move_to_tail(iter->second);
S
seemingwang 已提交
175 176
        }
      }
177 178 179 180 181
    }
    total_diff += node_size - remove_count - init_size;
    if (total_diff >= 500 || total_diff < -500) {
      father->handle_size_diff(total_diff);
      total_diff = 0;
S
seemingwang 已提交
182 183 184 185 186 187 188
    }
    pthread_rwlock_unlock(&father->rwlock);
    return LRUResponse::ok;
  }
  LRUResponse insert(K *keys, V *data, size_t length) {
    if (pthread_rwlock_tryrdlock(&father->rwlock) != 0)
      return LRUResponse::blocked;
189 190 191 192 193 194 195 196 197 198 199 200
    // pthread_rwlock_rdlock(&father->rwlock);
    int init_size = node_size - remove_count;
    process_redundant(length * 3);
    for (size_t i = 0; i < length; i++) {
      auto iter = key_map.find(keys[i]);
      if (iter != key_map.end()) {
        move_to_tail(iter->second);
        iter->second->ttl = global_ttl;
        iter->second->data = data[i];
      } else {
        LRUNode<K, V> *temp = new LRUNode<K, V>(keys[i], data[i], global_ttl);
        add_new(temp);
S
seemingwang 已提交
201 202
      }
    }
203 204 205 206 207 208
    total_diff += node_size - remove_count - init_size;
    if (total_diff >= 500 || total_diff < -500) {
      father->handle_size_diff(total_diff);
      total_diff = 0;
    }

S
seemingwang 已提交
209 210 211
    pthread_rwlock_unlock(&father->rwlock);
    return LRUResponse::ok;
  }
212 213
  void remove(LRUNode<K, V> *node) {
    fetch(node);
S
seemingwang 已提交
214
    node_size--;
215 216
    key_map.erase(node->key);
    delete node;
217 218 219
  }

  void process_redundant(int process_size) {
220
    int length = std::min(remove_count, process_size);
221 222 223
    while (length--) {
      remove(node_head);
      remove_count--;
S
seemingwang 已提交
224
    }
225
    // std::cerr<<"after remove_count = "<<remove_count<<std::endl;
S
seemingwang 已提交
226 227
  }

228 229 230 231 232 233 234 235 236 237 238 239
  void move_to_tail(LRUNode<K, V> *node) {
    fetch(node);
    place_at_tail(node);
  }

  void add_new(LRUNode<K, V> *node) {
    node->ttl = global_ttl;
    place_at_tail(node);
    node_size++;
    key_map[node->key] = node;
  }
  void place_at_tail(LRUNode<K, V> *node) {
S
seemingwang 已提交
240 241 242 243 244 245 246 247 248 249 250
    if (node_end == NULL) {
      node_head = node_end = node;
      node->next = node->pre = NULL;
    } else {
      node_end->next = node;
      node->pre = node_end;
      node->next = NULL;
      node_end = node;
    }
  }

251 252 253 254 255 256 257 258 259 260 261 262 263
  void fetch(LRUNode<K, V> *node) {
    if (node->pre) {
      node->pre->next = node->next;
    } else {
      node_head = node->next;
    }
    if (node->next) {
      node->next->pre = node->pre;
    } else {
      node_end = node->pre;
    }
  }

S
seemingwang 已提交
264
 private:
265 266
  std::unordered_map<K, LRUNode<K, V> *> key_map;
  ScaledLRU<K, V> *father;
267
  size_t global_ttl, size_limit;
268
  int node_size, total_diff;
S
seemingwang 已提交
269
  LRUNode<K, V> *node_head, *node_end;
270
  friend class ScaledLRU<K, V>;
271
  int remove_count;
S
seemingwang 已提交
272 273
};

274
template <typename K, typename V>
S
seemingwang 已提交
275 276
class ScaledLRU {
 public:
277
  ScaledLRU(size_t _shard_num, size_t size_limit, size_t _ttl)
S
seemingwang 已提交
278
      : size_limit(size_limit), ttl(_ttl) {
279
    shard_num = _shard_num;
S
seemingwang 已提交
280 281 282 283
    pthread_rwlock_init(&rwlock, NULL);
    stop = false;
    thread_pool.reset(new ::ThreadPool(1));
    global_count = 0;
284 285
    lru_pool = std::vector<RandomSampleLRU<K, V>>(shard_num,
                                                  RandomSampleLRU<K, V>(this));
S
seemingwang 已提交
286 287 288 289
    shrink_job = std::thread([this]() -> void {
      while (true) {
        {
          std::unique_lock<std::mutex> lock(mutex_);
290
          cv_.wait_for(lock, std::chrono::milliseconds(20000));
S
seemingwang 已提交
291 292 293 294 295
          if (stop) {
            return;
          }
        }
        auto status =
Z
zhaocaibei123 已提交
296
            thread_pool->enqueue([this]() -> int { return Shrink(); });
S
seemingwang 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        status.wait();
      }
    });
    shrink_job.detach();
  }
  ~ScaledLRU() {
    std::unique_lock<std::mutex> lock(mutex_);
    stop = true;
    cv_.notify_one();
  }
  LRUResponse query(size_t index, K *keys, size_t length,
                    std::vector<std::pair<K, V>> &res) {
    return lru_pool[index].query(keys, length, res);
  }
  LRUResponse insert(size_t index, K *keys, V *data, size_t length) {
    return lru_pool[index].insert(keys, data, length);
  }
Z
zhaocaibei123 已提交
314
  int Shrink() {
S
seemingwang 已提交
315 316
    int node_size = 0;
    for (size_t i = 0; i < lru_pool.size(); i++) {
317
      node_size += lru_pool[i].node_size - lru_pool[i].remove_count;
S
seemingwang 已提交
318 319
    }

320
    if ((size_t)node_size <= size_t(1.1 * size_limit) + 1) return 0;
S
seemingwang 已提交
321
    if (pthread_rwlock_wrlock(&rwlock) == 0) {
322 323 324 325
      global_count = 0;
      for (size_t i = 0; i < lru_pool.size(); i++) {
        global_count += lru_pool[i].node_size - lru_pool[i].remove_count;
      }
326
      if ((size_t)global_count > size_limit) {
327
        size_t remove = global_count - size_limit;
328
        for (size_t i = 0; i < lru_pool.size(); i++) {
329 330 331 332
          lru_pool[i].total_diff = 0;
          lru_pool[i].remove_count +=
              1.0 * (lru_pool[i].node_size - lru_pool[i].remove_count) /
              global_count * remove;
S
seemingwang 已提交
333 334 335 336 337 338 339
        }
      }
      pthread_rwlock_unlock(&rwlock);
      return 0;
    }
    return 0;
  }
340

S
seemingwang 已提交
341 342 343
  void handle_size_diff(int diff) {
    if (diff != 0) {
      __sync_fetch_and_add(&global_count, diff);
344
      if (global_count > int(1.25 * size_limit)) {
Z
zhaocaibei123 已提交
345
        thread_pool->enqueue([this]() -> int { return Shrink(); });
S
seemingwang 已提交
346 347 348 349 350 351 352 353
      }
    }
  }

  size_t get_ttl() { return ttl; }

 private:
  pthread_rwlock_t rwlock;
354
  size_t shard_num;
S
seemingwang 已提交
355
  int global_count;
356
  size_t size_limit, total, hit;
S
seemingwang 已提交
357 358 359
  size_t ttl;
  bool stop;
  std::thread shrink_job;
360
  std::vector<RandomSampleLRU<K, V>> lru_pool;
S
seemingwang 已提交
361 362 363
  mutable std::mutex mutex_;
  std::condition_variable cv_;
  std::shared_ptr<::ThreadPool> thread_pool;
364
  friend class RandomSampleLRU<K, V>;
S
seemingwang 已提交
365 366
};

367
/*
368 369 370 371 372 373 374 375 376 377 378 379
#ifdef PADDLE_WITH_HETERPS
enum GraphSamplerStatus { waiting = 0, running = 1, terminating = 2 };
class GraphTable;
class GraphSampler {
 public:
  GraphSampler() {
    status = GraphSamplerStatus::waiting;
    thread_pool.reset(new ::ThreadPool(1));
    callback = [](std::vector<paddle::framework::GpuPsCommGraph> &res) {
      return;
    };
  }
380 381 382
  virtual int loadData(const std::string &path){
    return 0;
  }
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  virtual int run_graph_sampling() = 0;
  virtual int start_graph_sampling() {
    if (status != GraphSamplerStatus::waiting) {
      return -1;
    }
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();
    graph_sample_task_over = thread_pool->enqueue([&prom, this]() {
      prom.set_value(0);
      status = GraphSamplerStatus::running;
      return run_graph_sampling();
    });
    return fut.get();
  }
  virtual void init(size_t gpu_num, GraphTable *graph_table,
                    std::vector<std::string> args) = 0;
  virtual void set_graph_sample_callback(
      std::function<void(std::vector<paddle::framework::GpuPsCommGraph> &)>
          callback) {
    this->callback = callback;
  }

  virtual int end_graph_sampling() {
    if (status == GraphSamplerStatus::running) {
      status = GraphSamplerStatus::terminating;
      return graph_sample_task_over.get();
    }
    return -1;
  }
  virtual GraphSamplerStatus get_graph_sampler_status() { return status; }

 protected:
  std::function<void(std::vector<paddle::framework::GpuPsCommGraph> &)>
      callback;
  std::shared_ptr<::ThreadPool> thread_pool;
  GraphSamplerStatus status;
  std::future<int> graph_sample_task_over;
  std::vector<paddle::framework::GpuPsCommGraph> sample_res;
};
#endif
423
*/
424

425
class GraphTable : public Table {
S
seemingwang 已提交
426
 public:
427 428 429 430
  GraphTable() {
    use_cache = false;
    shard_num = 0;
    rw_lock.reset(new pthread_rwlock_t());
431 432 433 434
#ifdef PADDLE_WITH_HETERPS
    next_partition = 0;
    total_memory_cost = 0;
#endif
435
  }
436
  virtual ~GraphTable();
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

  virtual void *GetShard(size_t shard_idx) { return 0; }

  static int32_t sparse_local_shard_num(uint32_t shard_num,
                                        uint32_t server_num) {
    if (shard_num % server_num == 0) {
      return shard_num / server_num;
    }
    size_t local_shard_num = shard_num / server_num + 1;
    return local_shard_num;
  }

  static size_t get_sparse_shard(uint32_t shard_num, uint32_t server_num,
                                 uint64_t key) {
    return (key % shard_num) / sparse_local_shard_num(shard_num, server_num);
  }

454
  virtual int32_t pull_graph_list(int type_id, int idx, int start, int size,
S
seemingwang 已提交
455 456 457 458
                                  std::unique_ptr<char[]> &buffer,
                                  int &actual_size, bool need_feature,
                                  int step);

459
  virtual int32_t random_sample_neighbors(
460
      int idx, int64_t *node_ids, int sample_size,
461
      std::vector<std::shared_ptr<char>> &buffers,
462
      std::vector<int> &actual_sizes, bool need_weight);
S
seemingwang 已提交
463

464 465
  int32_t random_sample_nodes(int type_id, int idx, int sample_size,
                              std::unique_ptr<char[]> &buffers,
S
seemingwang 已提交
466 467 468
                              int &actual_sizes);

  virtual int32_t get_nodes_ids_by_ranges(
469 470
      int type_id, int idx, std::vector<std::pair<int, int>> ranges,
      std::vector<int64_t> &res);
Z
zhaocaibei123 已提交
471 472
  virtual int32_t Initialize() { return 0; }
  virtual int32_t Initialize(const TableParameter &config,
473
                             const FsClientParameter &fs_config);
Z
zhaocaibei123 已提交
474 475
  virtual int32_t Initialize(const GraphParameter &config);
  int32_t Load(const std::string &path, const std::string &param);
S
seemingwang 已提交
476

477 478
  int32_t load_edges(const std::string &path, bool reverse,
                     const std::string &edge_type);
S
seemingwang 已提交
479

480 481
  std::vector<std::vector<int64_t>> get_all_id(int type, int idx,
                                               int slice_num);
S
seemingwang 已提交
482 483
  int32_t load_nodes(const std::string &path, std::string node_type);

484
  int32_t add_graph_node(int idx, std::vector<int64_t> &id_list,
485 486
                         std::vector<bool> &is_weight_list);

487
  int32_t remove_graph_node(int idx, std::vector<int64_t> &id_list);
488

489
  int32_t get_server_index_by_id(int64_t id);
490
  Node *find_node(int type_id, int idx, int64_t id);
S
seemingwang 已提交
491

Y
yaoxuefeng 已提交
492 493 494
  virtual int32_t Pull(TableContext &context) { return 0; }
  virtual int32_t Push(TableContext &context) { return 0; }

495
  virtual int32_t clear_nodes(int type, int idx);
Z
zhaocaibei123 已提交
496 497 498
  virtual void Clear() {}
  virtual int32_t Flush() { return 0; }
  virtual int32_t Shrink(const std::string &param) { return 0; }
S
seemingwang 已提交
499
  //指定保存路径
Z
zhaocaibei123 已提交
500
  virtual int32_t Save(const std::string &path, const std::string &converter) {
S
seemingwang 已提交
501 502
    return 0;
  }
Z
zhaocaibei123 已提交
503 504
  virtual int32_t InitializeShard() { return 0; }
  virtual int32_t SetShard(size_t shard_idx, size_t server_num) {
505 506 507 508 509 510 511 512 513 514 515 516
    _shard_idx = shard_idx;
    /*
    _shard_num is not used in graph_table, this following operation is for the
    purpose of
    being compatible with base class table.
    */
    _shard_num = server_num;
    this->server_num = server_num;
    return 0;
  }
  virtual uint32_t get_thread_pool_index_by_shard_index(int64_t shard_index);
  virtual uint32_t get_thread_pool_index(int64_t node_id);
517 518
  virtual std::pair<int32_t, std::string> parse_feature(int idx,
                                                        std::string feat_str);
S
seemingwang 已提交
519

520
  virtual int32_t get_node_feat(int idx, const std::vector<int64_t> &node_ids,
S
seemingwang 已提交
521 522 523
                                const std::vector<std::string> &feature_names,
                                std::vector<std::vector<std::string>> &res);

S
seemingwang 已提交
524
  virtual int32_t set_node_feat(
525
      int idx, const std::vector<int64_t> &node_ids,
S
seemingwang 已提交
526 527 528
      const std::vector<std::string> &feature_names,
      const std::vector<std::vector<std::string>> &res);

S
seemingwang 已提交
529
  size_t get_server_num() { return server_num; }
530
  void clear_graph(int idx);
531
  virtual int32_t make_neighbor_sample_cache(size_t size_limit, size_t ttl) {
532 533 534
    {
      std::unique_lock<std::mutex> lock(mutex_);
      if (use_cache == false) {
535
        scaled_lru.reset(new ScaledLRU<SampleKey, SampleResult>(
536
            task_pool_size_, size_limit, ttl));
537 538 539 540 541
        use_cache = true;
      }
    }
    return 0;
  }
542
  virtual void load_node_weight(int type_id, int idx, std::string path);
543
#ifdef PADDLE_WITH_HETERPS
544 545 546 547 548 549 550 551 552 553 554 555
  // virtual int32_t start_graph_sampling() {
  //   return this->graph_sampler->start_graph_sampling();
  // }
  // virtual int32_t end_graph_sampling() {
  //   return this->graph_sampler->end_graph_sampling();
  // }
  // virtual int32_t set_graph_sample_callback(
  //     std::function<void(std::vector<paddle::framework::GpuPsCommGraph> &)>
  //         callback) {
  //   graph_sampler->set_graph_sample_callback(callback);
  //   return 0;
  // }
556
  virtual void make_partitions(int idx, int64_t gb_size, int device_len);
557
  virtual void export_partition_files(int idx, std::string file_path);
558
  virtual char *random_sample_neighbor_from_ssd(
559 560 561 562
      int idx, int64_t id, int sample_size,
      const std::shared_ptr<std::mt19937_64> rng, int &actual_size);
  virtual int32_t add_node_to_ssd(int type_id, int idx, int64_t src_id,
                                  char *data, int len);
563
  virtual paddle::framework::GpuPsCommGraph make_gpu_ps_graph(
564
      int idx, std::vector<int64_t> ids);
565 566 567 568 569 570 571 572 573 574 575 576 577 578
  int32_t Load_to_ssd(const std::string &path, const std::string &param);
  int64_t load_graph_to_memory_from_ssd(int idx, std::vector<int64_t> &ids);
  int32_t make_complementary_graph(int idx, int64_t byte_size);
  int32_t dump_edges_to_ssd(int idx);
  int32_t get_partition_num(int idx) { return partitions[idx].size(); }
  std::vector<int64_t> get_partition(int idx, int index) {
    if (idx >= partitions.size() || index >= partitions[idx].size())
      return std::vector<int64_t>();
    return partitions[idx][index];
  }
  int32_t load_edges_to_ssd(const std::string &path, bool reverse_edge,
                            const std::string &edge_type);
  int32_t load_next_partition(int idx);
  void set_search_level(int search_level) { this->search_level = search_level; }
579
  int search_level;
580 581 582
  int64_t total_memory_cost;
  std::vector<std::vector<std::vector<int64_t>>> partitions;
  int next_partition;
583
#endif
584 585 586
  virtual int32_t add_comm_edge(int idx, int64_t src_id, int64_t dst_id);
  virtual int32_t build_sampler(int idx, std::string sample_type = "random");
  std::vector<std::vector<GraphShard *>> edge_shards, feature_shards;
S
seemingwang 已提交
587
  size_t shard_start, shard_end, server_num, shard_num_per_server, shard_num;
588
  int task_pool_size_ = 24;
S
seemingwang 已提交
589 590
  const int random_sample_nodes_ranges = 3;

591
  std::vector<std::vector<std::unordered_map<int64_t, double>>> node_weight;
592 593 594 595 596 597
  std::vector<std::vector<std::string>> feat_name;
  std::vector<std::vector<std::string>> feat_dtype;
  std::vector<std::vector<int32_t>> feat_shape;
  std::vector<std::unordered_map<std::string, int32_t>> feat_id_map;
  std::unordered_map<std::string, int> feature_to_id, edge_to_id;
  std::vector<std::string> id_to_feature, id_to_edge;
S
seemingwang 已提交
598 599 600 601
  std::string table_name;
  std::string table_type;

  std::vector<std::shared_ptr<::ThreadPool>> _shards_task_pool;
602
  std::vector<std::shared_ptr<std::mt19937_64>> _shards_task_rng_pool;
603
  std::shared_ptr<ScaledLRU<SampleKey, SampleResult>> scaled_lru;
604 605
  std::unordered_set<int64_t> extra_nodes;
  std::unordered_map<int64_t, size_t> extra_nodes_to_thread_index;
606
  bool use_cache, use_duplicate_nodes;
607 608
  int cache_size_limit;
  int cache_ttl;
609
  mutable std::mutex mutex_;
610 611 612
  std::shared_ptr<pthread_rwlock_t> rw_lock;
#ifdef PADDLE_WITH_HETERPS
  // paddle::framework::GpuPsGraphTable gpu_graph_table;
613 614 615 616
  paddle::distributed::RocksDBHandler *_db;
// std::shared_ptr<::ThreadPool> graph_sample_pool;
// std::shared_ptr<GraphSampler> graph_sampler;
// REGISTER_GRAPH_FRIEND_CLASS(2, CompleteGraphSampler, BasicBfsGraphSampler)
617 618 619
#endif
};

620
/*
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
#ifdef PADDLE_WITH_HETERPS
REGISTER_PSCORE_REGISTERER(GraphSampler);
class CompleteGraphSampler : public GraphSampler {
 public:
  CompleteGraphSampler() {}
  ~CompleteGraphSampler() {}
  // virtual pthread_rwlock_t *export_rw_lock();
  virtual int run_graph_sampling();
  virtual void init(size_t gpu_num, GraphTable *graph_table,
                    std::vector<std::string> args_);

 protected:
  GraphTable *graph_table;
  std::vector<std::vector<paddle::framework::GpuPsGraphNode>> sample_nodes;
  std::vector<std::vector<int64_t>> sample_neighbors;
  // std::vector<GpuPsCommGraph> sample_res;
  // std::shared_ptr<std::mt19937_64> random;
  int gpu_num;
};

class BasicBfsGraphSampler : public GraphSampler {
 public:
  BasicBfsGraphSampler() {}
  ~BasicBfsGraphSampler() {}
  // virtual pthread_rwlock_t *export_rw_lock();
  virtual int run_graph_sampling();
  virtual void init(size_t gpu_num, GraphTable *graph_table,
                    std::vector<std::string> args_);

 protected:
  GraphTable *graph_table;
  // std::vector<std::vector<GpuPsGraphNode>> sample_nodes;
  std::vector<std::vector<paddle::framework::GpuPsGraphNode>> sample_nodes;
  std::vector<std::vector<int64_t>> sample_neighbors;
  size_t gpu_num;
656
  int init_search_size, node_num_for_each_shard, edge_num_for_each_node;
657 658 659
  int rounds, interval;
  std::vector<std::unordered_map<int64_t, std::vector<int64_t>>>
      sample_neighbors_map;
S
seemingwang 已提交
660
};
661
#endif
662
*/
663
}  // namespace distributed
664

665
};  // namespace paddle
666 667 668 669 670 671

namespace std {

template <>
struct hash<paddle::distributed::SampleKey> {
  size_t operator()(const paddle::distributed::SampleKey &s) const {
672
    return s.idx ^ s.node_key ^ s.sample_size;
673 674
  }
};
675
}  // namespace std