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 printArrayMT( 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
)) verifyMT(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
];
110 void __attribute__((noinline
)) matmul(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
113 // ***************************** //
114 // **** ADD YOUR CODE HERE ***** //
115 // ***************************** //
117 // feel free to make a separate function for MI and MSI versions.
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
++)
129 for (i
=lda
*lda
/2;i
<lda
*lda
;i
++)
156 if (coreid
!=ncores
-1){
157 for (i
=space
*coreid
;i
<max
/4*4;i
+=4)
159 for(j
=0;j
<lda
/4*4;j
+=4)
162 temp1
=C
[j
+(i
+1)*lda
];
163 temp2
=C
[j
+(i
+2)*lda
];
164 temp3
=C
[j
+(i
+3)*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
];
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
];
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
];
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
;
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
;
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
;
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
;
204 C
[j
+(i
+1)*lda
]=temp1
;
205 C
[j
+(i
+2)*lda
]=temp2
;
206 C
[j
+(i
+3)*lda
]=temp3
;
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
;
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
;
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
;
227 for (i
=space
*coreid
;i
<lda
/4*4;i
+=4)
229 for(j
=0;j
<lda
/4*4;j
+=4)
232 temp1
=C
[j
+(i
+1)*lda
];
233 temp2
=C
[j
+(i
+2)*lda
];
234 temp3
=C
[j
+(i
+3)*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
];
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
];
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
];
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
;
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
;
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
;
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
;
274 C
[j
+(i
+1)*lda
]=temp1
;
275 C
[j
+(i
+2)*lda
]=temp2
;
276 C
[j
+(i
+3)*lda
]=temp3
;
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
;
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
;
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
;
302 //--------------------------------------------------------------------------
305 // all threads start executing thread_entry(). Use their "coreid" to
306 // differentiate between threads (each thread is running on a separate core).
308 void thread_entry(int cid
, int nc
)
313 // static allocates data in the binary, which is visible to both threads
314 static data_t results_data
[ARRAY_SIZE
];
317 // // Execute the provided, naive matmul
319 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
323 // verifyMT(ARRAY_SIZE, results_data, verify_data);
325 // // clear results from the first trial
328 // for (i=0; i < ARRAY_SIZE; i++)
329 // results_data[i] = 0;
333 // Execute your faster matmul
335 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
338 printArrayMT("results:", ARRAY_SIZE
, results_data
);
339 printArrayMT("verify :", ARRAY_SIZE
, verify_data
);
343 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);