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
[] )
115 size_t max_dim
= 32*32;
116 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};
117 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};
118 //for (i=coreid*max_dim/ncores; i<(max_dim/ncores+coreid*max_dim/ncores); i+=8){
119 for (i
=coreid
*max_dim
/ncores
; i
<(max_dim
/ncores
+coreid
*max_dim
/ncores
)/2; i
+=8){
121 data_t element2
= A
[i
+1];
122 data_t element3
= A
[i
+2];
123 data_t element4
= A
[i
+3];
124 data_t element5
= A
[i
+4];
125 data_t element6
= A
[i
+5];
126 data_t element7
= A
[i
+6];
127 data_t element8
= A
[i
+7];
128 data_t elementA2
= A
[i
+32*8];
129 data_t elementA21
= A
[i
+32*8+1];
130 data_t elementA22
= A
[i
+32*8+2];
131 data_t elementA23
= A
[i
+32*8+3];
132 data_t elementA24
= A
[i
+32*8+4];
133 data_t elementA25
= A
[i
+32*8+5];
134 data_t elementA26
= A
[i
+32*8+6];
135 data_t elementA27
= A
[i
+32*8+7];
136 int row
= (int)(i
/32)*32;
138 int column
= i
%32*32;
139 int column2
= (i
+1)%32*32;
140 int column3
= (i
+2)%32*32;
141 int column4
= (i
+3)%32*32;
142 int column5
= (i
+4)%32*32;
143 int column6
= (i
+5)%32*32;
144 int column7
= (i
+6)%32*32;
145 int column8
= (i
+7)%32*32;
147 for (j
=0; j
<32; j
++){
148 temp_mat
[j
]+=element
*B
[column
+j
]+element2
*B
[column2
+j
]+element3
*B
[column3
+j
]+element4
*B
[column4
+j
]+element5
*B
[column5
+j
]+element6
*B
[column6
+j
]+element7
*B
[column7
+j
]+element8
*B
[column8
+j
];
150 temp_mat2
[j
]+=elementA2
*B
[column
+j
]+elementA21
*B
[column2
+j
]+elementA22
*B
[column3
+j
]+elementA23
*B
[column4
+j
]+elementA24
*B
[column5
+j
]+elementA25
*B
[column6
+j
]+elementA26
*B
[column7
+j
]+elementA27
*B
[column8
+j
];
154 C
[row
+k
]=temp_mat
[k
];
155 C
[row2
+k
]=temp_mat2
[k
];
167 // ***************************** //
168 // **** ADD YOUR CODE HERE ***** //
169 // ***************************** //
171 // feel free to make a separate function for MI and MSI versions.
175 //--------------------------------------------------------------------------
178 // all threads start executing thread_entry(). Use their "coreid" to
179 // differentiate between threads (each thread is running on a separate core).
181 void thread_entry(int cid
, int nc
)
186 // static allocates data in the binary, which is visible to both threads
187 static data_t results_data
[ARRAY_SIZE
];
190 // Execute the provided, naive matmul
192 stats(matmul_naive(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
196 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);
198 // clear results from the first trial
201 for (i
=0; i
< ARRAY_SIZE
; i
++)
206 // Execute your faster matmul
208 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier(nc
));
211 printArrayMT("results:", ARRAY_SIZE
, results_data
);
212 printArrayMT("verify :", ARRAY_SIZE
, verify_data
);
216 verifyMT(ARRAY_SIZE
, results_data
, verify_data
);