C 使用pThread比使用Sequential更糟糕

C 使用pThread比使用Sequential更糟糕,c,multithreading,parallel-processing,pthreads,matrix-multiplication,C,Multithreading,Parallel Processing,Pthreads,Matrix Multiplication,使用pthread库,我已经为矩阵乘法编写了一些代码 另外,使用线程的程序要比不使用线程的程序快得多。但是,与我的预期不同。。。结果(经过的时间)完全相反,不使用线程的程序比线程程序快得多。发生了什么事。。我不知道为什么会这样 执行时间如下 Time ( # of thread: 4 ) 0.000451 sec Time ( no thread ) 0.000002 sec 我在一个文件(.c)中编写了这两个版本 但pThread总是比sequential差 串行版本(不使用线程)

使用pthread库,我已经为矩阵乘法编写了一些代码

另外,使用线程的程序要比不使用线程的程序快得多。但是,与我的预期不同。。。结果(经过的时间)完全相反,不使用线程的程序比线程程序快得多。发生了什么事。。我不知道为什么会这样

执行时间如下

Time ( # of thread: 4 )

0.000451 sec

Time ( no thread )

0.000002 sec
我在一个文件(.c)中编写了这两个版本

但pThread总是比sequential差

  • 串行版本(不使用线程)

    void serial_multi()
    {
    对于(int i=0;i
  • 使用螺纹(#螺纹:4)

    int步骤i=0;
    void*multi(void*arg)
    {
    int core=步骤i++;
    //每个线程计算1/4的矩阵乘法
    对于(int i=核心*MAX/4;i<(核心+1)*最大/4;i++)
    对于(int j=0;j
  • 主要功能

     int main()
    {
     printf("PID: %d\n",getpid());
     // generating random values in matA and matB
     for (int i = 0; i < MAX; i++){
         for (int j = 0; j < MAX; j++){
             matA[i][j] = rand() % 10;
             matB[i][j] = rand() % 10;
         }
     }
    
     //cout << endl << "Matrix A" << endl;
     for (int i = 0; i < MAX; i++){
         for (int j = 0; j < MAX; j++)
             printf("%d ",matA[i][j]);
         printf("\n");
     }
    
    
     //cout << endl << "Matrix B" << endl;
     for (int i = 0; i < MAX; i++){
         for (int j = 0; j < MAX; j++)
             printf("%d ",matB[i][j]);
         printf("\n");
     }
    
     // declaring 4 threads
     pthread_t threads[MAX_THREAD];
    
     // creating 4 threads, each evaluating its own part
         // Time Estimation
     clock_t start = clock();
     for (int i = 0; i < MAX_THREAD; i++){
    
         pthread_create(&threads[i], NULL, multi, NULL);
     }
    
     // joining and waiting for all threads to complete
     for (int i = 0; i < MAX_THREAD; i++)
         pthread_join(threads[i], NULL);
    
     clock_t end = clock();
     printf("Time: %lf\n", (double)(end-start)/CLOCKS_PER_SEC);
     // displaying the result matrix
     //cout << endl << "Multiplication of A and B" << endl;
    
     for (int i = 0; i < MAX; i++){
         for (int j = 0; j < MAX; j++)
             printf("%d ",matC[i][j]);
         printf("\n");
     }
    
    
    
     return 0;
    }
    
    intmain()
    {
    printf(“PID:%d\n”,getpid());
    //在matA和matB中生成随机值
    对于(int i=0;i//cout由于两个项目,线程程序运行较慢

  • 线程化为上下文切换花费额外的时间

  • 创建/销毁线程需要时间

  • 这两项是主要的“额外”时间消耗者

    另外,您应该注意,此程序是CPU绑定的,而不是I/O绑定的。I/O绑定的程序将受益于线程,但是,CPU绑定的程序由于所有上下文切换、线程的创建和线程的破坏而(显著)延迟


    注意:通过使用“线程池”可以很容易地使程序运行得更快。

    串行程序的运行速度比线程所施加的开销要快。
    MAX
    的值是多少?2微秒太小,无法期望得到改进,尤其是在创建线程而不是重用池中的线程的情况下。该代码也不是线程safe.
    step_i
    @RobertHarvey是的,它没有原子保护。在这个问题上使用线程是不合适的?@KenY-N实际上MAX的值是我将使用的线程数。因此,为了检查线程是否更快,我更改MAX-greater的值,但Sequential-one仍然很快。你的意思是,这个程序在sho时会很慢矩阵乘法的rt大小(但在矩阵大的情况下,它是强大的)因为cpu有限。但在I/O有限的情况下,就像套接字编程一样,这可能是有效的,因为不会受到同一个cpu的阻碍。我说的对吗?@HenryKIM,不是真的,让矩阵变大不会帮助程序更快。啊,我的意思是,与顺序程序相比。谢谢你的回答:)
     int step_i = 0;
     void* multi(void* arg)
     {
        int core = step_i++;
        // each thread computes 1/4th of matrix multiplication
        for (int i = core * MAX / 4; i < (core + 1) * MAX/4; i++)
            for(int j = 0; j < MAX; j++)
                for(int k = 0; k < MAX; k++)
                    matC[i][j] += matA[i][k] * matB[k][j];
        return NULL; 
     }
    
     int main()
    {
     printf("PID: %d\n",getpid());
     // generating random values in matA and matB
     for (int i = 0; i < MAX; i++){
         for (int j = 0; j < MAX; j++){
             matA[i][j] = rand() % 10;
             matB[i][j] = rand() % 10;
         }
     }
    
     //cout << endl << "Matrix A" << endl;
     for (int i = 0; i < MAX; i++){
         for (int j = 0; j < MAX; j++)
             printf("%d ",matA[i][j]);
         printf("\n");
     }
    
    
     //cout << endl << "Matrix B" << endl;
     for (int i = 0; i < MAX; i++){
         for (int j = 0; j < MAX; j++)
             printf("%d ",matB[i][j]);
         printf("\n");
     }
    
     // declaring 4 threads
     pthread_t threads[MAX_THREAD];
    
     // creating 4 threads, each evaluating its own part
         // Time Estimation
     clock_t start = clock();
     for (int i = 0; i < MAX_THREAD; i++){
    
         pthread_create(&threads[i], NULL, multi, NULL);
     }
    
     // joining and waiting for all threads to complete
     for (int i = 0; i < MAX_THREAD; i++)
         pthread_join(threads[i], NULL);
    
     clock_t end = clock();
     printf("Time: %lf\n", (double)(end-start)/CLOCKS_PER_SEC);
     // displaying the result matrix
     //cout << endl << "Multiplication of A and B" << endl;
    
     for (int i = 0; i < MAX; i++){
         for (int j = 0; j < MAX; j++)
             printf("%d ",matC[i][j]);
         printf("\n");
     }
    
    
    
     return 0;
    }