北京科技大学计算机与通信工程学院实验报告实验名称:学生姓名:专业:班级:学号:指导教师:实验成绩:________________________________实验地点:实验时间:2015年05月一、实验目的与实验要求1、实验目的1对比矩阵乘法的串行和并行算法,查看运行时间,得出相应的结论;2观察并行算法不同进程数运行结果,分析得出结论;2、实验要求1编写矩阵乘法的串行程序,多次运行得到结果汇总;2编写基于MPI,分别实现矩阵乘法的并行化。
对实现的并行程序进行正确性测试和性能测试,并对测试结果进行分析。
二、实验设备(环境)及要求《VS2013》C++语言MPICH2三、实验内容与步骤实验1,矩阵乘法的串行实验(1)实验内容编写串行程序,运行汇总结果。
(2)主要步骤按照正常的矩阵乘法计算方法,在《VS2013》上编写矩阵乘法的串行程序,编译后多次运行,得到结果汇总。
实验2矩阵乘法的并行化实验3个总进程5个总进程7个总进程9个进程16个进程四:实验结果与分析(一)矩阵乘法并行化矩阵并行化算法分析:并行策略:1间隔行带划分法算法描述:将C=A*B中的A矩阵按行划分,从进程分得其中的几行后同时进行计算,最后通信将从进程的结果合并的主进程的C矩阵中对于矩阵A*B如图:进程1:矩阵A第一行进程2:矩阵A第二行进程3:矩阵A第三行进程1:矩阵A第四行时间复杂度分析:f(n) =6+2+8+k*n+k*n+k*n+3+10+n+k*n+k*n+n+2(k为从进程分到的行数)因此O(n)=(n);空间复杂度分析:从进程的存储空间不共用,f(n)=n;因此O(n)=(n);2间隔行带划分法算法描述:将C=A*B中的A矩阵按行划分,从进程分得其中的几行后同时进行计算,最后通信将从进程的结果合并的主进程的C矩阵中对于矩阵A*B如图:进程1:矩阵A第一行进程2:矩阵A第二行进程3:矩阵A第三行进程3:矩阵A第四行时间复杂度分析:f(n) =6+2+8+k*n+k*n+k*n+3+10+n+k*n+k*n+n+2(k为从进程分到的行数)因此O(n)=(n);空间复杂度分析:从进程的存储空间不共用,f(n)=n;因此T(n)=O(n);测试环境简介:《VS2013》Win7旗舰版正确性测试结果:并行结果:串行结果:通过比较发现两者相同,因此可以确定运算结果正确。
性能测试结果:通过对比分析得出,并行算法能够有效提高矩阵乘法的运算效率;(二)串行算法分析时间复杂度:f(n)=2+n^2+2*n+3*n+7+n+n+7;因此T(n)=O(n^2)空间复杂度:T(n)=O(1)五:结论(讨论)1、实验结论(1)根据前面实验结果,可以证明并行算法比串行算法更能有效地运算矩阵乘法,可以提高效率数十倍(2)如下图,根据结果可以得出并行程序并不是进程数越多,程序运行时间越短,程序运行还受计算机运算器数的限制2、讨论1由于矩阵乘法的运算并不是很复杂,因此运算时间很短,达到了毫秒级,因此准确度有一定的影响;2因为时间紧迫,未能进一步分析代码,同时只用了一种并行算法。
六:代码附录串行算法:#include<stdio.h>#include<conio.h>#include<iostream>#include<time.h>#define N 30#define M 30void juzhen_mul(int m, int n, int *p1[M], int m1, int n1, int *p2[M]){int i, j, x = 0;int c[N][M] = { 0 }; 2for (i = 0; i<m;){for (j = 0; j<m1; j++){c[i][x] += *(p1[i] + j)**(p2[j] + x); 30*30 m1*m }printf("%d", c[i][x]);printf(" "); 2*30 2*mx++;if (x == n1){x = 0;i++;printf("\n"); 2*30 3*m}}}void main(){double start, finish;start = (double)clock();char H;int i, j;int *pa[M], *pb[M];int m = 30, n = 30;int a[M][N] = { 90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12};7for (i = 0; i<m; i++){pa[i] = a[i]; m}7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12};for (i = 0; i<m; i++){pb[i] = b[i]; m}int m1 = 30, n1 = 30;printf("矩阵A*B= \n");if (n == m1)juzhen_mul(m, n, pa, m1, n1, pb);elseprintf("输入的行数和列数有误,请重新输入!\n");finish = (double)clock();printf("程序运行时间为:%lfms\n", double(finish - start) / CLOCKS_PER_SEC*1000);getchar(); 7}并行#undef SEEK_SET#undef SEEK_END#undef SEEK_CUR#define MPICH_SKIP_MPICXX#include<mpi.h>#include<iostream>#include<vector>#include<conio.h>#include<iostream>#include<time.h>using namespace std;int master();int slave();int main(void) {int myid; //int numprocs; //MPI_Status status;//MPI程序的初始化MPI_Init(NULL, NULL);//获取当前进程的进程编号,保存在myid中MPI_Comm_rank(MPI_COMM_WORLD, &myid);//获取当前进程的进程总数,保存在numprocs中MPI_Comm_size(MPI_COMM_WORLD, &numprocs); 6//如果进程数小于2个,则退出程序if (numprocs<2){cout << "Too Few Processes!" << endl;MPI_Abort(MPI_COMM_WORLD, 99); 2 }if (myid == 0){//id是0的进程作为主进程double start = MPI_Wtime();int myid; //int numprocs; //MPI_Status status;//获取当前进程的进程编号,保存在myid中MPI_Comm_rank(MPI_COMM_WORLD, &myid);//获取当前进程的进程总数,保存在numprocs中MPI_Comm_size(MPI_COMM_WORLD, &numprocs);int const M(30), K(30), N(30);//结果矩阵Cint C[M*N];int V[M]; 8//将结果矩阵各元素初始化为0for (int i = 0; i<M*N; i++){C[i] = 0; M*N}//C矩阵共N列数据for (int i = 0; i<N; i++){//从各从进程接受数据for (int j = 1; j<numprocs; j++){MPI_Recv(V, M, MPI_INT, j, i, MPI_COMM_WORLD, &status); n*num//将各从进程的部分数据整合成完整的一个向量for (int ij = 0; ij<M; ij++){C[ij*N + i] = C[ij*N + i] + V[ij]; ij}}}//打印结果for (int i = 0; i<M; i++){for (int j = 0; j<N; j++){cout << C[i*N + j] << "\t"; M*N}cout << endl;}double finish = MPI_Wtime();printf("程序运行时间为:%lfms\n", double(finish - start) / CLOCKS_PER_SEC*1000);return 0; 3}else{//id是非0的进程作为从进程int myid; //int numprocs; //MPI_Status status;//获取当前进程的进程编号,保存在myid中MPI_Comm_rank(MPI_COMM_WORLD, &myid);//获取当前进程的进程总数,保存在numprocs中MPI_Comm_size(MPI_COMM_WORLD, &numprocs);int const M(30), K(30), N(30);vector<int> indexes;int V[M];int A[M*K] = { 90,6,55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99,53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12};int B[K*N] = { 90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49,15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12,90, 6, 55, 1, 81, 18, 40, 57, 43, 23, 51, 73, 97, 3, 89, 57, 98, 58, 7, 12, 72, 2, 35, 74, 60, 59, 23, 30, 67, 53,46, 35, 12, 64, 97, 21, 69, 31, 21, 77, 14, 64, 71, 97, 5, 46, 100, 57, 64, 34, 20, 3, 4, 5, 67, 20, 5, 41, 99, 53,63, 90, 70, 73, 34, 55, 42, 27, 1, 85, 32, 85, 26, 69, 91, 43, 31, 73, 32, 49, 15, 74, 79, 8, 90, 75, 8, 85, 49, 68,15, 38, 50, 39, 91, 22, 47, 97, 45, 82, 96, 66, 81, 40, 58, 75, 75, 26, 61, 59, 51, 17, 66, 56, 20, 39, 62, 29, 57, 94,81, 34, 9, 54, 58, 33, 96, 2, 55, 7, 38, 43, 87, 19, 72, 91, 16, 28, 53, 12, 76, 39, 50, 99, 63, 28, 89, 43, 19, 12, };//计算本进程分配A矩阵中哪些行,保存在indexes中//比如2个从进程,A是4行数据,id为1的从进程分配的是0和2行 10for (int i = myid - 1; i<M; i += numprocs - 1){indexes.push_back(i); n}// 连续划分时Int h=M/numprocs+1for (int i = (myid – 1)*h; i<myid*h and i<M; i ++){indexes.push_back(i); n}//依次计算B的N列数据for (int i = 0; i<N; i++){//将保存列数据的向量V各元素初始化为0for (int j = 0; j<M; j++){V[j] = 0; k*N}if (indexes.size()>0){for (int j = 0; j<indexes.size(); j++){for (int ij = 0; ij<K; ij++){V[indexes[j]] += A[indexes[j] * K + ij] * B[ij*N + i]; M*N }}MPI_Send(V, M, MPI_INT, 0, i, MPI_COMM_WORLD); M}}return 0;}//MPI程序释放资源MPI_Finalize();return EXIT_SUCCESS; 2}七、教师评审。