YauhenMardan

Untitled

Nov 11th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // CA-Lab3
  4. //
  5. // Created by Zhenya Mordan on 11/7/19.
  6. // Copyright © 2019 Zhenya Mordan. All rights reserved.
  7. //
  8.  
  9. #include <iomanip>
  10. #include <iostream>
  11. #include <string>
  12. #include <ctime>
  13. #include <math.h>
  14. #include <fstream>
  15. #include "mpi.h"
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. void allocateMemMatrix(float**& A, int n);
  21. void allocateMemMatrix(float**& A, int m, int n);
  22. void fillMatrix(float** A, int n);
  23. void fillMatrixAs1(float** A, int n);
  24. void writeMatrix(const char* fileName, const char* name, float** A, int n);
  25. void writeMatrix(const char* fileName, const char* name, float** A, int m, int n);
  26. void solveWithGaussMethod(float **A, float **X, float **B, int n);
  27. void directMove(float **A, float **B, int n);
  28. void reverseMove(float **A, float **X, float **B, int n, int a, int b);
  29.  
  30. using namespace std;
  31. int main(int argc, char** argv) {
  32.  
  33. int n=atoi(argv[1]); //matrix size
  34.  
  35. float **B;
  36. float **A;
  37. float **C;
  38. float **X;
  39.  
  40. //prepare matrix B
  41. allocateMemMatrix(B, n);
  42. fillMatrixAs1(B, n);
  43.  
  44. //prepare matrix A
  45. allocateMemMatrix(A, n);
  46. fillMatrix(A, n);
  47.  
  48. //init mpi
  49. int worldRank;
  50. int worldSize;
  51.  
  52. MPI_Init(&argc, &argv);
  53. MPI_Comm_rank(MPI_COMM_WORLD, &worldRank);
  54. MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
  55. MPI_Status status;
  56.  
  57. int size = n/worldSize;
  58.  
  59. //direct move
  60. if(worldRank==0){
  61. writeMatrix("matrix_A","A", A, n);
  62. writeMatrix("matrix_B","B", B, n);
  63. directMove(A, B, n);
  64. writeMatrix("matrix_A_after_direct_move","A after direct move", A, n);
  65. writeMatrix("matrix_B_after_direct_move","B after direct move", B, n);
  66. }
  67. MPI_Barrier(MPI_COMM_WORLD);
  68. for(int i=0;i<n;i++){
  69. MPI_Bcast(A[i], n, MPI_FLOAT, 0, MPI_COMM_WORLD);
  70. }
  71. for(int i=0;i<n;i++){
  72. MPI_Bcast(B[i], n, MPI_FLOAT, 0, MPI_COMM_WORLD);
  73. }
  74. //reverse move
  75. //prepare matrix X
  76. allocateMemMatrix(X, n,size);
  77. //reverse move
  78. reverseMove(A, X, B, n, worldRank*size, (worldRank+1)*size);
  79. MPI_Barrier(MPI_COMM_WORLD);
  80. //prepare matrix С
  81. allocateMemMatrix(C, n);
  82. for(int i=0;i<n;i++){
  83. MPI_Gather(X[i], size, MPI_FLOAT, C[i], size, MPI_FLOAT, 0, MPI_COMM_WORLD);
  84. }
  85. //finalize MPI
  86. MPI_Barrier(MPI_COMM_WORLD);
  87. MPI_Finalize();
  88. //print runtime
  89. if(worldRank==0){
  90. cout << "Total time = " << clock()/1000.0 << endl;
  91. writeMatrix("matrix_C", "C", C, n);
  92. }
  93. return 0;
  94. }
  95. void allocateMemMatrix(float**& A, int n)
  96. {
  97. A = new float*[n]; //i
  98. for (int j = 0; j < n; j++)
  99. {
  100.  
  101. A[j] = new float[n]; //j
  102. }
  103. }
  104. void allocateMemMatrix(float**& A, int m, int n)
  105. {
  106. A = new float*[m]; //i
  107. for (int j = 0; j < m; j++)
  108. {
  109. A[j] = new float[n]; //j
  110. }
  111. }
  112. void fillMatrix(float** A, int n)
  113. {
  114. srand(time(0));
  115. for (int i = 0; i < n; i++) //set whole matrix
  116. {
  117. for (int j = 0; j < n; j++)
  118. {
  119. A[i][j] = (rand() % 9);
  120. }
  121. }
  122. for (int i = 0; i < n; i++) //set diagonal elements aii
  123. {
  124. A[i][i] = 0;
  125. for (int j = 0; j < n; j++)
  126. {
  127. if (i != j)
  128. {
  129. A[i][i] += A[i][j];
  130. }
  131. }
  132. }
  133. }
  134. void fillMatrixAs1(float** A, int n)
  135. {
  136. for (int i = 0; i < n; i++)
  137. {
  138. for (int j = 0; j < n; j++)
  139. {
  140. if(i==j){
  141. A[i][j] = 1;
  142. }
  143. else{
  144. A[i][j] = 0;
  145. }
  146. }
  147. }
  148. }
  149. void writeMatrix(const char* filename, const char* name, float** A, int n)
  150. {
  151. ofstream ofile(filename);
  152. cout<< "Writting matrix "<< name <<"\n";
  153. if(ofile.is_open()){
  154. for (int j = 0; j < n; j++)
  155. {
  156. for (int i = 0; i < n; i++)
  157. {
  158. ofile << fixed << setprecision(8)<< A[j][i] << "\t\t";
  159. }
  160. ofile << endl;
  161. }
  162. }
  163. else
  164. {
  165. cout<<"Can not write matrix"<< name << " to the file "<< filename<<"\n";
  166. }
  167. ofile.close();
  168. }
  169. void writeMatrix(const char* filename, const char* name, float** A, int m, int n)
  170. {
  171. ofstream ofile(filename);
  172. cout<< "Writting matrix "<< name <<"\n";
  173. if(ofile.is_open()){
  174. for (int j = 0; j < m; j++)
  175. {
  176. for (int i = 0; i < n; i++)
  177. {
  178. ofile << fixed << setprecision(8)<< A[j][i] << "\t\t";
  179. }
  180. ofile << endl;
  181. }
  182. }
  183. else
  184. {
  185. cout<<"Can not write matrix"<< name << " to the file "<< filename<<"\n";
  186. }
  187. ofile.close();
  188. }
  189.  
  190. void directMove(float **A, float **B, int n)
  191. {
  192. cout<<"Direct move...\n";
  193. for (int k = 0; k < n - 1; k++) // k = 1, 2, … , n–1:
  194. {
  195. for (int i = k + 1; i < n; i++) //i = k + 1, k + 2, …, n:
  196. {
  197. float l = A[i][k] / A[k][k]; // temp l[i][k]= A[i][k]/A[k][k]
  198.  
  199. for(int p=0;p<n; p++){
  200. B[i][p] = B[i][p] - l * B[k][p];
  201. }
  202. for (int j = k; j < n; j++) //j = k + 1, k + 2, …, n:
  203. {
  204. A[i][j] = A[i][j] - l * A[k][j];
  205. }
  206. }
  207. }
  208. }
  209. void reverseMove(float **A, float **X, float **B, int n,int a, int b)
  210. {
  211. cout<<"Reverse move...\n";
  212. for(int p=0;p<b-a;p++){
  213. X[n - 1][p] = B[n - 1][p+a] / A[n - 1][n - 1];
  214. for (int i = n - 2; i > -1; i--) //i=n–1, n–2,...,1.
  215. {
  216. float l = 0; //l = sum(A [i][j]X[j]) below
  217. for (int j = i + 1; j < n; j++) //for j=i+1 to n
  218. {
  219. l += A[i][j] * X[j][p];
  220. }
  221. X[i][p] = (B[i][p+a] - l) / A[i][i];
  222. }
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment