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
];
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 i
, j
, k
, jj
, kk
;
120 int start_i
= coreid
*lda
/2;
121 int end_i
= start_i
+ lda
/2;
123 int start_k
, end_k
, start_j
, end_j
;
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
;
170 for( kk
= start_k
; kk
!= end_k
; kk
+=(step_k
*16) )
172 for( jj
= start_j
; jj
!= end_j
; jj
+=(step_j
*8) )
174 for ( i
= start_i
; i
< end_i
; i
+=8 )
176 //pos_C = i + jj*lda;
177 for ( j
= jj
; j
!= (jj
+(step_j
*8)) ; j
+=step_j
)
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)];
191 pos_C
= i
+ (j
+1)*lda
;
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)];
204 for ( k
= kk
; k
!= (kk
+(step_k
*16)) ; k
+=step_k
)
206 temp_A0
= A
[ pos_A
] ;
207 temp_A1
= A
[pos_A
+lda
];
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)];
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)];
227 pos_B
+= (lda
*step_k
) ;
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
;
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
;
253 //pos_C += step_j * lda;
264 //--------------------------------------------------------------------------
267 // all threads start executing thread_entry(). Use their "coreid" to
268 // differentiate between threads (each thread is running on a separate core).
270 void thread_entry(int cid
, int nc
)
275 // static allocates data in the binary, which is visible to both threads
276 static data_t results_data
[ARRAY_SIZE
];
279 // Execute the provided, naive matmul
281 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
285 verify(ARRAY_SIZE, results_data, verify_data);
287 // clear results from the first trial
290 for (i=0; i < ARRAY_SIZE; i++)
295 // Execute your faster matmul
297 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
302 printArray("results:", ARRAY_SIZE
, results_data
);
303 printArray("verify :", ARRAY_SIZE
, verify_data
);
307 verify(ARRAY_SIZE
, results_data
, verify_data
);
311 //printf("input1_data");