OPENMP conversion to TBB

OPENMP conversion to TBB

zzzelmu's picture

Hey,

I'm new in parallel programing but I've got the mission to convert this peace of code (method) from OPENMP to TBB

void carMoving()
{
	int j;
	#pragma omp parallel for private(j)
	for (int i = 0; i < circleSize; i++) {
		j = ((i + 1) % circleSize);
		if (circle[i] == -1 || circle[i] == j) {
			newCircle[j] = -1;
		}
		else {
			newCircle[j] = circle[i];
			newCircle[j] = -1;
		}
	}
}

So far searching in here as well as other places I've done this:

class CarMoving
{
	int m_j;
public:
    CarMoving ():m_j() {};
    void operator () ( const tbb::blocked_range& range) const{
        for ( int i = range.begin(); i != range.end(); ++i)
		{
			int j;
            j = ((i + 1) % circleSize);
			if (circle[i] == -1 || circle[i] == j) {
				newCircle[j] = -1;
			}
			else {
				newCircle[j] = circle[i];
				newCircle[j] = -1;
			}
		}
    }
};

void carMoving()
{
	CarMoving carmoving;
	tbb::parallel_for(tbb::blocked_range(0, circleSize), carmoving, tbb::auto_partitioner());
}

The code is executing and the program is running without errors but I'm wondering if the private variable is implemented right.Thanks.

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Raf Schietekat's picture

That seems quite all right (although I would go for "const int j = ((i + 1) % circleSize);").

Being less familiar with OpenMP myself, I wonder if shared/private couldn't be determined simply by the location of the declaration instead, and, if not, why not (maybe that's how it inevitably works in Fortran and the designers had the unfortunate idea to make OpenMP behave exactly alike in both languages)?

jimdempseyatthecove's picture

In the first (omp) example, j was declared outside the scope of the parallel region... but was declared private, thus importing the declaration inside the parallel region.

In the second (tbb) ecample, j was declared inside the parallel region, thus making it private to that region.

BTW, you could have written the omp example with placing int j= ((i+1)%circleSize); inside the parallel region.

Is your code correct? In particular lines 15, and 16.
When the compiler optimizes that loop, the if and its two branches will be replaced with

newCircle[j] = -1;

Jim Dempsey

www.quickthreadprogramming.com
zzzelmu's picture

Yes many thanks my mistake,
else block contains only the first statement (newCircle[j] = circle[i])

Login to leave a comment.