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.
119 int temp0, temp1,temp2,temp3,temp4,temp5,temp6,temp7;
120 int start = coreid*lda/2;
121 int end = start + lda/2;
124 int temp_A0, temp_A1, temp_A2, temp_A3 ;
126 for ( i = start; i < end; i+=8){
127 for ( j = 0; j < lda; j++)
130 temp0 = C[(i+0) + j_lda];
131 temp1 = C[(i+1) + j_lda];
132 temp2 = C[(i+2) + j_lda];
133 temp3 = C[(i+3) + j_lda];
134 temp4 = C[(i+4) + j_lda];
135 temp5 = C[(i+5) + j_lda];
136 temp6 = C[(i+6) + j_lda];
137 temp7 = C[(i+7) + j_lda];
141 for ( k = 0; k < lda; k+=4)
144 temp_A0 = A[j_lda + (k+0)] ;
145 temp_A1 = A[j_lda + (k+1)] ;
146 temp_A2 = A[j_lda + (k+2)] ;
147 temp_A3 = A[j_lda + (k+3)] ;
150 temp0 += temp_A0 * B[(k+0)*lda + temp_i];
151 temp0 += temp_A1 * B[(k+1)*lda + temp_i];
152 temp0 += temp_A2 * B[(k+2)*lda + temp_i];
153 temp0 += temp_A3 * B[(k+3)*lda + temp_i];
156 temp1 += temp_A0 * B[(k+0)*lda + temp_i];
157 temp1 += temp_A1 * B[(k+1)*lda + temp_i];
158 temp1 += temp_A2 * B[(k+2)*lda + temp_i];
159 temp1 += temp_A3 * B[(k+3)*lda + temp_i];
162 temp2 += temp_A0 * B[(k+0)*lda + temp_i];
163 temp2 += temp_A1 * B[(k+1)*lda + temp_i];
164 temp2 += temp_A2 * B[(k+2)*lda + temp_i];
165 temp2 += temp_A3 * B[(k+3)*lda + temp_i];
169 temp3 += temp_A0 * B[(k+0)*lda + temp_i];
170 temp3 += temp_A1 * B[(k+1)*lda + temp_i];
171 temp3 += temp_A2 * B[(k+2)*lda + temp_i];
172 temp3 += temp_A3 * B[(k+3)*lda + temp_i];
175 temp4 += temp_A0 * B[(k+0)*lda + temp_i];
176 temp4 += temp_A1 * B[(k+1)*lda + temp_i];
177 temp4 += temp_A2 * B[(k+2)*lda + temp_i];
178 temp4 += temp_A3 * B[(k+3)*lda + temp_i];
181 temp5 += temp_A0 * B[(k+0)*lda + temp_i];
182 temp5 += temp_A1 * B[(k+1)*lda + temp_i];
183 temp5 += temp_A2 * B[(k+2)*lda + temp_i];
184 temp5 += temp_A3 * B[(k+3)*lda + temp_i];
187 temp6 += temp_A0 * B[(k+0)*lda + temp_i];
188 temp6 += temp_A1 * B[(k+1)*lda + temp_i];
189 temp6 += temp_A2 * B[(k+2)*lda + temp_i];
190 temp6 += temp_A3 * B[(k+3)*lda + temp_i];
194 temp7 += temp_A0 * B[(k+0)*lda + temp_i];
195 temp7 += temp_A1 * B[(k+1)*lda + temp_i];
196 temp7 += temp_A2 * B[(k+2)*lda + temp_i];
197 temp7 += temp_A3 * B[(k+3)*lda + temp_i];
202 C[i + j*lda] = temp0;
203 C[(i+1) + j*lda] = temp1;
204 C[(i+2) + j*lda] = temp2;
205 C[(i+3) + j*lda] = temp3;
206 C[(i+4) + j*lda] = temp4;
207 C[(i+5) + j*lda] = temp5;
208 C[(i+6) + j*lda] = temp6;
209 C[(i+7) + j*lda] = temp7;
215 //--------------------------------------------------------------------------
218 // all threads start executing thread_entry(). Use their "coreid" to
219 // differentiate between threads (each thread is running on a separate core).
221 void thread_entry(int cid, int nc)
226 // static allocates data in the binary, which is visible to both threads
227 static data_t results_data[ARRAY_SIZE];
230 // Execute the provided, naive matmul
232 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
236 verify(ARRAY_SIZE, results_data, verify_data);
238 // clear results from the first trial
241 for (i=0; i < ARRAY_SIZE; i++)
246 // Execute your faster matmul
248 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
251 printArray("results:", ARRAY_SIZE, results_data);
252 printArray("verify :", ARRAY_SIZE, verify_data);
256 verify(ARRAY_SIZE, results_data, verify_data);