cf464f410930b04b78c83cef609ae495da988954
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
];
109 void __attribute__((noinline
)) matmul(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
138 static data_t BB
[1024];
142 if (coreid
== 0 | coreid
== 1 ) {
143 for ( k
= 0; k
< lda
; k
++) {
144 for ( i
= coreid
*(lda
/2); i
< (coreid
+1)*(lda
/2); i
++ ) {
145 BB
[i
*lda
+ k
] = B
[k
*lda
+ i
];
151 for ( int x
= 0; x
< ncores
; x
++) {
152 //split the i values into two chunks so the threads don't interfere on the B loads
153 //this could be generalized if needed, but I won't bother since it would be tricky
154 //and we already know the size and numthreads
155 start
= coreid
== x
? 0 : 16;
156 end
= coreid
== x
? 16 : 32;
157 for ( i
= start
; i
< end
; i
+=8 ) {
158 for ( j
= coreid
*(lda
/ncores
); j
< (coreid
+1)*(lda
/ncores
); j
++ ) {
159 c1
=0;c2
=0;c3
=0;c4
=0;c5
=0;c6
=0;c7
=0;c8
=0;
169 for ( k
= 0; k
< lda
; k
+=8 ) {
251 C
[i
+0 + j
*lda
] += c1
;
252 C
[i
+1 + j
*lda
] += c2
;
253 C
[i
+2 + j
*lda
] += c3
;
254 C
[i
+3 + j
*lda
] += c4
;
255 C
[i
+4 + j
*lda
] += c5
;
256 C
[i
+5 + j
*lda
] += c6
;
257 C
[i
+6 + j
*lda
] += c7
;
258 C
[i
+7 + j
*lda
] += c8
;
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(nc));
285 verifyMT(ARRAY_SIZE, results_data, verify_data);
287 // clear results from the first trial
290 for (i=0; i < ARRAY_SIZE; i++)
296 // Execute your faster matmul
298 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
301 printArrayMT("results:", ARRAY_SIZE
, results_data
);
302 printArrayMT("verify :", ARRAY_SIZE
, verify_data
);
306 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);