Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

c++ - How to do a parallel bilinear interpolation

I tried this but I don't know if it is right, and to calculate the time of execution sometimes it gives me 0 in the code, I firstly created a structure to stock all the coordinates of the point to be interpolated I have a table of points to be interpolated, so I used pragma parallel for to execute the for section to interpolate all the points. The variables R1, R2, and P are doubles, and shared among threads.

  #pragma omp parallel for
  for(i=0;i<N ; i++)
  {
     R1 =  BilinearInterpolation(Table[i].x1, Table[i].Q11,Table[i].x2, Table[i].Q21,Table[i].x);
     R2 =  BilinearInterpolation(Table[i].x1, Table[i].Q12, Table[i].x2, Table[i].Q22,Table[i].x);
     P =  BilinearInterpolation(Table[i].y1,  R1, Table[i].y2,  R2, Table[i].y);
     TableInter[i] = P;
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
 #pragma omp parallel for
 for(i=0;i<N ; i++)
 {
      R1 = BilinearInterpolation(Table[i].x1, Table[i].Q11,Table[i].x2, Table[i].Q21,Table[i].x);
      R2 = BilinearInterpolation(Table[i].x1, Table[i].Q12, Table[i].x2, Table[i].Q22,Table[i].x);
      P =  BilinearInterpolation(Table[i].y1,  R1, Table[i].y2,  R2, Table[i].y);
      TableInter[i] = P;
 }

The problem with your code is that R1, R2, and P are shared and updated by multiple threads, therefore you have a race condition. For example, one thread might be changing P while another adds P to the TableInter[i]. Nonetheless, you can easily solve this race condition by declaring those variables as private, i.e., declaring them inside the parallel region, or by using the OpenMP's private clause (#pragma omp parallel for private(R1, R2, P).

 #pragma omp parallel for private(R1, R2)
 for(i=0;i<N ; i++)
 {
      R1 = BilinearInterpolation(Table[i].x1, Table[i].Q11,Table[i].x2, Table[i].Q21,Table[i].x);
      R2 = BilinearInterpolation(Table[i].x1, Table[i].Q12, Table[i].x2, Table[i].Q22,Table[i].x);
      TableInter[i] = BilinearInterpolation(Table[i].y1,  R1, Table[i].y2,  R2, Table[i].y);
 }

As long as the BilinearInterpolation method does not modify shared state among threads, this code is race-condition free.

calculate the time of execution sometimes it gives me 0 in the code,

To calculate the time one can use the OpenMP function omp_get_wtime, as follows:

double start = omp_get_wtime();
// the code that you want to measure.
double end = omp_get_wtime();  
printf("%f
",end-start);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...