-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatmult.c
135 lines (127 loc) · 2.72 KB
/
matmult.c
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
#include<stdlib.h>
#include<pthread.h>
#include <stdio.h>
#define Item(X, I, J, NCOL) X[((J) + (I) * (NCOL))]
/*
* matrix multiplication
* C = A*B
* A has l rows and m cols
* B has m rows and n cols
* C has l rows and n cols
*/
void matmult(int* A, int* B, int* C, int l, int m, int n)
{
for(int i=0; i<l; i++)
for(int j=0; j<n; j++)
{
int sum = 0;
for(int k=0; k<m; k++)
sum += Item(A, i, k, m) * Item(B, k, j, n);
Item(C, i, j, n) = sum;
}
}
typedef struct{
int *A;
int *B;
int *C;
int l;
int m;
int n;
int row;
int col;
}data;
/*
* matrix multiplication
* C = A*B
* A has l rows and m cols
* B has m rows and n cols
* C has l rows and n cols
*/
void *threadmultE(void *arg){
data *arr = (data*) arg;
int i = arr->row;
int j = arr->col;
int*A=arr->A;
int*B=arr->B;
int*C=arr->C;
//int l=arr->l;
int m=arr->m;
int n=arr->n;
int sum = 0;
for(int k=0; k<m; k++){
sum += Item(A, i, k, m) * Item(B, k, j, n);
}
Item(C, i, j, n) = sum;
}
/*creating a thread for each element*/
void matmult_v1(int* A, int* B, int* C, int l, int m, int n)
{
//thread creation
data node[l][n];
pthread_t threads[l][n];
for(int i=0 ; i < l ; i++){
for(int j=0 ; j< n ; j++){
node[i][j].A=A;
node[i][j].B=B;
node[i][j].C=C;
node[i][j].l=l;
node[i][j].m=m;
node[i][j].n=n;
node[i][j].row=i;
node[i][j].col=j;
pthread_create(&threads[i][j], NULL , threadmultE, (void*)(&node[i][j]));
}
}
//each thread joins and waits the previous thread to complete its job
for(int i=0 ; i < l ; i++){
for(int j=0 ; j<n ; j++){
pthread_join(threads[i][j], NULL );
}
}
}
/*
* matrix multiplication
* C = A*B
* A has l rows and m cols
* B has m rows and n cols
* C has l rows and n cols
*/
void *threadmultR(void *arg){
data *arr = (data*) arg;
int i = arr->row;
//int j = arr->col;
int*A=arr->A;
int*B=arr->B;
int*C=arr->C;
//int l=arr->l;
int m=arr->m;
int n=arr->n;
for(int j=0; j<n; j++)
{
int sum = 0;
for(int k=0; k<m; k++)
sum += Item(A, i, k, m) * Item(B, k, j, n);
Item(C, i, j, n) = sum;
}
}
/*creating a thread for each row*/
void matmult_v2(int* A, int* B, int* C, int l, int m, int n)
{
data node[l];
//thread creation
pthread_t threads[l];
for(int i=0 ; i < l ; i++){
node[i].A=A;
node[i].B=B;
node[i].C=C;
node[i].l=l;
node[i].m=m;
node[i].n=n;
node[i].row=i;
pthread_create(&threads[i], NULL , threadmultR, (void*)&node[i]);
}
//each thread joins and waits the previous thread to complete its job
for(int i=0 ; i < l ; i++){
pthread_join(threads[i], NULL );
}
}