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 data_t
mult(data_t x
, data_t y
)
89 for (i
=0; i
< x
; i
++) {
94 //--------------------------------------------------------------------------
97 // single-thread, naive version
98 void __attribute__((noinline
)) matmul_naive(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
105 for ( i
= 0; i
< lda
; i
++ )
106 for ( j
= 0; j
< lda
; j
++ )
108 for ( k
= 0; k
< lda
; k
++ )
110 C
[i
+ j
*lda
] += A
[j
*lda
+ k
] * B
[k
*lda
+ i
];
117 void __attribute__((noinline
)) matmul(const int lda
, const data_t A
[], const data_t B
[], data_t C
[] )
121 int row
,row2
, column
, column2
, column3
, column4
, column5
, column6
, column7
, column8
;
122 data_t element
, element2
, element3
, element4
, element5
, element6
, element7
, element8
;
123 data_t B1
, B2
, B3
, B4
;
124 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};
125 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};
127 //for (i=coreid*max_dim/ncores; i<(max_dim/ncores+coreid*max_dim/ncores); i+=8){
128 for (l
=coreid
*local_lda
/ncores
; l
<local_lda
*(1+coreid
)/ncores
; l
+=2){
132 //element5 = A[row2];
133 for (i
=0; i
<local_lda
; i
+=4){
135 element2
= A
[row
+i
+1];
136 element3
= A
[row
+i
+2];
137 element4
= A
[row
+i
+3];
139 element5
= A
[row2
+i
];
140 element6
= A
[row2
+i
+1];
141 element7
= A
[row2
+i
+2];
142 element8
= A
[row2
+i
+3];
145 column2
=(i
+1)*local_lda
;
146 column3
=(i
+2)*local_lda
;
147 column4
=(i
+3)*local_lda
;
154 for (j
=0; j
<lda
; j
+=4){
155 temp_mat
[j
]+=element
*B1
+element2
*B2
+element3
*B3
+element4
*B4
;
156 temp_mat
[j
+1]+=element
*B
[column
+j
+1]+element2
*B
[column2
+j
+1]+element3
*B
[column3
+j
+1]+element4
*B
[column4
+j
+1];
157 temp_mat
[j
+2]+=element
*B
[column
+j
+2]+element2
*B
[column2
+j
+2]+element3
*B
[column3
+j
+2]+element4
*B
[column4
+j
+2];
158 temp_mat
[j
+3]+=element
*B
[column
+j
+3]+element2
*B
[column2
+j
+3]+element3
*B
[column3
+j
+3]+element4
*B
[column4
+j
+3];
160 temp_mat2
[j
]+=element5
*B1
+element6
*B2
+element7
*B3
+element8
*B4
;
161 temp_mat2
[j
+1]+=element5
*B
[column
+j
+1]+element6
*B
[column2
+j
+1]+element7
*B
[column3
+j
+1]+element8
*B
[column4
+j
+1];
162 temp_mat2
[j
+2]+=element5
*B
[column
+j
+2]+element6
*B
[column2
+j
+2]+element7
*B
[column3
+j
+2]+element8
*B
[column4
+j
+2];
163 temp_mat2
[j
+3]+=element5
*B
[column
+j
+3]+element6
*B
[column2
+j
+3]+element7
*B
[column3
+j
+3]+element8
*B
[column4
+j
+3];
171 //element = A[row+i+4];
172 //element5 = A[row2+i+4];
175 for(k
=0; k
<local_lda
; k
++){
176 C
[row
+k
]=temp_mat
[k
];
178 C
[row2
+k
]=temp_mat2
[k
];
186 // ***************************** //
187 // **** ADD YOUR CODE HERE ***** //
188 // ***************************** //
190 // feel free to make a separate function for MI and MSI versions.
193 //--------------------------------------------------------------------------
196 // all threads start executing thread_entry(). Use their "coreid" to
197 // differentiate between threads (each thread is running on a separate core).
199 void thread_entry(int cid
, int nc
)
204 // static allocates data in the binary, which is visible to both threads
205 static data_t results_data
[ARRAY_SIZE
];
208 // Execute the provided, naive matmul
210 stats(matmul_naive(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
214 verify(ARRAY_SIZE
, results_data
, verify_data
);
216 // clear results from the first trial
219 for (i
=0; i
< ARRAY_SIZE
; i
++)
224 // Execute your faster matmul
226 stats(matmul(DIM_SIZE
, input1_data
, input2_data
, results_data
); barrier());
229 printArray("results:", ARRAY_SIZE
, results_data
);
230 printArray("verify :", ARRAY_SIZE
, verify_data
);
234 verify(ARRAY_SIZE
, results_data
, verify_data
);