da9a764cf099bc3cb3a43843823fb85bd41fb6a9
[riscv-tests.git] / mt / ba_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 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 /*
93 int i, j, k;
94
95 if (coreid > 0)
96 return;
97
98 for ( i = 0; i < lda; i++ )
99 for ( j = 0; j < lda; j++ )
100 {
101 for ( k = 0; k < lda; k++ )
102 {
103 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
104 }
105 }
106 */
107 }
108
109
110
111 void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
112 {
113 size_t c_start = lda / ncores * coreid;
114 size_t c_row;
115 size_t c_col;
116 size_t colSplit = 0;
117 size_t i;
118 size_t useSplit = 0;
119 data_t a1;
120 data_t a2;
121 data_t a3;
122 data_t a4;
123 data_t a5;
124 data_t a6;
125 data_t a7;
126 data_t a8;
127 data_t c1;
128 data_t c2;
129 data_t c3;
130 data_t c4;
131 data_t c5;
132 data_t c6;
133 data_t c7;
134 data_t c8;
135 size_t block;
136 for (block = 0; block < 2; block++) {
137 for (colSplit = 0; colSplit < 4; colSplit++) {
138 useSplit = (coreid == 0) ? colSplit : (colSplit + 2 ) % 4;
139 for (c_row = c_start + block * 8; c_row < c_start + block * 8 + 8; c_row += 2) {
140 for (c_col = 0; c_col < lda; c_col+=4) {
141 c1 = C[c_row*lda+c_col];
142 c2 = C[(c_row+1)*lda+c_col];
143 c3 = C[c_row*lda+c_col+1];
144 c4 = C[(c_row+1)*lda+c_col+1];
145 c5 = C[c_row*lda+c_col+2];
146 c6 = C[(c_row+1)*lda+c_col+2];
147 c7 = C[c_row*lda+c_col+3];
148 c8 = C[(c_row+1)*lda+c_col+3];
149 for (i = useSplit * lda / 4; i < (useSplit + 1) * lda / 4; i+=4) {
150 a1 = A[c_row*lda+i];
151 a2 = A[(c_row+1)*lda+i];
152 a3 = A[c_row*lda+i+1];
153 a4 = A[(c_row+1)*lda+i+1];
154 a5 = A[c_row*lda+i+2];
155 a6 = A[(c_row+1)*lda+i+2];
156 a7 = A[c_row*lda+i+3];
157 a8 = A[(c_row+1)*lda+i+3];
158
159 c1 += a1 * B[i*lda+c_col];
160 c2 += a2 * B[i*lda+c_col];
161
162 c1 += a3 * B[(i+1)*lda+c_col];
163 c2 += a4 * B[(i+1)*lda+c_col];
164
165 c1 += a5 * B[(i+2)*lda+c_col];
166 c2 += a6 * B[(i+2)*lda+c_col];
167
168 c1 += a7 * B[(i+3)*lda+c_col];
169 c2 += a8 * B[(i+3)*lda+c_col];
170
171 c3 += a1 * B[i*lda+c_col+1];
172 c4 += a2 * B[i*lda+c_col+1];
173
174 c3 += a3 * B[(i+1)*lda+c_col+1];
175 c4 += a4 * B[(i+1)*lda+c_col+1];
176
177 c3 += a5 * B[(i+2)*lda+c_col+1];
178 c4 += a6 * B[(i+2)*lda+c_col+1];
179
180 c3 += a7 * B[(i+3)*lda+c_col+1];
181 c4 += a8 * B[(i+3)*lda+c_col+1];
182
183 c5 += a1 * B[i*lda+c_col+2];
184 c6 += a2 * B[i*lda+c_col+2];
185
186 c5 += a3 * B[(i+1)*lda+c_col+2];
187 c6 += a4 * B[(i+1)*lda+c_col+2];
188
189 c5 += a5 * B[(i+2)*lda+c_col+2];
190 c6 += a6 * B[(i+2)*lda+c_col+2];
191
192 c5 += a7 * B[(i+3)*lda+c_col+2];
193 c6 += a8 * B[(i+3)*lda+c_col+2];
194
195 c7 += a1 * B[i*lda+c_col+3];
196 c8 += a2 * B[i*lda+c_col+3];
197
198 c7 += a3 * B[(i+1)*lda+c_col+3];
199 c8 += a4 * B[(i+1)*lda+c_col+3];
200
201 c7 += a5 * B[(i+2)*lda+c_col+3];
202 c8 += a6 * B[(i+2)*lda+c_col+3];
203
204 c7 += a7 * B[(i+3)*lda+c_col+3];
205 c8 += a8 * B[(i+3)*lda+c_col+3];
206 }
207
208 C[c_row*lda+c_col] = c1;
209 C[(c_row+1)*lda+c_col] = c2;
210
211 C[c_row*lda+c_col+1] = c3;
212 C[(c_row+1)*lda+c_col+1] = c4;
213
214 C[c_row*lda+c_col+2] = c5;
215 C[(c_row+1)*lda+c_col+2] = c6;
216
217 C[c_row*lda+c_col+3] = c7;
218 C[(c_row+1)*lda+c_col+3] = c8;
219 }
220 }
221 }
222 }
223 }
224
225 //--------------------------------------------------------------------------
226 // Main
227 //
228 // all threads start executing thread_entry(). Use their "coreid" to
229 // differentiate between threads (each thread is running on a separate core).
230
231 void thread_entry(int cid, int nc)
232 {
233 coreid = cid;
234 ncores = nc;
235
236 // static allocates data in the binary, which is visible to both threads
237 static data_t results_data[ARRAY_SIZE];
238
239
240 // Execute the provided, naive matmul
241 // barrier();
242 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
243
244
245 // verify
246 // verify(ARRAY_SIZE, results_data, verify_data);
247
248 // clear results from the first trial
249 // size_t i;
250 // if (coreid == 0)
251 // for (i=0; i < ARRAY_SIZE; i++)
252 // results_data[i] = 0;
253 // barrier();
254
255
256 // Execute your faster matmul
257 barrier();
258 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
259
260 #ifdef DEBUG
261 printArray("results:", ARRAY_SIZE, results_data);
262 printArray("verify :", ARRAY_SIZE, verify_data);
263 #endif
264
265 // verify
266 verify(ARRAY_SIZE, results_data, verify_data);
267 barrier();
268
269 exit(0);
270 }
271