Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <time.h>
  5.  
  6. int r1 = 1,c1 = 1,r2 = 1,c2 = 1;
  7. float** matrix2;
  8. float** matrix1;
  9. int no_threads = 0;
  10. struct  thread_data
  11. {
  12.     int row ,column; //position of element that the thread will calculate
  13.     float ** mult_matrix;
  14. }thread_data;
  15. void get_sizes(char ** file_name,int * r ,int * c)
  16. {
  17.     //getting number of columns and rows
  18.     int flag = 0 ;
  19.     FILE *f;
  20.     f = fopen(file_name,"r");
  21.     if(f != NULL )
  22.     {
  23.          while (!feof(f))
  24.     {
  25.         char ch = fgetc(f);
  26.        if(ch == ' ' && flag == 0)
  27.        {
  28.            *c = *c + 1 ;
  29.  
  30.        }
  31.  
  32.        if(ch == '\n')
  33.       {
  34.           *r = *r + 1 ;
  35.           flag = 1; //flag to stop counting spaces
  36.  
  37.       }
  38.  
  39.     }
  40.  
  41.     fclose(f);
  42.     }
  43.     else
  44.   {
  45.       printf("error reading file");
  46.       exit(-1);
  47.   }
  48.  
  49.  
  50.  
  51. }
  52.  
  53. float ** build_matrix(char ** file_name,int * r ,int * c )
  54. {
  55.   FILE *f;
  56.   float temp[(*r)*(*c)];
  57.    int rows = *r , columns = *c;
  58.   int counter = 0,i = 0,j = 0;
  59.   //dynamic allocation for matrix
  60.   float **matrix;
  61.     matrix = malloc(sizeof(float*) * columns);
  62.  
  63.     for(i = 0; i < rows; i++) {
  64.         matrix[i] = malloc(sizeof(float*) * columns);
  65.     }
  66.  
  67.     //getting the values in a temporary array
  68.   f = fopen(file_name, "r");
  69.   if(f != NULL )
  70.   {
  71.       while (!feof(f))
  72.     {
  73.  
  74.       fscanf(f,"%f",&temp[counter]);
  75.  
  76.       counter++;
  77.     }
  78.  
  79.     // building the matrix
  80.  
  81.         counter = 0 ;
  82.         for(i = 0; i < *r; i++)
  83.         {
  84.  
  85.             for(j = 0; j < *c; j++)
  86.             {
  87.                 matrix[i][j] = temp[counter];
  88.                 counter++;
  89.             }
  90.  
  91.  
  92.         }
  93.  
  94.  
  95.  
  96.   }
  97.   else
  98.   {
  99.       printf("error reading file");
  100.       exit(-1);
  101.   }
  102.  
  103.  
  104.  
  105.   fclose(f);
  106.   return matrix;
  107.  
  108. }
  109. void print_matrix(int r,int c,float ** matrix)
  110. {
  111.     for(int i = 0; i < r; i++)
  112.         {
  113.             for(int j = 0; j < c; j++)
  114.             {
  115.                 printf("%.2f ",matrix[i][j]);
  116.  
  117.             }
  118.             printf("\n");
  119.         }
  120. }
  121.  
  122.  
  123. void threadedMatMultPerElement(void *s)
  124. {
  125.     //row and column of cell to be calculated and limits to loops
  126.     int row = ((struct thread_data *)s)->row;
  127.     int column = ((struct thread_data *)s)->column;
  128.     int sum = 0;
  129.     float ** mult_matrix = ((struct thread_data *)s)->mult_matrix;
  130.     for (int i = 0; i < c1 ;i++)
  131.     {
  132.  
  133.         sum  = sum + matrix1[row][i] * matrix2[i][column];
  134.  
  135.     }
  136.  
  137.     mult_matrix[row][column] = sum;
  138.     no_threads++;
  139.  
  140. }
  141. void threadedMatMultPerRow(void *s)
  142. {
  143.  
  144.     int sum = 0;
  145.     int row = ((struct thread_data*)s)->row;
  146.     float ** mult_matrix = ((struct thread_data *)s)->mult_matrix;
  147.     for(int i = 0; i < c2;i++)
  148.     {
  149.         for(int j = 0;j < c1;j++)
  150.         {
  151.             sum = sum + matrix1[row][j] * matrix2[j][i];
  152.         }
  153.  
  154.         mult_matrix[row][i] = sum;
  155.         sum = 0;
  156.  
  157.     }
  158.     no_threads++;
  159. }
  160. float ** matrix_multiplication(float ** matrix1,float ** matrix2)
  161. {
  162.  
  163.     float result = 0;
  164.      float **matrix3;
  165.     matrix3 = malloc(sizeof(float*) * c2);
  166.     for(int i = 0; i < r1; i++) {
  167.         matrix3[i] = malloc(sizeof(float*) * c2);
  168.     }
  169.  
  170.  
  171.     for (int i = 0; i <r1 ; i++ )
  172.     {
  173.         for(int j = 0;j < c2 ; j++)
  174.         {
  175.             for(int k = 0; k < r2;k++)
  176.             {
  177.                 result = result + matrix1[i][k]*matrix2[k][j];
  178.  
  179.             }
  180.  
  181.  
  182.             matrix3[i][j] = result;
  183.             result = 0;
  184.  
  185.  
  186.         }
  187.     }
  188.  
  189.  
  190.     return matrix3;
  191.  
  192. }
  193. void printOutFile(float ** matrix3)
  194. {
  195.     FILE *result;
  196.     result = fopen("result", "w");
  197.     for(int i = 0; i < r1; i++)
  198.         {
  199.             for(int j = 0; j < c2; j++)
  200.             {
  201.                 fprintf(result,"%.3f ",matrix3[i][j]);
  202.  
  203.             }
  204.             fprintf(result,"\n");
  205.         }
  206.     fclose(result);
  207. }
  208.  
  209.  
  210.  
  211. int main()
  212. {
  213.   float temp[50];
  214.   char file_name[25];
  215.   int x,flag = 0;
  216.  
  217.  
  218.  
  219.  
  220.   //getting first matrix
  221.   printf("enter the first matrix file name :  ");
  222.   scanf("%s",file_name);
  223.   get_sizes(file_name,&r1,&c1);
  224.  
  225.   matrix1 = build_matrix(file_name,&r1,&c1);
  226.   printf("%d  %d\n",r1,c1);
  227.   print_matrix(r1,c1,matrix1);
  228.   //getting second matrix
  229.  
  230.   printf("enter the second matrix file name :  ");
  231.   scanf("%s",file_name);
  232.   get_sizes(file_name,&r2,&c2);
  233.  
  234.   matrix2 = build_matrix(file_name,&r2,&c2);
  235.   printf("%d  %d\n",r2,c2);
  236.   print_matrix(r2,c2,matrix2);
  237.  
  238.   if(c1 != r2)
  239.   {
  240.       printf("invalid matrices dimensions");
  241.       exit(-1);
  242.   }
  243.   float **matrix3 = (float **)malloc(sizeof *matrix3 * c2);
  244.  
  245.     matrix3 = malloc(sizeof(float*) * c2);
  246.  
  247.     for( int i = 0; i < r1; i++)
  248.     {
  249.         matrix3[i] = malloc(sizeof(float*) * c2);
  250.     }
  251.  
  252.  
  253.      pthread_t threads[r1*c2];
  254.  
  255. while(1)
  256. {
  257.     printf("-------------------------------------------------------------------------------\n");
  258.      printf("choose: \n1-non threaded multiplication\n2-thread per element multiplication\n3-thread per row multiplication\n");
  259.      scanf("%d",&x);
  260.  
  261.     switch(x)
  262.   {
  263.  
  264.       case 1:
  265.           flag ==0;
  266.             clock_t t;
  267.                     t = clock();
  268.           matrix3 = matrix_multiplication(matrix1,matrix2);
  269.             t = clock() - t;
  270.                     double time_taken = ((double)t)/CLOCKS_PER_SEC;
  271.     printf("matrix took %.60f seconds to execute \n", time_taken);
  272.  
  273.         break;
  274.       case 2:
  275.  
  276.             flag ==0;
  277.                 for (int i = 0; i < r1; i++)
  278.             {
  279.                 for (int j =0; j< c2; j++)
  280.                 {
  281.                     struct thread_data *s = (struct thread_data *)malloc(sizeof(struct thread_data));
  282.                     s->row = i;
  283.                     s->column = j;
  284.                     s->mult_matrix = matrix3;
  285.                      clock_t t;
  286.                     t = clock();
  287.                     pthread_create(&threads[i], NULL, threadedMatMultPerElement,s);
  288.                     t = clock() - t;
  289.                     double time_taken = ((double)t)/CLOCKS_PER_SEC;
  290.     printf("matrix took %lf seconds to execute \n", time_taken);
  291.  
  292.                 }
  293.  
  294.             }
  295.             for (int i = 0; i < c2*r1; i++)
  296.             {
  297.               pthread_join(threads[i], NULL);
  298.             }
  299.  
  300.             break;
  301.       case 3:
  302.           flag ==0;
  303.                 for( int i = 0; i < r1; i++)
  304.             {
  305.                 matrix3[i] = malloc(sizeof(float*) * c2);
  306.             }
  307.                      for(int i = 0; i < r1; i++)
  308.                     {
  309.  
  310.                     struct thread_data *s = (struct thread_data *)malloc(sizeof(struct thread_data));
  311.                     s->row = i;
  312.                     s->mult_matrix = matrix3;
  313.  
  314.                     pthread_create(&threads[i], NULL, threadedMatMultPerRow,s);
  315.  
  316.  
  317.                     }
  318.  
  319.             for (int i = 0; i < r1; i++)
  320.             {
  321.               pthread_join(threads[i], NULL);
  322.             }
  323.         break;
  324.       default:
  325.           flag = 1;
  326.         printf("enter a valid option\n");
  327.  
  328.  
  329.   }
  330.  
  331.  
  332.  
  333.     if(flag == 0)
  334.     {
  335.         print_matrix(r1,c2,matrix3);
  336.         printOutFile(matrix3);
  337.     }
  338.  
  339. }
  340.  
  341.  
  342.     return 0;
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement