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
;
153 for( kk
= start_k
; kk
!= end_k
; kk
+=(step_k
*16) )
155 for( jj
= start_j
; jj
!= end_j
; jj
+=(step_j
*8) )
157 for ( i
= start_i
; i
< end_i
; i
+=8 )
159 //pos_C = i + jj*lda;
160 for ( j
= jj
; j
!= (jj
+(step_j
*8)) ; j
+=step_j
)
164 temp00
= C
[(pos_C
+ 0)];
165 temp01
= C
[(pos_C
+ 1)];
166 temp02
= C
[(pos_C
+ 2)];
167 temp03
= C
[(pos_C
+ 3)];
168 temp04
= C
[(pos_C
+ 4)];
169 temp05
= C
[(pos_C
+ 5)];
170 temp06
= C
[(pos_C
+ 6)];
171 temp07
= C
[(pos_C
+ 7)];
174 pos_C
= i
+ (j
+1)*lda
;
176 temp10
= C
[(pos_C
+ 0)];
177 temp11
= C
[(pos_C
+ 1)];
178 temp12
= C
[(pos_C
+ 2)];
179 temp13
= C
[(pos_C
+ 3)];
180 temp14
= C
[(pos_C
+ 4)];
181 temp15
= C
[(pos_C
+ 5)];
182 temp16
= C
[(pos_C
+ 6)];
183 temp17
= C
[(pos_C
+ 7)];
187 for ( k
= kk
; k
!= (kk
+(step_k
*16)) ; k
+=step_k
)
189 temp_A0
= A
[ pos_A
] ;
190 temp_A1
= A
[pos_A
+lda
];
192 temp00
+= temp_A0
* B
[(pos_B
+ 0)];
193 temp01
+= temp_A0
* B
[(pos_B
+ 1)];
194 temp02
+= temp_A0
* B
[(pos_B
+ 2)];
195 temp03
+= temp_A0
* B
[(pos_B
+ 3)];
196 temp04
+= temp_A0
* B
[(pos_B
+ 4)];
197 temp05
+= temp_A0
* B
[(pos_B
+ 5)];
198 temp06
+= temp_A0
* B
[(pos_B
+ 6)];
199 temp07
+= temp_A0
* B
[(pos_B
+ 7)];
201 temp10
+= temp_A1
* B
[(pos_B
+ 0)];
202 temp11
+= temp_A1
* B
[(pos_B
+ 1)];
203 temp12
+= temp_A1
* B
[(pos_B
+ 2)];
204 temp13
+= temp_A1
* B
[(pos_B
+ 3)];
205 temp14
+= temp_A1
* B
[(pos_B
+ 4)];
206 temp15
+= temp_A1
* B
[(pos_B
+ 5)];
207 temp16
+= temp_A1
* B
[(pos_B
+ 6)];
208 temp17
+= temp_A1
* B
[(pos_B
+ 7)];
210 pos_B
+= (lda
*step_k
) ;
215 C
[(pos_C
+ 0)] = temp10
;
216 C
[(pos_C
+ 1)] = temp11
;
217 C
[(pos_C
+ 2)] = temp12
;
218 C
[(pos_C
+ 3)] = temp13
;
219 C
[(pos_C
+ 4)] = temp14
;
220 C
[(pos_C
+ 5)] = temp15
;
221 C
[(pos_C
+ 6)] = temp16
;
222 C
[(pos_C
+ 7)] = temp17
;
227 C
[(pos_C
+ 0)] = temp00
;
228 C
[(pos_C
+ 1)] = temp01
;
229 C
[(pos_C
+ 2)] = temp02
;
230 C
[(pos_C
+ 3)] = temp03
;
231 C
[(pos_C
+ 4)] = temp04
;
232 C
[(pos_C
+ 5)] = temp05
;
233 C
[(pos_C
+ 6)] = temp06
;
234 C
[(pos_C
+ 7)] = temp07
;
236 //pos_C += step_j * lda;
247 //--------------------------------------------------------------------------
250 // all threads start executing thread_entry(). Use their "coreid" to
251 // differentiate between threads (each thread is running on a separate core).
253 void thread_entry(int cid
, int nc
)
258 // static allocates data in the binary, which is visible to both threads
259 static data_t results_data
[ARRAY_SIZE
];
262 // Execute the provided, naive matmul
264 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
268 verify(ARRAY_SIZE, results_data, verify_data);
270 // clear results from the first trial
273 for (i=0; i < ARRAY_SIZE; i++)
278 // Execute your faster matmul
280 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
285 printArray("results:", ARRAY_SIZE
, results_data
);
286 printArray("verify :", ARRAY_SIZE
, verify_data
);
290 verify(ARRAY_SIZE
, results_data
, verify_data
);
294 //printf("input1_data");