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