47b0992ab6b458ee8fb3c6eb4c4185fd59459a72
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
[] )
112 // feel free to make a separate function for MI and MSI versions.
114 data_t temp0
, temp1
, temp2
, temp3
, temp4
, temp5
, temp6
, temp7
;
115 data_t temp8
, temp9
, temp10
, temp11
, temp12
, temp13
, temp14
, temp15
;
119 for(j
= 0; j
< 32; j
++) {
121 temp1
= C
[1 + j
*lda
];
122 temp2
= C
[2 + j
*lda
];
123 temp3
= C
[3 + j
*lda
];
124 temp4
= C
[4 + j
*lda
];
125 temp5
= C
[5 + j
*lda
];
126 temp6
= C
[6 + j
*lda
];
127 temp7
= C
[7 + j
*lda
];
128 temp8
= C
[8 + j
*lda
];
129 temp9
= C
[9 + j
*lda
];
130 temp10
= C
[10 + j
*lda
];
131 temp11
= C
[11 + j
*lda
];
132 temp12
= C
[12 + j
*lda
];
133 temp13
= C
[13 + j
*lda
];
134 temp14
= C
[14 + j
*lda
];
135 temp15
= C
[15 + j
*lda
];
136 for(k
= 0; k
< 32; k
++) {
137 temp0
+= A
[j
*lda
+ k
] * B
[k
*lda
];
138 temp1
+= A
[j
*lda
+ k
] * B
[1 + k
*lda
];
139 temp2
+= A
[j
*lda
+ k
] * B
[2 + k
*lda
];
140 temp3
+= A
[j
*lda
+ k
] * B
[3 + k
*lda
];
141 temp4
+= A
[j
*lda
+ k
] * B
[4 + k
*lda
];
142 temp5
+= A
[j
*lda
+ k
] * B
[5 + k
*lda
];
143 temp6
+= A
[j
*lda
+ k
] * B
[6 + k
*lda
];
144 temp7
+= A
[j
*lda
+ k
] * B
[7 + k
*lda
];
145 temp8
+= A
[j
*lda
+ k
] * B
[8 + k
*lda
];
146 temp9
+= A
[j
*lda
+ k
] * B
[9 + k
*lda
];
147 temp10
+= A
[j
*lda
+ k
] * B
[10 + k
*lda
];
148 temp11
+= A
[j
*lda
+ k
] * B
[11 + k
*lda
];
149 temp12
+= A
[j
*lda
+ k
] * B
[12 + k
*lda
];
150 temp13
+= A
[j
*lda
+ k
] * B
[13 + k
*lda
];
151 temp14
+= A
[j
*lda
+ k
] * B
[14 + k
*lda
];
152 temp15
+= A
[j
*lda
+ k
] * B
[15 + k
*lda
];
155 C
[1 + j
*lda
] = temp1
;
156 C
[2 + j
*lda
] = temp2
;
157 C
[3 + j
*lda
] = temp3
;
158 C
[4 + j
*lda
] = temp4
;
159 C
[5 + j
*lda
] = temp5
;
160 C
[6 + j
*lda
] = temp6
;
161 C
[7 + j
*lda
] = temp7
;
162 C
[8 + j
*lda
] = temp8
;
163 C
[9 + j
*lda
] = temp9
;
164 C
[10 + j
*lda
] = temp10
;
165 C
[11 + j
*lda
] = temp11
;
166 C
[12 + j
*lda
] = temp12
;
167 C
[13 + j
*lda
] = temp13
;
168 C
[14 + j
*lda
] = temp14
;
169 C
[15 + j
*lda
] = temp15
;
174 for(j
= 16; j
< 32; j
++) {
175 temp0
= C
[16 + j
*lda
];
176 temp1
= C
[17 + j
*lda
];
177 temp2
= C
[18 + j
*lda
];
178 temp3
= C
[19 + j
*lda
];
179 temp4
= C
[20 + j
*lda
];
180 temp5
= C
[21 + j
*lda
];
181 temp6
= C
[22 + j
*lda
];
182 temp7
= C
[23 + j
*lda
];
183 temp8
= C
[24 + j
*lda
];
184 temp9
= C
[25 + j
*lda
];
185 temp10
= C
[26 + j
*lda
];
186 temp11
= C
[27 + j
*lda
];
187 temp12
= C
[28 + j
*lda
];
188 temp13
= C
[29 + j
*lda
];
189 temp14
= C
[30 + j
*lda
];
190 temp15
= C
[31 + j
*lda
];
191 for(k
= 0; k
< 32; k
++) {
192 temp0
+= A
[j
*lda
+ k
] * B
[16 + k
*lda
];
193 temp1
+= A
[j
*lda
+ k
] * B
[17 + k
*lda
];
194 temp2
+= A
[j
*lda
+ k
] * B
[18 + k
*lda
];
195 temp3
+= A
[j
*lda
+ k
] * B
[19 + k
*lda
];
196 temp4
+= A
[j
*lda
+ k
] * B
[20 + k
*lda
];
197 temp5
+= A
[j
*lda
+ k
] * B
[21 + k
*lda
];
198 temp6
+= A
[j
*lda
+ k
] * B
[22 + k
*lda
];
199 temp7
+= A
[j
*lda
+ k
] * B
[23 + k
*lda
];
200 temp8
+= A
[j
*lda
+ k
] * B
[24 + k
*lda
];
201 temp9
+= A
[j
*lda
+ k
] * B
[25 + k
*lda
];
202 temp10
+= A
[j
*lda
+ k
] * B
[26 + k
*lda
];
203 temp11
+= A
[j
*lda
+ k
] * B
[27 + k
*lda
];
204 temp12
+= A
[j
*lda
+ k
] * B
[28 + k
*lda
];
205 temp13
+= A
[j
*lda
+ k
] * B
[29 + k
*lda
];
206 temp14
+= A
[j
*lda
+ k
] * B
[30 + k
*lda
];
207 temp15
+= A
[j
*lda
+ k
] * B
[31 + k
*lda
];
209 C
[16 + j
*lda
] = temp0
;
210 C
[17 + j
*lda
] = temp1
;
211 C
[18 + j
*lda
] = temp2
;
212 C
[19 + j
*lda
] = temp3
;
213 C
[20 + j
*lda
] = temp4
;
214 C
[21 + j
*lda
] = temp5
;
215 C
[22 + j
*lda
] = temp6
;
216 C
[23 + j
*lda
] = temp7
;
217 C
[24 + j
*lda
] = temp8
;
218 C
[25 + j
*lda
] = temp9
;
219 C
[26 + j
*lda
] = temp10
;
220 C
[27 + j
*lda
] = temp11
;
221 C
[28 + j
*lda
] = temp12
;
222 C
[29 + j
*lda
] = temp13
;
223 C
[30 + j
*lda
] = temp14
;
224 C
[31 + j
*lda
] = temp15
;
226 for(j
= 0; j
<16; j
++) {
227 temp0
= C
[16 + j
*lda
];
228 temp1
= C
[17 + j
*lda
];
229 temp2
= C
[18 + j
*lda
];
230 temp3
= C
[19 + j
*lda
];
231 temp4
= C
[20 + j
*lda
];
232 temp5
= C
[21 + j
*lda
];
233 temp6
= C
[22 + j
*lda
];
234 temp7
= C
[23 + j
*lda
];
235 temp8
= C
[24 + j
*lda
];
236 temp9
= C
[25 + j
*lda
];
237 temp10
= C
[26 + j
*lda
];
238 temp11
= C
[27 + j
*lda
];
239 temp12
= C
[28 + j
*lda
];
240 temp13
= C
[29 + j
*lda
];
241 temp14
= C
[30 + j
*lda
];
242 temp15
= C
[31 + j
*lda
];
243 for(k
= 0; k
< 32; k
++) {
244 temp0
+= A
[j
*lda
+ k
] * B
[16 + k
*lda
];
245 temp1
+= A
[j
*lda
+ k
] * B
[17 + k
*lda
];
246 temp2
+= A
[j
*lda
+ k
] * B
[18 + k
*lda
];
247 temp3
+= A
[j
*lda
+ k
] * B
[19 + k
*lda
];
248 temp4
+= A
[j
*lda
+ k
] * B
[20 + k
*lda
];
249 temp5
+= A
[j
*lda
+ k
] * B
[21 + k
*lda
];
250 temp6
+= A
[j
*lda
+ k
] * B
[22 + k
*lda
];
251 temp7
+= A
[j
*lda
+ k
] * B
[23 + k
*lda
];
252 temp8
+= A
[j
*lda
+ k
] * B
[24 + k
*lda
];
253 temp9
+= A
[j
*lda
+ k
] * B
[25 + k
*lda
];
254 temp10
+= A
[j
*lda
+ k
] * B
[26 + k
*lda
];
255 temp11
+= A
[j
*lda
+ k
] * B
[27 + k
*lda
];
256 temp12
+= A
[j
*lda
+ k
] * B
[28 + k
*lda
];
257 temp13
+= A
[j
*lda
+ k
] * B
[29 + k
*lda
];
258 temp14
+= A
[j
*lda
+ k
] * B
[30 + k
*lda
];
259 temp15
+= A
[j
*lda
+ k
] * B
[31 + k
*lda
];
261 C
[16 + j
*lda
] = temp0
;
262 C
[17 + j
*lda
] = temp1
;
263 C
[18 + j
*lda
] = temp2
;
264 C
[19 + j
*lda
] = temp3
;
265 C
[20 + j
*lda
] = temp4
;
266 C
[21 + j
*lda
] = temp5
;
267 C
[22 + j
*lda
] = temp6
;
268 C
[23 + j
*lda
] = temp7
;
269 C
[24 + j
*lda
] = temp8
;
270 C
[25 + j
*lda
] = temp9
;
271 C
[26 + j
*lda
] = temp10
;
272 C
[27 + j
*lda
] = temp11
;
273 C
[28 + j
*lda
] = temp12
;
274 C
[29 + j
*lda
] = temp13
;
275 C
[30 + j
*lda
] = temp14
;
276 C
[31 + j
*lda
] = temp15
;
281 //--------------------------------------------------------------------------
284 // all threads start executing thread_entry(). Use their "coreid" to
285 // differentiate between threads (each thread is running on a separate core).
287 void thread_entry(int cid
, int nc
)
292 // static allocates data in the binary, which is visible to both threads
293 static data_t results_data
[ARRAY_SIZE
];
296 // // Execute the provided, naive matmul
298 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
302 // verify(ARRAY_SIZE, results_data, verify_data);
304 // // clear results from the first trial
307 // for (i=0; i < ARRAY_SIZE; i++)
308 // results_data[i] = 0;
312 // Execute your faster matmul
314 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
317 printArray("results:", ARRAY_SIZE
, results_data
);
318 printArray("verify :", ARRAY_SIZE
, verify_data
);
322 verify(ARRAY_SIZE
, results_data
, verify_data
);