445f924fe965a36d57803cab72404b213d0c535b
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 void matrix_sub(int size
, data_t A
[], data_t B
[], data_t C
[]) {
90 for(int i
= 0; i
< size
; i
++){
95 void matrix_add(int size
, data_t A
[], data_t B
[], data_t C
[]) {
99 for(int i
= 0; i
< size
; i
++){
104 void strassen_mult(int dime
, const data_t sA
[], const data_t sB
[], data_t sC
[]) {
110 int sub_size
= dime
*dime
/4;
112 // data_t A_11[sub_size], B_11[sub_size], C_11[sub_size],
113 // A_12[sub_size], B_12[sub_size], C_12[sub_size],
114 // A_21[sub_size], B_21[sub_size], C_21[sub_size],
115 // A_22[sub_size], B_22[sub_size], C_22[sub_size];
117 data_t
*A_11
= malloc(sub_size
*sizeof(data_t
));
118 data_t
*A_12
= malloc(sub_size
*sizeof(data_t
));
119 data_t
*A_21
= malloc(sub_size
*sizeof(data_t
));
120 data_t
*A_22
= malloc(sub_size
*sizeof(data_t
));
121 data_t
*B_11
= malloc(sub_size
*sizeof(data_t
));
122 data_t
*B_12
= malloc(sub_size
*sizeof(data_t
));
123 data_t
*B_21
= malloc(sub_size
*sizeof(data_t
));
124 data_t
*B_22
= malloc(sub_size
*sizeof(data_t
));
126 for(height
=0; height
< dime
/2; height
++) {
127 for(width
= 0; width
< dime
/2; width
++) {
128 A_11
[width
+(height
*dime
/2)] = sA
[width
+ height
*dime
];
129 B_11
[width
+(height
*dime
/2)] = sB
[width
+ height
*dime
];
131 A_12
[width
+(height
*dime
/2)] = sA
[dime
/2 + width
+ height
*dime
];
132 B_12
[width
+(height
*dime
/2)] = sB
[dime
/2 + width
+ height
*dime
];
134 A_21
[width
+(height
*dime
/2)] = sA
[(dime
*dime
)/2 + width
+ height
*dime
];
135 B_21
[width
+(height
*dime
/2)] = sB
[(dime
*dime
)/2 + width
+ height
*dime
];
137 A_22
[width
+(height
*dime
/2)] = sA
[(dime
*dime
)/2 + dime
/2 + width
+ height
*dime
];
138 B_22
[width
+(height
*dime
/2)] = sB
[(dime
*dime
)/2 + dime
/2 + width
+ height
*dime
];
142 // data_t H_1[sub_size], H_2[sub_size], H_3[sub_size], H_4[sub_size], H_5[sub_size],
143 // H_6[sub_size], H_7[sub_size], H_8[sub_size], H_9[sub_size], H_10[sub_size],
144 // H_11[sub_size], H_12[sub_size], H_13[sub_size], H_14[sub_size],
145 // H_15[sub_size], H_16[sub_size], H_17[sub_size], H_18[sub_size];
147 data_t
*H_1
= malloc(sub_size
*sizeof(data_t
));
148 data_t
*H_2
= malloc(sub_size
*sizeof(data_t
));
149 data_t
*H_3
= malloc(sub_size
*sizeof(data_t
));
150 data_t
*H_4
= malloc(sub_size
*sizeof(data_t
));
151 data_t
*H_5
= malloc(sub_size
*sizeof(data_t
));
152 data_t
*H_6
= malloc(sub_size
*sizeof(data_t
));
153 data_t
*H_7
= malloc(sub_size
*sizeof(data_t
));
154 data_t
*H_8
= malloc(sub_size
*sizeof(data_t
));
155 data_t
*H_9
= malloc(sub_size
*sizeof(data_t
));
156 data_t
*H_10
= malloc(sub_size
*sizeof(data_t
));
158 matrix_add(sub_size
, A_11
, A_22
, H_1
); //Helper1
159 matrix_add(sub_size
, B_11
, B_22
, H_2
); //Helper2
160 matrix_add(sub_size
, A_21
, A_22
, H_3
); //Helper3
161 matrix_sub(sub_size
, B_12
, B_22
, H_4
); //Helper4
162 matrix_sub(sub_size
, B_21
, B_11
, H_5
); //Helper5
163 matrix_add(sub_size
, A_11
, A_12
, H_6
); //Helper6
164 matrix_sub(sub_size
, A_21
, A_11
, H_7
); //Helper7
165 matrix_add(sub_size
, B_11
, B_12
, H_8
); //Helper8
166 matrix_sub(sub_size
, A_12
, A_22
, H_9
); //Helper9
167 matrix_add(sub_size
, B_21
, B_22
, H_10
); //Helper10
179 // data_t M_1[sub_size], M_2[sub_size], M_3[sub_size], M_4[sub_size],
180 // M_5[sub_size], M_6[sub_size], M_7[sub_size];
182 data_t
*M_1
= malloc(sub_size
*sizeof(data_t
));
183 data_t
*M_2
= malloc(sub_size
*sizeof(data_t
));
184 data_t
*M_3
= malloc(sub_size
*sizeof(data_t
));
185 data_t
*M_4
= malloc(sub_size
*sizeof(data_t
));
186 data_t
*M_5
= malloc(sub_size
*sizeof(data_t
));
187 data_t
*M_6
= malloc(sub_size
*sizeof(data_t
));
188 data_t
*M_7
= malloc(sub_size
*sizeof(data_t
));
191 M_1
[0] = H_1
[0]*H_2
[0];
192 M_2
[0] = H_3
[0]*B_11
[0];
193 M_3
[0] = A_11
[0]*H_4
[0];
194 M_4
[0] = A_22
[0]*H_5
[0];
195 M_5
[0] = H_6
[0]*B_22
[0];
196 M_6
[0] = H_7
[0]*H_8
[0];
197 M_7
[0] = H_9
[0]*H_10
[0];
199 strassen_mult(dime
/2, H_1
, H_2
, M_1
);
200 strassen_mult(dime
/2, H_3
, B_11
, M_2
);
201 strassen_mult(dime
/2, A_11
, H_4
, M_3
);
202 strassen_mult(dime
/2, A_22
, H_5
, M_4
);
203 strassen_mult(dime
/2, H_6
, B_22
, M_5
);
204 strassen_mult(dime
/2, H_7
, H_8
, M_6
);
205 strassen_mult(dime
/2, H_9
, H_10
, M_7
);
240 data_t
*H_11
= malloc(sub_size
*sizeof(data_t
));
241 data_t
*H_12
= malloc(sub_size
*sizeof(data_t
));
242 data_t
*H_13
= malloc(sub_size
*sizeof(data_t
));
243 data_t
*H_14
= malloc(sub_size
*sizeof(data_t
));
245 data_t
*C_11
= malloc(sub_size
*sizeof(data_t
));
246 data_t
*C_12
= malloc(sub_size
*sizeof(data_t
));
247 data_t
*C_21
= malloc(sub_size
*sizeof(data_t
));
248 data_t
*C_22
= malloc(sub_size
*sizeof(data_t
));
250 matrix_add(sub_size
, M_1
, M_4
, H_11
);
251 matrix_add(sub_size
, M_5
, M_7
, H_12
);
252 matrix_sub(sub_size
, H_11
, H_12
, C_11
);
254 matrix_add(sub_size
, M_3
, M_5
, C_12
);
256 matrix_add(sub_size
, M_2
, M_4
, C_21
);
258 matrix_sub(sub_size
, M_1
, M_2
, H_13
);
259 matrix_add(sub_size
, M_3
, M_6
, H_14
);
260 matrix_add(sub_size
, H_13
, H_14
, C_22
);
273 for(height
=0; height
< dime
/2; height
++) {
274 for(width
= 0; width
< dime
/2; width
++) {
275 sC
[width
+ height
*dime
] = C_11
[width
+(height
*dime
/2)];
276 sC
[dime
/2 + width
+ height
*dime
] = C_12
[width
+(height
*dime
/2)];
277 sC
[(dime
*dime
)/2 + width
+ height
*dime
] = C_21
[width
+(height
*dime
/2)];
278 sC
[(dime
*dime
)/2 + dime
/2 + width
+ height
*dime
] = C_22
[width
+(height
*dime
/2)];
294 //--------------------------------------------------------------------------
297 // single-thread, naive version
298 void __attribute__((noinline
)) matmul_naive(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
305 for ( i
= 0; i
< lda
; i
++ )
306 for ( j
= 0; j
< lda
; j
++ )
308 for ( k
= 0; k
< lda
; k
++ )
310 C
[i
+ j
*lda
] += A
[j
*lda
+ k
] * B
[k
*lda
+ i
];
318 void __attribute__((noinline
)) matmul(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
321 // ***************************** //
322 // **** ADD YOUR CODE HERE ***** //
323 // ***************************** //
325 // feel free to make a separate function for MI and MSI versions.
330 strassen_mult(lda
, A
, B
, C
);
334 //--------------------------------------------------------------------------
337 // all threads start executing thread_entry(). Use their "coreid" to
338 // differentiate between threads (each thread is running on a separate core).
340 void thread_entry(int cid
, int nc
)
345 // static allocates data in the binary, which is visible to both threads
346 static data_t results_data
[ARRAY_SIZE
];
349 // // Execute the provided, naive matmul
351 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
355 // verifyMT(ARRAY_SIZE, results_data, verify_data);
357 // // clear results from the first trial
360 // for (i=0; i < ARRAY_SIZE; i++)
361 // results_data[i] = 0;
365 // Execute your faster matmul
367 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
370 printArrayMT("results:", ARRAY_SIZE
, results_data
);
371 printArrayMT("verify :", ARRAY_SIZE
, verify_data
);
375 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);