memory_sparse_table.h 3.2 KB
Newer Older
Z
zhaocaibei123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 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>
#include <memory>
#include <mutex>  // NOLINT
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "Eigen/Dense"
27 28 29
#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 已提交
30 31 32 33 34 35 36 37 38
#include "paddle/fluid/string/string_helper.h"

#define PSERVER_SAVE_SUFFIX ".shard"

namespace paddle {
namespace distributed {

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

  // unused method begin
Z
zhaocaibei123 已提交
44 45 46
  virtual int32_t PullDense(float* pull_values, size_t num) { return 0; }
  virtual int32_t PushDenseParam(const float* values, size_t num) { return 0; }
  virtual int32_t PushDense(const float* values, size_t num) { return 0; }
Z
zhaocaibei123 已提交
47 48
  // unused method end

Y
yaoxuefeng 已提交
49 50 51
  virtual int32_t Pull(TableContext& context);
  virtual int32_t Push(TableContext& context);

Z
zhaocaibei123 已提交
52 53 54
  virtual int32_t Initialize();
  virtual int32_t InitializeShard() { return 0; }
  virtual int32_t InitializeValue();
Z
zhaocaibei123 已提交
55

Z
zhaocaibei123 已提交
56
  virtual int32_t Load(const std::string& path, const std::string& param);
Z
zhaocaibei123 已提交
57

Z
zhaocaibei123 已提交
58
  virtual int32_t Save(const std::string& path, const std::string& param);
Z
zhaocaibei123 已提交
59

Z
zhaocaibei123 已提交
60 61 62
  int32_t LoadLocalFS(const std::string& path, const std::string& param);
  int32_t SaveLocalFS(const std::string& path, const std::string& param,
                      const std::string& prefix);
Z
zhaocaibei123 已提交
63

Z
zhaocaibei123 已提交
64 65
  int64_t LocalSize();
  int64_t LocalMFSize();
66

Z
zhaocaibei123 已提交
67 68
  virtual std::pair<int64_t, int64_t> PrintTableStat();
  virtual int32_t PullSparse(float* values, const PullSparseValue& pull_value);
Z
zhaocaibei123 已提交
69

Z
zhaocaibei123 已提交
70 71
  virtual int32_t PullSparsePtr(char** pull_values, const uint64_t* keys,
                                size_t num);
Z
zhaocaibei123 已提交
72

Z
zhaocaibei123 已提交
73 74
  virtual int32_t PushSparse(const uint64_t* keys, const float* values,
                             size_t num);
Z
zhaocaibei123 已提交
75

Z
zhaocaibei123 已提交
76 77
  virtual int32_t PushSparse(const uint64_t* keys, const float** values,
                             size_t num);
Z
zhaocaibei123 已提交
78

Z
zhaocaibei123 已提交
79 80 81
  virtual int32_t Flush();
  virtual int32_t Shrink(const std::string& param);
  virtual void Clear();
Z
zhaocaibei123 已提交
82 83

 protected:
Z
zhaocaibei123 已提交
84 85
  virtual int32_t _PushSparse(const uint64_t* keys, const float** values,
                              size_t num);
Z
zhaocaibei123 已提交
86 87

 protected:
88 89 90 91 92 93
  const int _task_pool_size = 24;
  size_t _avg_local_shard_num;
  size_t _real_local_shard_num;
  size_t _sparse_table_shard_num;
  std::vector<std::shared_ptr<::ThreadPool>> _shards_task_pool;
  std::unique_ptr<shard_type[]> _local_shards;
Z
zhaocaibei123 已提交
94 95 96 97
};

}  // namespace distributed
}  // namespace paddle