#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
int main(int argc, char ** argv){
int i,j;
double a[10][10],b[10][10],c[10][10];
void MatrixMatrixMultiply(int, double *,double *,double *, MPI_Comm);
// MPI_Comm comm;
MPI_Init(&argc,&argv);
for (i=0;i<10;i++){
for(j=0;j<10;j++){
a[i][j]=1;
b[j][i]=1;
c[i][j]=0;
}
}
MatrixMatrixMultiply(10, a,b,c,MPI_COMM_WORLD);
for (i=0;i<10;i++){
printf("\n");
for(j=0;j<10;j++){
printf("%f", c[i][j]);
}
}
MPI_Finalize();
return 0;
}
MatrixMatrixMultiply(int n, double *a, double *b, double *c,
MPI_Comm comm)
{
int i;
int nlocal;
int npes, dims[2], periods[2];
int myrank, my2drank, mycoords[2];
int uprank, downrank, leftrank, rightrank, coords[2];
int shiftsource, shiftdest;
MPI_Status status;
MPI_Comm comm_2d;
/* Get the communicator related information */
MPI_Comm_size(comm, &npes);
MPI_Comm_rank(comm, &myrank);
/* Set up the Cartesian topology */
dims[0] = dims[1] = sqrt(npes);
/* Set the periods for wraparound connections */
periods[0] = periods[1] = 1;
/* Create the Cartesian topology, with rank reordering */
MPI_Cart_create(comm, 2, dims, periods, 1, &comm_2d);
/* Get the rank and coordinates with respect to the new topology */
MPI_Comm_rank(comm_2d, &my2drank);
MPI_Cart_coords(comm_2d, my2drank, 2, mycoords);
/* Compute ranks of the up and left shifts */
MPI_Cart_shift(comm_2d, 0, -1, &rightrank, &leftrank);
MPI_Cart_shift(comm_2d, 1, -1, &downrank, &uprank);
/* Determine the dimension of the local matrix block */
nlocal = n/dims[0];
/* Perform the initial matrix alignment. First for A and then for B */
MPI_Cart_shift(comm_2d, 0, -mycoords[0], &shiftsource, &shiftdest);
MPI_Sendrecv_replace(a, nlocal*nlocal, MPI_DOUBLE, shiftdest,
1, shiftsource, 1, comm_2d, &status);
MPI_Cart_shift(comm_2d, 1, -mycoords[1], &shiftsource, &shiftdest);
MPI_Sendrecv_replace(b, nlocal*nlocal, MPI_DOUBLE,
shiftdest, 1, shiftsource, 1, comm_2d, &status);
/* Get into the main computation loop */
for (i=0; i<dims[0]; i++) {
MatrixMultiply(nlocal, a, b, c); /*c=c+a*b*/
/* Shift matrix a left by one */
MPI_Sendrecv_replace(a, nlocal*nlocal, MPI_DOUBLE,
leftrank, 1, rightrank, 1, comm_2d, &status);
/* Shift matrix b up by one */
MPI_Sendrecv_replace(b, nlocal*nlocal, MPI_DOUBLE,
uprank, 1, downrank, 1, comm_2d, &status);
}
/* Restore the original distribution of a and b */
MPI_Cart_shift(comm_2d, 0, +mycoords[0], &shiftsource, &shiftdest);
MPI_Sendrecv_replace(a, nlocal*nlocal, MPI_DOUBLE,
shiftdest, 1, shiftsource, 1, comm_2d, &status);
MPI_Cart_shift(comm_2d, 1, +mycoords[1], &shiftsource, &shiftdest);
MPI_Sendrecv_replace(b, nlocal*nlocal, MPI_DOUBLE,
shiftdest, 1, shiftsource, 1, comm_2d, &status);
MPI_Comm_free(&comm_2d); /* Free up communicator */
}
/* This function performs a serial matrix-matrix multiplication c = a*b */
MatrixMultiply(int n, double *a, double *b, double *c)
{
int i, j, k;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
for (k=0; k<n; k++)
c[i*n+j] += a[i*n+k]*b[k*n+j];
}
con1.rar_cannon算法
版权申诉
190 浏览量
2022-09-19
19:59:16
上传
评论
收藏 1KB RAR 举报
周楷雯
- 粉丝: 80
- 资源: 1万+
最新资源
- 蒋拖车-2402041820.awb
- 网络安全-木马隐藏技术-实验分析-教学与研究-木马隐藏技术深度解析与实验分析:核心技术剖析与VMware网络模式指南.zip
- 水墨风格PPT模板:优雅设计,适合艺术与文化演示、 艺术与设计PPT模
- 蓝桥杯介绍例题相关练习.zip
- 基于JavaScript、Vue、HTML、CSS和Shell的无偿献血系统接口api设计源码
- 基于Vue实现的你画我猜网页版小游戏源代码
- java+vue+uinapp彩票系统多端多商家应用源码分享
- tensorflow-2.9.2-cp38-cp38-win-amd64.whl
- tensorflow-gpu-2.9.3-cp39-cp39-win-amd64.whl
- tensorflow-gpu-2.9.3-cp37-cp37m-win-amd64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0