c - 多线程计算矩阵乘积的时间成本

标签 c linux multithreading operating-system pthreads

最近在学习线程。在一个小实验中,我使用 pthread 尝试多线程来计算两个矩阵的乘积。然后我发现使用多线程比不使用多线程花费的时间更多。我试图扩大矩阵的体积,单线程性能更好。 这是我的测试代码和结果: 单线程:

#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#define M 3
#define K 2
#define N 3
int A[M][K]={{1,4},{2,5},{3,6}};
int B[K][N]={{8,7,6},{5,4,3}};
int C[M][N];
int main()
{
int begin = clock();
int result = 0;
int i,j,m;
for(i=0;i<M;i++)
    for(j=0;j<N;j++){
        for(m=0;m<K;m++){
        result+=A[i][m]*B[m][j];
}
C[i][j]=result;
result = 0;
}
int end = clock();
printf("time cost:%d\n",end-begin);
for(i=0;i<M;i++){
    for(j=0;j<N;j++){
        printf("%d ", C[i][j]);
    }
    printf("\n");
}
}

结果:
时间成本:1
28 23 18
41 34 27
54 45 36
多线程:

#include <stdio.h>
#include <pthread.h>
#include <malloc.h>
#include <sys/time.h>
#define M 3
#define K 2
#define N 3

/*structure for passing data to thread*/
struct v
{
    int i;
    /*row*/
    int j;
    /*column*/
};
void create_and_pass(struct v *data);
void* runner(void *param);
int A[M][K]={{1,4},{2,5},{3,6}};
int B[K][N]={{8,7,6},{5,4,3}};
int C[M][N];

int main(int argc, char* argv[])
{
/*We have to create M*N worker threads*/
    int begin = clock();
    int i,j;
    for(i=0;i<M;i++)
    for(j=0;j<N;j++){
        struct v *data = (struct v *)malloc(sizeof(struct v));
        data->i=i;
        data->j=j;
        /*Now create the thread passing it data as a parameter*/
        create_and_pass(data);
    }
    int end = clock();
    printf("花费时间:%d\n",end-begin);
        for(i=0;i<M;i++){
            for(j=0;j<N;j++){
                printf("%d ",C[i][j]);
            }
            printf("\n");
        }
}
void create_and_pass(struct v *data)
{
    pthread_t tid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_create(&tid,&attr,runner,(void *)data);
    pthread_join(tid,NULL);
}
void* runner(void *param)
{
    struct v *data = param;
    int result = 0;
    int m;
    for(m=0;m<K;m++)
        result+=A[data->i][m]*B[m][data->j];
    C[data->i][data->j]=result;
}

结果:
时间成本:1163
28 23 18
41 34 27
54 45 36

请帮忙,谢谢。

最佳答案

主线程创建并启动工作线程,并立即加入它。加入是阻塞操作,这意味着在该线程完成之前不会启动其他线程。实际上,执行是顺序的,包含内存分配、线程创建等的所有开销。

您也不太可能在如此小的数据集上看到任何 yield 。

关于c - 多线程计算矩阵乘积的时间成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853390/

相关文章:

c - 为什么我的 for 循环适用于 "&&"而不适用于 ","

c - 何时是使用结构或数组的最佳时机

linux - 启动时自动运行脚本 : script doesn't work correctly

c - 重新分配指针的指针的指针

linux - 如何从 bash shell 系统运行 tcsh 脚本?

linux - 设置特定模式格式和仅更改数据的最佳方法是什么

java - 使用 ThreadPoolExecutor 取消 SwingWorker

c++ - C++中的原子指针和线程间传递对象

java - 如何在Java中编写一个连续检查目录中是否有文件的线程

c - 在 C 中实现构造函数的正确方法