Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

OPENMP conversion to TBB

zzzelmu
Beginner
126 Views
Hey,

I'm new in parallel programing but I've got the mission to convert this peace of code (method) from OPENMP to TBB
[cpp]void carMoving()
{
	int j;
	#pragma omp parallel for private(j)
	for (int i = 0; i < circleSize; i++) {
		j = ((i + 1) % circleSize);
		if (circle == -1 || circle == j) {
			newCircle = -1;
		}
		else {
			newCircle = circle;
			newCircle = -1;
		}
	}
}[/cpp]

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

[cpp]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 == -1 || circle == j) {
				newCircle = -1;
			}
			else {
				newCircle = circle;
				newCircle = -1;
			}
		}
    }
};

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

The code is executing and the program is running without errors but I'm wondering if the private variable is implemented right.Thanks.
0 Kudos
3 Replies
RafSchietekat
Black Belt
126 Views
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
Black Belt
126 Views
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 = -1;

Jim Dempsey
zzzelmu
Beginner
126 Views
Yes many thanks my mistake,
else block contains only the first statement (newCircle = circle)
Reply