async_executor.h 2.7 KB
Newer Older
W
Wang Guibao 已提交
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

17
#include <time.h>
W
Wang Guibao 已提交
18 19
#include <map>
#include <memory>
D
dongdaxiang 已提交
20 21
#include <mutex>   // NOLINT
#include <random>  // local_random_engine
W
Wang Guibao 已提交
22 23 24 25 26 27 28 29
#include <set>
#include <string>
#include <thread>  // NOLINT
#include <typeinfo>
#include <vector>
#include "paddle/fluid/framework/data_feed.pb.h"
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/executor_thread_worker.h"
D
dongdaxiang 已提交
30
#include "paddle/fluid/framework/fleet/fleet_wrapper.h"
W
Wang Guibao 已提交
31 32
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
33
#include "paddle/fluid/framework/data_set.h"
W
Wang Guibao 已提交
34 35 36

namespace paddle {
namespace framework {
37 38

inline double current_realtime() {
D
dongdaxiang 已提交
39
#if !defined(_WIN32)
D
dongdaxiang 已提交
40 41 42
  struct timespec tp;
  clock_gettime(CLOCK_REALTIME, &tp);
  return tp.tv_sec + tp.tv_nsec * 1e-9;
D
dongdaxiang 已提交
43
#else
D
dongdaxiang 已提交
44
  return 0.0;
D
dongdaxiang 已提交
45
#endif
46 47 48
}

inline std::default_random_engine& local_random_engine() {
D
dongdaxiang 已提交
49 50 51 52 53 54 55 56 57 58 59
  struct engine_wrapper_t {
    std::default_random_engine engine;
    engine_wrapper_t() {
      static std::atomic<uint64_t> x(0);
      std::seed_seq sseq = {x++, x++, x++,
                            static_cast<uint64_t>(current_realtime() * 1000)};
      engine.seed(sseq);
    }
  };
  thread_local engine_wrapper_t r;
  return r.engine;
60 61
}

W
Wang Guibao 已提交
62 63 64 65 66
class AsyncExecutor {
 public:
  AsyncExecutor(Scope* scope, const platform::Place& place);
  virtual ~AsyncExecutor() {}
  void RunFromFile(const ProgramDesc& main_program,
D
dongdaxiang 已提交
67
                   const std::string& trainer_desc_str, const bool debug);
68

D
dongdaxiang 已提交
69
  // TODO(guru4elephant): make init server decoupled from executor
H
heqiaozhi 已提交
70
  void InitServer(const std::string& dist_desc, int index);
D
dongdaxiang 已提交
71 72 73
  void InitWorker(const std::string& dist_desc,
                  const std::vector<uint64_t>& host_sign_list, int node_num,
                  int index);
H
heqiaozhi 已提交
74
  uint64_t StartServer();
H
heqiaozhi 已提交
75
  void StopServer();
76
  void GatherServers(const std::vector<uint64_t>& host_sign_list, int node_num);
77 78
  void InitModel();
  void SaveModel(const std::string& path);
D
dongdaxiang 已提交
79

W
Wang Guibao 已提交
80
 public:
81
  std::shared_ptr<paddle::framework::FleetWrapper> fleet_ptr_;
W
Wang Guibao 已提交
82 83
  Scope* root_scope_;
  platform::Place place_;
D
dongdaxiang 已提交
84

85
 private:
86
  int actual_thread_num_;
W
Wang Guibao 已提交
87 88 89 90
};

}  // namespace framework
}  // namespace paddle