System information

Intel® Xeon Phi Coprocessor DEVELOPERS QUICK START GUIDE
25
Code Example 10: Wrapping the Intel TBB Header Files in C/C++
Functions called from within the offloaded construct and global data required on the Intel® Xeon Phi™
Coprocessor should be appended by the special function attribute __attribute__((target(mic))).
As an example, parallel_reduce recursively splits an array into subranges for each thread to work on. The
parallel_reduce uses a splitting constructor to make one or more copies for each thread. For each split, the
method join is invoked to accumulate the results.
1. Prefix the class by the macro __MIC__ and the class name by __attribute__((target(mic))) if
you want them to be generated for the coprocessor.
#ifdef __MIC__
class __attribute__((target(mic))) ReduceTBB
{
private:
float *my_data;
public:
float sum;
void operator()( const blocked_range<size_t>& r )
{
float *data = my_data;
for( size_t i=r.begin(); i!=r.end(); ++i)
{
sum += data[i];
}
}
ReduceTBB( ReduceTBB& x, split) : my_data(x.my_data), sum(0) {}
void join( const ReduceTBB& y) { sum += y.sum; }
ReduceTBB( float data[] ) : my_data(data), sum(0) {}
};
#endif
Code Example 11: Prefixing an Intel TBB Class for Intel® MIC Architecture code generation in C/C++
2. Prefix the function to be offloaded to the Intel® Xeon Phi™ Coprocessor by
__attribute__((target(mic)))
__attribute__((target(mic)))
float MICReductionTBB(float *data, int size)
{
ReduceTBB redc(data);
// initializing the library
task_scheduler_init init;
parallel_reduce(blocked_range<size_t>(0, size), redc);
return redc.sum;
}