Updated mt tests
[riscv-tests.git] / mt / bm_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
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 printArrayMT( 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)) verifyMT(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 static data_t B1[32*32];
122 if (coreid==ncores-1){
123 for (i=0; i<lda*lda/2;i++)
124 {
125 B1[i]=B[i];
126 }
127 }
128 else{
129 for (i=lda*lda/2;i<lda*lda;i++)
130 B1[i]=B[i];
131 }
132 data_t temp=0;
133 data_t temp1=0;
134 data_t temp2=0;
135 data_t temp3=0;
136 data_t tempB=0;
137
138 data_t temp_1=0;
139 data_t temp1_1=0;
140 data_t temp2_1=0;
141 data_t temp3_1=0;
142 data_t tempB_1=0;
143
144 data_t temp_2=0;
145 data_t temp1_2=0;
146 data_t temp2_2=0;
147 data_t temp3_2=0;
148 data_t tempB_2=0;
149
150 data_t temp_3=0;
151 data_t temp1_3=0;
152 data_t temp2_3=0;
153 data_t temp3_3=0;
154 data_t tempB_3=0;
155 barrier(nc);
156 if (coreid!=ncores-1){
157 for (i=space*coreid;i<max/4*4;i+=4)
158 {
159 for(j=0;j<lda/4*4;j+=4)
160 {
161 temp=C[j+i*lda];
162 temp1=C[j+(i+1)*lda];
163 temp2=C[j+(i+2)*lda];
164 temp3=C[j+(i+3)*lda];
165 temp_1=C[j+1+i*lda];
166 temp1_1=C[j+1+(i+1)*lda];
167 temp2_1=C[j+1+(i+2)*lda];
168 temp3_1=C[j+1+(i+3)*lda];
169 temp_2=C[j+2+i*lda];
170 temp1_2=C[j+2+(i+1)*lda];
171 temp2_2=C[j+2+(i+2)*lda];
172 temp3_2=C[j+2+(i+3)*lda];
173 temp_3=C[j+3+i*lda];
174 temp1_3=C[j+3+(i+1)*lda];
175 temp2_3=C[j+3+(i+2)*lda];
176 temp3_3=C[j+3+(i+3)*lda];
177 for (k=0;k<lda;k++)
178 {
179 tempB=B[j+k*lda];
180 temp+=A[k+i*lda]*tempB;
181 temp1+=A[k+(i+1)*lda]*tempB;
182 temp2+=A[k+(i+2)*lda]*tempB;
183 temp3+=A[k+(i+3)*lda]*tempB;
184
185 tempB_1=B[j+1+k*lda];
186 temp_1+=A[k+i*lda]*tempB_1;
187 temp1_1+=A[k+(i+1)*lda]*tempB_1;
188 temp2_1+=A[k+(i+2)*lda]*tempB_1;
189 temp3_1+=A[k+(i+3)*lda]*tempB_1;
190
191 tempB_2=B[j+2+k*lda];
192 temp_2+=A[k+i*lda]*tempB_2;
193 temp1_2+=A[k+(i+1)*lda]*tempB_2;
194 temp2_2+=A[k+(i+2)*lda]*tempB_2;
195 temp3_2+=A[k+(i+3)*lda]*tempB_2;
196
197 tempB_3=B[j+3+k*lda];
198 temp_3+=A[k+i*lda]*tempB_3;
199 temp1_3+=A[k+(i+1)*lda]*tempB_3;
200 temp2_3+=A[k+(i+2)*lda]*tempB_3;
201 temp3_3+=A[k+(i+3)*lda]*tempB_3;
202 }
203 C[j+i*lda]=temp;
204 C[j+(i+1)*lda]=temp1;
205 C[j+(i+2)*lda]=temp2;
206 C[j+(i+3)*lda]=temp3;
207
208 C[j+1+i*lda]=temp_1;
209 C[j+1+(i+1)*lda]=temp1_1;
210 C[j+1+(i+2)*lda]=temp2_1;
211 C[j+1+(i+3)*lda]=temp3_1;
212
213 C[j+2+i*lda]=temp_2;
214 C[j+2+(i+1)*lda]=temp1_2;
215 C[j+2+(i+2)*lda]=temp2_2;
216 C[j+2+(i+3)*lda]=temp3_2;
217
218 C[j+3+i*lda]=temp_3;
219 C[j+3+(i+1)*lda]=temp1_3;
220 C[j+3+(i+2)*lda]=temp2_3;
221 C[j+3+(i+3)*lda]=temp3_3;
222
223 }
224 }
225 }
226 else{
227 for (i=space*coreid;i<lda/4*4;i+=4)
228 {
229 for(j=0;j<lda/4*4;j+=4)
230 {
231 temp=C[j+i*lda];
232 temp1=C[j+(i+1)*lda];
233 temp2=C[j+(i+2)*lda];
234 temp3=C[j+(i+3)*lda];
235 temp_1=C[j+1+i*lda];
236 temp1_1=C[j+1+(i+1)*lda];
237 temp2_1=C[j+1+(i+2)*lda];
238 temp3_1=C[j+1+(i+3)*lda];
239 temp_2=C[j+2+i*lda];
240 temp1_2=C[j+2+(i+1)*lda];
241 temp2_2=C[j+2+(i+2)*lda];
242 temp3_2=C[j+2+(i+3)*lda];
243 temp_3=C[j+3+i*lda];
244 temp1_3=C[j+3+(i+1)*lda];
245 temp2_3=C[j+3+(i+2)*lda];
246 temp3_3=C[j+3+(i+3)*lda];
247 for (k=0;k<lda;k++)
248 {
249 tempB=B1[j+k*lda];
250 temp+=A[k+i*lda]*tempB;
251 temp1+=A[k+(i+1)*lda]*tempB;
252 temp2+=A[k+(i+2)*lda]*tempB;
253 temp3+=A[k+(i+3)*lda]*tempB;
254
255 tempB_1=B1[j+1+k*lda];
256 temp_1+=A[k+i*lda]*tempB_1;
257 temp1_1+=A[k+(i+1)*lda]*tempB_1;
258 temp2_1+=A[k+(i+2)*lda]*tempB_1;
259 temp3_1+=A[k+(i+3)*lda]*tempB_1;
260
261 tempB_2=B1[j+2+k*lda];
262 temp_2+=A[k+i*lda]*tempB_2;
263 temp1_2+=A[k+(i+1)*lda]*tempB_2;
264 temp2_2+=A[k+(i+2)*lda]*tempB_2;
265 temp3_2+=A[k+(i+3)*lda]*tempB_2;
266
267 tempB_3=B1[j+3+k*lda];
268 temp_3+=A[k+i*lda]*tempB_3;
269 temp1_3+=A[k+(i+1)*lda]*tempB_3;
270 temp2_3+=A[k+(i+2)*lda]*tempB_3;
271 temp3_3+=A[k+(i+3)*lda]*tempB_3;
272 }
273 C[j+i*lda]=temp;
274 C[j+(i+1)*lda]=temp1;
275 C[j+(i+2)*lda]=temp2;
276 C[j+(i+3)*lda]=temp3;
277
278 C[j+1+i*lda]=temp_1;
279 C[j+1+(i+1)*lda]=temp1_1;
280 C[j+1+(i+2)*lda]=temp2_1;
281 C[j+1+(i+3)*lda]=temp3_1;
282
283 C[j+2+i*lda]=temp_2;
284 C[j+2+(i+1)*lda]=temp1_2;
285 C[j+2+(i+2)*lda]=temp2_2;
286 C[j+2+(i+3)*lda]=temp3_2;
287
288 C[j+3+i*lda]=temp_3;
289 C[j+3+(i+1)*lda]=temp1_3;
290 C[j+3+(i+2)*lda]=temp2_3;
291 C[j+3+(i+3)*lda]=temp3_3;
292
293 }
294 }
295 }
296
297
298
299
300 }
301
302 //--------------------------------------------------------------------------
303 // Main
304 //
305 // all threads start executing thread_entry(). Use their "coreid" to
306 // differentiate between threads (each thread is running on a separate core).
307
308 void thread_entry(int cid, int nc)
309 {
310 coreid = cid;
311 ncores = nc;
312
313 // static allocates data in the binary, which is visible to both threads
314 static data_t results_data[ARRAY_SIZE];
315
316
317 // // Execute the provided, naive matmul
318 // barrier(nc);
319 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
320 //
321 //
322 // // verify
323 // verifyMT(ARRAY_SIZE, results_data, verify_data);
324 //
325 // // clear results from the first trial
326 // size_t i;
327 // if (coreid == 0)
328 // for (i=0; i < ARRAY_SIZE; i++)
329 // results_data[i] = 0;
330 // barrier(nc);
331
332
333 // Execute your faster matmul
334 barrier(nc);
335 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
336
337 #ifdef DEBUG
338 printArrayMT("results:", ARRAY_SIZE, results_data);
339 printArrayMT("verify :", ARRAY_SIZE, verify_data);
340 #endif
341
342 // verify
343 verifyMT(ARRAY_SIZE, results_data, verify_data);
344 barrier(nc);
345
346 exit(0);
347 }
348