Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.29 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. #include<pthread.h>
  5.  
  6. float **matrix1,**matrix2;
  7. int thread_count = 0;
  8.  
  9. //Declaring number of rows & Columns of the matrix..
  10. int rows1=0,cols1=1,rows2=0,cols2=1;
  11.  
  12. struct thread_data{
  13.    int i; /* row */
  14.    int j; /* column */
  15.    float **result_mat;
  16. }thread_data;
  17.  
  18. void get_sizes(char **file_name,int * r ,int * c)
  19. {
  20.     //getting number of columns and rows
  21.     bool newline_flag = false;
  22.     FILE *f;
  23.     f = fopen(file_name,"r");
  24.     if(f != NULL )
  25.     {
  26.          while (!feof(f))
  27.     {
  28.        char ch = fgetc(f);
  29.        if(ch == ' ' && !newline_flag)
  30.        {
  31.            *c = *c + 1 ;
  32.        }
  33.  
  34.        if(ch == '\n')
  35.       {
  36.           *r = *r + 1 ;
  37.           newline_flag = true; //flag to stop counting spaces
  38.       }
  39.     }
  40.     fclose(f);
  41.     }
  42.     else
  43.   {
  44.       printf("Error reading file");
  45.       exit(-1);
  46.   }
  47. }
  48.  
  49. float ** generate_matrix(char ** file_name,int * r ,int * c )
  50. {
  51.   FILE *f;
  52.   float temp[50];
  53.   int rows = *r , columns = *c;
  54.   int counter = 0,i = 0,j = 0;
  55.   //dynamic allocation for matrix
  56.     float **matrix;
  57.     matrix = malloc(sizeof(float*) * columns);
  58.     for(i = 0; i < rows; i++)
  59.     {matrix[i] = malloc(sizeof(float*) * columns);}
  60.  
  61.   //getting the values in a temporary 1D Array
  62.   f = fopen(file_name, "r");
  63.   if(f != NULL )
  64.   {
  65.       while (!feof(f))
  66.     {
  67.  
  68.       fscanf(f,"%f",&temp[counter]);
  69.       counter++;
  70.     }
  71.  
  72.     // building the matrix
  73.         counter = 0 ;
  74.         for(i = 0; i < *r; i++)
  75.         {
  76.             for(j = 0; j < *c; j++)
  77.             {
  78.                 matrix[i][j] = temp[counter];
  79.                 counter++;
  80.             }
  81.         }
  82.   }
  83.   else
  84.   {
  85.       printf("Error reading file");
  86.       exit(-1);
  87.   }
  88.  
  89.   fclose(f);
  90.   return matrix;
  91. }
  92.  
  93. void print_matrix(int r,int c,float ** matrix)
  94. {
  95.     for(int i = 0; i < r; i++)
  96.         {
  97.             for(int j = 0; j < c; j++)
  98.             {
  99.                 printf("%.2f ",matrix[i][j]);
  100.  
  101.             }
  102.             printf("\n");
  103.         }
  104. }
  105.  
  106. float ** matrix_multiplication(float ** matrix1,float ** matrix2)
  107. {
  108.     float result = 0;
  109.     float **matrix3;
  110.     matrix3 = malloc(sizeof(float*) * cols2);
  111.     //Allocating the resulting matrix
  112.     for(int i = 0; i < rows1; i++) {
  113.         matrix3[i] = malloc(sizeof(float*) * cols2);
  114.     }
  115.  
  116.     for (int i = 0; i <rows1 ; i++ )
  117.     {
  118.         for(int j = 0;j < cols2 ; j++)
  119.         {
  120.             for(int k = 0; k < rows2;k++)
  121.             {
  122.                 result = result + matrix1[i][k]*matrix2[k][j];
  123.  
  124.             }
  125.             matrix3[i][j] = result;
  126.             result = 0;
  127.          }
  128.     }
  129.     return matrix3;
  130. }
  131.  
  132. void ThreadedMatMultPerElement(void *args)
  133. {
  134.     struct thread_data *data = args; //structure holding data passed from the main function
  135.     int i;
  136.     float sum;
  137.  
  138.    //Row multiplied by column
  139.    for(i = 0; i< cols1; i++){
  140.       sum += matrix1[data->i][i] * matrix2[i][data->j];
  141.    }
  142.    //assign the sum to its coordinate
  143.    data->result_mat[data->i][data->j] = sum;
  144.    thread_count++;
  145. }
  146.  
  147. void ThreadedMatMultPerRow(void *args)
  148. {
  149.     struct thread_data *data = args;
  150.     float sum;
  151.     for(int i = 0; i < cols2;i++)
  152.     {
  153.         for(int j = 0;j < cols1;j++)
  154.         {
  155.             sum = sum + matrix1[data->i][j] * matrix2[j][i];
  156.         }
  157.         data->result_mat[data->i][i] = sum;
  158.         sum = 0;
  159.     }
  160.     thread_count++;
  161. }
  162.  
  163. int main()
  164. {
  165.     float **matrix3;
  166.     char file_name[100];
  167.  
  168. //First Matrix
  169. printf("Matrix #1: ");
  170. scanf("%s",file_name);
  171. //Getting Size
  172. get_sizes(file_name,&rows1,&cols1);
  173. //Generating Matrix
  174. matrix1 = generate_matrix(file_name,&rows1,&cols1);
  175. print_matrix(rows1,cols1,matrix1);
  176.  
  177.  
  178. //Second Matrix
  179. printf("Matrix #2: ");
  180. scanf("%s",file_name);
  181. //Getting Size
  182. get_sizes(file_name,&rows2,&cols2);
  183. //Generating Matrix
  184. matrix2 = generate_matrix(file_name,&rows2,&cols2);
  185. print_matrix(rows2,cols2,matrix2);
  186.  
  187.  
  188. //Flag to check if we can multiply the matrices or not
  189. bool multiplcation = true;
  190.  
  191. if(cols1!=rows2)
  192.     multiplcation=false;
  193. else
  194.     {
  195.         //Allocate matrix 3 which has the result of multiplication!
  196.         multiplcation=true;
  197.         float **matrix3 = (float **)malloc(sizeof *matrix3 * cols2);
  198.         matrix3 = malloc(sizeof(float*) * cols2);
  199.         for(int i = 0; i < rows1; i++)
  200.         {
  201.         matrix3[i] = malloc(sizeof(float*) * cols2);
  202.         }
  203.     }
  204.  
  205. if(multiplcation==false)
  206. {
  207.     printf("\nCan't multiply those two matrices \n");
  208.     return 0;
  209. }
  210.  
  211.      int x;
  212.      do{
  213.      printf("Multiplication Method: \n1-Non-threaded\n2-Thread per element\n3-Thread per row\n");
  214.      scanf("%d",&x);
  215.  
  216.   switch(x)
  217.   {
  218.  
  219.       case 1:
  220.       {
  221.         matrix3 = matrix_multiplication(matrix1,matrix2);
  222.         print_matrix(rows1,cols2,matrix3);
  223.         free(matrix3);
  224.       }
  225.       break;
  226.  
  227.       case 2:
  228.       {
  229.         pthread_t threads[rows1*cols2]; //Array of threads to store created threads!
  230.         for(int i = 0; i < rows1; i++) {
  231.             for(int j = 0; j < cols2; j++) {
  232.          //Assign a row and column for each thread
  233.          struct thread_data *data = (struct thread_data *) malloc(sizeof(struct thread_data));
  234.          data->i = i;
  235.          data->j = j;
  236.          data->result_mat = matrix3;
  237.          //Create the thread
  238.          pthread_create(&threads[i],NULL,ThreadedMatMultPerElement,data);
  239.             }
  240.         }
  241.         for(int k=0;k<(rows1*cols2);k++)
  242.          {pthread_join(threads[k], NULL);}
  243.         print_matrix(rows1,cols2,matrix3);
  244.         free(matrix3);
  245.       }
  246.       break;
  247.  
  248.       case 3:
  249.       {
  250.            for( int i = 0; i < rows1; i++)
  251.             {
  252.                 matrix3[i] = malloc(sizeof(float*) * cols2);
  253.             }
  254.             pthread_t threads[rows1];
  255.             for(int i = 0; i < rows1; i++){
  256.                     struct thread_data *data = (struct thread_data *)malloc(sizeof(struct thread_data));
  257.                     data->i = i;
  258.                     data->result_mat = matrix3;
  259.                     pthread_create(&threads[i], NULL,ThreadedMatMultPerRow,data);
  260.                     }
  261.             for (int i = 0; i < rows1; i++)
  262.             {
  263.               pthread_join(threads[i], NULL);
  264.             }
  265.  
  266.       }
  267.       break;
  268.  
  269.   }
  270.  
  271.  
  272.  
  273.   }while(x!=0);
  274.  
  275.     return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement