forked from sophon-ai-algo/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_queue.h
350 lines (311 loc) · 9.81 KB
/
thread_queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
67
68
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
115
116
117
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
// Created by yuan on 2/22/21.
//
#ifndef INFERENCE_FRAMEWORK_THREAD_QUEUE_H
#define INFERENCE_FRAMEWORK_THREAD_QUEUE_H
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <thread>
#ifdef __linux__
#include <sys/time.h>
#endif
#include <pthread.h>
#include "bmutility_timer.h"
static int cpu_index = 0;
template <typename T>
class BlockingQueue {
private:
size_t size_impl() const
{
return m_type == 0 ? m_queue.size() : m_vec.size();
}
void wait_and_push_one(T &&data) {
if (m_limit > 0 && this->size_impl() >= m_limit && !m_stop) {
std::cout << "WARNING: " << m_name << " queue_size(" << this->size_impl() << ") > "
<< m_limit << std::endl;
// flow control by dropping
if (m_drop_fn != nullptr) {
this->drop_half_();
std::cout << m_name << " queue_size after dropping, size: " << this->size_impl() << std::endl;;
} else {
// blocking
do
{
pthread_cond_wait(&m_pop_cond, &m_qmtx);
} while (m_limit > 0 && this->size_impl() >= m_limit && !m_stop);
}
} else if (this->size_impl() >= m_warning && !m_stop && this->size_impl() % 100 == 0) {
//std::cout << "WARNING: " << m_name << " queue_size is " << this->size_impl() << std::endl;
}
if (m_type == 0)
{
m_queue.push(std::move(data));
} else {
m_vec.push_back(std::move(data));
}
}
public:
BlockingQueue(const std::string& name="" ,int type=0, int limit = 0, int warning = 32)
: m_stop(false), m_limit(limit), m_drop_fn(nullptr), m_warning(warning) {
m_name = name;
m_type = type;
pthread_mutex_init(&m_qmtx, NULL);
pthread_cond_init(&m_condv, NULL);
pthread_cond_init(&m_pop_cond, NULL);
}
~BlockingQueue() {
pthread_mutex_lock(&m_qmtx);
std::cout<<"destroy "<<m_name<<",size:"<<m_queue.size()+m_vec.size()<<std::endl;
m_vec.clear();
std::queue<T> empty;
m_queue.swap(empty);
pthread_mutex_unlock(&m_qmtx);
}
void stop() {
pthread_mutex_lock(&m_qmtx);
m_stop = true;
std::cout<< "stop blocking queue:" << m_name << std::endl;
pthread_cond_broadcast(&m_pop_cond);
pthread_cond_broadcast(&m_condv);
pthread_mutex_unlock(&m_qmtx);
}
int push(T &data) {
pthread_mutex_lock(&m_qmtx);
this->wait_and_push_one(std::move(data));
int num = this->size_impl();
pthread_mutex_unlock(&m_qmtx);
pthread_cond_signal(&m_condv);
return num;
}
int push(std::vector<T> &datas) {
int num;
pthread_mutex_lock(&m_qmtx);
for (auto &data : datas)
{
this->wait_and_push_one(std::move(data));
if (m_stop) goto err;
}
num = this->size_impl();
pthread_cond_signal(&m_condv);
pthread_mutex_unlock(&m_qmtx);
return num;
err:
pthread_mutex_unlock(&m_qmtx);
return 0;
}
int pop_front(std::vector<T>& objs, int min_num, int max_num, long wait_ms = 0, bool* is_timeout=nullptr) {
bool timeout = false;
struct timespec to;
struct timeval now;
gettimeofday(&now, NULL);
double ms0 = now.tv_sec*1000+now.tv_usec/1000.0;
//std::cout<<m_name<<",pop:"<<now.tv_usec/1000.0;
if (wait_ms == 0) {
to.tv_sec = now.tv_sec + 9999999;
to.tv_nsec = now.tv_usec * 1000UL;
} else {
int nsec = now.tv_usec * 1000 + (wait_ms % 1000) * 1000000;
to.tv_sec = now.tv_sec + nsec / 1000000000 + wait_ms / 1000;
to.tv_nsec = nsec%1000000000;//(now.tv_usec + wait_ms * 1000UL) * 1000UL;
}
pthread_mutex_lock(&m_qmtx);
while ((m_type?m_vec.size() < min_num:m_queue.size() < min_num) && !m_stop) {
#ifdef BLOCKING_QUEUE_PERF
m_timer.tic();
#endif
// pthread_timestruc_t to;
int err = pthread_cond_timedwait(&m_condv, &m_qmtx, &to);
if (err == ETIMEDOUT) {
timeout = true;
break;
}
#ifdef BLOCKING_QUEUE_PERF
m_timer.toc();
if (m_timer.total_time_ > 1) {
m_timer.summary();
}
#endif
}
if (!timeout) {
if(m_type == 0){
int oc = 0;
while(oc < max_num && m_queue.size() > 0) {
auto o = std::move(m_queue.front());
m_queue.pop();
objs.push_back(o);
oc++;
}
}else{
int oc = 0;
while(oc < max_num && m_vec.size() > 0) {
auto o = std::move(m_vec[0]);
m_vec.erase(m_vec.begin());
objs.push_back(o);
oc++;
}
}
}
pthread_cond_broadcast(&m_pop_cond);
pthread_mutex_unlock(&m_qmtx);
if (m_stop) {
return 0;
}
if (is_timeout) {
*is_timeout = true;
return -1;
}
return 0;
}
size_t size() {
size_t queue_size;
pthread_mutex_lock(&m_qmtx);
queue_size = this->size_impl();
pthread_mutex_unlock(&m_qmtx);
return queue_size;
}
int set_drop_fn(std::function<void(T& obj)> fn) {
m_drop_fn = fn;
return m_limit;
}
void drop_half_() {
if (m_type == 0) {
std::queue<T> temp;
size_t num = m_queue.size();
for(size_t i = 0; i < num; i++) {
auto elem = m_queue.front();
if (i % 2 == 0) {
temp.push(elem);
}
else{
m_drop_fn(elem);
}
m_queue.pop();
}
m_queue.swap(temp);
}
else {
std::vector<T> temp;
size_t num = m_vec.size();
for (size_t i = 0; i < num; i++) {
auto elem = m_vec[i];
if (i % 2 == 0) {
temp.push_back(elem);
}
else{
m_drop_fn(elem);
}
}
m_vec.swap(temp);
}
}
void drop(int num=0){
int queue_size;
pthread_mutex_lock(&m_qmtx);
if (num == 0) {
num = this->size_impl();
}
if (this->size_impl() < num)
return;
if(m_type == 0){
queue_size = m_queue.size();
if(num>queue_size)
num=queue_size;
for(int i = 0; i<num;i++){
m_queue.pop();
}
}
else{
queue_size = m_vec.size();
if(num>queue_size)
num=queue_size;
m_vec.erase(m_vec.begin(),m_vec.begin()+num);
}
pthread_cond_broadcast(&m_pop_cond);
pthread_mutex_unlock(&m_qmtx);
}
const std::string &name() { return m_name; }
private:
bool m_stop;
// Timer m_timer;
std::string m_name;
std::vector<T> m_vec;
std::queue<T> m_queue;
pthread_mutex_t m_qmtx;
pthread_cond_t m_condv, m_pop_cond;
int m_type, m_limit; //0:queue,1:vector
int m_warning;
std::function<void(T& obj)> m_drop_fn;
};
template <typename T>
class WorkerPool {
BlockingQueue<T> *m_work_que;
int m_thread_num;
using OnWorkItemsCallback = std::function<void(std::vector<T> &item)>;
OnWorkItemsCallback m_work_item_func;
std::vector<std::thread *> m_threads;
int m_max_pop_num;
int m_min_pop_num;
public:
WorkerPool():m_work_que(nullptr),m_thread_num(0),m_work_item_func(nullptr),m_max_pop_num(1),
m_min_pop_num(1) {}
virtual ~WorkerPool() {}
int init(BlockingQueue<T> *que, int thread_num, int min_pop_num, int max_pop_num) {
m_work_que = que;
m_thread_num = thread_num;
m_min_pop_num = min_pop_num;
m_max_pop_num = max_pop_num;
return 0;
}
int startWork(OnWorkItemsCallback fn) {
m_work_item_func = fn;
for (int i = 0; i < m_thread_num; ++i) {
auto pth = new std::thread([this] {
while (true) {
std::vector<T> items;
//if (m_work_que->size() < 4) { bm::usleep(10); continue; }
if (m_work_que->pop_front(items, m_min_pop_num, m_max_pop_num) != 0) {
break;
}
if (items.empty())
break;
m_work_item_func(items);
}
});
//setCPU(*pth);
m_threads.push_back(pth);
}
return 0;
}
void setCPU(std::thread &th) {
static int cpu_count = std::thread::hardware_concurrency();
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu_index++ % cpu_count, &cpuset);
int ret = pthread_setaffinity_np(th.native_handle(),
sizeof(cpu_set_t),
&cpuset);
if (ret != 0) {
std::cerr << "[ERROR] caling pthread_setaffinity_np failed" << std::endl;
exit(-1);
} else {
std::cout<< "[SUCCESS] caling pthread_setaffinity_np success, " << cpu_index << std::endl;
}
}
int stopWork() {
m_work_que->stop();
for (int i = 0; i < m_thread_num; i++) {
m_threads[i]->join();
delete m_threads[i];
m_threads[i] = nullptr;
}
return 0;
}
int flush() {
m_work_que->stop();
return 0;
}
};
#endif //INFERENCE_FRAMEWORK_THREAD_QUEUE_H