multi_trainer.cc 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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 <vector>
17
#include "io/fs.h"
18 19 20
#include "paddle/fluid/framework/data_feed_factory.h"
#include "paddle/fluid/framework/device_worker_factory.h"
#include "paddle/fluid/framework/trainer.h"
21
#include "paddle/fluid/operators/distributed/distributed.h"
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  SetDataset(dataset);

  dump_fields_path_ = trainer_desc.dump_fields_path();
  dump_converter_ = trainer_desc.dump_converter();
  need_dump_field_ = false;
  if (trainer_desc.dump_fields_size() != 0 && dump_fields_path_ != "") {
    need_dump_field_ = true;
  }
  if (need_dump_field_) {
    auto& file_list = dataset->GetFileList();
    if (file_list.size() == 0) {
      need_dump_field_ = false;
    }
  }
  mpi_rank_ = trainer_desc.mpi_rank();
  mpi_size_ = trainer_desc.mpi_size();
  dump_file_num_ = trainer_desc.dump_file_num();

47 48 49 50 51
  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));
  }
52
  // get filelist from trainer_desc here
J
jiaqi 已提交
53
  const std::vector<paddle::framework::DataFeed*> readers =
D
dongdaxiang 已提交
54
      dataset->GetReaders();
55
  VLOG(3) << "readers num: " << readers.size();
56 57 58 59
  // change thread num to readers num
  thread_num_ = readers.size();
  VLOG(3) << "worker thread num: " << thread_num_;
  workers_.resize(thread_num_);
60 61 62 63 64 65 66 67

#ifdef PADDLE_WITH_DISTRIBUTE
  if (trainer_desc.thread_barrier()) {
    operators::distributed::Communicator::GetInstance()->BarrierTriggerReset(
        thread_num_);
  }
#endif

68 69 70
  for (int i = 0; i < thread_num_; ++i) {
    workers_[i] = DeviceWorkerFactory::CreateDeviceWorker(
        trainer_desc.device_worker_name());
D
dongdaxiang 已提交
71
    workers_[i]->Initialize(trainer_desc);
72
    workers_[i]->SetDeviceIndex(i);
D
dongdaxiang 已提交
73
    workers_[i]->SetDataFeed(readers[i]);
74
    workers_[i]->SetNeedDump(need_dump_field_);
75
  }
D
dongdaxiang 已提交
76 77

  // set debug here
78
  SetDebug(trainer_desc.debug());
79 80
}

81 82 83 84 85 86 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
void MultiTrainer::DumpWork(int tid) {
#ifdef _LINUX
  int err_no = 0;
  std::string path = string::format_string(
      "%s/part-%03d-%05d", dump_fields_path_.c_str(), mpi_rank_, tid);

  std::shared_ptr<FILE> fp = fs_open_write(path, &err_no, dump_converter_);
  while (1) {
    std::string out_str;
    if (!queue_->Get(out_str)) {
      break;
    }
    size_t write_count =
        fwrite_unlocked(out_str.data(), 1, out_str.length(), fp.get());
    if (write_count != out_str.length()) {
      VLOG(3) << "dump text failed";
      continue;
    }
    write_count = fwrite_unlocked("\n", 1, 1, fp.get());
    if (write_count != 1) {
      VLOG(3) << "dump text failed";
      continue;
    }
  }
#endif
}

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(
        std::thread(std::bind(&MultiTrainer::DumpWork, this, i)));
  }
}

void MultiTrainer::FinalizeDumpEnv() {
  queue_->Close();
  for (auto& th : dump_thread_) {
    th.join();
  }
  queue_.reset();
}

134 135 136 137 138
// 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);
139
    workers_[i]->SetReaderPlace(place);
140 141 142 143 144 145
    workers_[i]->SetRootScope(root_scope_);
    workers_[i]->CreateDeviceResource(main_program);  // Program
    workers_[i]->BindingDataFeedMemory();
  }
}

146 147 148 149 150 151 152
void MultiTrainer::InitOtherEnv(const ProgramDesc& main_program) {
  if (need_dump_field_) {
    InitDumpEnv();
  }
  VLOG(3) << "init other env done.";
}

153 154 155 156
Scope* MultiTrainer::GetWorkerScope(int thread_id) {
  return workers_[thread_id]->GetThreadScope();
}

157
void MultiTrainer::Run() {
158
  VLOG(3) << "Going to run";
159
  for (int thidx = 0; thidx < thread_num_; ++thidx) {
160 161 162 163 164 165 166
    if (!debug_) {
      threads_.push_back(
          std::thread(&DeviceWorker::TrainFiles, workers_[thidx].get()));
    } else {
      threads_.push_back(std::thread(&DeviceWorker::TrainFilesWithProfiler,
                                     workers_[thidx].get()));
    }
167 168 169 170 171 172
  }
  for (auto& th : threads_) {
    th.join();
  }
}

173 174 175 176 177 178
void MultiTrainer::Finalize() {
  if (need_dump_field_) {
    FinalizeDumpEnv();
  }
  root_scope_->DropKids();
}
D
Dong Daxiang 已提交
179

180 181
}  // end namespace framework
}  // end namespace paddle