Converting OpenMP to TBB

417 views Asked by At

I have some difficulties in converting an OpenMP code to TBB. Can someone help me?

I have the following code in OpenMP, where the results are pretty good

# pragma omp parallel \
shared ( b, count, count_max, g, r, x_max, x_min, y_max, y_min ) \
private ( i, j, k, x, x1, x2, y, y1, y2 )
{
 # pragma omp for

 for ( i = 0; i < m; i++ )
{
for ( j = 0; j < n; j++ )
{
 //cout << omp_get_thread_num() << " thread\n";
  x = ( ( double ) (     j - 1 ) * x_max   
      + ( double ) ( m - j     ) * x_min ) 
      / ( double ) ( m     - 1 );

  y = ( ( double ) (     i - 1 ) * y_max   
      + ( double ) ( n - i     ) * y_min ) 
      / ( double ) ( n     - 1 );

  count[i][j] = 0;

  x1 = x;
  y1 = y;

  for ( k = 1; k <= count_max; k++ )
  {
    x2 = x1 * x1 - y1 * y1 + x;
    y2 = 2 * x1 * y1 + y;

    if ( x2 < -2.0 || 2.0 < x2 || y2 < -2.0 || 2.0 < y2 )
    {
      count[i][j] = k;
      break;
    }
    x1 = x2;
    y1 = y2;
  }

  if ( ( count[i][j] % 2 ) == 1 )
  {
    r[i][j] = 255;
    g[i][j] = 255;
    b[i][j] = 255;
  }
  else
  {
    c = ( int ) ( 255.0 * sqrt ( sqrt ( sqrt ( 
      ( ( double ) ( count[i][j] ) / ( double ) ( count_max ) ) ) ) ) );
    r[i][j] = 3 * c / 5;
    g[i][j] = 3 * c / 5;
    b[i][j] = c;
  }
}
}
}

And the TBB version is 10 times slower then OpenMP

the code for TBB is:

tbb::parallel_for ( int(0), m, [&](int i)
{
for ( j = 0; j < n; j++)
{
  x = ( ( double ) (     j - 1 ) * x_max   
      + ( double ) ( m - j     ) * x_min ) 
      / ( double ) ( m     - 1 );

  y = ( ( double ) (     i - 1 ) * y_max   
      + ( double ) ( n - i     ) * y_min ) 
      / ( double ) ( n     - 1 );

  count[i][j] = 0;

  x1 = x;
  y1 = y;

  for ( k = 1; k <= count_max; k++ )
  {
    x2 = x1 * x1 - y1 * y1 + x;
    y2 = 2 * x1 * y1 + y;

    if ( x2 < -2.0 || 2.0 < x2 || y2 < -2.0 || 2.0 < y2 )
    {
      count[i][j] = k;
      break;
    }
    x1 = x2;
    y1 = y2;
  }

  if ( ( count[i][j] % 2 ) == 1 )
  {
    r[i][j] = 255;
    g[i][j] = 255;
    b[i][j] = 255;
  }
  else
  {
    c = ( int ) ( 255.0 * sqrt ( sqrt ( sqrt ( 
      ( ( double ) ( count[i][j] ) / ( double ) ( count_max ) ) ) ) ) );
    r[i][j] = 3 * c / 5;
    g[i][j] = 3 * c / 5;
    b[i][j] = c;
  }
}
});
1

There are 1 answers

0
Alex On

Pay attention to the private ( i, j, k, x, x1, x2, y, y1, y2 ) clause in OpenMP version of the code. This list of variables specifies private/local variable inside the parallel loop body. However, in TBB version of the code many of these variables are captured by lambda as references ([&]) so the code is incorrect. It has races and, in my opinion, slow down is caused by accessing these variables from multiple threads (cache coherence overhead and mess in loop indices) . So, if you want to fix the code, make these variables local, e.g.

tbb::parallel_for ( int(0), m, [&](int i)
{
double x, y, x1, x2, y1, y2; // !!!!
int j, k;                    // !!!!
for ( j = 0; j < n; j++)
{
  x = ( ( double ) (     j - 1 ) * x_max   
      + ( double ) ( m - j     ) * x_min ) 
      / ( double ) ( m     - 1 );
...