一 . 运行环境VS2017 + MPI
二 . 算法原理可见博客https://www.cnblogs.com/chihaoyuIsnotHere/p/10553617.html
三. 使用到的MPI关键技术:虚拟笛卡尔进程拓扑
四. 源代码:头文件Matrix.h与第三篇文章的类似,只需加上+号重载
#include#include"Matrix.h"
#include#include#include"mpi.h"
using namespace std;
const int N = 600;
int myid, numprocs,upRank,downRank,leftRank,rightRank,part,num;
int masterNode;
int coord[2];
double start, finish;
MPI_Comm Cart_Comm_World;
int isqrt(int x)
{
int ipart = sqrt(x);
if (ipart*ipart != x)
++ipart;
return ipart;
}
void MatrixScatter(Matrix &A)
{
double *memoryPool = nullptr;
if (coord[0] == 0 && coord[1] == 0)
{
Matrix tmp(N);
tmp.ranCreate();
//cout << tmp << endl;
memoryPool = new double[N*N];
int count = 0;
for (int k = 0; k < part; ++k)
for (int h = 0; h < part; ++h)
for (int i = k * num; i
五.运行结果(dim = 600,cannon算法在问题规模较小的情况下仍能获得不错的加速比)
注:个人PC仅有8个计算核心,因而9核模拟的效果稍差