-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixMult.c
213 lines (174 loc) · 4.58 KB
/
matrixMult.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Name: Neil Marcellini
* Lab/task: Lab 5
* Date: 09/30/19
**/
#include "matrixMult.h"
int main(int argc, char *argv[])
{
if (freopen(argv[1], "r", stdin) == 0)
oops("Cannot open the input file.\n", -1);
int **a1, **b1, **c1, **a2, **b2, **c2; // matrices
int m1, k1, n1, m2, k2, n2; // dimensions of the matices m x k and k x n
allocateAndLoadMatrices(&a1, &b1, &c1, &m1, &k1, &n1);
allocateAndLoadMatrices(&a2, &b2, &c2, &m2, &k2, &n2);
// the real magic happens in there
// TODO: implement
pthread_t **tids1, **tids2;
tids1 = multiply(a1, b1, c1, m1, k1, n1);
join(tids1, m1, n1);
free_tids(tids1, m1);
tids2 = multiply(a2, b2, c2, m2, k2, n2);
join(tids2, m2, n2);
free_tids(tids2, m2);
// dispaly results of matrix multiplication
printf("\nMATRIX A1\n");
displayMatrix(a1, m1, k1);
freeMatrix(a1, m1);
printf("\nMATRIX B1\n");
displayMatrix(b1, k1, n1);
freeMatrix(b1, k1);
printf("\nMATRIX A1 x B1\n");
displayMatrix(c1, m1, n1);
freeMatrix(c1, m1);
printf("\nMATRIX A2\n");
displayMatrix(a2, m2, k2);
freeMatrix(a2, m2);
printf("\nMATRIX B2\n");
displayMatrix(b2, k2, n2);
freeMatrix(b2, k2);
printf("\nMATRIX A2 x B2\n");
displayMatrix(c2, m2, n2);
freeMatrix(c2, m2);
return 0;
}
void *matrixThread(void *param)
{
// map the parameter onto the structure
MATRIX_CELL *cell = (MATRIX_CELL *)param;
// TODO: implement
//individual thread here
int stepr;
// k = length of row/col
for(stepr = 0; stepr < cell->k; stepr++) {
cell->c[cell->i][cell->j] += (cell->a[cell->i][stepr]) *(cell->b[stepr][cell->j]);
}
//end todo
free(cell);
return NULL;
}
void allocateAndLoadMatrices(int ***a, int ***b, int ***c, int *m, int *k, int *n)
// takes pointers to two-dimensional matrices, so they can be allocated in here
// and used by the caller
{
if (scanf("%d %d %d", m, k, n) == 0)
oops("Cannot read matrix sizes.\n", -2);
// TODO: implement
int i;
//mxk
(*a) = (int **) malloc(*m * sizeof(int *));
for (i = 0; i < *m; i++) {
(*a)[i] = (int *) malloc(*k * sizeof(int));
}
// k x n
(*b) = (int **) malloc(*k * sizeof(int *));
for (i = 0; i < *k; i++) {
(*b)[i] = (int *) malloc(*n * sizeof(int));
}
// mxn
(*c) = (int **) malloc(*m * sizeof(int *));
for (i = 0; i < *m; i++) {
(*c)[i] = (int *) malloc(*n * sizeof(int));
}
loadMatrix(a,*m,*k);
loadMatrix(b,*k,*n);
}
void loadMatrix(int ***matrix, int m, int n)
{
// TODO: implement
// load input into matrix
// m lines of k numbers for each matrix
// loop through matrix and scanf data
int i;
int j;
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
if(scanf("%d", &(*matrix)[i][j]) == 0) {
oops("Cannot read matrix input data.\n", -2);
}
}
}
}
void freeMatrix(int **matrix, int m)
{
// TODO: implement
int i;
for (i = 0; i < m; i++) {
free(matrix[i]);
}
free(matrix);
}
pthread_t **multiply(int **a, int **b, int **c, int m, int k, int n)
{
pthread_t **tids = alloc_tids(m, n);
// TODO: implement
// creation of threads here, organizing multiplication
int i;
int j;
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
MATRIX_CELL *data = (MATRIX_CELL *) malloc(sizeof(MATRIX_CELL));
data->a = a;
data->b = b;
data->c = c;
data->i = i;
data->j = j;
data->k = k;
pthread_create(&tids[i][j], NULL, matrixThread, (void *) data);
}
}
return tids;
}
pthread_t **alloc_tids(int m, int n)
{
pthread_t **tids;
// TODO: implement
// allocate tids array
int i;
tids = (pthread_t **) malloc(m * sizeof(pthread_t *));
for (i = 0; i < m; i++)
tids[i] = (pthread_t *) malloc(n * sizeof(pthread_t));
return tids;
}
void free_tids(pthread_t **tids, int m)
{
// TODO: implement
int i;
for (i = 0; i < m; i++) {
free(tids[i]);
}
free(tids);
}
void join(pthread_t **tids, int m, int n)
{
// TODO: implement
int i;
int j;
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
pthread_join(tids[i][j], NULL);
}
}
}
void displayMatrix(int **matrix, int m, int n)
{
// TODO: implement
int i;
int j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}