data_set.cc 9.9 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
#include "paddle/fluid/framework/io/fs.h"
22
#include "paddle/fluid/platform/timer.h"
23

D
dongdaxiang 已提交
24 25 26 27 28
#if defined _WIN32 || defined __APPLE__
#else
#define _LINUX
#endif

29 30 31
namespace paddle {
namespace framework {

X
xjqbest 已提交
32
// constructor
33
template <typename T>
D
dongdaxiang 已提交
34 35
DatasetImpl<T>::DatasetImpl() {
  thread_num_ = 1;
36 37
  trainer_num_ = 1;
  file_idx_ = 0;
D
dongdaxiang 已提交
38
}
39

X
xjqbest 已提交
40
// set filelist, file_idx_ will reset to zero.
41 42
template <typename T>
void DatasetImpl<T>::SetFileList(const std::vector<std::string>& filelist) {
43
  VLOG(3) << "filelist size: " << filelist.size();
44
  filelist_ = filelist;
45
  file_idx_ = 0;
46 47
}

X
xjqbest 已提交
48
// set expect thread num. actually it may change
49 50
template <typename T>
void DatasetImpl<T>::SetThreadNum(int thread_num) {
51
  VLOG(3) << "SetThreadNum thread_num=" << thread_num;
52 53 54
  thread_num_ = thread_num;
}

X
xjqbest 已提交
55 56 57
// if you run distributed, and want to do global shuffle,
// set this before global shuffle.
// be sure you call CreateReaders before SetTrainerNum
58
template <typename T>
X
xujiaqi01 已提交
59 60
void DatasetImpl<T>::SetTrainerNum(int trainer_num) {
  trainer_num_ = trainer_num;
61 62 63 64 65 66
  // should inform reader of trainer_num directly
  for (auto reader : readers_) {
    reader->SetTrainerNum(trainer_num);
  }
}

X
xjqbest 已提交
67 68 69 70 71 72 73 74 75 76 77
// if you run distributed, and want to do global shuffle,
// set this before global shuffle.
// be sure you call CreateReaders before SetFleetSendBatchSize
template <typename T>
void DatasetImpl<T>::SetFleetSendBatchSize(int64_t size) {
  fleet_send_batch_size_ = size;
  for (auto reader : readers_) {
    reader->SetFleetSendBatchSize(size);
  }
}

78 79 80
template <typename T>
void DatasetImpl<T>::SetHdfsConfig(const std::string& fs_name,
                                   const std::string& fs_ugi) {
X
xjqbest 已提交
81 82
  fs_name_ = fs_name;
  fs_ugi_ = fs_ugi;
83 84 85 86
  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 已提交
87
}
88

89 90
template <typename T>
void DatasetImpl<T>::SetDataFeedDesc(const std::string& data_feed_desc_str) {
91 92
  google::protobuf::TextFormat::ParseFromString(data_feed_desc_str,
                                                &data_feed_desc_);
93 94
}

X
xjqbest 已提交
95 96
// readers_.size() may not be equal to thread_num_,
// it changes when filelist_.size() < thread_num_
97 98
template <typename T>
std::vector<std::shared_ptr<paddle::framework::DataFeed>>&
D
dongdaxiang 已提交
99
DatasetImpl<T>::GetReaders() {
100 101 102
  return readers_;
}

103 104 105 106 107 108 109 110 111 112 113 114
// if sent message between workers, should first call this function
template <typename T>
void DatasetImpl<T>::RegisterClientToClientMsgHandler() {
  auto fleet_ptr = FleetWrapper::GetInstance();
  VLOG(3) << "RegisterClientToClientMsgHandler";
  fleet_ptr->RegisterClientToClientMsgHandler(
      0, [this](int msg_type, int client_id, const std::string& msg) -> int {
        return this->ReceiveFromClient(msg_type, client_id, msg);
      });
  VLOG(3) << "RegisterClientToClientMsgHandler done";
}

X
xjqbest 已提交
115 116
// load data into memory, Dataset hold this memory,
// which will later be fed into readers' channel
117 118 119
template <typename T>
void DatasetImpl<T>::LoadIntoMemory() {
  VLOG(3) << "DatasetImpl<T>::LoadIntoMemory() begin";
120 121
  platform::Timer timeline;
  timeline.Start();
122 123 124 125 126
  if (readers_.size() == 0) {
    CreateReaders();
  }
  std::vector<std::thread> load_threads;
  for (int64_t i = 0; i < thread_num_; ++i) {
D
dongdaxiang 已提交
127 128
    load_threads.push_back(std::thread(
        &paddle::framework::DataFeed::LoadIntoMemory, readers_[i].get()));
129 130 131 132
  }
  for (std::thread& t : load_threads) {
    t.join();
  }
133 134 135 136
  timeline.Pause();
  VLOG(3) << "DatasetImpl<T>::LoadIntoMemory() end"
          << ", memory data size=" << memory_data_.size()
          << ", cost time=" << timeline.ElapsedSec() << " seconds";
137 138
}

139 140 141 142 143
// release memory data
template <typename T>
void DatasetImpl<T>::ReleaseMemory() {
  VLOG(3) << "DatasetImpl<T>::ReleaseMemory() begin";
  std::vector<T>().swap(memory_data_);
144 145 146
  for (int i = 0; i < readers_.size(); ++i) {
    readers_[i]->ReleaseChannelData();
  }
147 148 149
  VLOG(3) << "DatasetImpl<T>::ReleaseMemory() end";
}

X
xjqbest 已提交
150
// do local shuffle
151 152 153
template <typename T>
void DatasetImpl<T>::LocalShuffle() {
  VLOG(3) << "DatasetImpl<T>::LocalShuffle() begin";
154 155
  platform::Timer timeline;
  timeline.Start();
156 157 158
  if (readers_.size() == 0) {
    CreateReaders();
  }
159 160 161
  // if it is not InMemory, memory_data_ is empty
  std::random_shuffle(memory_data_.begin(), memory_data_.end());

162 163
  std::vector<std::thread> local_shuffle_threads;
  for (int64_t i = 0; i < thread_num_; ++i) {
D
dongdaxiang 已提交
164 165
    local_shuffle_threads.push_back(std::thread(
        &paddle::framework::DataFeed::LocalShuffle, readers_[i].get()));
166 167 168 169
  }
  for (std::thread& t : local_shuffle_threads) {
    t.join();
  }
170
  std::vector<T>().swap(memory_data_);
171 172 173
  timeline.Pause();
  VLOG(3) << "DatasetImpl<T>::LocalShuffle() end, cost time="
          << timeline.ElapsedSec() << " seconds";
174 175
}

176 177 178
template <typename T>
void DatasetImpl<T>::GlobalShuffle() {
  VLOG(3) << "DatasetImpl<T>::GlobalShuffle() begin";
179 180 181 182 183
  platform::Timer timeline;
  timeline.Start();
  if (readers_.size() == 0) {
    CreateReaders();
  }
184 185 186 187
  auto fleet_ptr = FleetWrapper::GetInstance();
  // local shuffle all data before global shuffle
  std::shuffle(memory_data_.begin(), memory_data_.end(),
               fleet_ptr->LocalRandomEngine());
X
xujiaqi01 已提交
188
  VLOG(3) << "start global shuffle threads";
189
  std::vector<std::thread> global_shuffle_threads;
190
  for (int i = 0; i < thread_num_; ++i) {
D
dongdaxiang 已提交
191 192
    global_shuffle_threads.push_back(std::thread(
        &paddle::framework::DataFeed::GlobalShuffle, readers_[i].get()));
193 194 195
  }
  for (std::thread& t : global_shuffle_threads) {
    t.join();
196
  }
197 198 199 200
  std::vector<T>().swap(memory_data_);
  timeline.Pause();
  VLOG(3) << "DatasetImpl<T>::GlobalShuffle() end, cost time="
          << timeline.ElapsedSec() << " seconds";
201 202
}

203 204
template <typename T>
void DatasetImpl<T>::CreateReaders() {
205
  VLOG(3) << "Calling CreateReaders()";
206
  CHECK(thread_num_ > 0) << "thread_num should > 0";
207 208 209 210 211 212 213 214 215 216 217 218 219
  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;
  }
220 221
  VLOG(3) << "thread_num in Readers: " << thread_num_;
  VLOG(3) << "readers size: " << readers_.size();
222
  VLOG(3) << "Filelist size in readers: " << filelist_.size();
223 224 225
  if (readers_.size() != 0) {
    return;
  }
226
  VLOG(3) << "data feed class name: " << data_feed_desc_.name();
227
  for (int i = 0; i < thread_num_; ++i) {
228 229
    readers_.push_back(DataFeedFactory::CreateDataFeed(data_feed_desc_.name()));
    readers_.back()->Init(data_feed_desc_);
230 231 232 233 234
    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_);
235 236 237
    readers_.back()->SetFileListMutex(&mutex_for_pick_file_);
    readers_.back()->SetFileListIndex(&file_idx_);
    readers_.back()->SetFileList(filelist_);
238 239 240
  }
}

241 242 243 244 245 246 247 248 249 250 251
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 已提交
252 253 254
    fill_threads.push_back(
        std::thread(&paddle::framework::DataFeed::FillChannelToMemoryData,
                    readers_[i].get()));
255 256 257 258 259
  }
  for (std::thread& t : fill_threads) {
    t.join();
  }
  std::vector<std::shared_ptr<paddle::framework::DataFeed>>().swap(readers_);
260
  VLOG(3) << "readers size: " << readers_.size();
X
fix bug  
xjqbest 已提交
261
  // if memory_data_ is empty, which means it's not InMemory mode,
262
  // so the next epoch should read all data again
X
fix bug  
xjqbest 已提交
263
  if (memory_data_.size() == 0) {
264 265
    file_idx_ = 0;
  }
266 267
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281
template <typename T>
int64_t DatasetImpl<T>::GetMemoryDataSize() {
  return memory_data_.size();
}

template <typename T>
int64_t DatasetImpl<T>::GetShuffleDataSize() {
  int64_t sum = 0;
  for (int i = 0; i < readers_.size(); ++i) {
    sum += readers_[i]->GetChannelDataSize();
  }
  return sum;
}

282 283
template <typename T>
int DatasetImpl<T>::ReceiveFromClient(int msg_type, int client_id,
D
dongdaxiang 已提交
284
                                      const std::string& msg) {
D
dongdaxiang 已提交
285
#ifdef _LINUX
286
  VLOG(3) << "ReceiveFromClient msg_type=" << msg_type
287
          << ", client_id=" << client_id << ", msg length=" << msg.length();
288
  auto fleet_ptr = FleetWrapper::GetInstance();
289
  int64_t index = fleet_ptr->LocalRandomEngine()() % thread_num_;
290
  VLOG(3) << "ramdom index=" << index;
291
  readers_[index]->PutInsToChannel(msg);
D
dongdaxiang 已提交
292
#endif
293 294 295
  return 0;
}

296 297 298
// explicit instantiation
template class DatasetImpl<std::vector<MultiSlotType>>;

D
dongdaxiang 已提交
299 300
}  // end namespace framework
}  // end namespace paddle