Я пытаюсь создать двумерный массив, затем разделить его на два меньших, переставить 2 столбца, используя mpi, а затем снова собрать матрицу. Итак, моя исходная матрица:
0 1 2 3 4 5 6 78 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 3940 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 7172 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
99 100 101 102 103104 105 106 107 108 109
110 111 112 113 114 115 116 117 118 119 120
, но мой последний повторяет столбец 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();
}