f56168e29448528729c9d8c4421064c423ed2ce1
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
];
110 void __attribute__((noinline
)) matmul(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
118 size_t max_dim
= lda
*lda
;
119 size_t block_size
= lda
/2;
120 data_t temp_mat
[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
122 //making a 16x16 block
123 //First block: Top 16x16 block left of A and top left of B = top left of C
124 //Second block: top right 16x16 right block of A and top right of B = top right of C
125 for (j2
= 0; j2
< 2; j2
++) {
126 for (i2
= 0; i2
< 2; i2
++) {
127 //for (j2= 0; j2 < 2; j2++) {
128 //K represents which row of A and C
129 for (k
= 0; k
< block_size
; k
++) {
131 for (i
= i2
*block_size
; i
< i2
*block_size
+block_size
; i
++) {
132 int elementA
= A
[rowIndex
+i
];
133 int columnIndex
= i
%32*32;
134 for (j
= 0; j
< block_size
; j
++) {
135 temp_mat
[j
] += elementA
*B
[columnIndex
+j
+j2
*block_size
];
138 //Put temp_mat into actual result Matrix
139 for (k2
= 0; k2
< block_size
; k2
++) {
140 C
[rowIndex
+k2
+j2
*block_size
] += temp_mat
[k2
];
147 for (j2
= 0; j2
< 2; j2
++) {
148 for (i2
= 0; i2
< 2; i2
++) {
149 //for (j2= 0; j2 < 2; j2++) {
150 //K represents which row of A and C
151 for (k
= block_size
; k
< lda
; k
++) {
153 for (i
= i2
*block_size
; i
< i2
*block_size
+block_size
; i
++) {
154 int elementA
= A
[rowIndex
+i
];
155 int columnIndex
= i
%32*32;
156 for (j
= 0; j
< block_size
; j
++) {
157 temp_mat
[j
] += elementA
*B
[columnIndex
+j
+j2
*block_size
];
160 //Put temp_mat into actual result Matrix
161 for (k2
= 0; k2
< block_size
; k2
++) {
162 C
[rowIndex
+k2
+j2
*block_size
] += temp_mat
[k2
];
171 //size_t half_lda = lda/2;
172 // k = which pair of row we're on
180 for (k = coreid*lda/ncores; k < (lda/ncores + coreid*lda/ncores); k += 2) {
182 for (i = 0; i < lda ; i++) {
183 int elementA = A[32*k+i];
184 int elementA2 = A[i + 32*(k+1)];
185 int column = i%32*32;
186 for (j = 0; j < lda; j++) {
187 C[32*k + j] += elementA*B[column+j];
188 C[32*(k+1) + j] += elementA2*B[column+j];
197 data_t element2 = A[i+1];
198 data_t element3 = A[i+2];
199 data_t element4 = A[i+3];
200 data_t element5 = A[i+4];
201 data_t element6 = A[i+5];
202 data_t element7 = A[i+6];
203 data_t element8 = A[i+7];
204 int row= (int)(i/32)*32;
205 int row2 = (i+1)/32*32;
206 int row3 = (i+2)/32*32;
207 int row4 = (i+3)/32*32;
208 int row5 = (i+4)/32*32;
209 int row6 = (i+5)/32*32;
210 int row7 = (i+6)/32*32;
211 int row8 = (i+7)/32*32;
212 int column = i%32*32;
213 int column2 = (i+1)%32*32;
214 int column3 = (i+2)%32*32;
215 int column4 = (i+3)%32*32;
216 int column5 = (i+4)%32*32;
217 int column6 = (i+5)%32*32;
218 int column7 = (i+6)%32*32;
222 //int column8 = (i+7)%32*32;
225 for (j=0; j < lda; j++) {
227 C[row+j]+=element*B[column+j];
228 C[row2+j]+=element2*B[column2+j];
229 C[row3+j]+=element3*B[column3+j];
230 C[row4+j]+=element4*B[column4+j];
231 C[row5+j]+=element5*B[column5+j];
232 C[row6+j]+=element6*B[column6+j];
233 C[row7+j]+=element7*B[column7+j];
234 C[row8+j]+=element8*B[column8+j];
235 C[row+j]+=element*B[column+j]+element2*B[column2+j]+element3*B[column3+j]+element4*B[column4+j]+element5*B[column5+j]+element6*B[column6+j]+element7*B[column7+j]+element8*B[column8+j];
245 // ***************************** //
246 // **** ADD YOUR CODE HERE ***** //
247 // ***************************** //
249 // feel free to make a separate function for MI and MSI versions.
253 //--------------------------------------------------------------------------
256 // all threads start executing thread_entry(). Use their "coreid" to
257 // differentiate between threads (each thread is running on a separate core).
259 void thread_entry(int cid
, int nc
)
264 // static allocates data in the binary, which is visible to both threads
265 static data_t results_data
[ARRAY_SIZE
];
268 // Execute the provided, naive matmul
270 stats(matmul_naive(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
274 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);
276 // clear results from the first trial
279 for (i
=0; i
< ARRAY_SIZE
; i
++)
284 // Execute your faster matmul
286 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
289 printArrayMT("results:", ARRAY_SIZE
, results_data
);
290 printArrayMT("verify :", ARRAY_SIZE
, verify_data
);
294 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);