dist_multi_trainer.cc 4.0 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
#include "paddle/fluid/framework/data_feed_factory.h"
D
dongdaxiang 已提交
19
#include "paddle/fluid/framework/data_set.h"
20 21 22 23 24 25
#include "paddle/fluid/framework/device_worker_factory.h"
#include "paddle/fluid/framework/trainer.h"

namespace paddle {
namespace framework {

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

31 32 33 34 35 36 37 38 39 40 41 42 43
  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() / 2;
J
jiaqi 已提交
44
  const std::vector<paddle::framework::DataFeed*> readers =
45
      dataset->GetReaders();
46

47 48 49
  thread_num_ = readers.size();
  workers_.resize(thread_num_);

50 51 52 53
  for (int i = 0; i < thread_num_; ++i) {
    workers_[i] = DeviceWorkerFactory::CreateDeviceWorker(
        trainer_desc.device_worker_name());
    workers_[i]->SetDeviceIndex(i);
D
dongdaxiang 已提交
54
    workers_[i]->SetDataFeed(readers[i]);
55
    workers_[i]->Initialize(trainer_desc);
56
    workers_[i]->SetNeedDump(need_dump_field_);
57 58
  }

D
dongdaxiang 已提交
59
  VLOG(3) << "going to initialize pull dense worker";
60 61
  pull_dense_worker_ = PullDenseWorker::GetInstance();
  pull_dense_worker_->Initialize(trainer_desc);
D
dongdaxiang 已提交
62
  VLOG(3) << "initialize pull dense worker";
63
  SetDebug(trainer_desc.debug());
64 65
}

66 67 68 69 70 71 72 73 74 75 76 77 78 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
void DistMultiTrainer::DumpWork() {
#ifdef _LINUX
  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 DistMultiTrainer::InitDumpEnv() {
  queue_ = paddle::framework::MakeChannel<std::string>();
  int err_no = 0;
  std::string path = string::format_string(
      "%s/part-%03d", dump_fields_path_.c_str(), mpi_rank_);

  fp_ = fs_open_write(path, &err_no, dump_converter_);
  for (int i = 0; i < thread_num_; ++i) {
    workers_[i]->SetChannelWriter(queue_.get());
  }
  dump_thread_ = std::thread(&DistMultiTrainer::DumpWork, this);
}

void DistMultiTrainer::FinalizeDumpEnv() {
  queue_->Close();
  dump_thread_.join();
  queue_.reset();
}

107
void DistMultiTrainer::InitOtherEnv(const ProgramDesc& main_program) {
108 109 110
  if (need_dump_field_) {
    InitDumpEnv();
  }
111
  pull_dense_worker_->SetRootScope(root_scope_);
112
  pull_dense_worker_->Start();
D
dongdaxiang 已提交
113
  VLOG(3) << "init other env done.";
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127
void DistMultiTrainer::Run() {
  for (int thidx = 0; thidx < thread_num_; ++thidx) {
    if (!debug_) {
      threads_.push_back(
          std::thread(&DeviceWorker::TrainFiles, workers_[thidx].get()));
    } else {
      threads_.push_back(std::thread(&DeviceWorker::TrainFilesWithProfiler,
                                     workers_[thidx].get()));
    }
  }
}

128 129 130 131
void DistMultiTrainer::Finalize() {
  for (auto& th : threads_) {
    th.join();
  }
132 133 134
  if (need_dump_field_) {
    FinalizeDumpEnv();
  }
135
  pull_dense_worker_->Stop();
136
  root_scope_->DropKids();
137 138 139 140
}

}  // end namespace framework
}  // end namespace paddle