Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. int r1 = 1,c1 = 1,r2 = 1,c2 = 1;
  5. float** matrix2;
  6. float** matrix1;
  7. int no_threads = 0;
  8.  
  9.  
  10.  
  11. struct  thread_data
  12. {
  13.     int row ,column; //position of element that the thread will calculate
  14.     float ** mult_matrix;
  15. }thread_data;
  16. void get_sizes(char ** file_name,int * r ,int * c)
  17. {
  18.     //getting number of columns and rows
  19.     int flag = 0 ;
  20.     FILE *f;
  21.     f = fopen(file_name,"r");
  22.     if(f != NULL )
  23.     {
  24.          while (!feof(f))
  25.     {
  26.         char ch = fgetc(f);
  27.        if(ch == ' ' && flag == 0)
  28.        {
  29.            *c = *c + 1 ;
  30.  
  31.        }
  32.  
  33.        if(ch == '\n')
  34.       {
  35.           *r = *r + 1 ;
  36.           flag = 1; //flag to stop counting spaces
  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. float ** build_matrix(char ** file_name,int * r ,int * c )
  53. {
  54.   FILE *f;
  55.   float temp[50];
  56.    int rows = *r , columns = *c;
  57.   int counter = 0,i = 0,j = 0;
  58.   //dynamic allocation for matrix
  59.   float **matrix;
  60.     matrix = malloc(sizeof(float*) * columns);
  61.  
  62.     for(i = 0; i < rows; i++) {
  63.         matrix[i] = malloc(sizeof(float*) * columns);
  64.     }
  65.  
  66.     //getting the values in a temporary array
  67.   f = fopen(file_name, "r");
  68.   if(f != NULL )
  69.   {
  70.       while (!feof(f))
  71.     {
  72.  
  73.       fscanf(f,"%f",&temp[counter]);
  74.  
  75.       counter++;
  76.     }
  77.  
  78.     // building the matrix
  79.  
  80.         counter = 0 ;
  81.         for(i = 0; i < *r; i++)
  82.         {
  83.  
  84.             for(j = 0; j < *c; j++)
  85.             {
  86.                 matrix[i][j] = temp[counter];
  87.                 counter++;
  88.             }
  89.  
  90.         }
  91.  
  92.   }
  93.   else
  94.   {
  95.       printf("error reading file");
  96.       exit(-1);
  97.   }
  98.  
  99.  
  100.  
  101.   fclose(f);
  102.   return matrix;
  103.  
  104. }
  105.  
  106.  
  107. void threadedMatMultPerElement(void *s)
  108. {
  109.     //row and column of cell to be calculated and limits to loops
  110.     int row = ((struct thread_data *)s)->row;
  111.     int column = ((struct thread_data *)s)->column;
  112.     int sum = 0;
  113.     float ** mult_matrix = ((struct thread_data *)s)->mult_matrix;
  114.     for (int i = 0; i < c1 ;i++)
  115.     {
  116.  
  117.         sum  = sum + matrix1[row][i] * matrix2[i][column];
  118.  
  119.     }
  120.  
  121.     mult_matrix[row][column] = sum;
  122.     no_threads++;
  123.  
  124. }
  125. void print_matrix(int r,int c,float ** matrix)
  126. {
  127.     for(int i = 0; i < r; i++)
  128.         {
  129.             for(int j = 0; j < c; j++)
  130.             {
  131.                 printf("%.4f ",matrix[i][j]);
  132.  
  133.             }
  134.             printf("\n");
  135.         }
  136. }
  137.  
  138.  
  139. int main(int argc, char *argv[])
  140. {
  141.  
  142.      float temp[50];
  143.      char file_name[25];
  144.      pthread_t threads[r1*c2];
  145.  
  146.     //getting the 2 matrices
  147.     printf("enter the first matrix file name :  ");
  148.   scanf("%s",file_name);
  149.   get_sizes(file_name,&r1,&c1);
  150.  
  151.   matrix1 = build_matrix(file_name,&r1,&c1);
  152.   //print_matrix(r1,c1,matrix1);
  153.   //getting second matrix
  154.  
  155.   printf("enter the second matrix file name :  ");
  156.   scanf("%s",file_name);
  157.   get_sizes(file_name,&r2,&c2);
  158.  
  159.   matrix2 = build_matrix(file_name,&r2,&c2);
  160.   //print_matrix(r2,c2,matrix2);
  161.  
  162.  
  163.   for (int i = 0; i < r1; i++)
  164.     {
  165.         for (int j =0; j< c2; j++)
  166.         {
  167.             struct thread_data *s = (struct thread_data *)malloc(sizeof(struct thread_data));
  168.             s->row = i;
  169.             s->column = j;
  170.             s->mult_matrix = matrix3;
  171.             pthread_create(&threads[i], NULL, threadedMatMultPerElement,s);
  172.         }
  173.  
  174.     }
  175.  
  176.  
  177.  
  178.     for (int i = 0; i < c2*r1; i++)
  179.     {
  180.       pthread_join(threads[i], NULL);
  181.     }
  182.  
  183.     print_matrix(r1,c2,matrix3);
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement