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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
| #include <mpi.h> #include <iostream> #include <fstream> #include <cmath> #include <vector> #include <cstdlib> #include <algorithm>
using namespace std;
void read_matrix(const string& filename, int& n, vector<double>& values, vector<int>& row_ptr, vector<int>& col_idx) { ifstream fin(filename.c_str()); if (!fin) { cerr << "Failed to open matrix file: " << filename << endl; exit(1); } fin >> n; int nnz; fin >> nnz; values.resize(nnz); row_ptr.resize(n + 1); col_idx.resize(nnz); for (int i = 0; i <= n; i++) fin >> row_ptr[i]; for (int i = 0; i < nnz; i++) fin >> col_idx[i]; for (int i = 0; i < nnz; i++) fin >> values[i]; fin.close(); }
void read_vector(const string& filename, int n, vector<double>& b) { ifstream fin(filename.c_str()); if (!fin) { cerr << "Failed to open vector file: " << filename << endl; exit(1); } b.resize(n); for (int i = 0; i < n; i++) fin >> b[i]; fin.close(); }
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 (argc != 3) { if (rank == 0) cerr << "Usage: " << argv[0] << " matrix.txt vector.txt" << endl; MPI_Finalize(); return 1; }
string matrix_file = argv[1]; string vector_file = argv[2];
int n = 0; vector<double> values; vector<int> row_ptr, col_idx; vector<double> b;
if (rank == 0) { read_matrix(matrix_file, n, values, row_ptr, col_idx); read_vector(vector_file, n, b); }
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
vector<double> x(n, 0.0); vector<double> r(n); vector<double> p(n); vector<double> Ap(n); vector<double> b_local(n);
if (rank == 0) { for (int i = 0; i < n; i++) b_local[i] = b[i]; } MPI_Bcast(b_local.data(), n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
int nnz = 0; if (rank == 0) nnz = values.size(); MPI_Bcast(&nnz, 1, MPI_INT, 0, MPI_COMM_WORLD); if (rank != 0) { values.resize(nnz); row_ptr.resize(n + 1); col_idx.resize(nnz); } MPI_Bcast(values.data(), nnz, MPI_DOUBLE, 0, MPI_COMM_WORLD); MPI_Bcast(row_ptr.data(), n + 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(col_idx.data(), nnz, MPI_INT, 0, MPI_COMM_WORLD);
int rows_per_proc = n / size; int remainder = n % size; int start_row = rank * rows_per_proc + min(rank, remainder); int end_row = start_row + rows_per_proc + (rank < remainder ? 1 : 0);
vector<int> recv_counts(size), displs(size); int total = 0; for (int i = 0; i < size; i++) { int rows = rows_per_proc + (i < remainder ? 1 : 0); recv_counts[i] = rows; displs[i] = total; total += rows; }
for (int i = 0; i < n; i++) { x[i] = 0.0; r[i] = b_local[i]; p[i] = b_local[i]; }
double rho0 = 0.0; for (int i = 0; i < n; i++) rho0 += r[i] * r[i]; double global_rho0; MPI_Allreduce(&rho0, &global_rho0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
double tol = 1e-8; int max_iter = 1000; if (sqrt(global_rho0) < tol) { if (rank == 0) cout << "Initial residual satisfies tolerance" << endl; MPI_Finalize(); return 0; }
double rho_old = global_rho0; int iter; bool converged = false;
double total_compute_time = 0.0; double total_comm_time = 0.0; double start_time = MPI_Wtime();
for (iter = 0; iter < max_iter; iter++) { double compute_start = MPI_Wtime(); vector<double> Ap_local(n, 0.0); for (int i = start_row; i < end_row; i++) { double sum = 0.0; for (int j = row_ptr[i]; j < row_ptr[i + 1]; j++) { sum += values[j] * p[col_idx[j]]; } Ap_local[i] = sum; } double compute_end = MPI_Wtime(); total_compute_time += (compute_end - compute_start);
double comm_start = MPI_Wtime(); MPI_Allgatherv(Ap_local.data() + start_row, end_row - start_row, MPI_DOUBLE, Ap.data(), recv_counts.data(), displs.data(), MPI_DOUBLE, MPI_COMM_WORLD); double comm_end = MPI_Wtime(); total_comm_time += (comm_end - comm_start);
compute_start = MPI_Wtime(); double pAp_local = 0.0; for (int i = 0; i < n; i++) pAp_local += p[i] * Ap[i]; compute_end = MPI_Wtime(); total_compute_time += (compute_end - compute_start);
comm_start = MPI_Wtime(); double pAp_global; MPI_Allreduce(&pAp_local, &pAp_global, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); comm_end = MPI_Wtime(); total_comm_time += (comm_end - comm_start);
if (fabs(pAp_global) < 1e-15) { double rho_check = 0.0; for (int i = 0; i < n; i++) rho_check += r[i] * r[i]; double global_rho_check; MPI_Allreduce(&rho_check, &global_rho_check, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); if (sqrt(global_rho_check) < tol) { converged = true; } break; }
double alpha = rho_old / pAp_global;
compute_start = MPI_Wtime(); for (int i = 0; i < n; i++) { x[i] += alpha * p[i]; r[i] -= alpha * Ap[i]; } compute_end = MPI_Wtime(); total_compute_time += (compute_end - compute_start);
compute_start = MPI_Wtime(); double rho_new_local = 0.0; for (int i = 0; i < n; i++) rho_new_local += r[i] * r[i]; compute_end = MPI_Wtime(); total_compute_time += (compute_end - compute_start);
comm_start = MPI_Wtime(); double rho_new; MPI_Allreduce(&rho_new_local, &rho_new, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); comm_end = MPI_Wtime(); total_comm_time += (comm_end - comm_start);
if (sqrt(rho_new) < tol) { converged = true; if (rank == 0) cout << "Converged at iteration " << iter + 1 << endl; break; }
compute_start = MPI_Wtime(); double beta = rho_new / rho_old; for (int i = 0; i < n; i++) { p[i] = r[i] + beta * p[i]; } rho_old = rho_new; compute_end = MPI_Wtime(); total_compute_time += (compute_end - compute_start); }
double end_time = MPI_Wtime(); double total_time = end_time - start_time;
if (rank == 0) { vector<double> Ax(n, 0.0); for (int i = 0; i < n; i++) { double sum = 0.0; for (int j = row_ptr[i]; j < row_ptr[i + 1]; j++) { sum += values[j] * x[col_idx[j]]; } Ax[i] = sum; } double residual = 0.0; for (int i = 0; i < n; i++) { double diff = Ax[i] - b_local[i]; residual += diff * diff; } residual = sqrt(residual); cout << "\n========== 结果 ==========" << endl; cout << "迭代次数: " << iter + 1 << endl; cout << "总计算时间: " << total_time << " 秒" << endl; cout << " - 纯计算时间: " << total_compute_time << " 秒 (" << (total_compute_time/total_time*100) << "%)" << endl; cout << " - 通信时间: " << total_comm_time << " 秒 (" << (total_comm_time/total_time*100) << "%)" << endl; cout << "平均每次迭代:" << endl; cout << " - 计算: " << total_compute_time/(iter+1) << " 秒" << endl; cout << " - 通信: " << total_comm_time/(iter+1) << " 秒" << endl; cout << "解向量前10个元素: "; for (int i = 0; i < min(10, n); i++) cout << x[i] << " "; cout << endl; cout << "==========================" << endl; }
MPI_Finalize(); return 0; }
|