Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Convert from OpenMP to TBB

missro_j_
Beginner
917 Views

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-Kukanov
Employee
917 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;
    }
});

 

0 Kudos
Reply