forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_pipeline.cpp
More file actions
411 lines (349 loc) · 11 KB
/
batch_pipeline.cpp
File metadata and controls
411 lines (349 loc) · 11 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// A simple example to capture the following task dependencies.
//
// TaskA---->TaskB---->TaskD
// TaskA---->TaskC---->TaskD
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <chrono>
#include <taskflow/taskflow.hpp> // the only include you need
// https://software.intel.com/en-us/forums/intel-moderncode-for-parallel-architectures/topic/297118
#define compiler_fence() __asm__ __volatile__ ("" : : : "memory")
// load with 'consume' (data-dependent) memory ordering
template<typename T>
T load_consume(T const* addr) {
// hardware fence is implicit on x86
T v = *const_cast<T const volatile*>(addr);
compiler_fence();
//__memory_barrier(); // compiler fence
return v;
}
// store with 'release' memory ordering
template<typename T>
void store_release(T* addr, T v) {
// hardware fence is implicit on x86
//__memory_barrier(); // compiler fence
compiler_fence();
*const_cast<T volatile*>(addr) = v;
}
// cache line size on modern x86 processors (in bytes)
size_t const cache_line_size = 64;
// single-producer/single-consumer queue
template<typename T>
class spsc_queue {
public:
spsc_queue() {
node* n = new node;
n->next_ = 0;
tail_ = head_ = first_= tail_copy_ = n;
}
~spsc_queue() {
node* n = first_;
do
{
node* next = n->next_;
delete n;
n = next;
} while (n);
}
void enqueue(T v) {
node* n = alloc_node();
n->next_ = 0;
n->value_ = v;
store_release(&head_->next_, n);
head_ = n;
}
// returns 'false' if queue is empty
bool dequeue(T& v) {
if (load_consume(&tail_->next_)) {
v = tail_->next_->value_;
store_release(&tail_, tail_->next_);
return true;
}
else {
return false;
}
}
private:
// internal node structure
struct node {
node* next_;
T value_;
};
// consumer part
// accessed mainly by consumer, infrequently be producer
node* tail_; // tail of the queue
// delimiter between consumer part and producer part,
// so that they situated on different cache lines
char cache_line_pad_ [cache_line_size];
// producer part
// accessed only by producer
node* head_; // head of the queue
node* first_; // last unused node (tail of node cache)
node* tail_copy_; // helper (points somewhere between first_ and tail_)
node* alloc_node() {
// first tries to allocate node from internal node cache,
// if attempt fails, allocates node via ::operator new()
if (first_ != tail_copy_) {
node* n = first_;
first_ = first_->next_;
return n;
}
tail_copy_ = load_consume(&tail_);
if (first_ != tail_copy_) {
node* n = first_;
first_ = first_->next_;
return n;
}
node* n = new node;
return n;
}
spsc_queue(spsc_queue const&);
spsc_queue& operator = (spsc_queue const&);
};
class TextSlice {
//! Pointer to one past last character in sequence
char* logical_end;
//! Pointer to one past last available byte in sequence.
char* physical_end;
public:
//! Allocate a TextSlice object that can hold up to max_size characters.
static TextSlice* allocate( size_t max_size ) {
// +1 leaves room for a terminating null character.
TextSlice* t = static_cast<TextSlice*>(std::malloc(sizeof(TextSlice)+max_size+1));
t->logical_end = t->begin();
t->physical_end = t->begin()+max_size;
return t;
}
//! Free a TextSlice object
void free() {
std::free((char*)this);
//tbb::tbb_allocator<char>().deallocate((char*)this,sizeof(TextSlice)+(physical_end-begin())+1);
}
//! Pointer to beginning of sequence
char* begin() {return (char*)(this+1);}
//! Pointer to one past last character in sequence
char* end() {return logical_end;}
void end(char c) { *logical_end = c; }
//! Length of sequence
size_t size() const {return logical_end-(char*)(this+1);}
//! Maximum number of characters that can be appended to sequence
size_t avail() const {return physical_end-logical_end;}
//! Append sequence [first,last) to this sequence.
void append( char* first, char* last ) {
memcpy( logical_end, first, last-first );
logical_end += last-first;
}
//! Set end() to given value.
void set_end( char* p ) {logical_end=p;}
};
constexpr size_t MAX_CHAR_PER_INPUT_SLICE = 4000;
const std::string InputFileName = "input.txt";
const std::string OutputFileName = "output.txt";
bool input_task(TextSlice **next_slice, spsc_queue<TextSlice*>& input_queue, FILE* input_file) {
size_t m = (*next_slice)->avail();
size_t n = fread( (*next_slice)->end(), 1, m, input_file );
if( !n && (*next_slice)->size()==0 ) {
// No more characters to process
return true;
}
else {
// Have more characters to process.
TextSlice& t = **next_slice;
*next_slice = TextSlice::allocate( MAX_CHAR_PER_INPUT_SLICE );
char* p = t.end()+n;
if( n==m ) {
// Might have read partial number. If so, transfer characters of partial number to next slice.
while( p>t.begin() && isdigit(p[-1]) ) {
--p;
}
(*next_slice)->append( p, t.end()+n );
}
t.set_end(p);
input_queue.enqueue(&t);
return false;
}
}
void output_task(TextSlice* input, spsc_queue<TextSlice*>& output_queue) {
input->end('\0');
char* p = input->begin();
TextSlice& out = *TextSlice::allocate( 2*MAX_CHAR_PER_INPUT_SLICE );
char* q = out.begin();
for(;;) {
while( p<(*input).end() && !isdigit(*p) ) {
*q++ = *p++;
}
if( p==(*input).end() ) {
break;
}
long x = strtol( p, &p, 10 );
// Note: no overflow checking is needed here, as we have twice the
// input string length, but the square of a non-negative integer n
// cannot have more than twice as many digits as n.
long y = x*x;
sprintf(q,"%ld",y);
q = strchr(q,0);
}
out.set_end(q);
(*input).free();
output_queue.enqueue(&out);
}
void sequential() {
auto t1 = std::chrono::high_resolution_clock::now();
spsc_queue<TextSlice*> input_queue;
spsc_queue<TextSlice*> output_queue;
FILE* input_file = fopen( InputFileName.c_str(), "r" );
FILE* output_file = fopen( OutputFileName.c_str(), "w" );
size_t num_inputs {0};
TextSlice* next_slice = TextSlice::allocate( MAX_CHAR_PER_INPUT_SLICE );
while(!input_task(&next_slice, input_queue, input_file)) {
num_inputs ++;
}
auto t2 = std::chrono::high_resolution_clock::now();
for(size_t i=0; i<num_inputs; i++) {
TextSlice *slice;
assert(input_queue.dequeue(slice));
output_task(slice, output_queue);
}
auto t3 = std::chrono::high_resolution_clock::now();
for(size_t i=0; i<num_inputs; i++) {
TextSlice* out;
assert(output_queue.dequeue(out));
size_t n = fwrite( out->begin(), 1, out->size(), output_file );
if( n!=out->size() ) {
fprintf(stderr,"Can't write into file '%s'\n", OutputFileName.c_str());
exit(1);
}
out->free();
}
auto t4 = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2).count()/1000000.0 << std::endl;
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(t4 - t1).count()/1000000.0 << std::endl;
exit(1);
}
TextSlice* output_task(TextSlice* input) {
input->end('\0');
char* p = input->begin();
TextSlice& out = *TextSlice::allocate( 2*MAX_CHAR_PER_INPUT_SLICE );
char* q = out.begin();
for(;;) {
while( p<(*input).end() && !isdigit(*p) ) {
*q++ = *p++;
}
if( p==(*input).end() ) {
break;
}
long x = strtol( p, &p, 10 );
// Note: no overflow checking is needed here, as we have twice the
// input string length, but the square of a non-negative integer n
// cannot have more than twice as many digits as n.
long y = x*x;
sprintf(q,"%ld",y);
q = strchr(q,0);
}
out.set_end(q);
(*input).free();
return &out;
}
int main(){
//sequential();
auto t1 = std::chrono::high_resolution_clock::now();
tf::Taskflow tf(4);
tf::Framework f;
spsc_queue<TextSlice*> input_queue;
spsc_queue<TextSlice*> output_queue;
FILE* input_file = fopen( InputFileName.c_str(), "r" );
FILE* output_file = fopen( OutputFileName.c_str(), "w" );
constexpr size_t batch = 8;
std::array<TextSlice*, batch> temp;
std::vector<tf::Task> tasks;
tasks.resize(batch);
auto [transform, output] = f.emplace(
[&] (auto &subflow) {
subflow.join();
size_t total {0};
for(size_t i=0 ; i<batch; i++) {
TextSlice *input;
if(!input_queue.dequeue(input)) {
break;
}
tasks[i] = subflow.emplace([&, input=input, i=i](){
temp[i] = output_task(input);
});
total ++;
}
if(total) {
auto sync = subflow.emplace(
[&, total=total](){
for(size_t i=0; i<total; i++) {
output_queue.enqueue(temp[i]);
}
}
);
for(size_t i=0; i<total; i++) {
tasks[i].precede(sync);
}
}
},
[&] () {
for(size_t i=0; i<batch; i++) {
TextSlice* out;
if(!output_queue.dequeue(out)) {
break;
}
size_t n = fwrite( out->begin(), 1, out->size(), output_file );
if( n!=out->size() ) {
fprintf(stderr,"Can't write into file '%s'\n", OutputFileName.c_str());
exit(1);
}
out->free();
}
}
);
transform.name("transform");
output.name("output");
// Linear
transform.precede(output);
TextSlice* next_slice = TextSlice::allocate( MAX_CHAR_PER_INPUT_SLICE );
tf.pipeline_until(f, [&]() mutable {
// Read characters into space that is available in the next slice.
size_t m = next_slice->avail();
size_t n = fread( next_slice->end(), 1, m, input_file );
if( !n && next_slice->size()==0 ) {
// No more characters to process
std::puts("All Done");
return true;
}
else {
for(size_t i=0; i<batch ; i++) {
// Have more characters to process.
TextSlice& t = *next_slice;
next_slice = TextSlice::allocate( MAX_CHAR_PER_INPUT_SLICE );
char* p = t.end()+n;
if( n==m ) {
// Might have read partial number. If so, transfer characters of partial number to next slice.
while( p>t.begin() && isdigit(p[-1]) ) {
--p;
}
next_slice->append( p, t.end()+n );
}
t.set_end(p);
input_queue.enqueue(&t);
if(i == batch-1) break;
m = next_slice->avail();
n = fread( next_slice->end(), 1, m, input_file );
if( !n && next_slice->size()==0 ) break;
}
return false;
//return &t;
}
},
[](){}
).get();
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()/1000000.0 << std::endl;
std::fclose(input_file);
std::fclose(output_file);
return 0;
}