data_set.cc 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/fluid/framework/data_set.h"
D
dongdaxiang 已提交
16
#include <random>
17 18 19
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
20
#include "paddle/fluid/framework/data_feed_factory.h"
21 22
#include "paddle/fluid/platform/timer.h"
#include "paddle/fluid/framework/io/fs.h"
23 24 25 26

namespace paddle {
namespace framework {

27
template <typename T>
D
dongdaxiang 已提交
28 29
DatasetImpl<T>::DatasetImpl() {
  thread_num_ = 1;
30 31
  trainer_num_ = 1;
  file_idx_ = 0;
D
dongdaxiang 已提交
32
}
33

34 35
template <typename T>
void DatasetImpl<T>::SetFileList(const std::vector<std::string>& filelist) {
36
  VLOG(3) << "filelist size: " << filelist.size();
37
  filelist_ = filelist;
38
  file_idx_ = 0;
39
  /*
40 41
  int file_cnt = filelist_.size();
  if (thread_num_ > file_cnt) {
D
dongdaxiang 已提交
42 43
    VLOG(1) << "DataSet thread num = " << thread_num_
            << ", file num = " << file_cnt
44 45
            << ". Changing DataSet thread num = " << file_cnt;
    thread_num_ = file_cnt;
46
  }*/
47 48
}

49 50
// buggy here, a user should set filelist first before this function
// not user friendly
51 52
template <typename T>
void DatasetImpl<T>::SetThreadNum(int thread_num) {
53 54 55
  VLOG(3) << "SetThreadNum thread_num=" << thread_num;
  //int file_cnt = filelist_.size();
  /*
56
  if (file_cnt != 0 && thread_num > file_cnt) {
57
    VLOG(3) << "DataSet thread num = " << thread_num
D
dongdaxiang 已提交
58
            << ", file num = " << file_cnt
59 60
            << ". Changing DataSet thread num = " << file_cnt;
    thread_num = file_cnt;
61
  }*/
62 63 64
  thread_num_ = thread_num;
}

65
template <typename T>
X
xujiaqi01 已提交
66 67
void DatasetImpl<T>::SetTrainerNum(int trainer_num) {
  trainer_num_ = trainer_num;
68 69 70 71 72 73 74 75 76 77 78 79 80
  // should inform reader of trainer_num directly
  for (auto reader : readers_) {
    reader->SetTrainerNum(trainer_num);
  }
}

template <typename T>
void DatasetImpl<T>::SetHdfsConfig(const std::string& fs_name,
                                   const std::string& fs_ugi) {
  std::string cmd = std::string("hadoop fs");
  cmd += " -D fs.default.name=" + fs_name;
  cmd += " -D hadoop.job.ugi=" + fs_ugi;
  paddle::framework::hdfs_set_command(cmd);
X
xujiaqi01 已提交
81
}
82

83 84
template <typename T>
void DatasetImpl<T>::SetDataFeedDesc(const std::string& data_feed_desc_str) {
85 86
  google::protobuf::TextFormat::ParseFromString(data_feed_desc_str,
                                                &data_feed_desc_);
87 88
}

89 90
template <typename T>
std::vector<std::shared_ptr<paddle::framework::DataFeed>>&
D
dongdaxiang 已提交
91
DatasetImpl<T>::GetReaders() {
92 93 94
  return readers_;
}

95 96 97
template <typename T>
void DatasetImpl<T>::LoadIntoMemory() {
  VLOG(3) << "DatasetImpl<T>::LoadIntoMemory() begin";
98 99
  platform::Timer timeline;
  timeline.Start();
100 101 102 103 104
  if (readers_.size() == 0) {
    CreateReaders();
  }
  std::vector<std::thread> load_threads;
  for (int64_t i = 0; i < thread_num_; ++i) {
D
dongdaxiang 已提交
105 106
    load_threads.push_back(std::thread(
        &paddle::framework::DataFeed::LoadIntoMemory, readers_[i].get()));
107 108 109 110
  }
  for (std::thread& t : load_threads) {
    t.join();
  }
111 112 113 114
  timeline.Pause();
  VLOG(3) << "DatasetImpl<T>::LoadIntoMemory() end"
          << ", memory data size=" << memory_data_.size()
          << ", cost time=" << timeline.ElapsedSec() << " seconds";
115 116
}

117 118 119
template <typename T>
void DatasetImpl<T>::LocalShuffle() {
  VLOG(3) << "DatasetImpl<T>::LocalShuffle() begin";
120 121
  platform::Timer timeline;
  timeline.Start();
122 123 124
  if (readers_.size() == 0) {
    CreateReaders();
  }
125 126 127
  // if it is not InMemory, memory_data_ is empty
  std::random_shuffle(memory_data_.begin(), memory_data_.end());

128 129
  std::vector<std::thread> local_shuffle_threads;
  for (int64_t i = 0; i < thread_num_; ++i) {
D
dongdaxiang 已提交
130 131
    local_shuffle_threads.push_back(std::thread(
        &paddle::framework::DataFeed::LocalShuffle, readers_[i].get()));
132 133 134 135
  }
  for (std::thread& t : local_shuffle_threads) {
    t.join();
  }
136
  std::vector<T>().swap(memory_data_);
137 138 139
  timeline.Pause();
  VLOG(3) << "DatasetImpl<T>::LocalShuffle() end, cost time="
          << timeline.ElapsedSec() << " seconds";
140 141
}

142 143 144
template <typename T>
void DatasetImpl<T>::GlobalShuffle() {
  VLOG(3) << "DatasetImpl<T>::GlobalShuffle() begin";
145 146
  platform::Timer timeline;
  timeline.Start();
147
  auto fleet_ptr = FleetWrapper::GetInstance();
148
  VLOG(3) << "RegisterClientToClientMsgHandler";
D
dongdaxiang 已提交
149 150 151 152
  fleet_ptr->RegisterClientToClientMsgHandler(
      0, [this](int msg_type, int client_id, const std::string& msg) -> int {
        return this->ReceiveFromClient(msg_type, client_id, msg);
      });
153 154 155 156 157
  if (readers_.size() == 0) {
    CreateReaders();
  }
  // if it is not InMemory, memory_data_ is empty
  std::random_shuffle(memory_data_.begin(), memory_data_.end());
X
xujiaqi01 已提交
158
  VLOG(3) << "start global shuffle threads";
159
  std::vector<std::thread> global_shuffle_threads;
160
  for (int i = 0; i < thread_num_; ++i) {
D
dongdaxiang 已提交
161 162
    global_shuffle_threads.push_back(std::thread(
        &paddle::framework::DataFeed::GlobalShuffle, readers_[i].get()));
163 164 165
  }
  for (std::thread& t : global_shuffle_threads) {
    t.join();
166
  }
167 168 169 170
  std::vector<T>().swap(memory_data_);
  timeline.Pause();
  VLOG(3) << "DatasetImpl<T>::GlobalShuffle() end, cost time="
          << timeline.ElapsedSec() << " seconds";
171 172
}

173 174
template <typename T>
void DatasetImpl<T>::CreateReaders() {
175
  VLOG(3) << "Calling CreateReaders()";
176
  CHECK(thread_num_ > 0) << "thread_num should > 0";
177 178 179 180 181 182 183 184 185 186 187 188 189
  int file_cnt = filelist_.size();
  int memory_data_size = memory_data_.size();
  if (memory_data_size != 0 && thread_num_ > memory_data_size) {
    VLOG(3) << "Dataset thread num = " << thread_num_
            << ", memory data size = " << memory_data_size
            << ". Changing Dataset thread num = " << memory_data_size;
    thread_num_ = memory_data_size;
  } else if (file_cnt != 0 && thread_num_ > file_cnt) {
    VLOG(3) << "Dataset thread num = " << thread_num_
            << ", file num = " << file_cnt
            << ". Changing Dataset thread num = " << file_cnt;
    thread_num_ = file_cnt;
  }
190 191
  VLOG(3) << "thread_num in Readers: " << thread_num_;
  VLOG(3) << "readers size: " << readers_.size();
192
  VLOG(3) << "Filelist size in readers: " << filelist_.size();
193 194 195
  if (readers_.size() != 0) {
    return;
  }
196
  VLOG(3) << "data feed class name: " << data_feed_desc_.name();
197
  for (int i = 0; i < thread_num_; ++i) {
198 199
    readers_.push_back(DataFeedFactory::CreateDataFeed(data_feed_desc_.name()));
    readers_.back()->Init(data_feed_desc_);
200 201 202 203 204
    readers_.back()->SetMemoryData(&memory_data_);
    readers_.back()->SetMemoryDataMutex(&mutex_for_update_memory_data_);
    readers_.back()->SetThreadId(i);
    readers_.back()->SetThreadNum(thread_num_);
    readers_.back()->SetTrainerNum(trainer_num_);
205 206 207
    readers_.back()->SetFileListMutex(&mutex_for_pick_file_);
    readers_.back()->SetFileListIndex(&file_idx_);
    readers_.back()->SetFileList(filelist_);
208 209 210
  }
}

211 212 213 214 215 216 217 218 219 220 221
template <typename T>
void DatasetImpl<T>::DestroyReaders() {
  VLOG(3) << "Calling DestroyReaders()";
  // clear memory_data_ before fill it
  // because if LoadIntoMemory but no Shuffle,
  // memory_data_ has empty data which has been std::move to channel
  if (memory_data_.size() != 0) {
    std::vector<T>().swap(memory_data_);
  }
  std::vector<std::thread> fill_threads;
  for (int i = 0; i < thread_num_; ++i) {
D
dongdaxiang 已提交
222 223 224
    fill_threads.push_back(
        std::thread(&paddle::framework::DataFeed::FillChannelToMemoryData,
                    readers_[i].get()));
225 226 227 228 229
  }
  for (std::thread& t : fill_threads) {
    t.join();
  }
  std::vector<std::shared_ptr<paddle::framework::DataFeed>>().swap(readers_);
230
  VLOG(3) << "readers size: " << readers_.size();
231 232 233 234
}

template <typename T>
int DatasetImpl<T>::ReceiveFromClient(int msg_type, int client_id,
D
dongdaxiang 已提交
235
                                      const std::string& msg) {
236 237 238 239 240 241
  VLOG(3) << "ReceiveFromClient msg_type=" << msg_type
          << ", client_id=" << client_id << ", msg length="
          << msg.length();
  auto fleet_ptr = FleetWrapper::GetInstance();
  int64_t index = fleet_ptr->LocalRandomEngine()() % thread_num_;
  VLOG(3) << "ramdom index=" << index;
242 243 244 245
  readers_[index]->PutInsToChannel(msg);
  return 0;
}

246 247 248
// explicit instantiation
template class DatasetImpl<std::vector<MultiSlotType>>;

D
dongdaxiang 已提交
249 250
}  // end namespace framework
}  // end namespace paddle