multithreading tests from 152 lab 5
[riscv-tests.git] / mt / bm_matmul / bm_matmul.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
25 //--------------------------------------------------------------------------
26 // Input/Reference Data
27
28 typedef float data_t;
29 #include "dataset.h"
30
31
32 //--------------------------------------------------------------------------
33 // Basic Utilities and Multi-thread Support
34
35 __thread unsigned long coreid;
36 unsigned long ncores;
37
38 #include "util.h"
39
40 #define stringify_1(s) #s
41 #define stringify(s) stringify_1(s)
42 #define stats(code) do { \
43 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
44 code; \
45 _c += rdcycle(), _i += rdinstret(); \
46 if (coreid == 0) \
47 printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
48 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); \
49 } while(0)
50
51
52 //--------------------------------------------------------------------------
53 // Helper functions
54
55 void printArray( char name[], int n, data_t arr[] )
56 {
57 int i;
58 if (coreid != 0)
59 return;
60
61 printf( " %10s :", name );
62 for ( i = 0; i < n; i++ )
63 printf( " %3ld ", (long) arr[i] );
64 printf( "\n" );
65 }
66
67 void __attribute__((noinline)) verify(size_t n, const data_t* test, const data_t* correct)
68 {
69 if (coreid != 0)
70 return;
71
72 size_t i;
73 for (i = 0; i < n; i++)
74 {
75 if (test[i] != correct[i])
76 {
77 printf("FAILED test[%d]= %3ld, correct[%d]= %3ld\n",
78 i, (long)test[i], i, (long)correct[i]);
79 exit(-1);
80 }
81 }
82
83 return;
84 }
85
86 //--------------------------------------------------------------------------
87 // matmul function
88
89 // single-thread, naive version
90 void __attribute__((noinline)) matmul_naive(const int lda, const data_t A[], const data_t B[], data_t C[] )
91 {
92 int i, j, k;
93
94 if (coreid > 0)
95 return;
96
97 for ( i = 0; i < lda; i++ )
98 for ( j = 0; j < lda; j++ )
99 {
100 for ( k = 0; k < lda; k++ )
101 {
102 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
103 }
104 }
105
106 }
107
108
109
110 void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
111 {
112
113 // ***************************** //
114 // **** ADD YOUR CODE HERE ***** //
115 // ***************************** //
116 //
117 // feel free to make a separate function for MI and MSI versions.
118 int i, j, k;
119 int space=lda/ncores;
120 int max= space*coreid+space;
121 data_t temp=0;
122
123 data_t temp1=0;
124 data_t temp2=0;
125 data_t temp3=0;
126 data_t temp4=0;
127
128 data_t temp_1=0;
129
130 data_t temp1_1=0;
131 data_t temp2_1=0;
132 data_t temp3_1=0;
133 data_t temp4_1=0;
134
135 data_t temp_2=0;
136
137 data_t temp1_2=0;
138 data_t temp2_2=0;
139 data_t temp3_2=0;
140 data_t temp4_2=0;
141
142 data_t temp_3=0;
143
144 data_t temp1_3=0;
145 data_t temp2_3=0;
146 data_t temp3_3=0;
147 data_t temp4_3=0;
148
149 if (coreid!=ncores-1){
150 //main loop
151 for (i=space*coreid;i<max/4*4;i+=4)
152 {
153 for(j=0;j<lda;j+=4)
154 {
155 temp1=C[j+i*lda];
156 temp2=C[j+1+i*lda];
157 temp3=C[j+2+i*lda];
158 temp4=C[j+3+i*lda];
159
160 temp1_1=C[j+(i+1)*lda];
161 temp2_1=C[j+1+(i+1)*lda];
162 temp3_1=C[j+2+(i+1)*lda];
163 temp4_1=C[j+3+(i+1)*lda];
164
165 temp1_2=C[j+(i+2)*lda];
166 temp2_2=C[j+1+(i+2)*lda];
167 temp3_2=C[j+2+(i+2)*lda];
168 temp4_2=C[j+3+(i+2)*lda];
169
170 temp1_3=C[j+(i+3)*lda];
171 temp2_3=C[j+1+(i+3)*lda];
172 temp3_3=C[j+2+(i+3)*lda];
173 temp4_3=C[j+3+(i+3)*lda];
174 for (k=0;k<lda;k++)
175 {
176 temp=A[k+i*lda];
177 temp1+=temp*B[j+k*lda];
178 temp2+=temp*B[j+1+k*lda];
179 temp3+=temp*B[j+2+k*lda];
180 temp4+=temp*B[j+3+k*lda];
181
182 temp_1=A[k+(i+1)*lda];
183 temp1_1+=temp_1*B[j+k*lda];
184 temp2_1+=temp_1*B[j+1+k*lda];
185 temp3_1+=temp_1*B[j+2+k*lda];
186 temp4_1+=temp_1*B[j+3+k*lda];
187
188 temp_2=A[k+(i+2)*lda];
189 temp1_2+=temp_2*B[j+k*lda];
190 temp2_2+=temp_2*B[j+1+k*lda];
191 temp3_2+=temp_2*B[j+2+k*lda];
192 temp4_2+=temp_2*B[j+3+k*lda];
193
194 temp_3=A[k+(i+3)*lda];
195 temp1_3+=temp_3*B[j+k*lda];
196 temp2_3+=temp_3*B[j+1+k*lda];
197 temp3_3+=temp_3*B[j+2+k*lda];
198 temp4_3+=temp_3*B[j+3+k*lda];
199
200 }
201 C[j+i*lda]=temp1;
202 C[j+1+i*lda]=temp2;
203 C[j+2+i*lda]=temp3;
204 C[j+3+i*lda]=temp4;
205
206 C[j+(i+1)*lda]=temp1_1;
207 C[j+1+(i+1)*lda]=temp2_1;
208 C[j+2+(i+1)*lda]=temp3_1;
209 C[j+3+(i+1)*lda]=temp4_1;
210
211 C[j+(i+2)*lda]=temp1_2;
212 C[j+1+(i+2)*lda]=temp2_2;
213 C[j+2+(i+2)*lda]=temp3_2;
214 C[j+3+(i+2)*lda]=temp4_2;
215
216 C[j+(i+3)*lda]=temp1_3;
217 C[j+1+(i+3)*lda]=temp2_3;
218 C[j+2+(i+3)*lda]=temp3_3;
219 C[j+3+(i+3)*lda]=temp4_3;
220
221 }
222
223 }
224
225
226
227 }
228
229 //second core
230 else{
231 for (i=space*coreid;i<lda/4*4;i+=4)
232 {
233 for(j=0;j<lda;j+=4)
234 {
235 temp1=C[j+i*lda];
236 temp2=C[j+1+i*lda];
237 temp3=C[j+2+i*lda];
238 temp4=C[j+3+i*lda];
239
240 temp1_1=C[j+(i+1)*lda];
241 temp2_1=C[j+1+(i+1)*lda];
242 temp3_1=C[j+2+(i+1)*lda];
243 temp4_1=C[j+3+(i+1)*lda];
244
245 temp1_2=C[j+(i+2)*lda];
246 temp2_2=C[j+1+(i+2)*lda];
247 temp3_2=C[j+2+(i+2)*lda];
248 temp4_2=C[j+3+(i+2)*lda];
249
250 temp1_3=C[j+(i+3)*lda];
251 temp2_3=C[j+1+(i+3)*lda];
252 temp3_3=C[j+2+(i+3)*lda];
253 temp4_3=C[j+3+(i+3)*lda];
254 for (k=0;k<lda;k++)
255 {
256 temp=A[k+i*lda];
257 temp1+=temp*B[j+k*lda];
258 temp2+=temp*B[j+1+k*lda];
259 temp3+=temp*B[j+2+k*lda];
260 temp4+=temp*B[j+3+k*lda];
261
262 temp_1=A[k+(i+1)*lda];
263 temp1_1+=temp_1*B[j+k*lda];
264 temp2_1+=temp_1*B[j+1+k*lda];
265 temp3_1+=temp_1*B[j+2+k*lda];
266 temp4_1+=temp_1*B[j+3+k*lda];
267
268 temp_2=A[k+(i+2)*lda];
269 temp1_2+=temp_2*B[j+k*lda];
270 temp2_2+=temp_2*B[j+1+k*lda];
271 temp3_2+=temp_2*B[j+2+k*lda];
272 temp4_2+=temp_2*B[j+3+k*lda];
273
274 temp_3=A[k+(i+3)*lda];
275 temp1_3+=temp_3*B[j+k*lda];
276 temp2_3+=temp_3*B[j+1+k*lda];
277 temp3_3+=temp_3*B[j+2+k*lda];
278 temp4_3+=temp_3*B[j+3+k*lda];
279
280 }
281 C[j+i*lda]=temp1;
282 C[j+1+i*lda]=temp2;
283 C[j+2+i*lda]=temp3;
284 C[j+3+i*lda]=temp4;
285
286 C[j+(i+1)*lda]=temp1_1;
287 C[j+1+(i+1)*lda]=temp2_1;
288 C[j+2+(i+1)*lda]=temp3_1;
289 C[j+3+(i+1)*lda]=temp4_1;
290
291 C[j+(i+2)*lda]=temp1_2;
292 C[j+1+(i+2)*lda]=temp2_2;
293 C[j+2+(i+2)*lda]=temp3_2;
294 C[j+3+(i+2)*lda]=temp4_2;
295
296 C[j+(i+3)*lda]=temp1_3;
297 C[j+1+(i+3)*lda]=temp2_3;
298 C[j+2+(i+3)*lda]=temp3_3;
299 C[j+3+(i+3)*lda]=temp4_3;
300
301 }
302
303 }
304
305
306 }
307
308
309 }
310
311 //--------------------------------------------------------------------------
312 // Main
313 //
314 // all threads start executing thread_entry(). Use their "coreid" to
315 // differentiate between threads (each thread is running on a separate core).
316
317 void thread_entry(int cid, int nc)
318 {
319 coreid = cid;
320 ncores = nc;
321
322 // static allocates data in the binary, which is visible to both threads
323 static data_t results_data[ARRAY_SIZE];
324
325
326 // // Execute the provided, naive matmul
327 // barrier();
328 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
329 //
330 //
331 // // verify
332 // verify(ARRAY_SIZE, results_data, verify_data);
333 //
334 // // clear results from the first trial
335 // size_t i;
336 // if (coreid == 0)
337 // for (i=0; i < ARRAY_SIZE; i++)
338 // results_data[i] = 0;
339 // barrier();
340
341
342 // Execute your faster matmul
343 barrier();
344 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
345
346 #ifdef DEBUG
347 printArray("results:", ARRAY_SIZE, results_data);
348 printArray("verify :", ARRAY_SIZE, verify_data);
349 #endif
350
351 // verify
352 verify(ARRAY_SIZE, results_data, verify_data);
353 barrier();
354
355 exit(0);
356 }
357