Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.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[50];
- 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 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 print_matrix(int r,int c,float ** matrix)
- {
- for(int i = 0; i < r; i++)
- {
- for(int j = 0; j < c; j++)
- {
- printf("%.4f ",matrix[i][j]);
- }
- printf("\n");
- }
- }
- int main(int argc, char *argv[])
- {
- float temp[50];
- char file_name[25];
- pthread_t threads[r1*c2];
- //getting the 2 matrices
- printf("enter the first matrix file name : ");
- scanf("%s",file_name);
- get_sizes(file_name,&r1,&c1);
- matrix1 = build_matrix(file_name,&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);
- //print_matrix(r2,c2,matrix2);
- 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;
- pthread_create(&threads[i], NULL, threadedMatMultPerElement,s);
- }
- }
- for (int i = 0; i < c2*r1; i++)
- {
- pthread_join(threads[i], NULL);
- }
- print_matrix(r1,c2,matrix3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement