Advertisement
Guest User

LanzochMPI

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