heter_util.h 8.1 KB
Newer Older
T
Thunderbrook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 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 <fstream>
17
#include <map>
T
Thunderbrook 已提交
18 19 20 21 22 23
#include <memory>
#include <mutex>  // NOLINT
#include <string>
#include <thread>         // NOLINT
#include <unordered_map>  // NOLINT
#include <unordered_set>  // NOLINT
24
#include <utility>
T
Thunderbrook 已提交
25
#include <vector>
T
Thunderbrook 已提交
26
#if defined(PADDLE_WITH_PSLIB) && !defined(PADDLE_WITH_HETERPS)
T
Thunderbrook 已提交
27
#include "bthread/bthread.h"
T
Thunderbrook 已提交
28
#endif
T
Thunderbrook 已提交
29 30 31 32 33 34 35 36 37 38 39
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/timer.h"
namespace paddle {
namespace framework {
class DataFeed;
enum HeterTaskState { PULL_SPARSE, OP_RUN, XPU, OP_RUN_END, PUSH_GRAD, DONE };

class HeterTask {
 public:
  HeterTask() {}
40
  virtual ~HeterTask() {}
T
Thunderbrook 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  void Update() {
    if (state_ == PULL_SPARSE) {
      state_ = OP_RUN;
    } else if (state_ == OP_RUN) {
      state_ = XPU;
      // state_ = PUSH_GRAD;
      // state_ = PUSH_GRAD;
    } else if (state_ == XPU) {
      state_ = OP_RUN_END;
    } else if (state_ == OP_RUN_END) {
      state_ = PUSH_GRAD;
    } else if (state_ == PUSH_GRAD) {
      state_ = DONE;
    }
  }
  void Reset() {
    total_time = 0;
    read_time = 0;
    pack_time = 0;
    pull_sparse_local_time = 0;
    op_all_time = 0;
    xpu_op_time = 0;
    xpu_wait_time = 0;
    cpu_op_time = 0;
    collect_label_time = 0;
    fill_sparse_time = 0;
    push_sparse_time = 0;
    gpu_2_cpu_time = 0;
    cpu_2_gpu_time = 0;
    timeline.Reset();
  }
  void Show() {
    std::cout << "features size " << features_.size() << std::endl;
    for (size_t i = 0; i < features_.size(); ++i) {
      std::cout << "features[" << i << "] size " << features_[i].size()
                << std::endl;
    }
  }
80 81 82 83
  void PackTask(Scope* scope,
                int taskid,
                DataFeed* reader,
                int cur_batch,
T
Thunderbrook 已提交
84
                const ProgramDesc& program);
85 86
  void PackGpuTask(Scope* thread_scope,
                   DataFeed* reader,
T
Thunderbrook 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
                   const ProgramDesc& program);

  Scope* scope_{nullptr};
  int taskid_;
  int cur_batch_;
  HeterTaskState state_;
  // cache
  std::map<uint64_t, std::vector<uint64_t>> features_;
  std::map<uint64_t, std::vector<float>> feature_labels_;
  std::map<uint64_t, std::vector<std::vector<float>>> feature_values_;
  std::map<uint64_t, std::vector<std::vector<float>>> feature_grads_;
  std::map<uint64_t, std::vector<uint64_t>> sparse_push_keys_;
  double total_time{0};
  double read_time{0};
  double pack_time{0};
  double pull_sparse_local_time{0};
  double op_all_time{0};
  double xpu_op_time{0};
  double xpu_wait_time{0};
  double cpu_op_time{0};
  double collect_label_time{0};
  double fill_sparse_time{0};
  double push_sparse_time{0};
  double gpu_2_cpu_time{0};
  double cpu_2_gpu_time{0};
  platform::Timer timeline;
};
T
Thunderbrook 已提交
114

T
Thunderbrook 已提交
115 116 117 118
template <class T>
class HeterObjectPool {
 public:
  HeterObjectPool() {}
119
  virtual ~HeterObjectPool() {}
T
Thunderbrook 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  std::shared_ptr<T> Get() {
    std::lock_guard<std::mutex> lock(mutex_);
    if (pool_.empty()) {
      num_ += 1;
      return std::make_shared<T>();
    } else {
      auto ret = pool_.back();
      pool_.pop_back();
      return ret;
    }
  }
  void Push(std::shared_ptr<T> data) {
    std::lock_guard<std::mutex> lock(mutex_);
    pool_.push_back(std::move(data));
  }
  int Size() {
    std::lock_guard<std::mutex> lock(mutex_);
    return pool_.size();
  }
139 140 141 142
  bool Empty() {
    std::lock_guard<std::mutex> lock(mutex_);
    return pool_.empty();
  }
T
Thunderbrook 已提交
143 144 145 146 147 148 149 150
  std::shared_ptr<T>& GetElement(int i) { return pool_[i]; }

 private:
  std::vector<std::shared_ptr<T>> pool_;
  std::mutex mutex_;
  int num_{0};
};

T
Thunderbrook 已提交
151
#if defined(PADDLE_WITH_PSLIB) && !defined(PADDLE_WITH_HETERPS)
T
Thunderbrook 已提交
152
struct BthreadMutextGuard {
153
  explicit BthreadMutextGuard(bthread_mutex_t* rho) {
T
Thunderbrook 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    mutex_ = rho;
    bthread_mutex_lock(mutex_);
  }
  ~BthreadMutextGuard() { bthread_mutex_unlock(mutex_); }
  bthread_mutex_t* mutex_;
};

template <class T>
class BtObjectPool {
 public:
  BtObjectPool() {
    bthread_mutex_init(&mutex_, NULL);
    bthread_cond_init(&cond_, NULL);
  }

  virtual ~BtObjectPool() {
    bthread_cond_destroy(&cond_);
    bthread_mutex_destroy(&mutex_);
172
  }
T
Thunderbrook 已提交
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 224 225 226 227

  std::shared_ptr<T> Get() {
    BthreadMutextGuard guard(&mutex_);
    while (pool_.empty()) {
      bthread_cond_wait(&cond_, &mutex_);
    }
    auto ret = pool_.back();
    pool_.pop_back();
    return ret;
  }

  void Push(std::shared_ptr<T> data) {
    BthreadMutextGuard guard(&mutex_);
    pool_.push_back(std::move(data));
    bthread_cond_signal(&cond_);
  }

  int Size() { return pool_.size(); }

  std::shared_ptr<T>& GetElement(int i) { return pool_[i]; }

 private:
  std::vector<std::shared_ptr<T>> pool_;
  bthread_mutex_t mutex_;
  bthread_cond_t cond_;
  int num_{0};
};

template <class K, class T>
struct HeterNode {
  K key;
  T value;
  HeterNode* prev;
  HeterNode* next;
};

template <class K, class T>
class HeterList {
 public:
  HeterList() : head_(new HeterNode<K, T>), tail_(new HeterNode<K, T>) {
    head_->prev = NULL;
    head_->next = tail_;
    tail_->prev = head_;
    tail_->next = NULL;
    size = 0;
    cap_ = 1e9;
  }

  ~HeterList() {
    delete head_;
    delete tail_;
  }

  void SetCap(int num) { cap_ = num; }

228
  bool TryPut(const K& key, const T& value) {
T
Thunderbrook 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    std::unique_lock<std::mutex> lock(mutex_);
    cond_.wait(lock, [this] { return size < cap_; });
    if (task_map_.find(key) != task_map_.end()) {
      task_map_.erase(key);
      return false;
    } else {
      HeterNode<K, T>* node = new HeterNode<K, T>;
      node->key = key;
      node->value = value;
      map_[node->key] = node;
      attach(node);
      return true;
    }
  }

244
  bool Put(const K& key, const T& value) {
T
Thunderbrook 已提交
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    std::unique_lock<std::mutex> lock(mutex_);
    cond_.wait(lock, [this] { return size < cap_; });
    HeterNode<K, T>* node = new HeterNode<K, T>;
    node->key = key;
    node->value = value;
    map_[node->key] = node;
    attach(node);
    return true;
  }

  T TryGet(const K& key) {
    std::lock_guard<std::mutex> lock(mutex_);
    auto iter = map_.find(key);
    if (iter != map_.end()) {
      HeterNode<K, T>* node = iter->second;
      detach(node);
      cond_.notify_one();
      T ret = std::move(node->value);
      map_.erase(key);
      delete node;
      return ret;
    }
    task_map_.insert(key);
    return nullptr;
  }

  T Get(const K& key) {
    std::lock_guard<std::mutex> lock(mutex_);
    auto iter = map_.find(key);
    if (iter != map_.end()) {
      HeterNode<K, T>* node = iter->second;
      detach(node);
      cond_.notify_one();
      T ret = std::move(node->value);
      map_.erase(key);
      delete node;
      return ret;
    }
    return nullptr;
  }

  T Get() {
    std::lock_guard<std::mutex> lock(mutex_);
    HeterNode<K, T>* node = head_->next;
    if (node == tail_) {
      return nullptr;
    } else {
      detach(node);
      cond_.notify_one();
      T ret = std::move(node->value);
      map_.erase(node->key);
      delete node;
      return ret;
    }
  }

  bool Empty() {
    std::lock_guard<std::mutex> lock(mutex_);
    return head_->next == tail_;
  }

  int Size() {
    std::lock_guard<std::mutex> lock(mutex_);
    return size;
  }

 private:
  void detach(HeterNode<K, T>* node) {
    node->prev->next = node->next;
    node->next->prev = node->prev;
    size--;
  }

  void attach(HeterNode<K, T>* node) {
    node->prev = head_;
    node->next = head_->next;
    head_->next->prev = node;
    head_->next = node;
    size++;
  }

 private:
  HeterNode<K, T>* head_;
  HeterNode<K, T>* tail_;
  std::unordered_map<K, HeterNode<K, T>*> map_;
  std::unordered_set<K> task_map_;
  std::mutex mutex_;
  std::condition_variable cond_;
  int cap_;
  int size;
};
T
Thunderbrook 已提交
336
#endif
T
Thunderbrook 已提交
337 338
}  // namespace framework
}  // namespace paddle