Skip to content

Commit 92cbaa4

Browse files
committed
add GetTimeInSec
1 parent dd2dfeb commit 92cbaa4

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

cmake/external/gzstream.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ SET_PROPERTY(TARGET gzstream PROPERTY IMPORTED_LOCATION
4444
"${GZSTREAM_INSTALL_DIR}/lib/libgzstream.a")
4545

4646
include_directories(${GZSTREAM_INCLUDE_DIR})
47-
ADD_DEPENDENCIES(gzstream extern_gzstream)
47+
ADD_DEPENDENCIES(gzstream extern_gzstream zlib)

paddle/fluid/operators/reader/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function(reader_library TARGET_NAME)
1616
endfunction()
1717

1818
cc_library(buffered_reader SRCS buffered_reader.cc DEPS reader simple_threadpool)
19-
cc_library(ctr_reader SRCS ctr_reader.cc DEPS reader simple_threadpool boost gzstream)
19+
cc_library(ctr_reader SRCS ctr_reader.cc DEPS gzstream reader zlib)
2020
cc_test(ctr_reader_test SRCS ctr_reader_test.cc DEPS ctr_reader)
2121
reader_library(open_files_op SRCS open_files_op.cc DEPS buffered_reader)
2222
reader_library(create_ctr_reader_op SRCS create_ctr_reader_op.cc DEPS ctr_reader)

paddle/fluid/operators/reader/ctr_reader.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ static inline void parse_line(
5858
const std::string& item = ret[i];
5959
std::vector<std::string> feasign_and_slot;
6060
string_split(item, ':', &feasign_and_slot);
61-
auto& slot = feasign_and_slot[1];
6261
if (feasign_and_slot.size() == 2 &&
63-
slot_to_index.find(slot) != slot_to_index.end()) {
64-
const std::string& slot = feasign_and_slot[1];
62+
slot_to_index.find(feasign_and_slot[1]) != slot_to_index.end()) {
6563
int64_t feasign = std::strtoll(feasign_and_slot[0].c_str(), NULL, 10);
6664
(*slot_to_data)[feasign_and_slot[1]].push_back(feasign);
6765
}
@@ -164,7 +162,7 @@ void ReadThread(const std::vector<std::string>& file_list,
164162

165163
VLOG(3) << "reader inited";
166164

167-
clock_t t0 = clock();
165+
uint64_t t0 = GetTimeInSec();
168166

169167
int i = 0;
170168

@@ -219,13 +217,12 @@ void ReadThread(const std::vector<std::string>& file_list,
219217
memcpy(label_tensor_data, batch_label.data(), batch_label.size());
220218
lod_datas.push_back(label_tensor);
221219

222-
// queue->Push(lod_datas);
220+
queue->Push(lod_datas);
223221
VLOG(4) << "push one data, queue_size=" << queue->Size();
224222

225223
if (i != 0 && i % 100 == 0) {
226-
clock_t t1 = clock();
227-
float line_per_s = 100 * batch_size * static_cast<int64>(CLOCKS_PER_SEC) /
228-
static_cast<int>(t1 - t0);
224+
uint64_t t1 = GetTimeInSec();
225+
float line_per_s = 100 * batch_size / static_cast<int>(t1 - t0);
229226
VLOG(3) << "[" << thread_id << "]"
230227
<< " line_per_second = " << line_per_s;
231228
t0 = t1;

paddle/fluid/operators/reader/ctr_reader.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#pragma once
1616

17+
#include <sys/time.h>
18+
1719
#include <cstdlib>
1820
#include <fstream>
1921
#include <iostream>
@@ -37,6 +39,15 @@ void ReadThread(const std::vector<std::string>& file_list,
3739
int thread_id, std::vector<ReaderThreadStatus>* thread_status,
3840
std::shared_ptr<LoDTensorBlockingQueue> queue);
3941

42+
inline uint64_t GetTimeInSec() {
43+
using clock = std::conditional<std::chrono::high_resolution_clock::is_steady,
44+
std::chrono::high_resolution_clock,
45+
std::chrono::steady_clock>::type;
46+
return std::chrono::duration_cast<std::chrono::seconds>(
47+
clock::now().time_since_epoch())
48+
.count();
49+
}
50+
4051
class CTRReader : public framework::FileReader {
4152
public:
4253
explicit CTRReader(const std::shared_ptr<LoDTensorBlockingQueue>& queue,
@@ -88,7 +99,7 @@ class CTRReader : public framework::FileReader {
8899
private:
89100
void SplitFiles() {
90101
file_groups_.resize(thread_num_);
91-
for (int i = 0; i < file_list_.size(); ++i) {
102+
for (size_t i = 0; i < file_list_.size(); ++i) {
92103
auto& file_name = file_list_[i];
93104
std::ifstream f(file_name.c_str());
94105
PADDLE_ENFORCE(f.good(), "file %s not exist!", file_name);

paddle/fluid/operators/reader/ctr_reader_test.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ using paddle::operators::reader::LoDTensorBlockingQueue;
2525
using paddle::operators::reader::LoDTensorBlockingQueueHolder;
2626
using paddle::operators::reader::CTRReader;
2727
using paddle::framework::LoDTensor;
28+
using paddle::operators::reader::GetTimeInSec;
2829

2930
TEST(CTR_READER, read_data) {
3031
LoDTensorBlockingQueueHolder queue_holder;
3132
int capacity = 64;
32-
queue_holder.InitOnce(capacity, {}, true);
33+
queue_holder.InitOnce(capacity, {}, false);
3334

3435
std::shared_ptr<LoDTensorBlockingQueue> queue = queue_holder.GetQueue();
3536

3637
int batch_size = 10;
37-
int thread_num = 2;
38+
int thread_num = 4;
3839
std::vector<std::string> slots = {
3940
"6002", "6003", "6004", "6005", "6006", "6007", "6008", "6009", "6010",
4041
"6011", "6012", "6013", "6014", "6015", "6016", "6017", "6018", "6019",
@@ -109,7 +110,8 @@ TEST(CTR_READER, read_data) {
109110
std::vector<std::string> file_list = {
110111
"/Users/qiaolongfei/project/gzip_test/part-00000-A.gz",
111112
"/Users/qiaolongfei/project/gzip_test/part-00001-A.gz",
112-
"/Users/qiaolongfei/project/gzip_test/part-00002-A.gz"};
113+
"/Users/qiaolongfei/project/gzip_test/part-00002-A.gz",
114+
"/Users/qiaolongfei/project/gzip_test/part-00003-A.gz"};
113115

114116
CTRReader reader(queue, batch_size, thread_num, slots, file_list);
115117

@@ -118,13 +120,11 @@ TEST(CTR_READER, read_data) {
118120
std::cout << "start to reader data" << std::endl;
119121
std::vector<LoDTensor> out;
120122
int read_batch = 1000;
121-
clock_t t0 = clock();
123+
uint64_t t0 = GetTimeInSec();
122124
for (int i = 0; i < read_batch; ++i) {
123125
reader.ReadNext(&out);
124126
}
125-
clock_t t1 = clock();
126-
float line_per_s = read_batch * batch_size *
127-
static_cast<int64>(CLOCKS_PER_SEC) /
128-
static_cast<int>(t1 - t0);
127+
uint64_t t1 = GetTimeInSec();
128+
float line_per_s = read_batch * batch_size / static_cast<int>(t1 - t0);
129129
VLOG(3) << "line_per_second = " << line_per_s;
130130
}

0 commit comments

Comments
 (0)