Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdbool.h>
- #include<pthread.h>
- float **matrix1,**matrix2;
- int thread_count = 0;
- //Declaring number of rows & Columns of the matrix..
- int rows1=0,cols1=1,rows2=0,cols2=1;
- struct thread_data{
- int i; /* row */
- int j; /* column */
- float **result_mat;
- }thread_data;
- void get_sizes(char **file_name,int * r ,int * c)
- {
- //getting number of columns and rows
- bool newline_flag = false;
- FILE *f;
- f = fopen(file_name,"r");
- if(f != NULL )
- {
- while (!feof(f))
- {
- char ch = fgetc(f);
- if(ch == ' ' && !newline_flag)
- {
- *c = *c + 1 ;
- }
- if(ch == '\n')
- {
- *r = *r + 1 ;
- newline_flag = true; //flag to stop counting spaces
- }
- }
- fclose(f);
- }
- else
- {
- printf("Error reading file");
- exit(-1);
- }
- }
- float ** generate_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 1D 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");
- }
- }
- float ** matrix_multiplication(float ** matrix1,float ** matrix2)
- {
- float result = 0;
- float **matrix3;
- matrix3 = malloc(sizeof(float*) * cols2);
- //Allocating the resulting matrix
- for(int i = 0; i < rows1; i++) {
- matrix3[i] = malloc(sizeof(float*) * cols2);
- }
- for (int i = 0; i <rows1 ; i++ )
- {
- for(int j = 0;j < cols2 ; j++)
- {
- for(int k = 0; k < rows2;k++)
- {
- result = result + matrix1[i][k]*matrix2[k][j];
- }
- matrix3[i][j] = result;
- result = 0;
- }
- }
- return matrix3;
- }
- void ThreadedMatMultPerElement(void *args)
- {
- struct thread_data *data = args; //structure holding data passed from the main function
- int i;
- float sum;
- //Row multiplied by column
- for(i = 0; i< cols1; i++){
- sum += matrix1[data->i][i] * matrix2[i][data->j];
- }
- //assign the sum to its coordinate
- data->result_mat[data->i][data->j] = sum;
- thread_count++;
- }
- void ThreadedMatMultPerRow(void *args)
- {
- struct thread_data *data = args;
- float sum;
- for(int i = 0; i < cols2;i++)
- {
- for(int j = 0;j < cols1;j++)
- {
- sum = sum + matrix1[data->i][j] * matrix2[j][i];
- }
- data->result_mat[data->i][i] = sum;
- sum = 0;
- }
- thread_count++;
- }
- int main()
- {
- float **matrix3;
- char file_name[100];
- //First Matrix
- printf("Matrix #1: ");
- scanf("%s",file_name);
- //Getting Size
- get_sizes(file_name,&rows1,&cols1);
- //Generating Matrix
- matrix1 = generate_matrix(file_name,&rows1,&cols1);
- print_matrix(rows1,cols1,matrix1);
- //Second Matrix
- printf("Matrix #2: ");
- scanf("%s",file_name);
- //Getting Size
- get_sizes(file_name,&rows2,&cols2);
- //Generating Matrix
- matrix2 = generate_matrix(file_name,&rows2,&cols2);
- print_matrix(rows2,cols2,matrix2);
- //Flag to check if we can multiply the matrices or not
- bool multiplcation = true;
- if(cols1!=rows2)
- multiplcation=false;
- else
- {
- //Allocate matrix 3 which has the result of multiplication!
- multiplcation=true;
- float **matrix3 = (float **)malloc(sizeof *matrix3 * cols2);
- matrix3 = malloc(sizeof(float*) * cols2);
- for(int i = 0; i < rows1; i++)
- {
- matrix3[i] = malloc(sizeof(float*) * cols2);
- }
- }
- if(multiplcation==false)
- {
- printf("\nCan't multiply those two matrices \n");
- return 0;
- }
- int x;
- do{
- printf("Multiplication Method: \n1-Non-threaded\n2-Thread per element\n3-Thread per row\n");
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- {
- matrix3 = matrix_multiplication(matrix1,matrix2);
- print_matrix(rows1,cols2,matrix3);
- free(matrix3);
- }
- break;
- case 2:
- {
- pthread_t threads[rows1*cols2]; //Array of threads to store created threads!
- for(int i = 0; i < rows1; i++) {
- for(int j = 0; j < cols2; j++) {
- //Assign a row and column for each thread
- struct thread_data *data = (struct thread_data *) malloc(sizeof(struct thread_data));
- data->i = i;
- data->j = j;
- data->result_mat = matrix3;
- //Create the thread
- pthread_create(&threads[i],NULL,ThreadedMatMultPerElement,data);
- }
- }
- for(int k=0;k<(rows1*cols2);k++)
- {pthread_join(threads[k], NULL);}
- print_matrix(rows1,cols2,matrix3);
- free(matrix3);
- }
- break;
- case 3:
- {
- for( int i = 0; i < rows1; i++)
- {
- matrix3[i] = malloc(sizeof(float*) * cols2);
- }
- pthread_t threads[rows1];
- for(int i = 0; i < rows1; i++){
- struct thread_data *data = (struct thread_data *)malloc(sizeof(struct thread_data));
- data->i = i;
- data->result_mat = matrix3;
- pthread_create(&threads[i], NULL,ThreadedMatMultPerRow,data);
- }
- for (int i = 0; i < rows1; i++)
- {
- pthread_join(threads[i], NULL);
- }
- }
- break;
- }
- }while(x!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement