Community
cancel
Showing results for 
Search instead for 
Did you mean: 
missro_j_
Beginner
100 Views

Convert from OpenMP to TBB

Hi

I would like to convert  code to tbb but  i have problem set local variable private(x1,y1,sum) and shared variable on openmp to TBB  .

#pragma omp parallel for private (sum,x1,y1)
for (int y = 0; y < src.rows; y++) {
			for (int x = 0; x < src.cols; x++) {
				sum = 0.0;
				for (int k = -1;k <= 1; k++) {
                                for (int j = -1;j <= 1; j++) {
						x1 = circular(src.cols, x - j);
						y1 = circular(src.rows, y - k);
						sum = sum + Kernel[j + 1][k + 1] * src.at<uchar>(y1, x1);
						//	cout << "threads=" << omp_get_num_threads() << endl;

					}
				}
				dst.at<uchar>(y, x) = sum;
			}

 

0 Kudos
1 Reply
Alexey_K_Intel3
Employee
100 Views

Private variables should just be declared in the scope of a single iteration. Shared variables should be captured by reference into the loop body function.

With C++ lambda functions, the code might look like this:

tbb::parallel_for( 0, src.rows, [&](int y) { // capture shared variables into the lambda by reference
    for (int x = 0; x < src.cols; x++) {
        float sum = 0.0; // declared locally
        for (int k = -1;k <= 1; k++) {
            for (int j = -1;j <= 1; j++) {
                int x1 = circular(src.cols, x - j); // declared locally
                int y1 = circular(src.rows, y - k); // declared locally
                sum = sum + Kernel[j + 1][k + 1] * src.at<uchar>(y1, x1);
            }
        }
        dst.at<uchar>(y, x) = sum;
    }
});

 

Reply