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.
118 int i
, j
, k
, ii
, jj
, bsize
;
120 for ( jj
= bsize
*coreid
; jj
< lda
; jj
+= bsize
*ncores
) {
121 for ( ii
= 0; ii
< lda
; ii
+= bsize
) {
122 for ( j
= jj
; j
< lda
&& j
< jj
+ bsize
; j
++) {
123 for ( i
= ii
; i
< lda
&& i
< ii
+ bsize
; i
+= 8) {
124 data_t c1
= C
[i
+ j
*lda
];
125 data_t c2
= C
[i
+ j
*lda
+ 1];
126 data_t c3
= C
[i
+ j
*lda
+ 2];
127 data_t c4
= C
[i
+ j
*lda
+ 3];
128 data_t c5
= C
[i
+ j
*lda
+ 4];
129 data_t c6
= C
[i
+ j
*lda
+ 5];
130 data_t c7
= C
[i
+ j
*lda
+ 6];
131 data_t c8
= C
[i
+ j
*lda
+ 7];
132 for ( k
= 0; k
< lda
; k
+=4 ) {
133 for (int x
= 0; x
< 4; x
++) {
134 data_t a
= A
[j
*lda
+ k
+x
];
135 data_t b1
= B
[(k
+x
)*lda
+ i
];
136 data_t b2
= B
[(k
+x
)*lda
+ i
+ 1];
137 data_t b3
= B
[(k
+x
)*lda
+ i
+ 2];
138 data_t b4
= B
[(k
+x
)*lda
+ i
+ 3];
139 data_t b5
= B
[(k
+x
)*lda
+ i
+ 4];
140 data_t b6
= B
[(k
+x
)*lda
+ i
+ 5];
141 data_t b7
= B
[(k
+x
)*lda
+ i
+ 6];
142 data_t b8
= B
[(k
+x
)*lda
+ i
+ 7];
154 C
[i
+ j
*lda
+ 1] = c2
;
155 C
[i
+ j
*lda
+ 2] = c3
;
156 C
[i
+ j
*lda
+ 3] = c4
;
157 C
[i
+ j
*lda
+ 4] = c5
;
158 C
[i
+ j
*lda
+ 5] = c6
;
159 C
[i
+ j
*lda
+ 6] = c7
;
160 C
[i
+ j
*lda
+ 7] = c8
;
167 //--------------------------------------------------------------------------
170 // all threads start executing thread_entry(). Use their "coreid" to
171 // differentiate between threads (each thread is running on a separate core).
173 void thread_entry(int cid
, int nc
)
178 // static allocates data in the binary, which is visible to both threads
179 static data_t results_data
[ARRAY_SIZE
];
182 // // Execute the provided, naive matmul
184 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
188 // verify(ARRAY_SIZE, results_data, verify_data);
190 // // clear results from the first trial
193 // for (i=0; i < ARRAY_SIZE; i++)
194 // results_data[i] = 0;
198 // Execute your faster matmul
200 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
203 printArray("results:", ARRAY_SIZE
, results_data
);
204 printArray("verify :", ARRAY_SIZE
, verify_data
);
208 verify(ARRAY_SIZE
, results_data
, verify_data
);