6530a5d105261a2eb73d108096db060b8f6a6ad5
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
[] )
113 // ***************************** //
114 // **** ADD YOUR CODE HERE ***** //
115 // ***************************** //
117 // feel free to make a separate function for MI and MSI versions.
119 // I think I've got a way for this to not need the "shared" state to work nicely, so no MSI version
120 int i
, j
, k
, lda_over_2
;
128 // first half of topleft corner
129 for(i
= 0; i
< lda_over_2
; i
++) {
130 for(j
= 0; j
< lda_over_2
; j
++) {
131 for(k
= 0; k
< lda_over_2
; k
++) {
132 C
[i
*lda
+ j
] += A
[i
*lda
+ k
]*B
[k
*lda
+ j
];
136 // second half of topleft corner
137 for(i
= 0; i
< lda_over_2
; i
++) {
138 for(j
= 0; j
< lda_over_2
; j
++) {
139 for(k
= lda_over_2
; k
< lda
; k
++) {
140 C
[i
*lda
+ j
] += A
[i
*lda
+ k
]*B
[k
*lda
+ j
];
144 // second half of bottomleft corner
145 for(i
= lda_over_2
; i
< lda
; i
++) {
146 for(j
= 0; j
< lda_over_2
; j
++) {
147 for(k
= lda_over_2
; k
< lda
; k
++) {
148 C
[i
*lda
+ j
] += A
[i
*lda
+ k
]*B
[k
*lda
+ j
];
152 // first half of bottomleft corner
153 for(i
= lda_over_2
; i
< lda
; i
++) {
154 for(j
= 0; j
< lda_over_2
; j
++) {
155 for(k
= 0; k
< lda_over_2
; k
++) {
156 C
[i
*lda
+ j
] += A
[i
*lda
+ k
]*B
[k
*lda
+ j
];
163 // first half of bottomright corner
164 for(i
= lda_over_2
; i
< lda
; i
++) {
165 for(j
= lda_over_2
; j
< lda
; j
++) {
166 for(k
= 0; k
< lda_over_2
; k
++) {
167 C
[i
*lda
+ j
] += A
[i
*lda
+ k
]*B
[k
*lda
+ j
];
171 // second half of bottomright corner
172 for(i
= lda_over_2
; i
< lda
; i
++) {
173 for(j
= lda_over_2
; j
< lda
; j
++) {
174 for(k
= lda_over_2
; k
< lda
; k
++) {
175 C
[i
*lda
+ j
] += A
[i
*lda
+ k
]*B
[k
*lda
+ j
];
179 // second half of topright corner
180 for(i
= 0; i
< lda_over_2
; i
++) {
181 for(j
= lda_over_2
; j
< lda
; j
++) {
182 for(k
= lda_over_2
; k
< lda
; k
++) {
183 C
[i
*lda
+ j
] += A
[i
*lda
+ k
]*B
[k
*lda
+ j
];
187 // first half of topright corner
188 for(i
= 0; i
< lda_over_2
; i
++) {
189 for(j
= lda_over_2
; j
< lda
; j
++) {
190 for(k
= 0; k
< lda_over_2
; k
++) {
191 C
[i
*lda
+ j
] += A
[i
*lda
+ k
]*B
[k
*lda
+ j
];
200 //--------------------------------------------------------------------------
203 // all threads start executing thread_entry(). Use their "coreid" to
204 // differentiate between threads (each thread is running on a separate core).
206 void thread_entry(int cid
, int nc
)
211 // static allocates data in the binary, which is visible to both threads
212 static data_t results_data
[ARRAY_SIZE
];
215 // // Execute the provided, naive matmul
217 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
221 // verifyMT(ARRAY_SIZE, results_data, verify_data);
223 // // clear results from the first trial
226 // for (i=0; i < ARRAY_SIZE; i++)
227 // results_data[i] = 0;
231 // Execute your faster matmul
233 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
236 printArrayMT("results:", ARRAY_SIZE
, results_data
);
237 printArrayMT("verify :", ARRAY_SIZE
, verify_data
);
241 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);