YauhenMardan

Untitled

Nov 9th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 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=4; //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. if(worldRank==0){
  69. //send A to the all processes
  70. for(int w=0;w<worldSize;w++){
  71. for(int i=0;i<n;i++){
  72. MPI_Send(A[i],n,MPI_FLOAT,w,10,MPI_COMM_WORLD);
  73. }
  74. }
  75. }
  76. //recieve A
  77. for(int i=0;i<n;i++){
  78. MPI_Recv(A[i], n, MPI_FLOAT, 0, 10, MPI_COMM_WORLD, &status);
  79. }
  80. if(worldRank==0){
  81. //send B to the all processes
  82. for(int w=0;w<worldSize;w++){
  83. for(int i=0;i<n;i++){
  84. MPI_Send(B[i],n,MPI_FLOAT,w,10,MPI_COMM_WORLD);
  85. }
  86. }
  87. }
  88. //recieve B
  89. for(int i=0;i<n;i++){
  90. MPI_Recv(B[i], n, MPI_FLOAT, 0, 10, MPI_COMM_WORLD, &status);
  91. }
  92. //reverse move
  93. //prepare matrix X
  94. allocateMemMatrix(X, n,size);
  95. //reverse move
  96. reverseMove(A, X, B, n, worldRank*size, (worldRank+1)*size);
  97. MPI_Barrier(MPI_COMM_WORLD);
  98. //DEBUG
  99. string name="X";
  100. name.append(std::to_string(worldRank));
  101. ofstream ofile(name);
  102. if(ofile.is_open()){
  103. for (int j = 0; j < n; j++)
  104. {
  105. for (int i = 0; i < size; i++)
  106. {
  107. ofile << fixed << setprecision(8)<< X[j][i] << "\t\t";
  108. }
  109. ofile << endl;
  110. }
  111. }
  112. ofile.close();
  113. //DEBUG end
  114. //prepare matrix С
  115. allocateMemMatrix(C, n);
  116. for(int i=0;i<n;i++){
  117. MPI_Send(X[i],size,MPI_FLOAT,0,10,MPI_COMM_WORLD);
  118. }
  119. if(worldRank==0){
  120. for(int w=0;w<worldSize;w++){
  121. for(int i=0;i<n;i++){
  122. MPI_Recv(X[i], size, MPI_FLOAT, w, 10, MPI_COMM_WORLD, &status);
  123. }
  124. for(int k=0;k<size;k++){
  125. for(int j=0;j<n;j++){
  126. C[j][k+size*w]=X[j][k];
  127. }
  128. }
  129. }
  130. }
  131. //finalize MPI
  132. MPI_Barrier(MPI_COMM_WORLD);
  133. MPI_Finalize();
  134. //print runtime
  135. if(worldRank==0){
  136. cout << "Total time = " << clock()/1000.0 << endl;
  137. writeMatrix("matrix_C", "C", C, n);
  138. }
  139. return 0;
  140. }
  141. void allocateMemMatrix(float**& A, int n)
  142. {
  143. A = new float*[n]; //i
  144. for (int j = 0; j < n; j++)
  145. {
  146.  
  147. A[j] = new float[n]; //j
  148. }
  149. }
  150. void allocateMemMatrix(float**& A, int m, int n)
  151. {
  152. A = new float*[m]; //i
  153. for (int j = 0; j < m; j++)
  154. {
  155. A[j] = new float[n]; //j
  156. }
  157. }
  158. void fillMatrix(float** A, int n)
  159. {
  160. srand(time(0));
  161. for (int i = 0; i < n; i++) //set whole matrix
  162. {
  163. for (int j = 0; j < n; j++)
  164. {
  165. A[i][j] = (rand() % 9);
  166. }
  167. }
  168. for (int i = 0; i < n; i++) //set diagonal elements aii
  169. {
  170. A[i][i] = 0;
  171. for (int j = 0; j < n; j++)
  172. {
  173. if (i != j)
  174. {
  175. A[i][i] += A[i][j];
  176. }
  177. }
  178. }
  179. }
  180. void fillMatrixAs1(float** A, int n)
  181. {
  182. for (int i = 0; i < n; i++)
  183. {
  184. for (int j = 0; j < n; j++)
  185. {
  186. if(i==j){
  187. A[i][j] = 1;
  188. }
  189. else{
  190. A[i][j] = 0;
  191. }
  192. }
  193. }
  194. }
  195. void writeMatrix(const char* filename, const char* name, float** A, int n)
  196. {
  197. ofstream ofile(filename);
  198. cout<< "Writting matrix "<< name <<"\n";
  199. if(ofile.is_open()){
  200. for (int j = 0; j < n; j++)
  201. {
  202. for (int i = 0; i < n; i++)
  203. {
  204. ofile << fixed << setprecision(8)<< A[j][i] << "\t\t";
  205. }
  206. ofile << endl;
  207. }
  208. }
  209. else
  210. {
  211. cout<<"Can not write matrix"<< name << " to the file "<< filename<<"\n";
  212. }
  213. ofile.close();
  214. }
  215. void writeMatrix(const char* filename, const char* name, float** A, int m, int n)
  216. {
  217. ofstream ofile(filename);
  218. cout<< "Writting matrix "<< name <<"\n";
  219. if(ofile.is_open()){
  220. for (int j = 0; j < m; j++)
  221. {
  222. for (int i = 0; i < n; i++)
  223. {
  224. ofile << fixed << setprecision(8)<< A[j][i] << "\t\t";
  225. }
  226. ofile << endl;
  227. }
  228. }
  229. else
  230. {
  231. cout<<"Can not write matrix"<< name << " to the file "<< filename<<"\n";
  232. }
  233. ofile.close();
  234. }
  235.  
  236. void directMove(float **A, float **B, int n)
  237. {
  238. cout<<"Direct move...\n";
  239. for (int k = 0; k < n - 1; k++) // k = 1, 2, … , n–1:
  240. {
  241. for (int i = k + 1; i < n; i++) //i = k + 1, k + 2, …, n:
  242. {
  243. float l = A[i][k] / A[k][k]; // temp l[i][k]= A[i][k]/A[k][k]
  244.  
  245. for(int p=0;p<n; p++){
  246. B[i][p] = B[i][p] - l * B[k][p];
  247. }
  248. for (int j = k; j < n; j++) //j = k + 1, k + 2, …, n:
  249. {
  250. A[i][j] = A[i][j] - l * A[k][j];
  251. }
  252. }
  253. }
  254. }
  255. //void reverseMove(float **A, float **X, float **B, int n,int a, int b)
  256. //{
  257. // cout<<"Reverse move...\n";
  258. // for(int p=0;p<b-a;p++){
  259. // X[n - 1][p] = B[n - 1][p+a] / A[n - 1][n - 1];
  260. // for (int i = n - 2; i > -1; i--) //i=n–1, n–2,...,1.
  261. // {
  262. // float l = 0; //l = sum(A [i][j]X[j]) below
  263. // for (int j = i + 1; j < n; j++) //for j=i+1 to n
  264. // {
  265. // l += A[i][j] * X[j][p];
  266. // }
  267. // X[i][p] = (B[i][p+a] - l) / A[i][i];
  268. // }
  269. // }
  270. //}
  271.  
  272. void reverseMove(float **A, float **X, float **B, int n,int a, int b)
  273. {
  274. writeMatrix("matrix_B_DEBUG", "Debug B", B, n);
  275. //DEBUG
  276. string name="XAAA";
  277. name.append(std::to_string(a));
  278. ofstream ofile(name);
  279. cout<<"Reverse move...\n";
  280. ofile<<"a= "<<a<<" b= "<<b<<"\n";
  281. for(int p=0;p<b-a;p++){
  282. X[n - 1][p] = B[n - 1][p+a] / A[n - 1][n - 1];
  283. ofile<<X[n-1][p]<<"="<<B[n - 1][p+a]<<"/"<< A[n - 1][n - 1]<<"\n";
  284. for (int i = n - 2; i > -1; i--) //i=n–1, n–2,...,1.
  285. {
  286. float l = 0; //l = sum(A [i][j]X[j]) below
  287. for (int j = i + 1; j < n; j++) //for j=i+1 to n
  288. {
  289. l += A[i][j] * X[j][p];
  290. }
  291. X[i][p] = (B[i][p+a] - l) / A[i][i];
  292. ofile<<X[i][p]<<"="<<"("<<B[i][p+a]<<"-"<<l<<")"<<"/"<< A[i][i]<<"\n";
  293. }
  294. }
  295. ofile.close();
  296. //DEBUG end
  297. }
Advertisement
Add Comment
Please, Sign In to add comment