common_graph_table.h 13.9 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>
S
seemingwang 已提交
20 21 22 23 24 25
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <ctime>
#include <functional>
#include <iostream>
S
seemingwang 已提交
26
#include <list>
S
seemingwang 已提交
27
#include <map>
S
seemingwang 已提交
28 29
#include <memory>
#include <mutex>  // NOLINT
S
seemingwang 已提交
30 31 32
#include <numeric>
#include <queue>
#include <set>
S
seemingwang 已提交
33
#include <string>
S
seemingwang 已提交
34
#include <thread>
S
seemingwang 已提交
35
#include <unordered_map>
S
seemingwang 已提交
36
#include <unordered_set>
S
seemingwang 已提交
37 38 39 40
#include <utility>
#include <vector>
#include "paddle/fluid/distributed/table/accessor.h"
#include "paddle/fluid/distributed/table/common_table.h"
S
seemingwang 已提交
41
#include "paddle/fluid/distributed/table/graph/graph_node.h"
S
seemingwang 已提交
42 43 44 45 46 47 48 49
#include "paddle/fluid/framework/rw_lock.h"
#include "paddle/fluid/string/string_helper.h"
namespace paddle {
namespace distributed {
class GraphShard {
 public:
  size_t get_size();
  GraphShard() {}
S
seemingwang 已提交
50
  GraphShard(int shard_num) { this->shard_num = shard_num; }
51
  ~GraphShard();
S
seemingwang 已提交
52 53 54 55
  std::vector<Node *> &get_bucket() { return bucket; }
  std::vector<Node *> get_batch(int start, int end, int step);
  std::vector<uint64_t> get_ids_by_range(int start, int end) {
    std::vector<uint64_t> res;
56
    for (int i = start; i < end && i < (int)bucket.size(); i++) {
S
seemingwang 已提交
57 58 59 60
      res.push_back(bucket[i]->get_id());
    }
    return res;
  }
S
seemingwang 已提交
61

S
seemingwang 已提交
62 63 64
  GraphNode *add_graph_node(uint64_t id);
  FeatureNode *add_feature_node(uint64_t id);
  Node *find_node(uint64_t id);
65 66
  void delete_node(uint64_t id);
  void clear();
S
seemingwang 已提交
67
  void add_neighbor(uint64_t id, uint64_t dst_id, float weight);
S
seemingwang 已提交
68 69 70 71 72 73 74 75 76
  std::unordered_map<uint64_t, int> get_node_location() {
    return node_location;
  }

 private:
  std::unordered_map<uint64_t, int> node_location;
  int shard_num;
  std::vector<Node *> bucket;
};
S
seemingwang 已提交
77 78 79 80 81 82

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

struct SampleKey {
  uint64_t node_key;
  size_t sample_size;
83
  SampleKey(uint64_t _node_key, size_t _sample_size)
S
seemingwang 已提交
84
      : node_key(_node_key), sample_size(_sample_size) {}
S
seemingwang 已提交
85 86 87 88 89 90 91 92
  bool operator==(const SampleKey &s) const {
    return node_key == s.node_key && sample_size == s.sample_size;
  }
};

class SampleResult {
 public:
  size_t actual_size;
93 94 95 96 97 98 99
  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 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
};

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;
  }
  std::chrono::milliseconds ms;
  // the last hit time
  K key;
  V data;
  size_t ttl;
  // time to live
  LRUNode<K, V> *pre, *next;
};
116
template <typename K, typename V>
S
seemingwang 已提交
117 118
class ScaledLRU;

119
template <typename K, typename V>
S
seemingwang 已提交
120 121
class RandomSampleLRU {
 public:
122
  RandomSampleLRU(ScaledLRU<K, V> *_father) : father(_father) {
S
seemingwang 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    node_size = 0;
    node_head = node_end = NULL;
    global_ttl = father->ttl;
  }

  ~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;
    int init_node_size = node_size;
    try {
      for (size_t i = 0; i < length; i++) {
        auto iter = key_map.find(keys[i]);
        if (iter != key_map.end()) {
S
seemingwang 已提交
144
          res.emplace_back(keys[i], iter->second->data);
S
seemingwang 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
          iter->second->ttl--;
          if (iter->second->ttl == 0) {
            remove(iter->second, true);
          } else {
            remove(iter->second);
            add_to_tail(iter->second);
          }
        }
      }
    } catch (...) {
      pthread_rwlock_unlock(&father->rwlock);
      father->handle_size_diff(node_size - init_node_size);
      return LRUResponse::err;
    }
    pthread_rwlock_unlock(&father->rwlock);
    father->handle_size_diff(node_size - init_node_size);
    return LRUResponse::ok;
  }
  LRUResponse insert(K *keys, V *data, size_t length) {
    if (pthread_rwlock_tryrdlock(&father->rwlock) != 0)
      return LRUResponse::blocked;
    int init_node_size = node_size;
    try {
      for (size_t i = 0; i < length; i++) {
        auto iter = key_map.find(keys[i]);
        if (iter != key_map.end()) {
          iter->second->ttl = global_ttl;
          remove(iter->second);
          add_to_tail(iter->second);
          iter->second->data = data[i];
        } else {
          LRUNode<K, V> *temp = new LRUNode<K, V>(keys[i], data[i], global_ttl);
          add_to_tail(temp);
          key_map[keys[i]] = temp;
        }
      }
    } catch (...) {
      pthread_rwlock_unlock(&father->rwlock);
      father->handle_size_diff(node_size - init_node_size);
      return LRUResponse::err;
    }
    pthread_rwlock_unlock(&father->rwlock);
    father->handle_size_diff(node_size - init_node_size);
    return LRUResponse::ok;
  }
  void remove(LRUNode<K, V> *node, bool del = false) {
    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;
    }
    node_size--;
    if (del) {
      delete node;
      key_map.erase(node->key);
    }
  }

  void add_to_tail(LRUNode<K, V> *node) {
    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;
    }
    node_size++;
    node->ms = std::chrono::duration_cast<std::chrono::milliseconds>(
        std::chrono::system_clock::now().time_since_epoch());
  }

 private:
224 225
  std::unordered_map<K, LRUNode<K, V> *> key_map;
  ScaledLRU<K, V> *father;
S
seemingwang 已提交
226 227 228
  size_t global_ttl;
  int node_size;
  LRUNode<K, V> *node_head, *node_end;
229
  friend class ScaledLRU<K, V>;
S
seemingwang 已提交
230 231
};

232
template <typename K, typename V>
S
seemingwang 已提交
233 234 235 236 237 238 239 240
class ScaledLRU {
 public:
  ScaledLRU(size_t shard_num, size_t size_limit, size_t _ttl)
      : size_limit(size_limit), ttl(_ttl) {
    pthread_rwlock_init(&rwlock, NULL);
    stop = false;
    thread_pool.reset(new ::ThreadPool(1));
    global_count = 0;
241 242
    lru_pool = std::vector<RandomSampleLRU<K, V>>(shard_num,
                                                  RandomSampleLRU<K, V>(this));
S
seemingwang 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    shrink_job = std::thread([this]() -> void {
      while (true) {
        {
          std::unique_lock<std::mutex> lock(mutex_);
          cv_.wait_for(lock, std::chrono::milliseconds(3000));
          if (stop) {
            return;
          }
        }

        auto status =
            thread_pool->enqueue([this]() -> int { return shrink(); });
        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);
  }
  int shrink() {
    int node_size = 0;
    std::string t = "";
    for (size_t i = 0; i < lru_pool.size(); i++) {
      node_size += lru_pool[i].node_size;
    }

    if (node_size <= size_limit) return 0;
    if (pthread_rwlock_wrlock(&rwlock) == 0) {
      try {
        global_count = 0;
        std::priority_queue<RemovedNode, std::vector<RemovedNode>,
                            std::greater<RemovedNode>>
            q;
        for (size_t i = 0; i < lru_pool.size(); i++) {
          if (lru_pool[i].node_size > 0) {
            global_count += lru_pool[i].node_size;
            q.push({lru_pool[i].node_head, &lru_pool[i]});
          }
        }
        if (global_count > size_limit) {
S
seemingwang 已提交
293
          // VLOG(0)<<"before shrinking cache, cached nodes count =
S
seemingwang 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306
          // "<<global_count<<std::endl;
          size_t remove = global_count - size_limit;
          while (remove--) {
            RemovedNode remove_node = q.top();
            q.pop();
            auto next = remove_node.node->next;
            if (next) {
              q.push({next, remove_node.lru_pointer});
            }
            global_count--;
            remove_node.lru_pointer->key_map.erase(remove_node.node->key);
            remove_node.lru_pointer->remove(remove_node.node, true);
          }
S
seemingwang 已提交
307
          // VLOG(0)<<"after shrinking cache, cached nodes count =
S
seemingwang 已提交
308 309 310
          // "<<global_count<<std::endl;
        }
      } catch (...) {
S
seemingwang 已提交
311
        // VLOG(0) << "shrink cache failed"<<std::endl;
S
seemingwang 已提交
312 313 314 315 316 317 318 319 320 321 322 323
        pthread_rwlock_unlock(&rwlock);
        return -1;
      }
      pthread_rwlock_unlock(&rwlock);
      return 0;
    }
    return 0;
  }
  void handle_size_diff(int diff) {
    if (diff != 0) {
      __sync_fetch_and_add(&global_count, diff);
      if (global_count > int(1.5 * size_limit)) {
S
seemingwang 已提交
324
        // VLOG(0)<<"global_count too large "<<global_count<<" enter start
S
seemingwang 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        // shrink task\n";
        thread_pool->enqueue([this]() -> int { return shrink(); });
      }
    }
  }

  size_t get_ttl() { return ttl; }

 private:
  pthread_rwlock_t rwlock;
  int global_count;
  size_t size_limit;
  size_t ttl;
  bool stop;
  std::thread shrink_job;
340
  std::vector<RandomSampleLRU<K, V>> lru_pool;
S
seemingwang 已提交
341 342 343 344
  mutable std::mutex mutex_;
  std::condition_variable cv_;
  struct RemovedNode {
    LRUNode<K, V> *node;
345
    RandomSampleLRU<K, V> *lru_pointer;
S
seemingwang 已提交
346 347 348
    bool operator>(const RemovedNode &a) const { return node->ms > a.node->ms; }
  };
  std::shared_ptr<::ThreadPool> thread_pool;
349
  friend class RandomSampleLRU<K, V>;
S
seemingwang 已提交
350 351
};

S
seemingwang 已提交
352 353
class GraphTable : public SparseTable {
 public:
354
  GraphTable() { use_cache = false; }
S
seemingwang 已提交
355 356 357 358 359 360
  virtual ~GraphTable() {}
  virtual int32_t pull_graph_list(int start, int size,
                                  std::unique_ptr<char[]> &buffer,
                                  int &actual_size, bool need_feature,
                                  int step);

361
  virtual int32_t random_sample_neighbors(
S
seemingwang 已提交
362
      uint64_t *node_ids, int sample_size,
363
      std::vector<std::shared_ptr<char>> &buffers,
S
seemingwang 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      std::vector<int> &actual_sizes);

  int32_t random_sample_nodes(int sample_size, std::unique_ptr<char[]> &buffers,
                              int &actual_sizes);

  virtual int32_t get_nodes_ids_by_ranges(
      std::vector<std::pair<int, int>> ranges, std::vector<uint64_t> &res);
  virtual int32_t initialize();

  int32_t load(const std::string &path, const std::string &param);

  int32_t load_edges(const std::string &path, bool reverse);

  int32_t load_nodes(const std::string &path, std::string node_type);

379 380 381 382 383
  int32_t add_graph_node(std::vector<uint64_t> &id_list,
                         std::vector<bool> &is_weight_list);

  int32_t remove_graph_node(std::vector<uint64_t> &id_list);

S
seemingwang 已提交
384
  int32_t get_server_index_by_id(uint64_t id);
S
seemingwang 已提交
385 386
  Node *find_node(uint64_t id);

387 388
  virtual int32_t pull_sparse(float *values,
                              const PullSparseValue &pull_value) {
S
seemingwang 已提交
389 390
    return 0;
  }
391

S
seemingwang 已提交
392 393 394 395
  virtual int32_t push_sparse(const uint64_t *keys, const float *values,
                              size_t num) {
    return 0;
  }
396

397
  virtual int32_t clear_nodes();
S
seemingwang 已提交
398 399 400 401 402 403 404 405
  virtual void clear() {}
  virtual int32_t flush() { return 0; }
  virtual int32_t shrink(const std::string &param) { return 0; }
  //指定保存路径
  virtual int32_t save(const std::string &path, const std::string &converter) {
    return 0;
  }
  virtual int32_t initialize_shard() { return 0; }
406
  virtual uint32_t get_thread_pool_index_by_shard_index(uint64_t shard_index);
S
seemingwang 已提交
407 408 409 410 411 412 413
  virtual uint32_t get_thread_pool_index(uint64_t node_id);
  virtual std::pair<int32_t, std::string> parse_feature(std::string feat_str);

  virtual int32_t get_node_feat(const std::vector<uint64_t> &node_ids,
                                const std::vector<std::string> &feature_names,
                                std::vector<std::vector<std::string>> &res);

S
seemingwang 已提交
414 415 416 417 418
  virtual int32_t set_node_feat(
      const std::vector<uint64_t> &node_ids,
      const std::vector<std::string> &feature_names,
      const std::vector<std::vector<std::string>> &res);

S
seemingwang 已提交
419 420
  size_t get_server_num() { return server_num; }

421
  virtual int32_t make_neighbor_sample_cache(size_t size_limit, size_t ttl) {
422 423 424
    {
      std::unique_lock<std::mutex> lock(mutex_);
      if (use_cache == false) {
425
        scaled_lru.reset(new ScaledLRU<SampleKey, SampleResult>(
426 427 428 429 430 431 432
            shard_end - shard_start, size_limit, ttl));
        use_cache = true;
      }
    }
    return 0;
  }

S
seemingwang 已提交
433 434
 protected:
  std::vector<GraphShard> shards;
S
seemingwang 已提交
435
  size_t shard_start, shard_end, server_num, shard_num_per_server, shard_num;
S
seemingwang 已提交
436
  const int task_pool_size_ = 24;
S
seemingwang 已提交
437 438 439 440 441 442 443 444 445 446
  const int random_sample_nodes_ranges = 3;

  std::vector<std::string> feat_name;
  std::vector<std::string> feat_dtype;
  std::vector<int32_t> feat_shape;
  std::unordered_map<std::string, int32_t> feat_id_map;
  std::string table_name;
  std::string table_type;

  std::vector<std::shared_ptr<::ThreadPool>> _shards_task_pool;
447
  std::vector<std::shared_ptr<std::mt19937_64>> _shards_task_rng_pool;
448
  std::shared_ptr<ScaledLRU<SampleKey, SampleResult>> scaled_lru;
449 450
  bool use_cache;
  mutable std::mutex mutex_;
S
seemingwang 已提交
451
};
452
}  // namespace distributed
453

454
};  // namespace paddle
455 456 457 458 459 460 461 462 463 464

namespace std {

template <>
struct hash<paddle::distributed::SampleKey> {
  size_t operator()(const paddle::distributed::SampleKey &s) const {
    return s.node_key ^ s.sample_size;
  }
};
}