You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Paddle/paddle/fluid/operators/reader/ctr_reader.h

107 lines
3.4 KiB

6 years ago
// 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.
#pragma once
6 years ago
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
6 years ago
#include <vector>
6 years ago
6 years ago
#include "paddle/fluid/framework/reader.h"
6 years ago
#include "paddle/fluid/framework/threadpool.h"
6 years ago
#include "paddle/fluid/operators/reader/lod_tensor_blocking_queue.h"
namespace paddle {
namespace operators {
namespace reader {
enum ReaderThreadStatus { Running, Stopped };
6 years ago
void ReadThread(const std::vector<std::string>& file_list,
const std::vector<std::string>& slots, int batch_size,
int thread_id, std::vector<ReaderThreadStatus>* thread_status,
6 years ago
std::shared_ptr<LoDTensorBlockingQueue> queue);
6 years ago
class CTRReader : public framework::FileReader {
public:
6 years ago
explicit CTRReader(const std::shared_ptr<LoDTensorBlockingQueue>& queue,
int batch_size, int thread_num,
const std::vector<std::string>& slots,
const std::vector<std::string>& file_list)
: batch_size_(batch_size), slots_(slots), file_list_(file_list) {
6 years ago
PADDLE_ENFORCE(queue != nullptr, "LoDTensorBlockingQueue must not be null");
PADDLE_ENFORCE_GT(file_list.size(), 0, "file list should not be empty");
thread_num_ =
file_list_.size() > thread_num_ ? thread_num_ : file_list_.size();
6 years ago
queue_ = queue;
6 years ago
SplitFiles();
for (int i = 0; i < thread_num; ++i) {
read_thread_status_.push_back(Stopped);
}
6 years ago
}
6 years ago
~CTRReader() { queue_->Close(); }
6 years ago
void ReadNext(std::vector<framework::LoDTensor>* out) override {
bool success;
*out = queue_->Pop(&success);
if (!success) out->clear();
}
6 years ago
void Shutdown() override {
VLOG(3) << "Shutdown reader";
for (auto& read_thread : read_threads_) {
read_thread->join();
}
read_threads_.clear();
queue_->Close();
}
6 years ago
6 years ago
void Start() override {
6 years ago
VLOG(3) << "Start reader";
6 years ago
queue_->ReOpen();
for (int thread_id = 0; thread_id < file_groups_.size(); thread_id++) {
read_threads_.emplace_back(new std::thread(
std::bind(&ReadThread, file_groups_[thread_id], slots_, batch_size_,
thread_id, &read_thread_status_, queue_)));
6 years ago
}
6 years ago
}
private:
6 years ago
void SplitFiles() {
file_groups_.resize(thread_num_);
6 years ago
for (int i = 0; i < file_list_.size(); ++i) {
file_groups_[i % thread_num_].push_back(file_list_[i]);
}
}
6 years ago
private:
int thread_num_;
6 years ago
const int batch_size_;
const std::vector<std::string> slots_;
const std::vector<std::string> file_list_;
6 years ago
std::shared_ptr<LoDTensorBlockingQueue> queue_;
6 years ago
std::vector<std::unique_ptr<std::thread>> read_threads_;
std::vector<ReaderThreadStatus> read_thread_status_;
6 years ago
std::vector<std::vector<std::string>> file_groups_;
6 years ago
};
} // namespace reader
} // namespace operators
} // namespace paddle