欢迎来到天天文库
浏览记录
ID:38471897
大小:50.50 KB
页数:4页
时间:2019-06-13
《操作系统概念LAB4—矩阵相乘—实验报告》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、LAB4实验报告实验目的:1.矩阵相乘实验内容:给定两个矩阵A和B,其中A是具有M行、K列的矩阵,B为K行、N列矩阵,A和B的矩阵积为C,C为M行,N列。矩阵C中第i行、第j列的元素Ci,j就是矩阵A第i行每个元素和矩阵B第j列每个元素乘积的和。本实验的目的是通过对矩阵乘法是多线程实现,让学生加深对线程的理解与调用。实验步骤:在Linux操作系统上,利用Linux处理多线程的优点,编写程序用多线程实现矩阵乘法。实现A、B两个矩阵的乘法,并输出计算结果。实验代码如下:#include#include#incl
2、ude#include#include#include#defineRANGE100#defineM2#defineK2#defineN2voidgene_matrix();voidread_matrix();intmatrixA[M][N]={{1,1},{2,2}};intmatrixB[N][M]={{4,14},{5,25}};intarr[M][M][N];intres[M][N]={0};void*func(void*arg);main(){clo
3、ck_tstart=clock();inti,j,k,cnt,cnt2;pthread_ttids[N];for(i=0;i4、res[i][j]+=arr[i][j][k];printf("res[%d][%d]=%d",i,j,res[i][j]);}clock_tfinish=clock();printf("n",(long)(finish-start)/1E6);}void*func(void*arg){intk=*(int*)arg;inti,j;printf("Enterfunc");for(i=0;i5、"func=%d",arr[i][j][k]);}}pthread_exit(NULL);}voidgene_matrix(){FILE*file1,*file2;if((file1=fopen("/home/timelong/Desktop/matrixA","wt"))==NULL){perror("fopen");exit(1);}if((file2=fopen("/home/timelong/Desktop/matrixB","wt"))==NULL){perror("fopen");exit(1);}inti,j;srand(6、(unsigned)time(NULL));for(i=0;i7、1=fopen("/home/timelong/Desktop/matrixA","rt"))==NULL){perror("fopen");exit(1);}if((file2=fopen("/home/timelong/Desktop/matrixB","rt"))==NULL){perror("fopen");exit(1);}inti,j;for(i=0;i8、atrixA[i][j]);}fclose(file1);for(i=0;i
4、res[i][j]+=arr[i][j][k];printf("res[%d][%d]=%d",i,j,res[i][j]);}clock_tfinish=clock();printf("n",(long)(finish-start)/1E6);}void*func(void*arg){intk=*(int*)arg;inti,j;printf("Enterfunc");for(i=0;i5、"func=%d",arr[i][j][k]);}}pthread_exit(NULL);}voidgene_matrix(){FILE*file1,*file2;if((file1=fopen("/home/timelong/Desktop/matrixA","wt"))==NULL){perror("fopen");exit(1);}if((file2=fopen("/home/timelong/Desktop/matrixB","wt"))==NULL){perror("fopen");exit(1);}inti,j;srand(6、(unsigned)time(NULL));for(i=0;i7、1=fopen("/home/timelong/Desktop/matrixA","rt"))==NULL){perror("fopen");exit(1);}if((file2=fopen("/home/timelong/Desktop/matrixB","rt"))==NULL){perror("fopen");exit(1);}inti,j;for(i=0;i8、atrixA[i][j]);}fclose(file1);for(i=0;i
5、"func=%d",arr[i][j][k]);}}pthread_exit(NULL);}voidgene_matrix(){FILE*file1,*file2;if((file1=fopen("/home/timelong/Desktop/matrixA","wt"))==NULL){perror("fopen");exit(1);}if((file2=fopen("/home/timelong/Desktop/matrixB","wt"))==NULL){perror("fopen");exit(1);}inti,j;srand(
6、(unsigned)time(NULL));for(i=0;i7、1=fopen("/home/timelong/Desktop/matrixA","rt"))==NULL){perror("fopen");exit(1);}if((file2=fopen("/home/timelong/Desktop/matrixB","rt"))==NULL){perror("fopen");exit(1);}inti,j;for(i=0;i8、atrixA[i][j]);}fclose(file1);for(i=0;i
7、1=fopen("/home/timelong/Desktop/matrixA","rt"))==NULL){perror("fopen");exit(1);}if((file2=fopen("/home/timelong/Desktop/matrixB","rt"))==NULL){perror("fopen");exit(1);}inti,j;for(i=0;i8、atrixA[i][j]);}fclose(file1);for(i=0;i
8、atrixA[i][j]);}fclose(file1);for(i=0;i
此文档下载收益归作者所有