一个多线程计算矩阵程序

本文介绍了一个使用C语言编写的多线程程序,该程序专注于矩阵计算。通过展示具体的代码片段,展示了如何利用多线程来加速1000x1000矩阵的运算过程,并给出了测试结果。
摘要由CSDN通过智能技术生成

文章引用了部分代码;http://www.cnblogs.com/zhangchaoyang/articles/1853822.html

以下是我的代码

1、多线程的代码

#include<stdio.h>
#include<time.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<memory.h>
 
/*定义矩阵中元素的上限,避免相乘后溢出*/
#define RANGE 100
/*矩阵A有M行N列,矩阵B有N行M列*/
#define M 1000
#define N 1000
#define P 500
int pthred_count=8; 
void gene_matrix();
void read_matrix();
void *a11_b11();//1
void *a12_b21();//2
void *a11_b12();//3
void *a12_b22();//4
void *a21_b11();//5
void *a22_b21();//6
void *a21_b12();//7
void *a22_b22();//8
void output_result();
int matrixA[M][N];
int matrixB[N][M];
int matrixResult1[M][N]={0};
int matrixResult2[M][N]={0};

//int arr[pthred_count][M][N];
void *func(void *arg);

main()
{
	
    gene_matrix();                                      //用随机数产生两个待相乘的矩阵,并分别存入两个文件中
    read_matrix();                                      //从两个文件中读出数据赋给matrixA和matrixB
    clock_t start=clock();                              //开始计时
    int res[M][N]={0};                                  //保存矩阵相乘的结果。非全局变量一定要显示初始化为0,否则为随机的一个数
    int i=0,j,k;
	pthread_t tids[8];
	pthread_create(&tids[0],NULL,a11_b11,NULL);
	pthread_create(&tids[1],NULL,a12_b21,NULL);
	pthread_create(&tids[2],NULL,a11_b12,NULL);
	pthread_create(&tids[3],NULL,a12_b22,NULL);
	pthread_create(&tids[4],NULL,a21_b11,NULL);
	pthread_create(&tids[5],NULL,a22_b21,NULL);
	pthread_create(&tids[6],NULL,a21_b12,NULL);
	pthread_create(&tids[7],NULL,a22_b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值