多线程程序中的错误(工作缓慢)

时间:2014-03-03 14:37:12

标签: c multithreading time pthreads

我必须制作一个多线程程序(用旋转方法求解方程组)。我的节目给出了正确的答案。但是当我创建更多线程时它运行得更慢。有人能帮我这个吗? 我的部分代码:

typedef struct DATA
 {
double *a; 
int n;
int num_thr; 
int total_thr;
int num_row1;
int num_row2;
double cos;
double sin; 
 }  DATA;


 void synchronize(int total_threads)
  {
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t condvar_in = PTHREAD_COND_INITIALIZER;
static pthread_cond_t condvar_out = PTHREAD_COND_INITIALIZER;
static int threads_in = 0;
static int threads_out = 0;

pthread_mutex_lock(&mutex);

threads_in++;
if (threads_in >= total_threads)
{
    threads_out = 0;
    pthread_cond_broadcast(&condvar_in);
} else
    while (threads_in < total_threads)
        pthread_cond_wait(&condvar_in,&mutex);

threads_out++;
if (threads_out >= total_threads)
{
    threads_in = 0;
    pthread_cond_broadcast(&condvar_out);
} else
    while (threads_out < total_threads)
        pthread_cond_wait(&condvar_out,&mutex);

pthread_mutex_unlock(&mutex);
 }

void rotation (double *a,int n, int num_thr,int total_thr,int num_row1,int num_row2,double cos,double sin)
{
int k;
double m;
int first;

first=n-1-num_thr;
for (k=first;k>=num_row1;k=k-total_thr)
{
    m=a[num_row1*n+k];
    a[num_row1*n+k]=cos*a[num_row1*n+k]+sin*a[num_row2*n+k];
    a[num_row2*n+k]=-sin*m+cos*a[num_row2*n+k];

}
    synchronize (total_thr);


 }
void * rotation_threaded(void *pa)
 {

DATA *data=(DATA*)pa ;
rotation(data->a,data->n,data->num_thr,data->total_thr,data->num_row1,data->num_row2,data->cos,data->sin);
return 0;
 }



int main(int argc, char * argv[])
 {
................


    for(i=0;i<n;i++)
{
    for(j=i+1;j<n;j++)
    {
        n1=a[j*n+i];
            m=a[i*n+i];

            cos=m/sqrt(m*m+n1*n1);
            sin=n1/sqrt(m*m+n1*n1);
            for (t=0;t<total_thr;t++)
            {
                data[t].n=n;
                data[t].a=a;
                data[t].total_thr=total_thr;
                data[t].num_thr=t;
                data[t].num_row1=i;
                data[t].num_row2=j;
                data[t].cos=cos;
                data[t].sin=sin;
            }

            for (k=0;k<total_thr;k++)
            {
                if (pthread_create (threads+k,0,rotation_threaded,data+k))                  {
                    printf (" Couldn't create %d thread",k);
                    return 3;
                }

            }
            for (k=0;k<total_thr;k++)
            {

                if (pthread_join (threads[k],0))
                printf ("Mistake %d \n",k);
            }
            h=b[i];
            b[i]=cos*b[i]+sin*b[j];
            b[j]=-sin*h+cos*b[j];
    } 
}

..............
  }

2 个答案:

答案 0 :(得分:1)

回答具体问题:因为您的线程花费更多时间来获取锁定并等待条件变量而不是实际工作。多线程不是一个免费获得更多的电源方案,如果你必须不断获得高度竞争的锁,你将会受到严重的开销惩罚。你拥有的线程越多,他们就会越多地争夺锁定并且在另一个线程持有锁定时花费被阻止。

要解决此问题:尝试仅在必要时同步数据。排队进行大量更改,和/或一次完成更多工作以实际利用CPU上的线程时间。同步时,尝试仅在最短的绝对必要时间内保持锁定。

最后但并非最不重要:更多线程可能并不总是更好。如果在作业队列中有多个线程处理,那么通常最好只旋转与逻辑CPU核心一样多的线程,这样线程就不必在单个核心上进行争夺。与所有内容一样,正确的分析将告诉您问题所在。

答案 1 :(得分:0)

从我所看到的,您为每个(i,j)对重新创建线程,然后等待所有线程完成。

在开头创建线程并让它们等待条件可能更有意义。然后可以重用线程。

你似乎也在复制每个迭代中每个线程不变的大量信息(这可能不是减速的原因,但为什么不明确什么是变量)。 data中每个线程的唯一信息是num_thrna的值永远不会更改,cossinij的值可以保存在{for-t之外1}}循环。

synchronize方法的用途是什么。它似乎等待所有线程通过threads_in“屏障”然后通过threads_out“屏障”但它保护什么?

相关问题