.. _Elementwise: Elementwise =========== .. container:: section .. rubric:: Problem :class: sectiontitle Initiate similar independent computations across items in a data set, and wait until all complete. .. container:: section .. rubric:: Context :class: sectiontitle Many serial algorithms sweep over a set of items and do an independent computation on each item. However, if some kind of summary information is collected, use the Reduction pattern instead. .. container:: section .. rubric:: Forces :class: sectiontitle No information is carried or merged between the computations. .. container:: section .. rubric:: Solution :class: sectiontitle If the number of items is known in advance, use ``oneapi::tbb::parallel_for``. If not, consider using ``oneapi::tbb::parallel_for_each``. Use agglomeration if the individual computations are small relative to scheduler overheads. If the pattern is followed by a reduction on the same data, consider doing the element-wise operation as part of the reduction, so that the combination of the two patterns is accomplished in a single sweep instead of two sweeps. Doing so may improve performance by reducing traffic through the memory hierarchy. .. container:: section .. rubric:: Example :class: sectiontitle Convolution is often used in signal processing. The convolution of a filter ``c`` and signal ``x`` is computed as: |image0| Serial code for this computation might look like: :: // Assumes c[0..clen-1] and x[1-clen..xlen-1] are defined for( int i=0; i(0,xlen+clen-1,1000), [=]( oneapi::tbb::blocked_range r ) { int end = r.end(); for( int i=r.begin(); i!=end; ++i ) { float tmp = 0; for( int j=0; j