使用OpenMP在C中的点产品

时间:2016-04-29 20:15:13

标签: c openmp

我使用OpenMP在dot产品上编写了代码,但是当我执行它时会发生死锁。这是代码:

double test02 ( int n, double x[], double y[] )
{
   double xdoty = 0.0;
   int nthrds; 
   int tid, nthreads, i ;
   double dot_seg[2];
   omp_set_num_threads(2);

   #pragma omp parallel default (none) \
    shared (n, nthrds, x, y,  dot_seg) private( i, nthreads, tid)
    {
         nthrds = omp_get_num_threads();
         tid = omp_get_thread_num();
         if (tid == 0) nthreads = nthrds;


         for(i=tid, dot_seg[tid] =0.0; i< n ; i+= nthreads)
         {
              dot_seg[tid] = dot_seg[tid] + x[i]*y[i];

         }
     }

   for (int j=0 ; j< nthrds; j=j+1)      
           xdoty = xdoty + dot_seg[j];

   return xdoty;

}

0 个答案:

没有答案