1 //**************************************************************************
2 // Multi-threaded Matrix Multiply benchmark
3 //--------------------------------------------------------------------------
4 // TA : Christopher Celio
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
14 // print out arrays, etc.
17 //--------------------------------------------------------------------------
25 //--------------------------------------------------------------------------
26 // Input/Reference Data
32 //--------------------------------------------------------------------------
33 // Basic Utilities and Multi-thread Support
35 __thread
unsigned long coreid
;
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(); \
45 _c += rdcycle(), _i += rdinstret(); \
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); \
52 //--------------------------------------------------------------------------
55 void printArray( char name
[], int n
, data_t arr
[] )
61 printf( " %10s :", name
);
62 for ( i
= 0; i
< n
; i
++ )
63 printf( " %3ld ", (long) arr
[i
] );
67 void __attribute__((noinline
)) verify(size_t n
, const data_t
* test
, const data_t
* correct
)
73 for (i
= 0; i
< n
; i
++)
75 if (test
[i
] != correct
[i
])
77 printf("FAILED test[%d]= %3ld, correct[%d]= %3ld\n",
78 i
, (long)test
[i
], i
, (long)correct
[i
]);
86 //--------------------------------------------------------------------------
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
[] )
97 for ( i
= 0; i
< lda
; i
++ )
98 for ( j
= 0; j
< lda
; j
++ )
100 for ( k
= 0; k
< lda
; k
++ )
102 C
[i
+ j
*lda
] += A
[j
*lda
+ k
] * B
[k
*lda
+ i
];
108 void __attribute__((noinline
)) matmul_MI_transpose(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
111 data_t B_trans
[32*32];
112 data_t acc_temp0
, acc_temp1
;
114 data_t
*A_j_k
, *B_i_k
;
117 //for (i = 0; i < 32; i++) {
118 // for (j = 0; j < 32; j++) {
119 // B_trans[i*lda+j] = B[i+j*lda];
124 for (i
= 0; i
< 32; i
++) {
126 for (z
= 0; z
< 32; z
++) {
127 *(B_i
+z
) = B
[i
+z
*32];
129 for (j
= 0; j
< 16; j
+=2) {
132 for (k
= 0; k
< 32; k
+=8) {
135 acc_temp0
+= *(A_j_k
) * *(B_i_k
);
136 acc_temp0
+= *(A_j_k
+ 1) * *(B_i_k
+ 1);
137 acc_temp0
+= *(A_j_k
+ 2) * *(B_i_k
+ 2);
138 acc_temp0
+= *(A_j_k
+ 3) * *(B_i_k
+ 3);
139 acc_temp0
+= *(A_j_k
+ 4) * *(B_i_k
+ 4);
140 acc_temp0
+= *(A_j_k
+ 5) * *(B_i_k
+ 5);
141 acc_temp0
+= *(A_j_k
+ 6) * *(B_i_k
+ 6);
142 acc_temp0
+= *(A_j_k
+ 7) * *(B_i_k
+ 7);
147 for (k
= 0; k
< 32; k
+=8) {
148 acc_temp1
+= *(A_j
+k
) * *(B_i
+k
);
149 acc_temp1
+= *(A_j
+k
+ 1) * *(B_i
+k
+ 1);
150 acc_temp1
+= *(A_j
+k
+ 2) * *(B_i
+k
+ 2);
151 acc_temp1
+= *(A_j
+k
+ 3) * *(B_i
+k
+ 3);
152 acc_temp1
+= *(A_j
+k
+ 4) * *(B_i
+k
+ 4);
153 acc_temp1
+= *(A_j
+k
+ 5) * *(B_i
+k
+ 5);
154 acc_temp1
+= *(A_j
+k
+ 6) * *(B_i
+k
+ 6);
155 acc_temp1
+= *(A_j
+k
+ 7) * *(B_i
+k
+ 7);
158 C
[i
+ j
*lda
] = acc_temp0
;
159 C
[i
+ (j
+1)*lda
] = acc_temp1
;
162 } else if (coreid
== 1) {
163 for (i
= 0; i
< 32; i
++) {
165 for (z
= 0; z
< 32; z
++) {
166 *(B_i
+z
) = B
[i
+z
*32];
168 for (j
= 16; j
< 32; j
+=2) {
171 for (k
= 0; k
< 32; k
+=8) {
172 acc_temp0
+= *(A_j
+k
) * *(B_i
+k
);
173 acc_temp0
+= *(A_j
+k
+ 1) * *(B_i
+k
+ 1);
174 acc_temp0
+= *(A_j
+k
+ 2) * *(B_i
+k
+ 2);
175 acc_temp0
+= *(A_j
+k
+ 3) * *(B_i
+k
+ 3);
176 acc_temp0
+= *(A_j
+k
+ 4) * *(B_i
+k
+ 4);
177 acc_temp0
+= *(A_j
+k
+ 5) * *(B_i
+k
+ 5);
178 acc_temp0
+= *(A_j
+k
+ 6) * *(B_i
+k
+ 6);
179 acc_temp0
+= *(A_j
+k
+ 7) * *(B_i
+k
+ 7);
184 for (k
= 0; k
< 32; k
+=8) {
185 acc_temp1
+= *(A_j
+k
) * *(B_i
+k
);
186 acc_temp1
+= *(A_j
+k
+ 1) * *(B_i
+k
+ 1);
187 acc_temp1
+= *(A_j
+k
+ 2) * *(B_i
+k
+ 2);
188 acc_temp1
+= *(A_j
+k
+ 3) * *(B_i
+k
+ 3);
189 acc_temp1
+= *(A_j
+k
+ 4) * *(B_i
+k
+ 4);
190 acc_temp1
+= *(A_j
+k
+ 5) * *(B_i
+k
+ 5);
191 acc_temp1
+= *(A_j
+k
+ 6) * *(B_i
+k
+ 6);
192 acc_temp1
+= *(A_j
+k
+ 7) * *(B_i
+k
+ 7);
194 C
[i
+ j
*lda
] = acc_temp0
;
195 C
[i
+ (j
+1)*lda
] = acc_temp1
;
201 void __attribute__((noinline
)) matmul_MI(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
206 int j_start
= coreid
*16;
207 int j_end
= (coreid
*16)+16;
209 for ( i
= 0; i
< 32; i
++ ) {
211 for ( j
= j_start
; j
< j_end
; j
++ )
215 for ( k
= 0; k
< 32; k
++ )
217 acc_temp
+= *(A_j
+ k
) * *(B_i
+ k
*32);
219 C
[i
+ j
*32] = acc_temp
;
222 } else if (coreid
== 1) {
223 for ( i
= 16; i
< 32; i
++ ) {
225 for ( j
= j_start
; j
< j_end
; j
++ )
229 for ( k
= 0; k
< 32; k
+=4 )
231 acc_temp
+= *(A_j
+ k
) * *(B_i
+ k
*32);
232 acc_temp
+= *(A_j
+ k
+ 1) * *(B_i
+ (k
+1)*32);
233 acc_temp
+= *(A_j
+ k
+ 2) * *(B_i
+ (k
+2)*32);
234 acc_temp
+= *(A_j
+ k
+ 3) * *(B_i
+ (k
+3)*32);
236 C
[i
+ j
*32] = acc_temp
;
239 for ( i
= 0; i
< 16; i
++ ) {
241 for ( j
= j_start
; j
< j_end
; j
++ )
245 for ( k
= 0; k
< 32; k
+=4 )
247 acc_temp
+= *(A_j
+ k
) * *(B_i
+ k
*32);
248 acc_temp
+= *(A_j
+ k
+ 1) * *(B_i
+ (k
+1)*32);
249 acc_temp
+= *(A_j
+ k
+ 2) * *(B_i
+ (k
+2)*32);
250 acc_temp
+= *(A_j
+ k
+ 3) * *(B_i
+ (k
+3)*32);
252 C
[i
+ j
*32] = acc_temp
;
259 void __attribute__((noinline
)) matmul_MSI(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
264 int j_start
= coreid
*16;
265 int j_end
= (coreid
*16)+16;
266 for ( i
= 0; i
< 32; i
++ ) {
268 for ( j
= j_start
; j
< j_end
; j
++ )
272 for ( k
= 0; k
< 32; k
++ )
274 acc_temp
+= *(A_j
+ k
) * *(B_i
+ k
*32);
276 C
[i
+ j
*32] = acc_temp
;
281 void __attribute__((noinline
)) matmul(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
284 // ***************************** //
285 // **** ADD YOUR CODE HERE ***** //
286 // ***************************** //
288 // feel free to make a separate function for MI and MSI versions.
289 // ENABLE_SHARING = false is MI
290 // ENABLE_SHARING = true is MSI
291 matmul_MI_transpose(lda
, A
, B
, C
);
292 //matmul_MSI(lda, A, B, C);
295 //--------------------------------------------------------------------------
298 // all threads start executing thread_entry(). Use their "coreid" to
299 // differentiate between threads (each thread is running on a separate core).
301 void thread_entry(int cid
, int nc
)
306 // static allocates data in the binary, which is visible to both threads
307 static data_t results_data
[ARRAY_SIZE
];
310 // // Execute the provided, naive matmul
312 // //stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
316 // //verify(ARRAY_SIZE, results_data, verify_data);
318 // // clear results from the first trial
321 // for (i=0; i < ARRAY_SIZE; i++)
322 // results_data[i] = 0;
326 // Execute your faster matmul
328 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
331 printArray("results:", ARRAY_SIZE
, results_data
);
332 printArray("verify :", ARRAY_SIZE
, verify_data
);
336 verify(ARRAY_SIZE
, results_data
, verify_data
);