Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include<pthread.h>
- float ** AllocateMatrix(char* filename);
- void PrintMatrix(float **array);
- void ReadMatrixFromFile(char *filename,float **array);
- float** MultiplyNoThreads(float** mat1, float ** mat2,int matrixOnerows,int matrixOnecols,int matrixTworows,int matrixTwocols);
- float** MultiplyElyByElyThreads(float** mat1, float** mat2,int matrixOnrerows ,int matrixOnecols ,int matrixtworows ,int matrixTwocols);
- void* CalculateElement (void* );
- int row= 0;
- int col= 1;
- float** mat1 ;
- float** mat2 ;
- struct InfoForThread
- {
- int i ;
- int j ;
- float** result1 ;
- //float** mat1 ;
- //float** mat2 ;
- int matrixOnecols ;
- }InfoForThread;
- int main()
- {
- int matrixOnerows=0;
- int matrixOnecols=0;
- int matrixTworows=0;
- int matrixTwocols=0;
- float **array1=AllocateMatrix("matrix1");
- matrixOnerows=row;
- matrixOnecols=col;
- // PrintMatrix(array1);
- ReadMatrixFromFile("matrix1",array1);
- printf("\n");
- PrintMatrix(array1);
- printf("Matrix 1 of size: %d * %d",matrixOnerows,matrixOnecols);
- printf("\n");
- float **array2=AllocateMatrix("matrix2");
- matrixTworows=row;
- matrixTwocols=col;
- //PrintMatrix(array2);
- ReadMatrixFromFile("matrix2",array2);
- printf("\n");
- PrintMatrix(array2);
- printf("Matrix 2 of size: %d * %d",matrixTworows,matrixTwocols);
- float **result=MultiplyNoThreads(array1,array2,matrixOnerows,matrixOnecols,matrixTworows,matrixTwocols);
- printf("\n");
- PrintMatrix(result);
- float **resultOfThreads =MultiplyElyByElyThreads(array1,array2,matrixOnerows,matrixOnecols,matrixTworows,matrixTwocols);
- printf("\n");
- PrintMatrix(resultOfThreads);
- return 0;
- }
- float ** AllocateMatrix(char* filename){
- int c, i, j;
- row=0;
- col=1;
- int cind=0;
- FILE *myfile;
- float **array;
- myfile=fopen(filename,"r");
- while ((c = getc(myfile)) != EOF)
- {
- if(cind ==0)
- {
- if(c == ' ')
- { col++;
- }
- }
- if (c == '\n')
- {
- row++;
- cind=1;
- }
- }
- fclose(myfile);
- array=malloc(sizeof(float *)* row); // allcocate rows
- for(i=0;i<row;i++)
- array[i]=calloc(col,sizeof(float));
- // allocate array[NumberofColumns] to each row and fill it with zeros
- return array;
- }
- void PrintMatrix(float **array){
- for(int i=0;i<row;i++) //print 2D array
- for(int j=0;j<col;j++)
- {
- if (j== col - 1)
- {
- printf("%f",array[i][j]);
- printf("\n");
- }
- else
- {
- printf("%f",array[i][j]);
- printf("\t");
- }
- }
- }
- void ReadMatrixFromFile(char *filename,float **array){
- FILE *file;
- file=fopen(filename,"r");
- for(int i = 0; i < row; i++) // read the matrix from matrix file
- {
- for(int j = 0; j < col; j++)
- {
- if (!fscanf(file, "%f", &array[i][j]))
- break;
- }
- }
- fclose(file);
- }
- float** MultiplyNoThreads(float** mat1, float ** mat2,int matrixOnerows,int matrixOnecols,int matrixTworows,int matrixTwocols){
- float **result;
- result=malloc(sizeof(float *)*matrixOnerows);
- for(int i=0;i< matrixOnerows;i++)
- result[i]=calloc(matrixTwocols,sizeof(float));
- for (int i = 0; i < matrixOnerows; i++)
- {
- for (int j = 0; j < matrixTwocols; j++)
- {
- for (int k = 0; k < matrixOnecols; k++)
- result[i][j] += mat1[i][k]*mat2[k][j];
- }
- }
- return result;
- }
- float** MultiplyElyByElyThreads(float** mat1, float** mat2,int matrixOnrerows ,int matrixOnecols ,int matrixtworows ,int matrixTwocols){
- float **result ;
- pthread_t threads[matrixOnrerows][matrixTwocols];
- result = malloc(sizeof(float*)*matrixOnrerows);
- for (int i=0 ;i<matrixOnrerows;i++)
- result[i]= calloc(matrixTwocols,sizeof(float));
- for (int i=0 ;i< matrixOnrerows; i++)
- {
- for (int j = 0; j<matrixTwocols; j++)
- {
- struct InfoForThread *data = (struct InfoForThread *)malloc(sizeof(struct InfoForThread));
- data ->i = i;
- data ->j = j;
- //data ->mat1 = mat1;
- //data ->mat2 = mat2;
- data ->matrixOnecols = matrixOnecols;
- data ->result1 = result;
- int err;
- err=pthread_create(&threads[i][j], NULL, CalculateElement,data );
- if (err != 0)
- printf("\ncan't create thread :[%s]", strerror(err));
- else
- printf("\n Thread[%d][%d] created successfully\n",i,j);
- }
- }
- for (int i=0 ;i< matrixOnrerows; i++)
- {
- for (int j = 0; j<matrixTwocols; j++)
- {
- pthread_join(threads[i][j],NULL);
- }
- }
- return result ;
- }
- void* CalculateElement (void* data )
- {
- //float accumulator = 0;
- int i = ((struct InfoForThread *)data)->i;
- int j = ((struct InfoForThread *)data)->j;
- int m = ((struct InfoForThread *)data)->matrixOnecols ;
- for (int k =0 ; k<m; k++)
- {
- //accumulator = accumulator +mat1[i][k]*mat2[k][j];
- ((struct InfoForThread *)data)->result1[i][j] += mat1[i][k]*mat2[k][j];
- }
- // ((struct InfoForThread *)data)->result1[i][j] = accumulator;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement