-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
145 lines (107 loc) · 2.89 KB
/
main.c
File metadata and controls
145 lines (107 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
int x,r1,r2,c1,c2;
int **a;
int **b;
int **mult; //dynamic allocation to matrix as global variables
void *runner(void *param);
int main() {
int i,j, count = 0;
printf("enter rows of first matrix: ");
scanf("%d",&r1);
printf("enter columns of first matrix: ");
scanf("%d",&c1);
printf("enter rows of second matrix: ");
scanf("%d",&r2);
printf("enter columns of second matrix: ");
scanf("%d",&c2);
a=(int **)malloc( r1*sizeof(int *));// creation of matrix according to input rows and columns
for(i=0;i<r1;i++){
a[i]=(int *)malloc( c1*sizeof(int ));
}
b=(int **)malloc( r2*sizeof(int *));
for(i=0;i<r2;i++){
b[i]=(int *)malloc( c2*sizeof(int ));
}
mult=(int **)malloc( r1*sizeof(int *));
for(i=0;i<r1;i++){
mult[i]=(int *)malloc( c2*sizeof(int ));
}
//creating matrix A and B
for(i = 0; i < c1; i++) {
for(j = 0; j < r1; j++) {
srandom(time(NULL));
a[i][j]=rand()%10;
}
}
for(i = 0; i < c1; i++) {
for(j = 0; j < r1; j++) {
srandom(time(NULL));
b[i][j]=rand()%10;
}
}
//print matrix A and B
printf("Matrix A: \n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Matrix B: \n");
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
printf("%d ", b[i][j]);
}
printf("\n");
}
// calculation of time before prog
struct timespec starttime, endtime, ttime;
double timeinterval;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &starttime);
int size;
for(i = 0; i < r1; i++) {
pthread_t tid; //Thread ID
pthread_attr_t attr; //Set of thread attributes
//Get the default attributes
pthread_attr_init(&attr);
//Create the thread
x=i;
pthread_create(&tid,&attr,runner,NULL);//thread creation
//Make sure the parent waits for all thread to complete
pthread_join(tid, NULL);
count++;
}
//time after
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &endtime);
ttime.tv_sec = endtime.tv_sec - starttime.tv_sec;
ttime.tv_nsec = endtime.tv_nsec - starttime.tv_nsec;
timeinterval = ttime.tv_sec * 1000000000 + ttime.tv_nsec;
//Print out the resulting matrix
printf("number of threads : %d \n",count);// no if threads ie equal no of rows
printf("Time in milliseconds %lf ms \n", (double) timeinterval/1000000);
// output matrix
printf("Result matrix : \n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d ", mult[i][j]);
}
printf("\n");
}
}
void *runner(void *param) {
int n, sum = 0;
//row multiplied by column
int j;
for(j = 0; j < c2; j++) {
for(n = 0; n< c1; n++){
mult[x][j]= mult[x][j]+ a[x][n] * b[n][j];
}
}
pthread_exit(0);
}