-
Notifications
You must be signed in to change notification settings - Fork 0
/
gt_matrix.c
199 lines (157 loc) · 3.72 KB
/
gt_matrix.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
#include <stdio.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sched.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <assert.h>
#include "gt_include.h"
//#define ROWS 512
//#define COLS ROWS
//#define SIZE COLS
//#define NUM_CPUS 10
//#define NUM_GROUPS NUM_CPUS
//#define PER_GROUP_COLS (SIZE/NUM_GROUPS)
#define NUM_THREADS 32 // Number of threads per matrix
//#define PER_THREAD_ROWS (SIZE/NUM_THREADS)
#define MAXSIZE 512
/* A[SIZE][SIZE] X B[SIZE][SIZE] = C[SIZE][SIZE]
* Let T(g, t) be thread 't' in group 'g'.
* T(g, t) is responsible for multiplication :
* A(rows)[(t-1)*SIZE -> (t*SIZE - 1)] X B(cols)[(g-1)*SIZE -> (g*SIZE - 1)] */
typedef struct matrix
{
int m[MAXSIZE][MAXSIZE];
int rows;
int cols;
unsigned int reserved[2];
} matrix_t;
typedef struct __uthread_arg
{
matrix_t *_A, *_B, *_C;
unsigned int reserved0;
unsigned int tid;
unsigned int gid;
int start_row; /* start_row -> (start_row + PER_THREAD_ROWS) */
int start_col; /* start_col -> (start_col + PER_GROUP_COLS) */
int matrix_size;
}uthread_arg_t;
struct timeval tv1;
static void generate_matrix(matrix_t *mat, int val, int size)
{
int i,j;
mat->rows = size;
mat->cols = size;
for(i = 0; i < mat->rows;i++)
for( j = 0; j < mat->cols; j++ )
{ //printf("%d %d\n", i, j);
mat->m[i][j] = val;
}
return;
}
static void print_matrix(matrix_t *mat, int size)
{
int i, j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
printf(" %d ",mat->m[i][j]);
printf("\n");
}
return;
}
static void * uthread_mulmat(void *p)
{
int i, j, k;
int start_row, end_row;
int start_col, end_col;
unsigned int cpuid;
struct timeval tv2;
#define ptr ((uthread_arg_t *)p)
i=0; j= 0; k=0;
start_row = ptr->start_row;
end_row = (ptr->start_row + ptr->matrix_size/NUM_THREADS);
#ifdef GT_GROUP_SPLIT
start_col = ptr->start_col;
end_col = (ptr->start_col + ptr->matrix_size/NUM_THREADS);
#else
start_col = 0;
end_col = ptr->matrix_size;
#endif
#ifdef GT_THREADS
cpuid = kthread_cpu_map[kthread_apic_id()]->cpuid;
#else
#endif
for(i = start_row; i < end_row; i++)
for(j = start_col; j < end_col; j++)
for(k = 0; k < ptr->matrix_size; k++) {
ptr->_C->m[i][j] += ptr->_A->m[i][k] * ptr->_B->m[k][j];
}
#ifdef GT_THREADS
#else
gettimeofday(&tv2,NULL);
#endif
#undef ptr
return 0;
}
// Four matrices, of sizes 32, 64, 128, and 256
matrix_t A[4], B[4], C[4];
int sizes[4] = {32, 64, 128, 256};
static void init_matrices() {
int i = 0;
for (i=0; i<4; ++i) {
generate_matrix(&A[i], 1, sizes[i]);
generate_matrix(&B[i], 1, sizes[i]);
generate_matrix(&C[i], 0, sizes[i]);
}
return;
}
uthread_arg_t uargs[NUM_THREADS*4];
uthread_t utids[NUM_THREADS*4];
int main()
{
uthread_arg_t *uarg;
int inx, i ,j;
gtthread_app_init();
init_matrices();
// print_matrix(&A);
// print_matrix(&B);
// print_matrix(&C);
gettimeofday(&tv1,NULL);
j = 0;
for (i=0; i<4; ++i)
{
for(inx=0; inx<NUM_THREADS; inx++)
{
uarg = &uargs[inx];
uarg->_A = &A[i];
uarg->_B = &B[i];
uarg->_C = &C[i];
uarg->tid = inx;
uarg->gid = i; // Every matrix in a different group
uarg->matrix_size = sizes[i];
uarg->start_row = (inx * sizes[i]/NUM_THREADS);
#ifdef GT_GROUP_SPLIT
/* Wanted to split the columns by groups !!! */
uarg->start_col = (uarg->gid * PER_GROUP_COLS);
#endif
uthread_create(&utids[j], uthread_mulmat, uarg, uarg->gid);
j++;
}
}
// for(i=0; i < 100000; ++i) {
// for(j=0; j<9000; ++j) {
//
// }
// }
gtthread_app_exit();
// print_matrix(&A);
// print_matrix(&B);
// print_matrix(&C);
// fprintf(stderr, "********************************");
return(0);
}