DataProviderGroup.h 4.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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. */

#pragma once

#include "DataProvider.h"

namespace paddle {

template <class T>
class DataProviderGroup : public DataProvider {
W
Wu Yi 已提交
23
 protected:
Z
zhangjinchao01 已提交
24 25 26 27 28 29 30 31
  typedef T ProviderType;
  typedef std::shared_ptr<ProviderType> ProviderPtrType;
  ProviderPtrType provider_;

  std::vector<std::string> fileList_;
  std::mutex lock_;
  std::unique_ptr<MultiThreadWorker<ProviderType>> loader_;

W
Wu Yi 已提交
32
 public:
Z
zhangjinchao01 已提交
33 34 35 36 37 38 39 40
  DataProviderGroup(const DataConfig& config, bool useGpu);
  ~DataProviderGroup() {}

  virtual void reset();
  virtual void shuffle() {}
  virtual int64_t getSize() { return -1; }
  virtual int64_t getNextBatchInternal(int64_t size, DataBatch* batch);

W
Wu Yi 已提交
41
 private:
Z
zhangjinchao01 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  void startLoader();
  void stopLoader();
  void forceStopLoader();
  ProviderPtrType loadFile(const std::vector<std::string>& fileList);
};

template <class T>
DataProviderGroup<T>::DataProviderGroup(const DataConfig& config, bool useGpu)
    : DataProvider(config, useGpu) {
  // load file list
  loadFileList(config_.files(), fileList_);
  CHECK_GT(fileList_.size(), 0LU);
  LOG(INFO) << "load file list, numfiles=" << fileList_.size()
            << ", max_num_of_data_providers_in_memory="
            << (1 + config_.file_group_conf().queue_capacity() +
                config_.file_group_conf().load_thread_num());
}

template <class T>
void DataProviderGroup<T>::reset() {
  forceStopLoader();
  CHECK(!loader_);
  provider_ = nullptr;

  // shuffle file list
67 68
  std::shuffle(
      fileList_.begin(), fileList_.end(), ThreadLocalRandomEngine::get());
Z
zhangjinchao01 已提交
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 107 108 109 110 111 112 113 114

  startLoader();
  DataProvider::reset();
}

template <class T>
int64_t DataProviderGroup<T>::getNextBatchInternal(int64_t size,
                                                   DataBatch* batch) {
  std::lock_guard<std::mutex> guard(lock_);

  if (!loader_) {
    return 0;
  }
  if (provider_) {
    int64_t ret = provider_->getNextBatchInternal(size, batch);
    if (ret > 0) {
      return ret;
    }
  }

  // else get data from next data provider
  if (loader_->testResult()) {
    LOG(INFO) << "WAIT provider";
  }
  provider_ = loader_->waitResult();
  if (!provider_) {
    stopLoader();  // All the data providers have been returned
    return 0;
  }
  int64_t ret = provider_->getNextBatchInternal(size, batch);
  CHECK(ret > 0) << "new data provider does not contain any valid samples!";
  return ret;
}

template <class T>
void DataProviderGroup<T>::startLoader() {
  loader_.reset(new MultiThreadWorker<ProviderType>(
      config_.file_group_conf().load_thread_num(),
      config_.file_group_conf().queue_capacity()));

  int loadFileCount = config_.file_group_conf().load_file_count();
  for (size_t startPos = 0; startPos < fileList_.size();
       startPos += loadFileCount) {
    size_t endPos = std::min(fileList_.size(), startPos + loadFileCount);
    std::vector<std::string> fileVec(fileList_.begin() + startPos,
                                     fileList_.begin() + endPos);
115 116 117
    loader_->addJob([this, fileVec]() -> ProviderPtrType {
      return this->loadFile(fileVec);
    });
Z
zhangjinchao01 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  }
  loader_->stopAddJob();
}

template <class T>
void DataProviderGroup<T>::stopLoader() {
  if (loader_) {
    loader_->stop();
    loader_ = nullptr;
  }
}

template <class T>
void DataProviderGroup<T>::forceStopLoader() {
  if (loader_) {
    loader_->forceStop();
    loader_ = nullptr;
  }
}

template <class T>
std::shared_ptr<T> DataProviderGroup<T>::loadFile(
    const std::vector<std::string>& fileList) {
  // disable async_load_data in sub dataprovider
  DataConfig subConfig = config_;
  subConfig.set_async_load_data(false);

  CHECK(!fileList.empty()) << "fileList is empty";
  ProviderPtrType provider =
      std::make_shared<ProviderType>(subConfig, useGpu_, false);
  provider->loadData(fileList);
  provider->reset();
  return provider;
}

}  // namespace paddle