1b31d86a0b23d7c6da0d471c2ab366b642b233bc
1 //**************************************************************************
2 // Multi-threaded Matrix Multiply benchmark
3 //--------------------------------------------------------------------------
4 // TA : Christopher Celio
5 // Student: ME STEPHANIE TUNG
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.
118 int i
, j
, k
, ii
, jj
, kk
;
119 int block
= lda
/ ncores
;
120 int leftover
= lda
% ncores
;
121 int start
= block
* coreid
;
125 for ( j
= start
; j
< (start
+block
); j
++ )
126 for ( k
= 0; k
< lda
; k
++ )
128 for ( i
= 0; i
< lda
; i
++ )
130 C
[i
+ j
*lda
] += A
[j
*lda
+ k
] * B
[k
*lda
+ i
];
136 for ( j = coreid; j < lda; j += ncores )
137 for ( k = 0; k < lda; k++ )
139 for ( i = 0; i < lda; i++ )
141 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
150 for ( j = (lda - leftover); j < lda; j++ )
151 for ( i = 0; i < lda; i++ )
153 for ( k = 0; k < lda; k++ )
155 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
167 for (jj = start; jj < start+block; jj += 4) {
168 for (kk = 0; kk < lda; kk += 4) {
169 for (ii = 0; ii < lda; ii += 4) {
170 for (i = ii; i < ii+4; i += 4) {
172 for (j = jj; j < jj+4; j++) {
173 for (k = kk; k < kk+4; k++) {
175 float a = A[k + j*lda];
177 C[i + j*lda] += a * B[k*lda + i];
178 C[i + j*lda + 1] += a * B[k*lda + i + 1];
179 C[i + j*lda + 2] += a * B[k*lda + i + 2];
180 C[i + j*lda + 3] += a * B[k*lda + i + 3];
192 //--------------------------------------------------------------------------
195 // all threads start executing thread_entry(). Use their "coreid" to
196 // differentiate between threads (each thread is running on a separate core).
198 void thread_entry(int cid
, int nc
)
203 // static allocates data in the binary, which is visible to both threads
204 static data_t results_data
[ARRAY_SIZE
];
207 // // Execute the provided, naive matmul
209 // stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
213 // verifyMT(ARRAY_SIZE, results_data, verify_data);
215 // // clear results from the first trial
218 // for (i=0; i < ARRAY_SIZE; i++)
219 // results_data[i] = 0;
223 // Execute your faster matmul
225 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
228 printArrayMT("results:", ARRAY_SIZE
, results_data
);
229 printArrayMT("verify :", ARRAY_SIZE
, verify_data
);
233 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);