multithreading tests from 152 lab 5
[riscv-tests.git] / mt / bc_matmul / matmul_mi.c
1 //**************************************************************************
2 // Multi-threaded Matrix Multiply benchmark
3 //--------------------------------------------------------------------------
4 // TA : Christopher Celio
5 // Student:
6 //
7 //
8 // This benchmark multiplies two 2-D arrays together and writes the results to
9 // a third vector. The input data (and reference data) should be generated
10 // using the matmul_gendata.pl perl script and dumped to a file named
11 // dataset.h.
12
13
14 // print out arrays, etc.
15 //#define DEBUG
16
17 //--------------------------------------------------------------------------
18 // Includes
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #define REG_I 8
25 #define REG_J 2
26 #define BLOCK_I 32
27 #define BLOCK_J 16
28 #define BLOCK_K 16
29 #define LDA 32
30 #define NCORES 2
31 #define MIN(X,Y) (X < Y ? X : Y)
32
33 //--------------------------------------------------------------------------
34 // Input/Reference Data
35
36 typedef float data_t;
37 #include "dataset.h"
38
39
40 //--------------------------------------------------------------------------
41 // Basic Utilities and Multi-thread Support
42
43 __thread unsigned long coreid;
44 unsigned long ncores;
45
46 #include "util.h"
47
48 #define stringify_1(s) #s
49 #define stringify(s) stringify_1(s)
50 #define stats(code) do { \
51 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
52 code; \
53 _c += rdcycle(), _i += rdinstret(); \
54 if (coreid == 0) \
55 printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
56 stringify(code), _c, _c/DIM_SIZE/DIM_SIZE/DIM_SIZE, 10*_c/DIM_SIZE/DIM_SIZE/DIM_SIZE%10, _c/_i, 10*_c/_i%10); \
57 } while(0)
58
59
60 //--------------------------------------------------------------------------
61 // Helper functions
62
63 void printArray( char name[], int n, data_t arr[] )
64 {
65 int i;
66 if (coreid != 0)
67 return;
68
69 printf( " %10s :", name );
70 for ( i = 0; i < n; i++ )
71 printf( " %3ld ", (long) arr[i] );
72 printf( "\n" );
73 }
74
75 void __attribute__((noinline)) verify(size_t n, const data_t* test, const data_t* correct)
76 {
77 if (coreid != 0)
78 return;
79
80 size_t i;
81 for (i = 0; i < n; i++)
82 {
83 if (test[i] != correct[i])
84 {
85 printf("FAILED test[%d]= %3ld, correct[%d]= %3ld\n",
86 i, (long)test[i], i, (long)correct[i]);
87 exit(-1);
88 }
89 }
90
91 return;
92 }
93
94 //--------------------------------------------------------------------------
95 // matmul function
96
97 // single-thread, naive version
98 void __attribute__((noinline)) matmul_naive(const int lda, const data_t A[], const data_t B[], data_t C[] )
99 {
100 int i, j, k;
101
102 if (coreid > 0)
103 return;
104
105 for ( i = 0; i < lda; i++ )
106 for ( j = 0; j < lda; j++ )
107 {
108 for ( k = 0; k < lda; k++ )
109 {
110 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
111 }
112 }
113
114 }
115
116
117
118 void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
119 {
120
121 // ***************************** //
122 // **** ADD YOUR CODE HERE ***** //
123 // ***************************** //
124 //
125 // feel free to make a separate function for MI and MSI versions.
126
127 int i, j, k, ri, rj, ii, jj, kk;
128 data_t *Aj, *Cj, *Bi;
129 data_t c[REG_I][REG_J], a[REG_J], b[REG_I];
130 size_t start = coreid * (LDA / NCORES), end = (coreid == NCORES - 1 ? LDA : (coreid + 1) * (LDA / NCORES));
131
132 /* if (coreid > 0) { */
133 /* return; */
134 /* } */
135 /* start = 0, end = lda; */
136 if (ncores == NCORES && lda == LDA) {
137 for (jj = start; jj < end; jj += BLOCK_J) {
138 int kk_start= (coreid == 0 ? 0 : LDA/2) ,kk_end = (coreid == 0 ? LDA/2 : LDA);
139 for (kk = kk_start; kk < kk_end; kk += BLOCK_K) {
140 // for (ii = 0; ii < LDA; ii += BLOCK_I)
141 for (j = jj; j < MIN(end, jj + BLOCK_J); j += REG_J) {
142 Aj = A + j*LDA;
143 Cj = C + j*LDA;
144 for (i = 0; i < LDA/*, ii + BLOCK_I)*/; i += REG_I) {
145 /* Load C in register blocks. */
146 Bi = B + i;
147 for (ri = 0; ri < REG_I; ri++) {
148 for (rj = 0; rj < REG_J; rj++) {
149 c[ri][rj] = Cj[i + ri + ( rj)*LDA];
150 }
151 }
152
153
154 for (k = kk; k < MIN(LDA, kk + BLOCK_K); k++) {
155 for (ri = 0; ri < REG_I; ri++) {
156 b[ri] = Bi[k*LDA + ri];
157 }
158 /* Compute C in register blocks. */
159 for (rj = 0; rj < REG_J; rj++) {
160 a[rj] = Aj[(rj)*LDA + k];
161 for (ri = 0; ri < REG_I; ri++) {
162 c[ri][rj] += a[rj] * b[ri];
163 }
164 }
165 }
166
167 /* store C in register blocks. */
168 for (ri = 0; ri < REG_I; ri++) {
169 for (rj = 0; rj < REG_J; rj++) {
170 Cj[i + ri + ( rj)*LDA] = c[ri][rj];
171 }
172 }
173 }
174 }
175 /* barrier(); */
176
177 /* kk_start= (coreid == 1 ? 0 : LDA/2); */
178 /* kk_end = (coreid == 1 ? LDA/2 : LDA); */
179 /* for (kk = kk_start; kk < kk_end; kk += BLOCK_K) { */
180 /* // for (ii = 0; ii < LDA; ii += BLOCK_I) */
181 /* for (j = jj; j < MIN(end, jj + BLOCK_J); j += REG_J) { */
182 /* Aj = A + j*LDA; */
183 /* Cj = C + j*LDA; */
184 /* for (i = 0; i < LDA/\*, ii + BLOCK_I)*\/; i += REG_I) { */
185 /* /\* Load C in register blocks. *\/ */
186 /* Bi = B + i; */
187 /* for (ri = 0; ri < REG_I; ri++) { */
188 /* for (rj = 0; rj < REG_J; rj++) { */
189 /* c[ri][rj] = Cj[i + ri + ( rj)*LDA]; */
190 /* } */
191 /* } */
192
193
194 /* for (k = kk; k < MIN(LDA, kk + BLOCK_K); k++) { */
195 /* for (ri = 0; ri < REG_I; ri++) { */
196 /* b[ri] = Bi[k*LDA + ri]; */
197 /* } */
198 /* /\* Compute C in register blocks. *\/ */
199 /* for (rj = 0; rj < REG_J; rj++) { */
200 /* a[rj] = Aj[(rj)*LDA + k]; */
201 /* for (ri = 0; ri < REG_I; ri++) { */
202 /* c[ri][rj] += a[rj] * b[ri]; */
203 /* } */
204 /* } */
205 /* } */
206
207 /* store C in register blocks. */
208 /* for (ri = 0; ri < REG_I; ri++) { */
209 /* for (rj = 0; rj < REG_J; rj++) { */
210 /* Cj[i + ri + ( rj)*LDA] = c[ri][rj]; */
211 /* } */
212 /* } */
213 /* } */
214 /* } */
215 }
216 }
217
218
219 //barrier();
220 for (jj = start; jj < end; jj += BLOCK_J) {
221 int kk_start= (coreid != 0 ? 0 : LDA/2), kk_end = (coreid != 0 ? LDA/2 : LDA);
222 for (kk = kk_start; kk < kk_end; kk += BLOCK_K) {
223 // for (ii = 0; ii < LDA; ii += BLOCK_I)
224 for (j = jj; j < MIN(end, jj + BLOCK_J); j += REG_J) {
225 Aj = A + j*LDA;
226 Cj = C + j*LDA;
227 for (i = 0; i < LDA/*, ii + BLOCK_I)*/; i += REG_I) {
228 /* Load C in register blocks. */
229 Bi = B + i;
230 for (ri = 0; ri < REG_I; ri++) {
231 for (rj = 0; rj < REG_J; rj++) {
232 c[ri][rj] = Cj[i + ri + ( rj)*LDA];
233 }
234 }
235
236
237 for (k = kk; k < MIN(LDA, kk + BLOCK_K); k++) {
238 for (ri = 0; ri < REG_I; ri++) {
239 b[ri] = Bi[k*LDA + ri];
240 }
241 /* Compute C in register blocks. */
242 for (rj = 0; rj < REG_J; rj++) {
243 a[rj] = Aj[(rj)*LDA + k];
244 for (ri = 0; ri < REG_I; ri++) {
245 c[ri][rj] += a[rj] * b[ri];
246 }
247 }
248 }
249
250 /* store C in register blocks. */
251 for (ri = 0; ri < REG_I; ri++) {
252 for (rj = 0; rj < REG_J; rj++) {
253 Cj[i + ri + ( rj)*LDA] = c[ri][rj];
254 }
255 }
256 }
257 }
258 }
259 }
260 /* We only care about performance for 32x32 matrices and 2 cores. Otherwise just naive mat_mul */
261 } else {
262 if (coreid > 0)
263 return;
264
265 for ( i = 0; i < lda; i++ )
266 for ( j = 0; j < lda; j++ )
267 for ( k = 0; k < lda; k++ )
268 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
269 }
270 }
271
272 //--------------------------------------------------------------------------
273 // Main
274 //
275 // all threads start executing thread_entry(). Use their "coreid" to
276 // differentiate between threads (each thread is running on a separate core).
277
278 void thread_entry(int cid, int nc)
279 {
280 coreid = cid;
281 ncores = nc;
282
283 // static allocates data in the binary, which is visible to both threads
284 static data_t results_data[ARRAY_SIZE];
285
286
287 // /* // Execute the provided, naive matmul */
288 // barrier();
289 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
290 //
291 //
292 // // verify
293 // verify(ARRAY_SIZE, results_data, verify_data);
294 //
295 // // clear results from the first trial
296 // size_t i;
297 // if (coreid == 0)
298 // for (i=0; i < ARRAY_SIZE; i++)
299 // results_data[i] = 0;
300 // barrier();
301
302
303 // Execute your faster matmul
304 barrier();
305 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
306
307 #ifdef DEBUG
308 printArray("results:", ARRAY_SIZE, results_data);
309 printArray("verify :", ARRAY_SIZE, verify_data);
310 #endif
311
312 // verify
313 verify(ARRAY_SIZE, results_data, verify_data);
314 barrier();
315
316 exit(0);
317 }
318