.. WorkingontheAssemblyLinepipeline: Working on the Assembly Line: parallel_pipeline…
- User
.. _Working_on_the_Assembly_Line_pipeline:
Working on the Assembly Line: parallel_pipeline
Pipelining is a common parallel pattern that mimics a traditional manufacturing assembly line. Data flows through a series of pipeline filters and each filter processes the data in some way. Given an incoming stream of data, some of these filters can operate in parallel, and others cannot. For example, in video processing, some operations on frames do not depend on other frames, and so can be done on multiple frames at the same time. On the other hand, some operations on frames require processing prior frames first.
The |full_name| classes
parallel_pipelineand filter implement the pipeline pattern. A simple text processing example will be used to demonstrate the usage ofparallel_pipelineand filter to perform parallel formatting. The example reads a text file, squares each decimal numeral in the text, and writes the modified text to a new file. Below is a picture of the pipeline... CAUTION:: Since the body object provided to the filters of the
parallel_pipelinemight be copied, itsoperator()should not modify the body. Otherwise the modification might or might not become visible to the thread that invokedparallel_pipeline, depending upon whetheroperator()is acting on the original or a copy. As a reminder of this nuance,parallel_pipelinerequires that the body object'soperator()be declaredconst... container:: tablenoborder
.. list-table:: :header-rows: 0
* - Read chunk from input file - |image0| - Square numerals in chunk - |image1| - Write chunk to output fileAssume that the raw file I/O is sequential. The squaring filter can be done in parallel. That is, if you can serially read
nchunks very quickly, you can transform each of thenchunks in parallel, as long as they are written in the proper order to the output file. Though the raw I/O is sequential, the formatting of input and output can be moved to the middle filter, and thus be parallel.To amortize parallel scheduling overheads, the filters operate on chunks of text. Each input chunk is approximately 4000 characters. Each chunk is represented by an instance of class
TextSlice:::
// Holds a slice of text. /** Instances must be allocated/freed using methods herein, because the C++ declaration represents only the header of a much larger object in memory. / 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 = (TextSlice*)oneapi::tbb::tbb_allocator().allocate( sizeof(TextSlice)+max_size+1 ); t->logical_end = t->begin(); t->physical_end = t->begin()+max_size; return t; } // Free this TextSlice object void free() { oneapi::tbb::tbb_allocator().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;} // 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;} };
- Assistant