SHARE
TWEET

Untitled

a guest Mar 26th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include "mpi.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. // utility functions
  7. int ** alloc_matrix(int n, int m);
  8. void init_matrix(int n, int m, int ** a);
  9. void print_matrix(int n, int m, int ** a);
  10. void init_unit_matrix(int n, int m, int ** a);
  11.  
  12. // matrix operations
  13. int ** prod_matrix(int n, int l, int m, int ** a, int ** b);
  14. int ** trans_matrix(int n, int m, int ** a);
  15. int ** pseudo_prod_matrix(int n, int l, int m, int ** a, int ** b);
  16.  
  17. // MPI functions
  18. int MPI_Prod_matrix(int n, int ** a, int ** b, int ** c, int root, MPI_Comm comm);
  19. int MPI_Pseudo_Prod_matrix(int n, int ** a, int ** b, int ** c, int root, MPI_Comm comm);
  20.  
  21. int main(int argc, char ** argv){
  22.       int size, rank, tag=1, i,j,  n=1200, **a, **b, **c, **a1, **c1;
  23.       double time;
  24.  
  25.     MPI_Status stat;
  26.     MPI_Datatype rowtype;
  27.  
  28.     MPI_Init(&argc,&argv);
  29.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  30.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  31.  
  32.     MPI_Type_contiguous(n, MPI_INT, &rowtype);
  33.     MPI_Type_commit(&rowtype);
  34.  
  35.     a=alloc_matrix(n,n);
  36.     b=alloc_matrix(n,n);
  37.     c=alloc_matrix(n,n);
  38.  
  39.     if (rank == 0) {
  40.  
  41.         // initialise the matrices
  42.  
  43.         init_matrix(n,n,a);
  44.         init_unit_matrix(n,n,b);
  45.  
  46.     }
  47.    
  48.     time = MPI_Wtime();
  49.    
  50.     MPI_Pseudo_Prod_matrix(n, a, trans_matrix(n,n,b), c, 0, MPI_COMM_WORLD);
  51.    
  52.     time = MPI_Wtime() - time;
  53.    
  54.     printf("Processor %d worked for %lf\n\n", rank, time);
  55.    
  56.     if (rank == 0) {
  57.  
  58.         // initialise the matrices
  59.  
  60. //        printf("Matrix a:\n\n");print_matrix(n,n,a);
  61. //        printf("Matrix b:\n\n");print_matrix(n,n,b);
  62. //        printf("Matrix c:\n\n");print_matrix(n,n,c);
  63.        
  64.  
  65.     }
  66.  
  67.  
  68.     MPI_Type_free(&rowtype);
  69.     MPI_Finalize();
  70. }
  71.  
  72.  
  73.  
  74. int MPI_Prod_matrix(int n, int ** a, int ** b, int ** c, int root, MPI_Comm comm)
  75. {
  76.    
  77.     // scatter a, bcast b, compute local_C = local_a * b, gather local_c
  78.    
  79.     int rank,size;
  80.     MPI_Comm_rank(comm,&rank);
  81.     MPI_Comm_size(comm,&size);
  82.     // get rank and size of comm
  83.    
  84.     // alocate space for local_a and local_c
  85.     int ** local_a = alloc_matrix(n/size,n);
  86.     int ** local_c = alloc_matrix(n/size,n);
  87.    
  88.     // scatter a to local_a and bcast b
  89.    
  90.     MPI_Scatter(&a[0][0],n*n/size,MPI_INT, &local_a[0][0],n*n/size,MPI_INT,root,comm);
  91.     MPI_Bcast(&b[0][0], n*n,MPI_INT,root,comm);
  92.    
  93.     // calculate local_c = local_a * b
  94.     local_c = prod_matrix(n/size,n,n,local_a,b);
  95.    
  96.     // gather local_c
  97.     MPI_Gather(&local_c[0][0],n*n/size,MPI_INT,&c[0][0],n*n/size,MPI_INT,root,comm);
  98.    
  99.     return MPI_SUCCESS;
  100.    
  101. }
  102.  
  103.  
  104. int MPI_Pseudo_Prod_matrix(int n, int ** a, int ** b, int ** c, int root, MPI_Comm comm)
  105. {
  106.    
  107.     // scatter a, bcast b, compute local_C = local_a * b, gather local_c
  108.    
  109.     int rank,size;
  110.     MPI_Comm_rank(comm,&rank);
  111.     MPI_Comm_size(comm,&size);
  112.     // get rank and size of comm
  113.    
  114.     // alocate space for local_a and local_c
  115.     int ** local_a = alloc_matrix(n/size,n);
  116.     int ** local_c = alloc_matrix(n/size,n);
  117.    
  118.     // scatter a to local_a and bcast b
  119.    
  120.     MPI_Scatter(&a[0][0],n*n/size,MPI_INT, &local_a[0][0],n*n/size,MPI_INT,root,comm);
  121.     MPI_Bcast(&b[0][0], n*n,MPI_INT,root,comm);
  122.    
  123.     // calculate local_c = local_a * b
  124.     local_c = pseudo_prod_matrix(n/size,n,n,local_a,b);
  125.    
  126.     // gather local_c
  127.     MPI_Gather(&local_c[0][0],n*n/size,MPI_INT,&c[0][0],n*n/size,MPI_INT,root,comm);
  128.    
  129.     return MPI_SUCCESS;
  130.    
  131. }
  132.  
  133.  
  134. /*
  135.  
  136. The function alloc_matrix is to allocate dynamically a matrix of integers with
  137.  n rows and m columns.
  138.  
  139. The function arguments are;
  140.  
  141.     n - number of rows
  142.     m - number of columns
  143.  
  144. The function returns the double pointer representing the matrix.
  145.  
  146.  
  147.  
  148. */
  149.  
  150. int ** alloc_matrix(int n, int m){
  151.  
  152.     int i, j, **a, *aa;
  153.  
  154.     aa=(int *) calloc(n*m, sizeof(int));
  155.     a=(int **) calloc(n, sizeof(int*));
  156.  
  157.     for(i=0;i<n;i++)a[i]=aa+i*m;
  158.  
  159.     for(i=0;i<n;i++)for(j=0;j<m;j++)a[i][j]=0;
  160.  
  161.     return a;
  162. }
  163.  
  164.  
  165. /*
  166.  
  167. The function init_matrix is to initialize the matrix a with random integers.
  168.  
  169. The function arguments are:
  170.  
  171.     n - number of rows
  172.     m - number of columns
  173.     a - the matrix
  174.  
  175.  
  176. */
  177.  
  178.  
  179.  
  180. void init_matrix(int n, int m, int ** a){
  181.  
  182.     int i, j;
  183.  
  184.     for(i=0;i<n;i++)for(j=0;j<m;j++)a[i][j]=rand()%10;
  185.  
  186.  
  187. }
  188.  
  189. void init_unit_matrix(int n, int m, int ** a){
  190.    
  191.     int i, j;
  192.    
  193.     for(i=0;i<n;i++)for(j=0;j<m;j++)a[i][j]= (i==j)?1:0;
  194.    
  195.    
  196. }
  197.  
  198.  
  199. /*
  200.  
  201. The function print_matrix is to display the matrix a.
  202.  
  203. The function arguments are:
  204.  
  205.     n - number of rows
  206.     m - number of columns
  207.     a - the matrix
  208.  
  209.  
  210. */
  211.  
  212.  
  213.  
  214. void print_matrix(int n, int m, int ** a){
  215.  
  216.     int i, j;
  217.  
  218.     printf("\n");
  219.  
  220.     for(i=0;i<n;i++){
  221.  
  222.         for(j=0;j<m;j++)printf("%d ",a[i][j]);
  223.  
  224.         printf("\n");
  225.  
  226.     }
  227.  
  228.  
  229. }
  230.  
  231.  
  232. /*
  233.  
  234. The function prod_matrix is to multiply the matrices a and b.
  235.  
  236. The function arguments are;
  237.  
  238.     n - number of rows of a
  239.     l - number of columns of a // the matrix b must have l rows
  240.     m - number of columns of b
  241.  
  242.     a - the first matrix
  243.     b - the second matrix
  244.  
  245. The function returns the double pointer representing the product matrix.
  246.  
  247.  
  248.  
  249. */
  250.  
  251.  
  252. int ** prod_matrix(int n, int l, int m, int ** a, int ** b){
  253.  
  254.     int i,j,k,** c;
  255.  
  256.     c=alloc_matrix(n,m);
  257.  
  258.     for(i=0;i<n;i++)for(j=0;j<m;j++){
  259.  
  260.         c[i][j]=0;
  261.  
  262.         for(k=0;k<l;k++){
  263.             c[i][j]=c[i][j]+a[i][k]*b[k][j];
  264.         }
  265.  
  266.     }
  267.  
  268.     return c;
  269.  
  270. }
  271.  
  272. int ** pseudo_prod_matrix(int n, int l, int m, int ** a, int ** b){
  273.    
  274.     int i,j,k,** c;
  275.    
  276.     c=alloc_matrix(n,m);
  277.    
  278.     for(i=0;i<n;i++)for(j=0;j<m;j++){
  279.        
  280.         c[i][j]=0;
  281.        
  282.         for(k=0;k<l;k++){
  283.             c[i][j]=c[i][j]+a[i][k]*b[j][k];
  284.         }
  285.        
  286.     }
  287.    
  288.     return c;
  289.    
  290. }
  291.  
  292.  
  293. int ** trans_matrix(int n, int m, int ** a){
  294.  
  295.         int i,j;
  296.     int ** b;
  297.  
  298.     b=alloc_matrix(m,n);
  299.  
  300.     for(j=0;j<m;j++)for(i=0;i<n;i++){
  301.  
  302.         b[j][i]=a[i][j];
  303.  
  304.     }
  305.  
  306.     return b;
  307.  
  308. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top