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
28 typedef double data_t
;
32 //--------------------------------------------------------------------------
33 // Basic Utilities and Multi-thread Support
35 __thread
unsigned long coreid
;
39 #define stringify_1(s) #s
40 #define stringify(s) stringify_1(s)
41 #define stats(code) do { \
42 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
44 _c += rdcycle(), _i += rdinstret(); \
46 printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
47 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); \
50 //--------------------------------------------------------------------------
53 // single-thread, naive version
54 void __attribute__((noinline
)) matmul_naive(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
61 for ( i
= 0; i
< lda
; i
++ )
62 for ( j
= 0; j
< lda
; j
++ )
64 for ( k
= 0; k
< lda
; k
++ )
66 C
[i
+ j
*lda
] += A
[j
*lda
+ k
] * B
[k
*lda
+ i
];
74 void __attribute__((noinline
)) matmul(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
77 // ***************************** //
78 // **** ADD YOUR CODE HERE ***** //
79 // ***************************** //
81 // feel free to make a separate function for MI and MSI versions.
85 //--------------------------------------------------------------------------
88 // all threads start executing thread_entry(). Use their "coreid" to
89 // differentiate between threads (each thread is running on a separate core).
91 void thread_entry(int cid
, int nc
)
95 // static allocates data in the binary, which is visible to both threads
96 static data_t results_data
[ARRAY_SIZE
];
99 // Execute the provided, naive matmul
101 stats(matmul_naive(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
105 int res
= verifyDouble(ARRAY_SIZE
, results_data
, verify_data
);
110 // clear results from the first trial
113 for (i
=0; i
< ARRAY_SIZE
; i
++)
118 // Execute your faster matmul
120 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
123 printArray("results:", ARRAY_SIZE
, results_data
);
124 printArray("verify :", ARRAY_SIZE
, verify_data
);
128 res
= verify(ARRAY_SIZE
, results_data
, verify_data
);