本文所写内容是根据《并行程序设计导论》第三章中的矩阵向量乘法代码总结而来的完整代码。
完整代码如下Mat_vect_mult.c
#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>void Get_input(int my_rank,int *m,int *n)
{if(my_rank==0){printf("Please enter m,n:\n");scanf("%d %d",m,n);}MPI_Bcast(m,1,MPI_INT,0,MPI_COMM_WORLD);MPI_Bcast(n,1,MPI_INT,0,MPI_COMM_WORLD);
}//得到矩阵
void Get_matrix(int n, int m, double *local_matrix, int local_m, int my_rank)
{double *A;if (!my_rank){A = (double *)malloc(m * n * sizeof(double));printf("Please enter the matrix:\n");for (int i = 0; i < m; ++i)for (int j = 0; j < n; ++j)scanf("%lf", &A[i * n + j]);}//MPI_Scatter函数将矩阵分发出去MPI_Scatter(A, local_m * n, MPI_DOUBLE, local_matrix, local_m * n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
}//打印矩阵
//MPI_Gather函数将local_matrix从各个进程聚集到0号进程输出
void Print_matrix(int my_rank,int n,int m,int local_m,double *local_matrix,MPI_Comm comm)
{double *matrix = NULL;int i,j;if(my_rank==0){matrix = malloc(m*n*sizeof(double));MPI_Gather(local_matrix,local_m*n,MPI_DOUBLE,matrix,local_m*n,MPI_DOUBLE,0,comm);printf("The matrix is:\n");for(i=0;i<m;++i){for(j=0;j<n;++j){printf("%f ",matrix[i*n+j]);}printf("\n");}free(matrix);}else{MPI_Gather(local_matrix,local_m*n,MPI_DOUBLE,matrix,local_m*n,MPI_DOUBLE,0,comm);}
}//得到向量并分发
void Get_vector(int my_rank,int n,int local_n,double *local_vector,MPI_Comm comm)
{double *vector = NULL;int i;if(my_rank==0){vector=(double *)malloc(n*sizeof(double));printf("Please enter the vector:\n");for(i=0;i<n;i++){scanf("%lf",&vector[i]);}}printf("\n");MPI_Scatter(vector,local_n,MPI_DOUBLE,local_vector,local_n,MPI_DOUBLE,0,comm);
}//聚合向量到0号进程并且输出
void Print_vector(int my_rank,int n,int local_n,double *local_vector,MPI_Comm comm)
{double *vector = NULL;int i,j;if(my_rank==0){vector = malloc(n*sizeof(double));MPI_Gather(local_vector,local_n,MPI_DOUBLE,vector,local_n,MPI_DOUBLE,0,comm);printf("The vector is:\n");for(i=0;i<n;i++){printf("%f ",vector[i]);}printf("\n");free(vector);}else{MPI_Gather(local_vector,local_n,MPI_DOUBLE,vector,local_n,MPI_DOUBLE,0,comm);}
}//实现矩阵乘法void Mat_vect_mult(double *local_matrix,double *local_vector,double *local_y,int local_m,int n,int local_n,MPI_Comm comm)
{int local_i,j;double *x;x=malloc(n*sizeof(double));//将向量聚合到所有进程,MPI_Allgather和MPI_Gather的区别就在于Allgather的所//有进程都会知道你聚合到的的向量,相当于聚合到0号进程之后又bcast广播了一次MPI_Allgather(local_vector,local_n,MPI_DOUBLE,x,local_n,MPI_DOUBLE,comm);for(local_i=0;local_i<local_m;local_i++){local_y[local_i]=0.0;for(j=0;j<n;j++){local_y[local_i]+=local_matrix[local_i*n+j]*x[j];}}free(x);
}//打印结果
void Print_y(int my_rank,double *local_y,int m,int local_m,MPI_Comm comm)
{double *y=NULL;int i;if(my_rank==0){y=malloc(m*sizeof(double));MPI_Gather(local_y,local_m,MPI_DOUBLE,y,local_m,MPI_DOUBLE,0,comm);printf("The vector y is:\n");for(i=0;i<m;i++){printf("%lf ",y[i]);}printf("\n");free(y);}else{MPI_Gather(local_y,local_m,MPI_DOUBLE,y,local_m,MPI_DOUBLE,0,comm);}
}int main()
{int comm_sz,my_rank,i;int m,n,local_m,local_n;double *local_matrix,*local_vector;double *local_y;MPI_Init(NULL,NULL);MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);Get_input(my_rank,&m,&n);local_m=m/comm_sz;local_n=n/comm_sz;local_matrix=(double *)malloc(local_m*n*sizeof(double));local_vector=(double *)malloc(local_n*sizeof(double));local_y=(double *)malloc(local_m*sizeof(double));Get_matrix(n,m,local_matrix,local_m,my_rank);Print_matrix(my_rank,n,m,local_m,local_matrix,MPI_COMM_WORLD);Get_vector(my_rank,n,local_n,local_vector,MPI_COMM_WORLD);Print_vector(my_rank,n,local_n,local_vector,MPI_COMM_WORLD);Mat_vect_mult(local_matrix,local_vector,local_y,local_m,n,local_n,MPI_COMM_WORLD);Print_y(my_rank,local_y,m,local_m,MPI_COMM_WORLD);MPI_Finalize();return 0;
}

编译运行指令为

mpicc -o a.out Mat_vect_mult.c  //编译
mpiexec -n <进程数> ./a,out   //运行

运行实例如图

MPI矩阵向量乘法代码《并行程序设计导论》相关推荐

  1. 并行程序设计导论_C程序设计导论

    并行程序设计导论 基本术语 (Basic Terminology) Before we start the technicalities of our topic, let us understand ...

  2. 并行程序设计导论pdf电子书_并行程序设计导论学习笔记——OpenMP(1)

    使用OpenMP需要在编译器上打开OpenMP开关,并包含omp.h文件.我使用的是在Windows下的Visual Studio 2015,只需在工程选项中打开OpenMP支持就可以了.按照书上的说 ...

  3. openmp 互斥锁 mysql_openMP 函数总结(并行程序设计导论)

    本篇文章只是记录api的用法和回顾,方便记忆 openMP openMP提供"基于指令"的共享内存API.这就意味着在c和c++中,有一些特殊的预处理指令pragma.在系统中加入 ...

  4. 并行程序设计导论期末复习

    任务并行.数据并行的应用 任务并行 将待解决问题所需要执行的各个任务分配到各个核上执行. 数据并行 将待解决问题所需要处理的数据分配给各个核,每个核在分配到的数据集上执行大致相似的操作. 冯诺依曼体系 ...

  5. 编写一个完整的矩阵向量乘法的MPI编程代码

    答:以下是一个完整的矩阵向量乘法的MPI编程代码:#include <stdio.h> #include <stdlib.h> #include <mpi.h>in ...

  6. CUDA编程--并行矩阵向量乘法【80+行代码】

    简述 矩阵向量乘法. 读取文件data.txt 并输入到output.txt文件中 用typedef方便的修改数据类型(要是写成模板也是可以的) 代码 #include "cuda_runt ...

  7. 基于mpi的奇偶排序_并行程序设计(第2版)pdf

    并行程序设计(第2版) 内容简介 本书系统介绍并行程序设计原理及应用.除介绍常用的一些算法范例,包括分治.流水.同步计算.主从及工作池,还介绍了一些常用的经典数值和非数值算法,如排序.矩阵相乘.线性方 ...

  8. 并行程序设计报告(MPI并行计算π,实现mandelbrot集)

    代码的github地址 一.熟悉MPI并行程序设计环境 1.硬件 电脑:HP暗夜精灵 内存:4G 处理器:ntel® Core™ i5-6300HQ CPU @ 2.30GHz × 4 显卡:NVID ...

  9. 基于MPI的H.264并行编码代码移植与优化

    2010 03 25 基于MPI的H.264并行编码代码移植与优化 范 文 洛阳理工学院计算机信息工程系 洛阳 471023 摘 要 H.264获得出色压缩效果和质量的代价是压缩编码算法复杂度的增加. ...

最新文章

  1. 外媒:英贸易大臣将与其他国家商脱欧后贸易协议
  2. Call apply 用法
  3. mysql删除数据后不释放空间问题
  4. Zabbix的应用(6)----常见错误
  5. Python风格总结:遍历技巧
  6. python输出24进制时间_python-如何在给定时间原点的情况下将日期时间从十进制转换为“%y-%m-%d%H:%M:%S”?...
  7. 剑指offer面试题[42]-反转单词顺序VS左旋转字符串
  8. HDOJ水题集合7:记忆化搜索
  9. 6. 分类图显示和保存
  10. H3C防火墙——回环流量问题(内网终端通过外网IP访问内部服务器)
  11. python 计算斜率
  12. c++双向列表释放_SAIL-C-R-T4-A1-V0-B1温度控制仪-老友网
  13. Linux-系统管理16-磁盘配额
  14. HTTP状态码监测分析
  15. Cyclone V LAB ALM结构
  16. 使用lombok编写优雅的Bean对象
  17. SparkStreaming读Kafka- Couldn't find leaders for Set
  18. 太原师范学院java期末试题_2013-2014(2)太原师范学院期末试题(微分几何)A
  19. 第9章 maven的插件和生命周期
  20. 前端css外部样式引入html

热门文章

  1. 微信给好友发送这个“隐形代码”,1秒检测出谁删除谁拉黑了你!
  2. ctf-web-秋名山车神
  3. r420服务器安装系统,r420服务器bios设置
  4. 这个宝藏自媒体平台,你get到了吗?
  5. 几种奥比中光SDK的功能说明以及使用方法
  6. OS X 初次安装MYSQL
  7. Ceph入门系列(一)
  8. 勾股定理(计算)C++
  9. attiny13a程序实例_如何使用Arduino IDE编程ATTINY13/ATTINY13A单片机
  10. Windows CE下中文输入法编辑器