async_executor.h 3.4 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 20 21 22 23 24 25
#include <map>
#include <memory>
#include <mutex>  // NOLINT
#include <set>
#include <string>
#include <thread>  // NOLINT
#include <typeinfo>
#include <vector>
26
#include <random>  // local_random_engine
W
Wang Guibao 已提交
27 28 29 30 31 32 33 34
#include "paddle/fluid/framework/data_feed.pb.h"
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/executor_thread_worker.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"

namespace paddle {
namespace framework {
35 36 37 38 39 40 41 42 43 44 45

inline double current_realtime() {
    struct timespec tp;
    clock_gettime(CLOCK_REALTIME, &tp);
    return tp.tv_sec + tp.tv_nsec * 1e-9;
}

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

W
Wang Guibao 已提交
57 58 59 60 61 62 63 64 65
class AsyncExecutor {
 public:
  AsyncExecutor(Scope* scope, const platform::Place& place);
  virtual ~AsyncExecutor() {}
  void RunFromFile(const ProgramDesc& main_program,
                   const std::string& data_feed_desc_str,
                   const std::vector<std::string>& filelist,
                   const int thread_num,
                   const std::vector<std::string>& fetch_names,
66
                   const std::string& mode,
W
Wang Guibao 已提交
67
                   const bool debug = false);
H
heqiaozhi 已提交
68
#ifdef PADDLE_WITH_PSLIB
H
heqiaozhi 已提交
69
  void InitServer(const std::string& dist_desc, int index);
70 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);
H
heqiaozhi 已提交
79
  void InitParamConfig();
H
heqiaozhi 已提交
80
#endif
D
dongdaxiang 已提交
81

W
Wang Guibao 已提交
82 83 84 85 86 87 88
 private:
  void CreateThreads(ExecutorThreadWorker* worker,
                     const ProgramDesc& main_program,
                     const std::shared_ptr<DataFeed>& reader,
                     const std::vector<std::string>& fetch_var_names,
                     Scope* root_scope, const int thread_index,
                     const bool debug);
H
heqiaozhi 已提交
89
#ifdef PADDLE_WITH_PSLIB
H
heqiaozhi 已提交
90
  void PrepareDenseThread(const std::string& mode);
H
heqiaozhi 已提交
91
#endif
D
dongdaxiang 已提交
92

W
Wang Guibao 已提交
93
 public:
H
heqiaozhi 已提交
94
#ifdef PADDLE_WITH_PSLIB
95 96
  std::shared_ptr<paddle::distributed::PSlib>  _pslib_ptr;
  std::shared_ptr<DensePullThread>  _pull_dense_thread;
H
heqiaozhi 已提交
97 98
  AsyncWorkerParamConfig _param_config;
#endif
W
Wang Guibao 已提交
99 100
  Scope* root_scope_;
  platform::Place place_;
101 102 103
  
 private:
  int actual_thread_num;
H
heqiaozhi 已提交
104

W
Wang Guibao 已提交
105 106
};

107 108


W
Wang Guibao 已提交
109 110
}  // namespace framework
}  // namespace paddle