Skip to content

Commit c8bd521

Browse files
committed
add reader thread status
1 parent 71cbc8b commit c8bd521

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

paddle/fluid/operators/reader/ctr_reader.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ class MultiGzipReader : public Reader {
124124

125125
void ReadThread(const std::vector<std::string>& file_list,
126126
const std::vector<std::string>& slots, int batch_size,
127+
int thread_id, std::vector<ReaderThreadStatus>* thread_status,
127128
std::shared_ptr<LoDTensorBlockingQueue> queue) {
129+
(*thread_status)[thread_id] = Running;
130+
128131
std::string line;
129132

130133
std::vector<std::unordered_map<std::string, std::vector<int64_t>>> batch_data;
@@ -181,6 +184,8 @@ void ReadThread(const std::vector<std::string>& file_list,
181184

182185
queue->Push(lod_datas);
183186
}
187+
188+
(*thread_status)[thread_id] = Stopped;
184189
}
185190

186191
} // namespace reader

paddle/fluid/operators/reader/ctr_reader.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ namespace paddle {
3030
namespace operators {
3131
namespace reader {
3232

33+
enum ReaderThreadStatus { Running, Stopped };
34+
3335
void ReadThread(const std::vector<std::string>& file_list,
3436
const std::vector<std::string>& slots, int batch_size,
37+
int thread_id, std::vector<ReaderThreadStatus>* thread_status,
3538
std::shared_ptr<LoDTensorBlockingQueue> queue);
3639

3740
class CTRReader : public framework::FileReader {
@@ -40,13 +43,16 @@ class CTRReader : public framework::FileReader {
4043
int batch_size, int thread_num,
4144
const std::vector<std::string>& slots,
4245
const std::vector<std::string>& file_list)
43-
: thread_num_(thread_num),
44-
batch_size_(batch_size),
45-
slots_(slots),
46-
file_list_(file_list) {
46+
: batch_size_(batch_size), slots_(slots), file_list_(file_list) {
4747
PADDLE_ENFORCE(queue != nullptr, "LoDTensorBlockingQueue must not be null");
48+
PADDLE_ENFORCE_GT(file_list.size(), 0, "file list should not be empty");
49+
thread_num_ =
50+
file_list_.size() > thread_num_ ? thread_num_ : file_list_.size();
4851
queue_ = queue;
4952
SplitFiles();
53+
for (int i = 0; i < thread_num; ++i) {
54+
read_thread_status_.push_back(Stopped);
55+
}
5056
}
5157

5258
~CTRReader() { queue_->Close(); }
@@ -69,28 +75,29 @@ class CTRReader : public framework::FileReader {
6975
void Start() override {
7076
VLOG(3) << "Start reader";
7177
queue_->ReOpen();
72-
for (int i = 0; i < file_groups_.size(); i++) {
73-
read_threads_.emplace_back(new std::thread(std::bind(
74-
&ReadThread, file_groups_[i], slots_, batch_size_, queue_)));
78+
for (int thread_id = 0; thread_id < file_groups_.size(); thread_id++) {
79+
read_threads_.emplace_back(new std::thread(
80+
std::bind(&ReadThread, file_groups_[thread_id], slots_, batch_size_,
81+
thread_id, &read_thread_status_, queue_)));
7582
}
7683
}
7784

7885
private:
7986
void SplitFiles() {
80-
file_groups_.resize(file_list_.size() > thread_num_ ? thread_num_
81-
: file_list_.size());
87+
file_groups_.resize(thread_num_);
8288
for (int i = 0; i < file_list_.size(); ++i) {
8389
file_groups_[i % thread_num_].push_back(file_list_[i]);
8490
}
8591
}
8692

8793
private:
88-
const int thread_num_;
94+
int thread_num_;
8995
const int batch_size_;
9096
const std::vector<std::string> slots_;
9197
const std::vector<std::string> file_list_;
9298
std::shared_ptr<LoDTensorBlockingQueue> queue_;
9399
std::vector<std::unique_ptr<std::thread>> read_threads_;
100+
std::vector<ReaderThreadStatus> read_thread_status_;
94101
std::vector<std::vector<std::string>> file_groups_;
95102
};
96103

0 commit comments

Comments
 (0)