Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <fstream>
  5. #include <mpi.h>
  6.  
  7. using namespace std;
  8.  
  9. void init_vec(double *obj,int N, int is_mat) {
  10. srand(time(NULL));
  11. if (is_mat == 0) {
  12. for (int k=0; k<N; ++k) {
  13. obj[k] = rand() % 10;
  14. }
  15. }
  16. else {
  17. for (int k = 0; k < N; ++k) {
  18. for (int p = k; p < N; ++p) {
  19. obj[k*N+p] = rand() % 10;
  20. obj[p*N+k] = obj[k*N+p];
  21. }
  22. }
  23. }
  24. }
  25.  
  26.  
  27. void dotproduct(double *a, double *b, double *res, int N) {
  28. double result = 0;
  29. for (int i = 0; i < N; ++i) {
  30. result += a[i]*b[i];
  31. }
  32. *res = result;
  33. }
  34.  
  35. void constproduct(double *a, double b, double *res, int N) {
  36. for (int i = 0; i < N; ++i) {
  37. res[i] = a[i]*b;
  38. }
  39. }
  40.  
  41. void sum(double *a, double *b, double *res, int N) {
  42. for (int i = 0; i < N; ++i) {
  43. res[i] = a[i] + b[i];
  44. }
  45. }
  46.  
  47. void diff(double *a, double *b, double *res, int N) {
  48. for (int i = 0; i < N; ++i) {
  49. res[i] = a[i] - b[i];
  50. }
  51. }
  52.  
  53. void ort(double *v,double *vec_storage, int curent_iter, int N) {
  54. double *c = (double*)calloc(N, sizeof(double));
  55. double *s = (double*)calloc(N, sizeof(double));
  56. double ress = 0;
  57. for (int j = 0; j<curent_iter;j++) {
  58. dotproduct(v, &vec_storage[N*j], &ress, N);
  59. constproduct(&vec_storage[N*j], ress, c, N);
  60. sum(s, c, s, N);
  61. }
  62. constproduct(s, -1, s, N);
  63. sum(v, s, v, N);
  64. }
  65.  
  66.  
  67. void matvec(double *vec, int N, int loc_rows, double *res, int MAX_ITER) {
  68. double t2 = MPI_Wtime();
  69. double *local_mat = new double[loc_rows*N];
  70. double *local_res = new double[loc_rows];
  71. double *total_res;
  72. double *vec_storage;
  73. int rank;
  74.  
  75. double *a = new double[MAX_ITER];
  76. double *b = new double[MAX_ITER];
  77.  
  78.  
  79. double t3 = MPI_Wtime();
  80. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  81. if (rank == 0) {
  82. vec_storage = new double[MAX_ITER*N];
  83. for (int k = 0; k < N; ++k) {
  84. vec_storage[k] = vec[k];
  85. }
  86. }
  87.  
  88. MPI_Scatter(&mat[0], loc_rows*N, MPI_DOUBLE, &local_mat[0], loc_rows*N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  89. for (int ii = 0; ii < loc_rows; ii++) {
  90. for (int jj =0; jj < N; ++ jj) {
  91. local_mat[ii*N+jj]= (ii*jj) / sqrt(ii*ii + jj*jj);
  92. }
  93. }
  94.  
  95.  
  96. for (int iter = 1; iter < MAX_ITER-1; iter ++) {
  97. MPI_Bcast(&vec[0], N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  98. t3 = MPI_Wtime() - t3;
  99. for (int i =0; i < loc_rows; ++i) {
  100. local_res[i] = 0;
  101. for (int j = 0; j<N;++j) {
  102. local_res[i] += local_mat[i*N+j] * vec[j];
  103. }
  104. }
  105. if (rank == 0) {
  106. total_res = new double[N];
  107. }
  108. MPI_Gather(&local_res[0], loc_rows, MPI_DOUBLE, &total_res[0], loc_rows, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  109. if (rank == 0) {
  110. for (int i = 0; i < N; ++i) {
  111. vec[i] = total_res[i];
  112. }
  113. double *c = new double[N];
  114.  
  115. dotproduct(&vec_storage[N*iter],total_res, &a[iter],N);
  116.  
  117. constproduct(&vec_storage[N*iter], a[iter], c, N);
  118. diff(total_res,c,total_res,N);
  119.  
  120. if (iter!=0) {
  121. constproduct(&vec_storage[N*(iter-1)],b[iter],c,N);
  122. diff(total_res ,c,total_res ,N);
  123. }
  124. MPI_Bcast(&vec[0], N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  125. ort(total_res,vec_storage,iter,N);
  126. ort(total_res,vec_storage,iter,N);
  127. double d;
  128. dotproduct(total_res,total_res,&d,N);
  129. b[iter+1]=sqrt(d);
  130. if (b[iter+1]==0) {
  131. break;
  132. }
  133. constproduct(total_res,1/b[iter+1], &vec_storage[N*(iter+1)],N);
  134. }
  135. }
  136. if (rank == 0) {
  137. delete[] vec_storage;
  138. delete[] total_res;
  139.  
  140. }
  141. delete[] local_res;
  142. delete[] local_mat;
  143. t2 = MPI_Wtime()- t2;
  144.  
  145. }
  146.  
  147. int main(int argc, char* argv[]) {
  148. MPI_Init(&argc,&argv);
  149.  
  150. int size = 0;
  151. int rank = 0;
  152. MPI_Comm_size(MPI_COMM_WORLD, &size);
  153. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  154. ofstream fout("res.txt");
  155. for (int MAX_IT = 42; MAX_IT < 50; MAX_IT +=2) {
  156. for (int dim = 4992; dim <= 4992; dim += 4992) {
  157. double t1 = MPI_Wtime();
  158. int N = 20000;
  159. N = dim;
  160. int *send;
  161. double *mat;
  162. double *vec = new double[N];
  163. double *result;
  164.  
  165. if (N % size != 0) {
  166. MPI_Abort(MPI_COMM_WORLD,10);
  167. fout << "SIZE mod N_processors != 0" << endl;
  168. return(-1);
  169. }
  170. int mat_s;
  171. if (rank == 0) {
  172. mat_s = N*N;
  173. int elems_per_proc = mat_s / size;
  174. init_vec(vec, N, 0);
  175. result = new double[N];
  176. }
  177. matvec(vec, N, N/size, result, MAX_IT);
  178. if (rank == 0) {
  179. t1 = MPI_Wtime()- t1;
  180. delete[] vec;
  181. delete[] result;
  182. fout << size << "," <<dim << "," << MAX_IT << "," <<t1 << endl;
  183. }
  184.  
  185. }
  186. }
  187. MPI_Finalize();
  188. fout.close();
  189. return(0);
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement