multi_trainer.cc 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2016 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. */

#include <string>
#include "paddle/fluid/framework/device_worker_factory.h"
#include "paddle/fluid/framework/trainer.h"
T
tangwei12 已提交
18

T
tangwei12 已提交
19
#if defined PADDLE_WITH_PSCORE
T
tangwei12 已提交
20 21
#include "paddle/fluid/distributed/service/communicator.h"
#endif
22 23 24 25

namespace paddle {
namespace framework {

D
dongdaxiang 已提交
26
void MultiTrainer::Initialize(const TrainerDesc& trainer_desc,
27
                              Dataset* dataset) {
28
  thread_num_ = trainer_desc.thread_num();
29 30
  SetDataset(dataset);

H
hutuxian 已提交
31
  ParseDumpConfig(trainer_desc);
32 33 34 35
  mpi_rank_ = trainer_desc.mpi_rank();
  mpi_size_ = trainer_desc.mpi_size();
  dump_file_num_ = trainer_desc.dump_file_num();

36 37 38 39 40
  for (int i = 0; i < trainer_desc.downpour_param().stat_var_names_size();
       i++) {
    need_merge_var_names_.push_back(
        trainer_desc.downpour_param().stat_var_names(i));
  }
41
  // get filelist from trainer_desc here
J
jiaqi 已提交
42
  const std::vector<paddle::framework::DataFeed*> readers =
D
dongdaxiang 已提交
43
      dataset->GetReaders();
44
  VLOG(3) << "readers num: " << readers.size();
45 46 47 48
  // change thread num to readers num
  thread_num_ = readers.size();
  VLOG(3) << "worker thread num: " << thread_num_;
  workers_.resize(thread_num_);
49

T
tangwei12 已提交
50
#if defined PADDLE_WITH_PSCORE
51
  if (trainer_desc.thread_barrier()) {
T
tangwei12 已提交
52
    paddle::distributed::Communicator::GetInstance()->BarrierTriggerReset(
53 54 55 56
        thread_num_);
  }
#endif

57 58 59
  for (int i = 0; i < thread_num_; ++i) {
    workers_[i] = DeviceWorkerFactory::CreateDeviceWorker(
        trainer_desc.device_worker_name());
H
hutuxian 已提交
60 61 62 63 64
    workers_[i]->SetNeedDumpField(need_dump_field_);
    workers_[i]->SetNeedDumpParam(need_dump_param_);
    workers_[i]->SetDumpFieldVector(dump_fields_);
    workers_[i]->SetDumpParamVector(dump_param_);
    workers_[i]->InitRandomDumpConfig(trainer_desc);
D
dongdaxiang 已提交
65
    workers_[i]->Initialize(trainer_desc);
66
    workers_[i]->SetDeviceIndex(i);
D
dongdaxiang 已提交
67
    workers_[i]->SetDataFeed(readers[i]);
68
  }
D
dongdaxiang 已提交
69 70

  // set debug here
71
  SetDebug(trainer_desc.debug());
72 73
}

H
hutuxian 已提交
74
std::string MultiTrainer::GetDumpPath(int tid) {
Y
yaoxuefeng 已提交
75 76 77 78
  if (user_define_dump_filename_ != "") {
    return string::format_string("%s/part-%s-%05d", dump_fields_path_.c_str(),
                                 user_define_dump_filename_.c_str(), tid);
  }
H
hutuxian 已提交
79 80
  return string::format_string("%s/part-%03d-%05d", dump_fields_path_.c_str(),
                               mpi_rank_, tid);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

void MultiTrainer::InitDumpEnv() {
  queue_ = paddle::framework::MakeChannel<std::string>();
  for (int i = 0; i < thread_num_; ++i) {
    workers_[i]->SetChannelWriter(queue_.get());
  }
  dump_thread_num_ = 1;
  if (dump_file_num_ > mpi_size_) {
    dump_thread_num_ = dump_file_num_ / mpi_size_;
    if (dump_file_num_ % mpi_size_ > mpi_rank_) {
      dump_thread_num_ += 1;
    }
  }
  for (int i = 0; i < dump_thread_num_; i++) {
    dump_thread_.push_back(
H
hutuxian 已提交
97
        std::thread(std::bind(&TrainerBase::DumpWork, this, i)));
98 99 100
  }
}

101 102 103 104 105
// call only after all resources are set in current trainer
void MultiTrainer::InitTrainerEnv(const ProgramDesc& main_program,
                                  const platform::Place& place) {
  for (int i = 0; i < thread_num_; ++i) {
    workers_[i]->SetPlace(place);
106
    workers_[i]->SetReaderPlace(place);
107 108 109
    workers_[i]->SetRootScope(root_scope_);
    workers_[i]->CreateDeviceResource(main_program);  // Program
    workers_[i]->BindingDataFeedMemory();
T
Thunderbrook 已提交
110
    workers_[i]->CacheProgram(main_program);
111 112 113
  }
}

114
void MultiTrainer::InitOtherEnv(const ProgramDesc& main_program) {
X
xujiaqi01 已提交
115
  if (need_dump_field_ || need_dump_param_) {
116 117 118 119 120
    InitDumpEnv();
  }
  VLOG(3) << "init other env done.";
}

121 122 123 124
Scope* MultiTrainer::GetWorkerScope(int thread_id) {
  return workers_[thread_id]->GetThreadScope();
}

125
void MultiTrainer::Run() {
126
  VLOG(3) << "Going to run";
P
phlrain 已提交
127
  LOG(ERROR) << "multi run " << thread_num_ << "\t" << debug_;
128
  for (int thidx = 0; thidx < thread_num_; ++thidx) {
129 130 131 132 133 134 135
    if (!debug_) {
      threads_.push_back(
          std::thread(&DeviceWorker::TrainFiles, workers_[thidx].get()));
    } else {
      threads_.push_back(std::thread(&DeviceWorker::TrainFilesWithProfiler,
                                     workers_[thidx].get()));
    }
136 137 138 139 140 141
  }
  for (auto& th : threads_) {
    th.join();
  }
}

142
void MultiTrainer::Finalize() {
X
xujiaqi01 已提交
143
  if (need_dump_field_ || need_dump_param_) {
144 145 146 147
    FinalizeDumpEnv();
  }
  root_scope_->DropKids();
}
D
Dong Daxiang 已提交
148

149 150
}  // end namespace framework
}  // end namespace paddle