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
|
#include <tbb/flow_graph.h> #include <tbb/tbb.h>
#include <iostream> #include <vector>
#include "tasks.hpp"
using namespace tbb; using namespace tbb::flow;
int main() { graph g;
input_node<DataChunk> reader( g, [](flow_control& fc) -> DataChunk { DataChunk chunk(1024); if (!read_from_network(chunk)) { fc.stop(); return DataChunk(); } return chunk; });
function_node<DataChunk, CompressedChunk> processor( g, unlimited, [](const DataChunk& input) -> CompressedChunk { CompressedChunk output(input.data.size());
tbb::parallel_for( tbb::blocked_range<size_t>(0, input.data.size()), [&](const tbb::blocked_range<size_t>& r) { for (size_t i = r.begin(); i != r.end(); ++i) { output.data[i] = compress_byte(input.data[i]); } });
return output; });
function_node<CompressedChunk> writer( g, serial, [](const CompressedChunk& output) { write_to_file(output); });
make_edge(reader, processor); make_edge(processor, writer);
reader.activate(); g.wait_for_all();
return 0; }
|