-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.cpp
143 lines (134 loc) · 3.37 KB
/
01.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <mpi.h>
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
const int N = 1000, M = N;
int getSize(int dataSize, int rank, int size)
{
return dataSize / size + (rank < dataSize % size ? 1 : 0);
}
int getOffset(int dataSize, int rank, int size)
{
return rank < dataSize % size ? (dataSize / size + 1) * rank :
(dataSize - (dataSize / size) * (size - rank));
}
void fill_A(vector<int> &row, int nRow)
{
for (int i = 0; i < row.size(); i++)
row[i] = nRow == i ? 2 : (nRow - 1 == i || nRow + 1 == i ? 1 : 0);
}
void fill_x(int &xElem, int n)
{
xElem = 1;
}
void mulPart(int rank, int size, vector<vector<int> > &A, vector<int> &_x, vector<int> &res)
{
int initSize = _x.size();
int blockSize = getSize(N, rank, size);
int blockOffset = getOffset(N, rank, size);
const int rowCount = blockSize;
_x.resize(N / size + 1);
int nPrev = (rank - 1 + size) % size;
int nNext = (rank + 1) % size;
if (size == 1)
for (int i = 0; i < size; i++)
for (int j = 0; j < rowCount; j++)
for (int k = 0; k < blockSize; k++)
res[j] += A[j][blockOffset + k] * _x[k];
else
{
vector<double> __x(N / size + 1);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < rowCount; j++)
for (int k = 0; k < blockSize; k++)
res[j] += A[j][blockOffset + k] * _x[k];
int curSize = blockSize;
blockSize = getSize(N, (nPrev - i + size) % size, size);
blockOffset = getOffset(N, (nPrev - i + size) % size, size);
MPI_Sendrecv(&_x[0], curSize, MPI_DOUBLE, nNext, 0,
&__x[0], blockSize, MPI_DOUBLE, nPrev, 0,
MPI_COMM_WORLD, MPI_STATUS_IGNORE);
_x = __x;
}
}
_x.resize(initSize);
}
void show()
{
cout<<"A:\n";
for (int i = 0; i < M; i++)
{
vector<int> row(N);
fill_A(row, i);
for (int j = 0; j < N; j++)
cout<<setw(2)<<row[j]<<" ";
cout<<endl;
}
cout<<"x:\n";
vector<int> x(N);
for (int i = 0; i < N; i++)
{
fill_x(x[i], i);
cout<<setw(2)<<x[i]<<" ";
}
cout<<endl;
}
int main(int argc, char **argv)
{
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
//if (rank == 0)
// show();
int rowCount = getSize(M, rank, size);
int rowOffset = getOffset(M, rank, size);
vector<vector<int> > A(rowCount);
vector<int> _b(rowCount);
for (int i = 0; i < rowCount; i++)
{
A[i].resize(N);
fill_A(A[i], i + rowOffset);
}
int xSize = getSize(N, rank, size);
int xOffset = getOffset(N, rank, size);
vector<int> _x(xSize);//
for (int i = 0; i < xSize; i++)
fill_x(_x[i], i + xOffset);
double t = MPI_Wtime();
mulPart(rank, size, A, _x, _b);
t = MPI_Wtime() - t;
for (int i = 0; i < size; i++)
{
if (i == rank)
cout<<"rank"<<i<<": "<<t * 1000<<"ms"<<endl;
MPI_Barrier(MPI_COMM_WORLD);
}
return 0;
if (rank != 0)
MPI_Gatherv(&_b[0], rowCount, MPI_INT,
NULL, NULL, NULL, MPI_INT, 0, MPI_COMM_WORLD);
else
{
vector<int> b(M);
vector<int> recvcounts(size);
vector<int> displs(size);
recvcounts[0] = getSize(M, 0, size);
for (int i = 1; i < size; i++)
{
recvcounts[i] = getSize(M, i, size);
displs[i] = displs[i - 1] + recvcounts[i - 1];
}
MPI_Gatherv(&_b[0], rowCount, MPI_INT,
&b[0], &recvcounts[0], &displs[0], MPI_INT,
0, MPI_COMM_WORLD);
cout<<"Ax:"<<endl;
for (int i = 0; i < M; i++)
cout<<setw(2)<<b[i]<<" ";
cout<<endl;
}
MPI_Finalize();
return 0;
}