Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include<stdio.h>
- #include<pthread.h>
- #include<stdlib.h>
- #include <time.h>
- int **Holder;
- int numberOfThreadsR;
- int numberOfThreadsE;
- //int *K;
- //this function will branch into either element thread or row thread multplication
- void fillMatrices(){
- FILE *file1;
- int r1 , c1 , r2 , c2;
- file1 = fopen("input.txt", "r"); // read mode
- if (file1 == NULL)
- {
- perror("Error!!!\n");
- exit(-1);
- }
- fscanf(file1 , "%d" , &r1);
- printf("matrix1 row size= %d\n" , r1);
- fscanf(file1 , "%d" , &c1);
- printf("matrix1 column size= %d\n" , c1);
- int M1[r1][c1];
- int i,j;
- //filling in matrix 1 after knowing its size from 1st line in file
- for(i=0 ; i<r1 ; i++){
- for(j=0 ; j<c1 ; j++){
- fscanf(file1 , "%d" , &M1[i][j]);
- }
- }
- //printing content fo M1 to be sure
- printf("content of matrix 1 is: \n");
- int k , m;
- for(k=0 ; k<r1 ; k++){
- for(m=0 ; m<c1 ; m++){
- printf("%d " , M1[k][m]);
- }
- printf("\n");
- }
- fscanf(file1 , "%d" , &r2);
- printf("matrix2 row size is= %d\n" , r2);
- fscanf(file1 , "%d" , &c2);
- printf("matrix2 column size is= %d\n" , c2);
- int M2[r2][c2];
- //filling in matrix 2 after knowing its size
- for(i=0 ; i<r2 ; i++){
- for(j=0 ; j<c2 ; j++){
- fscanf(file1 , "%d" , &M2[i][j]);
- }
- }
- //printing matrix2
- printf("content of matrix2 is: \n");
- for(k=0 ; k<r2 ; k++){
- for(m=0 ; m<c2 ; m++){
- printf("%d " , M2[k][m]);
- }
- printf("\n");
- }
- int variation;
- //checking on condition of multiplication of matrices
- if(c1 == r2){
- //printf(" Press 1 for ELEMENT thread or 2 for ROW thread multiplication: \n ");
- // scanf("%d" , &variation);
- //switch case on which variation of program is desired
- // switch (variation){
- // case 1:
- ThreadCreation(r1 , c1 , r2 , c2 , M1 , M2);
- /* break;
- case 2: ThreadCreation(r1 , c1 , r2 , c2 , M1 , M2);
- break;
- default:
- printf("weird number!!!");
- break;
- }*/
- }
- else{
- printf("Matrices can't be multiplied together!!!\n");
- exit(-1);
- }
- fclose(file1);
- }
- /* void *multiplyElement(int mergedIntoOne[] ){
- int k = 0, i = 0;
- //printf("this is first element %d \n" ,mergedIntoOne[0] );
- for (i = 1; i <= mergedIntoOne[0]; i++){
- k += mergedIntoOne[i]*mergedIntoOne[i+mergedIntoOne[0]];
- }
- int *p = (int*)malloc(sizeof(int));
- *p = k;
- pthread_exit(p);
- }*/
- /*void ElementThreadCreation(int r1 , int c1 , int r2 , int c2 , int M1[r1][c1] , int M2 [r2][c2]){
- printf("\n\n --------------1ST VARIATION------------- \n\n");
- int numberOfOutputElements = r1 * c2;
- int numberOfElementsInBothMatrices = ((r1*c1) + (r2*c2));
- pthread_t Thread;
- Thread=(pthread_t*)malloc(numberOfOutputElements*sizeof(pthread_t));
- int i , j, k,m ,l;
- int *mergedIntoOne;
- mergedIntoOne = (int *)malloc((numberOfElementsInBothMatrices)*sizeof(int));
- mergedIntoOne[0] = c1;
- for (i = 0; i < r1; i++){
- for (j = 0; j < c2; j++){
- for (k = 0; k < c1; k++) {//moving through columns of matrix 1 and once done we fill corrosponding row fro matrix 2
- mergedIntoOne[k+1] = M1[i][k];
- }
- for (l= 0; l < r2; l++) {//step is the size of column of matrix 1 with moving through rows of matrix 2
- mergedIntoOne[l+c1+1] = M2[l][j];
- }
- pthread_create(&Thread, NULL,multiplyElement, mergedIntoOne);
- }
- }
- printf("size of merged into one %d\n" , sizeof(mergedIntoOne));
- for(i=0 ; i< numberOfElementsInBothMatrices; i++){
- printf("%d ", mergedIntoOne[i]);
- printf("\n");
- }
- printf("RESULTANT MATRIX IS :- \n");
- for(i=0 ; i<numberOfOutputElements ; i++){
- void *k;
- //Joining all threads and collecting return value
- pthread_join(Thread, &k);
- int *p = (int *)k;
- printf("%d ",*p);
- if ((i + 1) % c2 == 0)
- printf("\n");
- }
- }*/
- void *multiplyRow(int *infoCarrier){
- int r1=infoCarrier[0] , c1= infoCarrier[1] , r2=infoCarrier[2] ,c2= infoCarrier[3] ,i,j,s ,k;
- int M1[r1][c1] , M2[r2][c2];
- int numberOfElementsInBothMatrices = (c1*r1) + (c2*r2);
- //filling M1
- s=4;
- while(s<(4+(r1*c1))){
- for(i=0 ; i<r1 ; i++){
- for(j=0 ; j<c1 ; j++){
- M1[i][j]=infoCarrier[s];
- s++;
- }
- }
- }
- //filling M2
- s= (4+(r1*c1));
- while(s<(4+numberOfElementsInBothMatrices)){
- for(i=0 ; i<r2; i++){
- for(j=0 ; j<c2 ; j++){
- M2[i][j]=infoCarrier[s];
- s++;
- }
- }
- }
- // rowsHolder[r1][c2];
- //initializing my row holder array
- for (i = 0; i < r1; i++) {
- for (j = 0; j < c2; j++)
- {Holder[i][j]=0;
- }
- }
- // printf("Hiiiiiiiiiiiiiii\n");
- //multiplying
- //rowsHolder
- for(i=0;i<r1;i++)
- {
- for(j=0;j<c2;j++)
- {
- for(k=0;k<c1;k++)
- {
- Holder[i][j]+=M1[i][k]*M2[k][j];
- //rowsHolder[i][j]/=2;
- }
- }
- }
- // return NULL;
- // printf("im here TOOO\n");
- pthread_exit(NULL);
- }
- void ThreadCreation(int r1 , int c1 , int r2 , int c2 , int M1[r1][c1] , int M2 [r2][c2]){
- int i , j, k ,m;
- //int variation;
- // FILE *output;
- //output = fopen("output.txt" , "w+");
- printf("\n\n");
- numberOfThreadsR=r1;
- numberOfThreadsE=r1*c2;
- // scanf("%d" , &variation);
- // switch(variation){
- //case 1:
- // break;
- // case 2:
- // break;
- //}
- //number of threads here will be equal to number of rows in 1st matrix (r1) and is going to be array of pointer to integers
- pthread_t ThreadR;
- //ThreadR=(pthread_t*)malloc(r1*sizeof(pthread_t));
- int numberOfOutputElements = r1 * c2;
- int numberOfElementsInBothMatrices = ((r1*c1) + (r2*c2));
- int infoCarrier[4+numberOfElementsInBothMatrices];
- // rowsHolder[r1][c2];
- Holder = (int*)malloc(r1*sizeof(int*));
- for( i=0; i<r1; i++)
- (Holder)[i] = (int)malloc(c2*sizeof(int));
- //printf("holderrr\n");
- for (i = 0; i < r1; i++) {
- for (j = 0; j < c2; j++)
- {Holder[i][j]=0;}
- }
- // printf("holderrr2\n");
- infoCarrier[0]= r1;
- infoCarrier[1]=c1;
- infoCarrier[2]= r2;
- infoCarrier[3]=c2;
- int s;
- s=4;
- while(s<(4+(r1*c1))){
- for(i=0 ; i<r1 ; i++){
- for(j=0 ; j<c1 ; j++){
- infoCarrier[s] = M1[i][j];
- s++;
- }
- }
- }
- // printf("holderrr3\n");
- s= (4+(r1*c1));
- while(s<(4+numberOfElementsInBothMatrices)){
- for(m=0 ; m<r2; m++){
- for(k=0 ; k<c2 ; k++){
- infoCarrier[s] = M2[m][k];
- s++;
- }
- }
- }
- // printf("holderrr4\n");
- infoCarrier[4+numberOfElementsInBothMatrices] = NULL;
- /*printf("content of infoCarrier is: \n");
- for(i=0 ; i<(4+numberOfElementsInBothMatrices) ; i++) {
- printf("%d \n" , infoCarrier[i]);
- }*/
- /* s=4;
- while(s<(4+(r1*c1))){
- for(i=0 ; i<r1 ; i++){
- for(j=0 ; j<c1 ; j++){
- M1[i][j]=infoCarrier[s];
- s++;
- }
- }
- }*/
- clock_t start1= clock();
- for(i=0 ; i<numberOfThreadsR;i++){
- pthread_create(&ThreadR, NULL,multiplyRow,infoCarrier );
- // printf("im here!!\n");
- }
- pthread_join(ThreadR,NULL);
- FILE *output;
- output = fopen("output.txt" , "a+");
- clock_t end1 = clock();
- double elapsed_time=((double)(end1-start1))/CLOCKS_PER_SEC;
- printf("Result of ROW Multiplication\n");
- for(i=0;i<r1;i++)
- {
- for(j=0;j<c2;j++)
- {
- printf("%d ",Holder[i][j]);
- if(j==c2-1)
- printf("\n");
- // fprintf(output , "%d" ,&Holder[i][j]);
- // if(j==c2-1)
- //fprintf(output,"\n");
- }
- }
- printf("Elapsed Time Of ROW Threading is= %f\n\n\n" , elapsed_time);
- //fprintf(output , "%f" , &elapsed_time);
- clock_t start2 = clock();
- for(i=0 ; i<numberOfThreadsE;i++){
- pthread_create(&ThreadR, NULL,multiplyRow,infoCarrier );
- // printf("im here!!\n");
- }
- pthread_join(ThreadR,NULL);
- clock_t end2 = clock();
- double elapsed_time2 = (double)(end2-start2) /CLOCKS_PER_SEC;
- printf("Result of Element Multiplication\n");
- for(i=0;i<r1;i++)
- {
- for(j=0;j<c2;j++)
- {
- printf("%d ",Holder[i][j]);
- if(j==c2-1)
- printf("\n");
- // fprintf(output , "%d" ,&Holder[i][j]);
- // if(j==c2-1)
- // fprintf(output,"\n");
- }
- }
- printf("Elapsed time of ELEMENT threading is= %f" , elapsed_time2);
- for(i=0;i<r1;i++)
- {
- for(j=0;j<c2;j++)
- {
- fprintf(output , "%d " ,Holder[i][j]);
- if(j==c2-1)
- fprintf(output,"\n");
- }
- }
- fprintf(output , " elapsed time of row: %f \n" , elapsed_time);
- fprintf(output , " elapsed time of element: %f \n" , elapsed_time2);
- fclose(output);
- }
- int main()
- {
- fillMatrices();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement