Updated mt tests
[riscv-tests.git] / mt / be_matmul / be_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 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
119 int i, j, k , jj , kk;
120 int start_i = coreid*lda/2;
121 int end_i = start_i + lda/2;
122 int step_j, step_k;
123 int start_k, end_k, start_j, end_j;
124 int j_lda;
125 int pos_A , pos_B, pos_C;
126 data_t temp00, temp01,temp02,temp03,temp04,temp05,temp06,temp07;
127 data_t temp10, temp11,temp12,temp13,temp14,temp15,temp16,temp17;
128 data_t temp_A0, temp_A1, temp_A2, temp_A3, temp_A4, temp_A5, temp_A6, temp_A7;
129
130 temp00 = 0;
131 temp01 = 0;
132 temp02 = 0;
133 temp03 = 0;
134 temp04 = 0;
135 temp05 = 0;
136 temp06 = 0;
137 temp07 = 0;
138
139 temp10 = 0;
140 temp11 = 0;
141 temp12 = 0;
142 temp13 = 0;
143 temp14 = 0;
144 temp15 = 0;
145 temp16 = 0;
146 temp17 = 0;
147
148 if (coreid == 0)
149 {
150 step_k = 1;
151 start_k= 0;
152 end_k = lda;
153
154 step_j = 2;
155 start_j= 0;
156 end_j = lda;
157
158 }else
159 {
160
161 step_k = -1;
162 start_k = lda-1;
163 end_k = -1;
164
165 step_j = -2;
166 start_j= lda-2;
167 end_j = -2;
168 }
169
170 for( kk = start_k ; kk!= end_k ; kk+=(step_k*16) )
171 {
172 for( jj = start_j ; jj!= end_j ; jj+=(step_j*8) )
173 {
174 for ( i = start_i; i < end_i; i+=8 )
175 {
176 //pos_C = i + jj*lda;
177 for ( j = jj; j != (jj+(step_j*8)) ; j+=step_j )
178 {
179
180 pos_C = i + j*lda;
181 temp00 = C[(pos_C + 0)];
182 temp01 = C[(pos_C + 1)];
183 temp02 = C[(pos_C + 2)];
184 temp03 = C[(pos_C + 3)];
185 temp04 = C[(pos_C + 4)];
186 temp05 = C[(pos_C + 5)];
187 temp06 = C[(pos_C + 6)];
188 temp07 = C[(pos_C + 7)];
189
190 //pos_C += lda;
191 pos_C = i + (j+1)*lda;
192
193 temp10 = C[(pos_C + 0)];
194 temp11 = C[(pos_C + 1)];
195 temp12 = C[(pos_C + 2)];
196 temp13 = C[(pos_C + 3)];
197 temp14 = C[(pos_C + 4)];
198 temp15 = C[(pos_C + 5)];
199 temp16 = C[(pos_C + 6)];
200 temp17 = C[(pos_C + 7)];
201
202 pos_B = kk*lda + i;
203 pos_A = j*lda + kk;
204 for ( k = kk; k != (kk+(step_k*16)) ; k+=step_k )
205 {
206 temp_A0 = A[ pos_A ] ;
207 temp_A1 = A[pos_A +lda];
208
209 temp00 += temp_A0 * B[(pos_B + 0)];
210 temp01 += temp_A0 * B[(pos_B + 1)];
211 temp02 += temp_A0 * B[(pos_B + 2)];
212 temp03 += temp_A0 * B[(pos_B + 3)];
213 temp04 += temp_A0 * B[(pos_B + 4)];
214 temp05 += temp_A0 * B[(pos_B + 5)];
215 temp06 += temp_A0 * B[(pos_B + 6)];
216 temp07 += temp_A0 * B[(pos_B + 7)];
217
218 temp10 += temp_A1 * B[(pos_B + 0)];
219 temp11 += temp_A1 * B[(pos_B + 1)];
220 temp12 += temp_A1 * B[(pos_B + 2)];
221 temp13 += temp_A1 * B[(pos_B + 3)];
222 temp14 += temp_A1 * B[(pos_B + 4)];
223 temp15 += temp_A1 * B[(pos_B + 5)];
224 temp16 += temp_A1 * B[(pos_B + 6)];
225 temp17 += temp_A1 * B[(pos_B + 7)];
226
227 pos_B += (lda*step_k) ;
228 pos_A += step_k;
229 }
230 //barrier(nc);
231
232 C[(pos_C + 0)] = temp10;
233 C[(pos_C + 1)] = temp11;
234 C[(pos_C + 2)] = temp12;
235 C[(pos_C + 3)] = temp13;
236 C[(pos_C + 4)] = temp14;
237 C[(pos_C + 5)] = temp15;
238 C[(pos_C + 6)] = temp16;
239 C[(pos_C + 7)] = temp17;
240 //barrier(nc);
241
242 pos_C = i + j*lda;
243 //pos_C -= lda;
244 C[(pos_C + 0)] = temp00;
245 C[(pos_C + 1)] = temp01;
246 C[(pos_C + 2)] = temp02;
247 C[(pos_C + 3)] = temp03;
248 C[(pos_C + 4)] = temp04;
249 C[(pos_C + 5)] = temp05;
250 C[(pos_C + 6)] = temp06;
251 C[(pos_C + 7)] = temp07;
252 //barrier(nc);
253 //pos_C += step_j * lda;
254 }
255 //barrier(nc);
256 }
257 //barrier(nc);
258
259 }
260 //barrier(nc);
261 }
262 }
263
264 //--------------------------------------------------------------------------
265 // Main
266 //
267 // all threads start executing thread_entry(). Use their "coreid" to
268 // differentiate between threads (each thread is running on a separate core).
269
270 void thread_entry(int cid, int nc)
271 {
272 coreid = cid;
273 ncores = nc;
274
275 // static allocates data in the binary, which is visible to both threads
276 static data_t results_data[ARRAY_SIZE];
277
278 /*
279 // Execute the provided, naive matmul
280 barrier(nc);
281 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
282
283
284 // verify
285 verifyMT(ARRAY_SIZE, results_data, verify_data);
286
287 // clear results from the first trial
288 size_t i;
289 if (coreid == 0)
290 for (i=0; i < ARRAY_SIZE; i++)
291 results_data[i] = 0;
292 barrier(nc);
293
294 */
295 // Execute your faster matmul
296 barrier(nc);
297 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
298
299
300
301 #ifdef DEBUG
302 printArrayMT("results:", ARRAY_SIZE, results_data);
303 printArrayMT("verify :", ARRAY_SIZE, verify_data);
304 #endif
305
306 // verify
307 verifyMT(ARRAY_SIZE, results_data, verify_data);
308 barrier(nc);
309
310
311 //printf("input1_data");
312 exit(0);
313
314 }