memory_sparse_table.h 3.4 KB
Newer Older
Z
zhaocaibei123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (c) 2020 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

Z
zhaocaibei123 已提交
21 22 23 24 25 26
#include <memory>
#include <mutex>  // NOLINT
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
27

Z
zhaocaibei123 已提交
28
#include "Eigen/Dense"
29 30 31
#include "paddle/fluid/distributed/ps/table/accessor.h"
#include "paddle/fluid/distributed/ps/table/common_table.h"
#include "paddle/fluid/distributed/ps/table/depends/feature_value.h"
Z
zhaocaibei123 已提交
32 33 34 35 36 37 38
#include "paddle/fluid/string/string_helper.h"

#define PSERVER_SAVE_SUFFIX ".shard"

namespace paddle {
namespace distributed {

39
class MemorySparseTable : public Table {
Z
zhaocaibei123 已提交
40
 public:
41
  typedef SparseTableShard<uint64_t, FixedFeatureValue> shard_type;
Z
zhaocaibei123 已提交
42 43 44 45
  MemorySparseTable() {}
  virtual ~MemorySparseTable() {}

  // unused method end
46 47 48 49 50 51 52 53
  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;
  }
Z
zhaocaibei123 已提交
54

55 56
  static size_t get_sparse_shard(uint32_t shard_num,
                                 uint32_t server_num,
57 58 59
                                 uint64_t key) {
    return (key % shard_num) / sparse_local_shard_num(shard_num, server_num);
  }
Y
yaoxuefeng 已提交
60

61 62
  int32_t Pull(TableContext& context) override;
  int32_t Push(TableContext& context) override;
Z
zhaocaibei123 已提交
63

64 65 66
  int32_t Initialize() override;
  int32_t InitializeShard() override { return 0; }
  int32_t InitializeValue();
Z
zhaocaibei123 已提交
67

Z
zhaocaibei123 已提交
68 69
  virtual int32_t Load(const std::string& path,
                       const std::string& param) override;
70

Z
zhaocaibei123 已提交
71 72
  virtual int32_t Save(const std::string& path,
                       const std::string& param) override;
Z
zhaocaibei123 已提交
73

Z
zhaocaibei123 已提交
74
  int32_t LoadLocalFS(const std::string& path, const std::string& param);
75 76
  int32_t SaveLocalFS(const std::string& path,
                      const std::string& param,
Z
zhaocaibei123 已提交
77
                      const std::string& prefix);
Z
zhaocaibei123 已提交
78

Z
zhaocaibei123 已提交
79 80
  int64_t LocalSize();
  int64_t LocalMFSize();
81

82 83
  std::pair<int64_t, int64_t> PrintTableStat() override;
  int32_t PullSparse(float* values, const PullSparseValue& pull_value);
Z
zhaocaibei123 已提交
84

85
  int32_t PullSparsePtr(char** pull_values, const uint64_t* keys, size_t num);
Z
zhaocaibei123 已提交
86

87
  int32_t PushSparse(const uint64_t* keys, const float* values, size_t num);
Z
zhaocaibei123 已提交
88

89
  int32_t PushSparse(const uint64_t* keys, const float** values, size_t num);
Z
zhaocaibei123 已提交
90

91
  int32_t Flush() override;
Z
zhaocaibei123 已提交
92
  virtual int32_t Shrink(const std::string& param) override;
93
  void Clear() override;
Z
zhaocaibei123 已提交
94

95 96 97
  void* GetShard(size_t shard_idx) override {
    return &_local_shards[shard_idx];
  }
Z
zhaocaibei123 已提交
98 99

 protected:
100
  const int _task_pool_size = 24;
Z
zhaocaibei123 已提交
101 102 103
  int _avg_local_shard_num;
  int _real_local_shard_num;
  int _sparse_table_shard_num;
104 105
  std::vector<std::shared_ptr<::ThreadPool>> _shards_task_pool;
  std::unique_ptr<shard_type[]> _local_shards;
Z
zhaocaibei123 已提交
106 107 108 109
};

}  // namespace distributed
}  // namespace paddle