-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
177 lines (143 loc) · 5.45 KB
/
Copy pathB.cpp
File metadata and controls
177 lines (143 loc) · 5.45 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <vector>
#include <chrono>
#include <random>
#include <iostream>
#include <mpi.h>
using namespace std;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
class Matrix {
public:
Matrix(int rows, int cols)
: rows_(rows), cols_(cols), data_(rows * cols) {}
int* data() { return data_.data(); }
const int* data() const { return data_.data(); }
int& operator()(int row, int col) {
return data_[row * cols_ + col];
}
const int& operator()(int row, int col) const {
return data_[row * cols_ + col];
}
int rows() const { return rows_; }
int cols() const { return cols_; }
void resize(int rows, int cols) {
rows_ = rows;
cols_ = cols;
data_.resize(rows * cols);
}
private:
int rows_, cols_;
vector<int> data_;
};
void local_matmul_optimized(const Matrix& A, const Matrix& B, Matrix& C) {
const int K = A.cols();
const int M = B.cols();
const int A_rows = A.rows();
fill(C.data(), C.data() + C.rows()*C.cols(), 0);
#pragma omp parallel for num_threads(64) collapse(2) if (A_rows*M > 10000)
for (int i = 0; i < A_rows; ++i) {
for (int j = 0; j < M; ++j) {
int sum = 0;
for (int k = 0; k < K; ++k) {
sum += A(i, k) * B(k, j);
}
C(i, j) = sum;
}
}
}
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 && argc < 4) {
cerr << "Usage: " << argv[0]
<< " <testcase_count> [N K M]...\n";
MPI_Abort(MPI_COMM_WORLD, 1);
}
int testcase_count = 0;
vector<int> params;
if (rank == 0) {
testcase_count = atoi(argv[1]);
params.reserve(3 * testcase_count);
for (int i = 0; i < testcase_count; ++i) {
params.push_back(atoi(argv[2 + 3*i]));
params.push_back(atoi(argv[3 + 3*i]));
params.push_back(atoi(argv[4 + 3*i]));
}
}
MPI_Bcast(&testcase_count, 1, MPI_INT, 0, MPI_COMM_WORLD);
params.resize(3 * testcase_count);
MPI_Bcast(params.data(), 3 * testcase_count, MPI_INT, 0, MPI_COMM_WORLD);
vector<int> counts(size), displs(size);
vector<int> send_counts(size), send_displs(size);
for (int t = 0; t < testcase_count; ++t) {
const int N = params[3*t];
const int K = params[3*t+1];
const int M = params[3*t+2];
Matrix A_local(0, K), B(K, M), C_local(0, M);
if (rank == 0) {
int base = N / size;
int remainder = N % size;
for (int i = 0; i < size; ++i) {
counts[i] = base + (i < remainder ? 1 : 0);
displs[i] = (i == 0) ? 0 : displs[i-1] + counts[i-1];
send_counts[i] = counts[i] * K;
send_displs[i] = displs[i] * K;
}
}
MPI_Bcast(counts.data(), size, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
Matrix A_global(N, K);
Matrix B_global(K, M);
uniform_int_distribution<int> dist(0, 99);
for (int i = 0; i < N*K; ++i)
A_global.data()[i] = dist(RNG);
for (int i = 0; i < K*M; ++i)
B_global.data()[i] = dist(RNG);
A_local.resize(counts[0], K);
memcpy(A_local.data(), A_global.data(), counts[0]*K*sizeof(int));
MPI_Request* requests = new MPI_Request[size-1];
for (int i = 1; i < size; ++i) {
MPI_Isend(A_global.data() + send_displs[i], send_counts[i],
MPI_INT, i, 0, MPI_COMM_WORLD, &requests[i-1]);
}
// 等待所有发送完成
MPI_Waitall(size-1, requests, MPI_STATUSES_IGNORE);
delete[] requests;
// 同步广播B的数据
MPI_Bcast(B_global.data(), K*M, MPI_INT, 0, MPI_COMM_WORLD);
B = B_global;
} else {
const int local_rows = counts[rank];
A_local.resize(local_rows, K);
// 接收A的数据
MPI_Recv(A_local.data(), local_rows*K, MPI_INT,
0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// 同步接收B的广播
MPI_Bcast(B.data(), K*M, MPI_INT, 0, MPI_COMM_WORLD);
}
C_local.resize(counts[rank], M);
MPI_Barrier(MPI_COMM_WORLD);
auto start = chrono::high_resolution_clock::now();
local_matmul_optimized(A_local, B, C_local);
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
Matrix C_global(N, M);
vector<int> recv_counts(size), recv_displs(size);
if (rank == 0) {
for (int i = 0; i < size; ++i) {
recv_counts[i] = counts[i] * M;
recv_displs[i] = (i == 0) ? 0 : recv_displs[i-1] + recv_counts[i-1];
}
}
MPI_Gatherv(C_local.data(), counts[rank]*M, MPI_INT,
C_global.data(), recv_counts.data(), recv_displs.data(),
MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
cout << "Testcase " << t+1 << ": Processes=" << size
<< ", Time=" << duration.count() << " ms" << endl;
}
}
MPI_Finalize();
return 0;
}