Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <time.h>
- int r1 = 1,c1 = 1,r2 = 1,c2 = 1;
- float** matrix2;
- float** matrix1;
- int no_threads = 0;
- struct thread_data
- {
- int row ,column; //position of element that the thread will calculate
- float ** mult_matrix;
- }thread_data;
- void get_sizes(char ** file_name,int * r ,int * c)
- {
- //getting number of columns and rows
- int flag = 0 ;
- FILE *f;
- f = fopen(file_name,"r");
- if(f != NULL )
- {
- while (!feof(f))
- {
- char ch = fgetc(f);
- if(ch == ' ' && flag == 0)
- {
- *c = *c + 1 ;
- }
- if(ch == '\n')
- {
- *r = *r + 1 ;
- flag = 1; //flag to stop counting spaces
- }
- }
- fclose(f);
- }
- else
- {
- printf("error reading file");
- exit(-1);
- }
- }
- float ** build_matrix(char ** file_name,int * r ,int * c )
- {
- FILE *f;
- float temp[(*r)*(*c)];
- int rows = *r , columns = *c;
- int counter = 0,i = 0,j = 0;
- //dynamic allocation for matrix
- float **matrix;
- matrix = malloc(sizeof(float*) * columns);
- for(i = 0; i < rows; i++) {
- matrix[i] = malloc(sizeof(float*) * columns);
- }
- //getting the values in a temporary array
- f = fopen(file_name, "r");
- if(f != NULL )
- {
- while (!feof(f))
- {
- fscanf(f,"%f",&temp[counter]);
- counter++;
- }
- // building the matrix
- counter = 0 ;
- for(i = 0; i < *r; i++)
- {
- for(j = 0; j < *c; j++)
- {
- matrix[i][j] = temp[counter];
- counter++;
- }
- }
- }
- else
- {
- printf("error reading file");
- exit(-1);
- }
- fclose(f);
- return matrix;
- }
- void print_matrix(int r,int c,float ** matrix)
- {
- for(int i = 0; i < r; i++)
- {
- for(int j = 0; j < c; j++)
- {
- printf("%.2f ",matrix[i][j]);
- }
- printf("\n");
- }
- }
- void threadedMatMultPerElement(void *s)
- {
- //row and column of cell to be calculated and limits to loops
- int row = ((struct thread_data *)s)->row;
- int column = ((struct thread_data *)s)->column;
- int sum = 0;
- float ** mult_matrix = ((struct thread_data *)s)->mult_matrix;
- for (int i = 0; i < c1 ;i++)
- {
- sum = sum + matrix1[row][i] * matrix2[i][column];
- }
- mult_matrix[row][column] = sum;
- no_threads++;
- }
- void threadedMatMultPerRow(void *s)
- {
- int sum = 0;
- int row = ((struct thread_data*)s)->row;
- float ** mult_matrix = ((struct thread_data *)s)->mult_matrix;
- for(int i = 0; i < c2;i++)
- {
- for(int j = 0;j < c1;j++)
- {
- sum = sum + matrix1[row][j] * matrix2[j][i];
- }
- mult_matrix[row][i] = sum;
- sum = 0;
- }
- no_threads++;
- }
- float ** matrix_multiplication(float ** matrix1,float ** matrix2)
- {
- float result = 0;
- float **matrix3;
- matrix3 = malloc(sizeof(float*) * c2);
- for(int i = 0; i < r1; i++) {
- matrix3[i] = malloc(sizeof(float*) * c2);
- }
- for (int i = 0; i <r1 ; i++ )
- {
- for(int j = 0;j < c2 ; j++)
- {
- for(int k = 0; k < r2;k++)
- {
- result = result + matrix1[i][k]*matrix2[k][j];
- }
- matrix3[i][j] = result;
- result = 0;
- }
- }
- return matrix3;
- }
- void printOutFile(float ** matrix3)
- {
- FILE *result;
- result = fopen("result", "w");
- for(int i = 0; i < r1; i++)
- {
- for(int j = 0; j < c2; j++)
- {
- fprintf(result,"%.3f ",matrix3[i][j]);
- }
- fprintf(result,"\n");
- }
- fclose(result);
- }
- int main()
- {
- float temp[50];
- char file_name[25];
- int x,flag = 0;
- //getting first matrix
- printf("enter the first matrix file name : ");
- scanf("%s",file_name);
- get_sizes(file_name,&r1,&c1);
- matrix1 = build_matrix(file_name,&r1,&c1);
- printf("%d %d\n",r1,c1);
- print_matrix(r1,c1,matrix1);
- //getting second matrix
- printf("enter the second matrix file name : ");
- scanf("%s",file_name);
- get_sizes(file_name,&r2,&c2);
- matrix2 = build_matrix(file_name,&r2,&c2);
- printf("%d %d\n",r2,c2);
- print_matrix(r2,c2,matrix2);
- if(c1 != r2)
- {
- printf("invalid matrices dimensions");
- exit(-1);
- }
- float **matrix3 = (float **)malloc(sizeof *matrix3 * c2);
- matrix3 = malloc(sizeof(float*) * c2);
- for( int i = 0; i < r1; i++)
- {
- matrix3[i] = malloc(sizeof(float*) * c2);
- }
- pthread_t threads[r1*c2];
- while(1)
- {
- printf("-------------------------------------------------------------------------------\n");
- printf("choose: \n1-non threaded multiplication\n2-thread per element multiplication\n3-thread per row multiplication\n");
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- flag ==0;
- clock_t t;
- t = clock();
- matrix3 = matrix_multiplication(matrix1,matrix2);
- t = clock() - t;
- double time_taken = ((double)t)/CLOCKS_PER_SEC;
- printf("matrix took %.60f seconds to execute \n", time_taken);
- break;
- case 2:
- flag ==0;
- for (int i = 0; i < r1; i++)
- {
- for (int j =0; j< c2; j++)
- {
- struct thread_data *s = (struct thread_data *)malloc(sizeof(struct thread_data));
- s->row = i;
- s->column = j;
- s->mult_matrix = matrix3;
- clock_t t;
- t = clock();
- pthread_create(&threads[i], NULL, threadedMatMultPerElement,s);
- t = clock() - t;
- double time_taken = ((double)t)/CLOCKS_PER_SEC;
- printf("matrix took %lf seconds to execute \n", time_taken);
- }
- }
- for (int i = 0; i < c2*r1; i++)
- {
- pthread_join(threads[i], NULL);
- }
- break;
- case 3:
- flag ==0;
- for( int i = 0; i < r1; i++)
- {
- matrix3[i] = malloc(sizeof(float*) * c2);
- }
- for(int i = 0; i < r1; i++)
- {
- struct thread_data *s = (struct thread_data *)malloc(sizeof(struct thread_data));
- s->row = i;
- s->mult_matrix = matrix3;
- pthread_create(&threads[i], NULL, threadedMatMultPerRow,s);
- }
- for (int i = 0; i < r1; i++)
- {
- pthread_join(threads[i], NULL);
- }
- break;
- default:
- flag = 1;
- printf("enter a valid option\n");
- }
- if(flag == 0)
- {
- print_matrix(r1,c2,matrix3);
- printOutFile(matrix3);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement