Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cmath>
  5. #include <mpi.h>
  6.  
  7. using namespace std;
  8.  
  9. void readArraysFromFile(string filename, double* aMatrix, double* fMatrix, int size) {
  10. ifstream fin(filename);
  11. int m, n;
  12. fin >> m >> n;
  13.  
  14. m = 0;
  15. n = 0;
  16. for (int i = 0; i < size * (size + 1); ++i) {
  17. if (i == size * (m + 1) + m) {
  18. fin >> fMatrix[m];
  19. m++;
  20. } else {
  21. fin >> aMatrix[n];
  22. n++;
  23. }
  24. }
  25. }
  26.  
  27. void readArrayFromFile(string filename, double* xMatrix) {
  28. ifstream fin(filename);
  29. int m;
  30. fin >> m;
  31.  
  32. for (int i = 0; i < m; ++i) {
  33. fin >> xMatrix[i];
  34. }
  35. }
  36.  
  37. void createMatrixFile(string filename, int m, int n) {
  38. ofstream fout;
  39. fout.open(filename);
  40. fout << m;
  41. if (n != 1){
  42. fout << " " << n;
  43. }
  44. fout << endl;
  45.  
  46. for (int i = 0; i < m; i++) {
  47. for (int j = 0; j < n; j++) {
  48. if (i == j) {
  49. fout << rand() % 9 + m * 10 << " ";
  50. } else {
  51. fout << rand() % 9 + 1 << " ";
  52. }
  53. }
  54. fout << endl;
  55. }
  56. }
  57.  
  58. void writeMatrixInFile(string filename, double* matrix, int size) {
  59. ofstream fout;
  60. fout.open(filename);
  61. fout << size << endl;
  62.  
  63. for (int i = 0; i < size; i++ ) {
  64. fout << matrix[i] << endl;
  65. }
  66. }
  67.  
  68. double* Jacobi (int N, double* A, double* F, double* X, double eps, int M, int rank, int* receiveCount, int* displs)
  69. {
  70. auto* TempX = new double[N];
  71. double norm = 0.0;
  72. double globalNorm = 0.0;
  73.  
  74. do {
  75. for (int i = 0; i < N; i++) {
  76. TempX[i] = F[i];
  77. for (int g = 0; g < M; g++) {
  78. if (i + rank * N != g) {
  79. TempX[i] -= A[i * M + g] * X[g];
  80. }
  81. }
  82. TempX[i] /= A[i * M + i + displs[rank]];
  83. }
  84.  
  85. norm = fabs(X[displs[rank]] - TempX[0]);
  86. for (int h = 0; h < N; h++) {
  87. if (fabs(X[h + displs[rank]] - TempX[h]) > norm) {
  88. norm = fabs(X[h + displs[rank]] - TempX[h]);
  89. }
  90. X[h + displs[rank]] = TempX[h];
  91. }
  92.  
  93. for (int i = 0; i < N; i++) {
  94. //cout << rank << " " << TempX[i] << endl;
  95. }
  96.  
  97. MPI_Allgatherv(TempX, N, MPI_DOUBLE, X, receiveCount, displs, MPI_DOUBLE, MPI_COMM_WORLD);
  98. if (X[0] > -200 && X[0] < 200 && rank == 0) {
  99. for (int i = 0; i < M; i++) {
  100. //cout << rank << " " << X[i] << endl;
  101. }
  102. //cout << endl;
  103. }
  104. MPI_Reduce(&norm, &globalNorm, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  105. MPI_Bcast(&globalNorm, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  106. norm = globalNorm;
  107.  
  108. } while (norm > eps);
  109. delete[] TempX;
  110. return X;
  111. }
  112.  
  113. int main(int argc, char** argv) {
  114. int m = atoi(argv[1]);
  115. string aMatrixFile = argv[2];
  116. string matrixFile = argv[3];
  117. string answerFile = argv[4];
  118. double eps = atof(argv[5]);
  119.  
  120. createMatrixFile(aMatrixFile, m, m + 1);
  121. createMatrixFile(matrixFile, m, 1);
  122.  
  123. MPI_Init(&argc, &argv);
  124. auto* aMatrix = new double[m * m]; // матрица коэфициентов
  125. auto* fMatrix = new double[m]; // свободные члены
  126. auto* xMatrix = new double[m]; // начальное приближение
  127.  
  128. double time = 0.0; // счетчик времени
  129.  
  130. int rank;
  131. int worldSize;
  132. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  133. MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
  134.  
  135. if (rank == 0) {
  136. readArraysFromFile(aMatrixFile, aMatrix, fMatrix, m);
  137. readArrayFromFile(matrixFile, xMatrix);
  138. time = MPI_Wtime();
  139. }
  140. MPI_Bcast(xMatrix, m, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  141.  
  142. auto* sendcountsA = new int[worldSize];
  143. auto* sendcountsF = new int[worldSize];
  144. auto* displsA = new int[worldSize];
  145. auto* displsF = new int[worldSize];
  146. int remainder = m % worldSize;
  147. int sum = 0;
  148.  
  149. for (int i = 0; i < worldSize; i++) {
  150. sendcountsF[i] = m / worldSize;
  151. if (remainder > 0) {
  152. sendcountsF[i]++;
  153. remainder--;
  154. }
  155. sendcountsA[i] = sendcountsF[i] * m;
  156. displsF[i] = sum;
  157. displsA[i] = sum * m;
  158. sum += sendcountsF[i];
  159. }
  160.  
  161. auto* subAMatrix = new double[sendcountsA[rank]]; // подматрица коэфициентов
  162. auto* subFMatrix = new double[sendcountsF[rank]]; // подматрица свободных членов
  163.  
  164. MPI_Scatterv(aMatrix, sendcountsA, displsA, MPI_DOUBLE, subAMatrix, sendcountsA[rank], MPI_DOUBLE, 0, MPI_COMM_WORLD);
  165. MPI_Scatterv(fMatrix, sendcountsF, displsF, MPI_DOUBLE, subFMatrix, sendcountsF[rank], MPI_DOUBLE, 0, MPI_COMM_WORLD);
  166.  
  167. for (int i = 0; i < sendcountsA[rank]; i++) {
  168. cout << rank << " " << subAMatrix[i] << endl;
  169. }
  170. for (int i = 0; i < sendcountsF[rank]; i++) {
  171. //cout << rank << " " << subFMatrix[i] << endl;
  172. }
  173.  
  174. xMatrix = Jacobi(sendcountsF[rank], subAMatrix, subFMatrix, xMatrix, eps, m, rank, sendcountsF, displsF);
  175.  
  176. if (rank == 0) {
  177. time = MPI_Wtime() - time;
  178. cout << time << endl;
  179. writeMatrixInFile(answerFile, xMatrix, m);
  180. }
  181.  
  182. MPI_Finalize();
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement