Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include<pthread.h>
  5.  
  6. float ** AllocateMatrix(char* filename);
  7. void PrintMatrix(float **array);
  8. void ReadMatrixFromFile(char *filename,float **array);
  9. float** MultiplyNoThreads(float** mat1, float ** mat2,int matrixOnerows,int  matrixOnecols,int matrixTworows,int matrixTwocols);
  10. float** MultiplyElyByElyThreads(float** mat1, float** mat2,int matrixOnrerows ,int matrixOnecols ,int matrixtworows ,int matrixTwocols);
  11. void* CalculateElement (void* );
  12. int row= 0;
  13. int col= 1;
  14. float** mat1 ;
  15. float** mat2 ;
  16.  
  17. struct InfoForThread
  18. {
  19.     int i ;
  20.     int j ;
  21.     float** result1 ;
  22.     //float** mat1 ;
  23.     //float** mat2 ;
  24.     int matrixOnecols ;
  25.  
  26. }InfoForThread;
  27.  
  28.  
  29.  
  30.  
  31. int main()
  32. {
  33.     int matrixOnerows=0;
  34.     int matrixOnecols=0;
  35.     int matrixTworows=0;
  36.     int matrixTwocols=0;
  37.  
  38.  
  39.     float **array1=AllocateMatrix("matrix1");
  40.     matrixOnerows=row;
  41.     matrixOnecols=col;
  42.    // PrintMatrix(array1);
  43.     ReadMatrixFromFile("matrix1",array1);
  44.     printf("\n");
  45.     PrintMatrix(array1);
  46.     printf("Matrix 1 of size: %d * %d",matrixOnerows,matrixOnecols);
  47.     printf("\n");
  48.     float **array2=AllocateMatrix("matrix2");
  49.     matrixTworows=row;
  50.     matrixTwocols=col;
  51.     //PrintMatrix(array2);
  52.     ReadMatrixFromFile("matrix2",array2);
  53.     printf("\n");
  54.     PrintMatrix(array2);
  55.     printf("Matrix 2 of size: %d * %d",matrixTworows,matrixTwocols);
  56.  
  57.     float **result=MultiplyNoThreads(array1,array2,matrixOnerows,matrixOnecols,matrixTworows,matrixTwocols);
  58.     printf("\n");
  59.     PrintMatrix(result);
  60.  
  61.     float **resultOfThreads =MultiplyElyByElyThreads(array1,array2,matrixOnerows,matrixOnecols,matrixTworows,matrixTwocols);
  62.     printf("\n");
  63.     PrintMatrix(resultOfThreads);
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     return 0;
  73. }
  74. float ** AllocateMatrix(char* filename){
  75.         int c, i, j;
  76.         row=0;
  77.         col=1;
  78.         int cind=0;
  79.         FILE *myfile;
  80.         float **array;
  81.         myfile=fopen(filename,"r");
  82.         while ((c = getc(myfile)) != EOF)
  83.     {
  84.  
  85.         if(cind ==0)
  86.         {
  87.             if(c == ' ')
  88.                { col++;
  89.                 }
  90.         }
  91.  
  92.         if (c == '\n')
  93.            {
  94.                 row++;
  95.                 cind=1;
  96.             }
  97.  
  98.  
  99.     }
  100.     fclose(myfile);
  101.  
  102.     array=malloc(sizeof(float *)* row); // allcocate rows
  103.     for(i=0;i<row;i++)
  104.         array[i]=calloc(col,sizeof(float));
  105.         // allocate array[NumberofColumns] to each row and fill it with zeros
  106.     return array;
  107. }
  108. void PrintMatrix(float **array){
  109.     for(int i=0;i<row;i++)  //print 2D array
  110.         for(int j=0;j<col;j++)
  111.             {
  112.                 if (j== col - 1)
  113.                 {
  114.                     printf("%f",array[i][j]);
  115.                     printf("\n");
  116.                 }
  117.                 else
  118.                 {
  119.                     printf("%f",array[i][j]);
  120.                     printf("\t");
  121.                 }
  122.             }
  123.  
  124. }
  125. void ReadMatrixFromFile(char *filename,float **array){
  126.  
  127.  
  128.     FILE *file;
  129.     file=fopen(filename,"r");
  130.     for(int i = 0; i < row; i++) // read the matrix from matrix file
  131.   {
  132.       for(int j = 0; j < col; j++)
  133.       {
  134.  
  135.        if (!fscanf(file, "%f", &array[i][j]))
  136.            break;
  137.       }
  138.  
  139.   }
  140.   fclose(file);
  141. }
  142. float** MultiplyNoThreads(float** mat1, float ** mat2,int matrixOnerows,int  matrixOnecols,int matrixTworows,int matrixTwocols){
  143.     float **result;
  144.     result=malloc(sizeof(float *)*matrixOnerows);
  145.     for(int i=0;i< matrixOnerows;i++)
  146.         result[i]=calloc(matrixTwocols,sizeof(float));
  147.  
  148.     for (int i = 0; i < matrixOnerows; i++)
  149.     {
  150.         for (int j = 0; j < matrixTwocols; j++)
  151.         {
  152.  
  153.             for (int k = 0; k < matrixOnecols; k++)
  154.                 result[i][j] += mat1[i][k]*mat2[k][j];
  155.         }
  156.     }
  157.  
  158.     return result;
  159.  
  160.  
  161. }
  162. float** MultiplyElyByElyThreads(float** mat1, float** mat2,int matrixOnrerows ,int matrixOnecols ,int matrixtworows ,int matrixTwocols){
  163.     float **result ;
  164.  
  165.     pthread_t threads[matrixOnrerows][matrixTwocols];
  166.  
  167.     result = malloc(sizeof(float*)*matrixOnrerows);
  168.     for (int i=0 ;i<matrixOnrerows;i++)
  169.         result[i]= calloc(matrixTwocols,sizeof(float));
  170.  
  171.     for (int i=0 ;i< matrixOnrerows; i++)
  172.     {
  173.         for (int j = 0; j<matrixTwocols; j++)
  174.         {
  175.             struct InfoForThread *data = (struct InfoForThread *)malloc(sizeof(struct InfoForThread));
  176.             data ->i = i;
  177.             data ->j = j;
  178.             //data ->mat1 = mat1;
  179.             //data ->mat2 = mat2;
  180.             data ->matrixOnecols = matrixOnecols;
  181.             data ->result1 = result;
  182.             int err;
  183.             err=pthread_create(&threads[i][j], NULL, CalculateElement,data );
  184.             if (err != 0)
  185.                 printf("\ncan't create thread :[%s]", strerror(err));
  186.             else
  187.                 printf("\n Thread[%d][%d]  created successfully\n",i,j);
  188.         }
  189.  
  190.     }
  191.  
  192.     for (int i=0 ;i< matrixOnrerows; i++)
  193.     {
  194.         for (int j = 0; j<matrixTwocols; j++)
  195.         {
  196.             pthread_join(threads[i][j],NULL);
  197.         }
  198.     }
  199.  
  200.  
  201.     return result ;
  202.  
  203. }
  204.  
  205. void* CalculateElement (void* data )
  206. {
  207.  
  208.     //float accumulator = 0;
  209.     int i = ((struct InfoForThread *)data)->i;
  210.     int j = ((struct InfoForThread *)data)->j;
  211.     int m = ((struct InfoForThread *)data)->matrixOnecols ;
  212.     for (int k =0 ; k<m; k++)
  213.     {
  214.         //accumulator = accumulator +mat1[i][k]*mat2[k][j];
  215.         ((struct InfoForThread *)data)->result1[i][j] += mat1[i][k]*mat2[k][j];
  216.  
  217.  
  218.     }
  219.  
  220.        // ((struct InfoForThread *)data)->result1[i][j] = accumulator;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement