我试图创建一个2D数组,然后使用mpi将其分成两个较小的矩阵,互换2列,然后再次组装矩阵。所以我的初始矩阵是:
0 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40 41 42 43
44 45 46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63 64 65
66 67 68 69 70 71 72 73 74 75 76
77 78 79 80 81 82 83 84 85 86 87
88 89 90 91 92 93 94 95 96 97 98
99 100 101 102 103 104 105 106 107 108 109
110111112113114115116117118119120
但是我的最后一个重复了5次两次。
我使用的代码是:
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <iomanip>
#include "mpi.h"
using namespace std;
int main(int argc, char* argv[]) {
int gridpointsx = 11;
int gridpointsy = 11;
int L=10;
int Np=2;
int s,r;
vector <double> x,y;
vector<vector<double> > u;
/*populate x and y */
int vector_sizex = gridpointsx;
int vector_sizey = gridpointsy;
x.resize(vector_sizex);
y.resize(vector_sizey);
u.resize(gridpointsx);
//cout<<size<<endl;
float dx = L/(gridpointsx-1);
float numberForVector=-L/2;
for(int i = 0; i < x.size(); i++){
x[i] = numberForVector;
y[i] = numberForVector;
numberForVector += dx;
}
//cout<<"done"<<endl;
for(int i=0; i<gridpointsx; i++)
{
for(int j=0; j<gridpointsx; j++)
{
u[i].push_back(0);
}
}
int k =0;
for(int i=0;i<gridpointsx;i++)
{
//cout<<"i:"<<i<<endl;
for(int j=0;j<gridpointsy;j++)
{
u[i][j] = k;
k++;
}
}
ofstream MatrixInit;
MatrixInit.open("MatrixInit.txt");
cout << fixed;
cout << setprecision(4);
for (int i = 0; i < gridpointsx; i++)
{
for (int j = 0; j < gridpointsy; j++)
{
MatrixInit << u[i][j] << "\t\n" [j == gridpointsy-1];
}
MatrixInit << endl;
}
MatrixInit.close();
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &r);
MPI_Comm_size(MPI_COMM_WORLD, &s);
MPI_Datatype vectory;
MPI_Type_vector(gridpointsy, 1, 6, MPI_DOUBLE, &vectory);
MPI_Type_commit(&vectory);
double u_local[gridpointsy][(gridpointsx+1)/2];
int t=r*4;
for(int row = 0;row<gridpointsy;row++)
{
for(int col = 0; col<(gridpointsx+1)/2; col++)
{
u_local[row][col] = u[row][t+col];
}
}
if (r==0){
ofstream Matrix1;
Matrix1.open("Matrix1.txt");
cout << fixed;
cout << setprecision(4);
for (int row = 0; row < gridpointsy; row++)
{
for (int col = 0; col < (gridpointsx+1)/2; col++)
{
Matrix1 << u_local[row][col] << "\t\n" [col == (gridpointsx+1)/2-1];
}
Matrix1 << endl;
}
Matrix1.close();
MPI_Send(&u_local[0][(gridpointsx-1)/2], 1, vectory, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&u_local[0][(gridpointsx+1)/2], 1, vectory, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//MPI_Send(&v_local[0][(gridpointsx-2)/2], 1, vectory, 1, 0, MPI_COMM_WORLD);
//MPI_Recv(&v_local[0][0], 1, vectory, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
else if (r==1){
ofstream Matrix2;
Matrix2.open("Matrix2.txt");
cout << fixed;
cout << setprecision(4);
for (int row = 0; row < gridpointsy; row++)
{
for (int col = 0; col < (gridpointsx+1)/2; col++)
{
Matrix2 << u_local[row][col] << "\t\n" [col == (gridpointsx+1)/2-1];
}
Matrix2 << endl;
}
Matrix2.close();
MPI_Recv(&u_local[0][0], 1, vectory, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Send(&u_local[0][1], 1, vectory, 0, 0, MPI_COMM_WORLD);
//MPI_Recv(&v_local[0][(gridpointsx)/2], 1, vectory, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//MPI_Send(&v_local[0][1], 1, vectory, 0, 0, MPI_COMM_WORLD);
}
for(int row = 0;row<gridpointsy;row++)
{
for(int col = 0; col<(gridpointsx+3)/2; col++)
{
u[row][(4*r)+col] = u_local[row][col];
//v[row][t+col] = v_local[row][col];
}
}
MPI_Finalize();
ofstream Matrixf;
Matrixf.open("MatrixF.txt");
cout << fixed;
cout << setprecision(4);
for (int i = 0; i < gridpointsx; i++)
{
for (int j = 0; j < gridpointsy; j++)
{
Matrixf << u[i][j] << "\t\n" [j == gridpointsy-1];
}
Matrixf << endl;
}
Matrixf.close();
}