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
];
109 void __attribute__((noinline
)) matmul(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
112 int row
, row2
, row3
, row4
, column
, column2
;
113 data_t element
, element2
, element3
, element4
, element5
, element6
, element7
, element8
;
114 data_t temp_mat
[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
115 data_t temp_mat2
[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
116 data_t temp_mat3
[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
117 data_t temp_mat4
[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
118 //for (i=coreid*max_dim/ncores; i<(max_dim/ncores+coreid*max_dim/ncores); i+=8){
119 for (l
=coreid
*32/ncores
; l
<32*(1+coreid
)/ncores
; l
+=4){
124 for (i
=0; i
<lda
; i
+=2){
126 element2
= A
[row
+i
+1];
128 element3
= A
[row2
+i
];
129 element4
= A
[row2
+i
+1];
131 element5
= A
[row3
+i
];
132 element6
= A
[row3
+i
+1];
134 element7
= A
[row4
+i
];
135 element8
= A
[row4
+i
+1];
140 for (j
=0; j
<lda
; j
+=2){
141 temp_mat
[j
]+=element
*B
[column
+j
]+element2
*B
[column2
+j
];
142 temp_mat2
[j
]+=element3
*B
[column
+j
]+element4
*B
[column2
+j
];
143 temp_mat3
[j
]+=element5
*B
[column
+j
]+element6
*B
[column2
+j
];
144 temp_mat4
[j
]+=element7
*B
[column
+j
]+element8
*B
[column2
+j
];
146 temp_mat
[j
+1]+=element
*B
[column
+j
+1]+element2
*B
[column2
+j
+1];
147 temp_mat2
[j
+1]+=element3
*B
[column
+j
+1]+element4
*B
[column2
+j
+1];
148 temp_mat3
[j
+1]+=element5
*B
[column
+j
+1]+element6
*B
[column2
+j
+1];
149 temp_mat4
[j
+1]+=element7
*B
[column
+j
+1]+element8
*B
[column2
+j
+1];
157 for(k
=0; k
<32; k
+= 4){
158 C
[row
+k
]=temp_mat
[k
];
160 C
[row2
+k
]=temp_mat2
[k
];
162 C
[row3
+k
]=temp_mat3
[k
];
164 C
[row4
+k
]=temp_mat4
[k
];
167 C
[row
+k
+1]=temp_mat
[k
+1];
169 C
[row2
+k
+1]=temp_mat2
[k
+1];
171 C
[row3
+k
+1]=temp_mat3
[k
+1];
173 C
[row4
+k
+1]=temp_mat4
[k
+1];
176 C
[row
+k
+2]=temp_mat
[k
+2];
178 C
[row2
+k
+2]=temp_mat2
[k
+2];
180 C
[row3
+k
+2]=temp_mat3
[k
+2];
182 C
[row4
+k
+2]=temp_mat4
[k
+2];
185 C
[row
+k
+3]=temp_mat
[k
+3];
187 C
[row2
+k
+3]=temp_mat2
[k
+3];
189 C
[row3
+k
+3]=temp_mat3
[k
+3];
191 C
[row4
+k
+3]=temp_mat4
[k
+3];
199 // ***************************** //
200 // **** ADD YOUR CODE HERE ***** //
201 // ***************************** //
203 // feel free to make a separate function for MI and MSI versions.
206 //--------------------------------------------------------------------------
209 // all threads start executing thread_entry(). Use their "coreid" to
210 // differentiate between threads (each thread is running on a separate core).
212 void thread_entry(int cid
, int nc
)
217 // static allocates data in the binary, which is visible to both threads
218 static data_t results_data
[ARRAY_SIZE
];
221 // Execute the provided, naive matmul
223 stats(matmul_naive(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
227 verify(ARRAY_SIZE
, results_data
, verify_data
);
229 // clear results from the first trial
232 for (i
=0; i
< ARRAY_SIZE
; i
++)
237 // Execute your faster matmul
239 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
242 printArray("results:", ARRAY_SIZE
, results_data
);
243 printArray("verify :", ARRAY_SIZE
, verify_data
);
247 verify(ARRAY_SIZE
, results_data
, verify_data
);