multithreading tests from 152 lab 5
[riscv-tests.git] / mt / am_matmul / matmul4.c
1 //**************************************************************************
2 // Multi-threaded Matrix Multiply benchmark
3 //--------------------------------------------------------------------------
4 // TA : Christopher Celio
5 // Student:
6 //
7 //
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
11 // dataset.h.
12
13
14 // print out arrays, etc.
15 //#define DEBUG
16
17 //--------------------------------------------------------------------------
18 // Includes
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23
24
25 //--------------------------------------------------------------------------
26 // Input/Reference Data
27
28 typedef float data_t;
29 #include "dataset.h"
30
31
32 //--------------------------------------------------------------------------
33 // Basic Utilities and Multi-thread Support
34
35 __thread unsigned long coreid;
36 unsigned long ncores;
37
38 #include "util.h"
39
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(); \
44 code; \
45 _c += rdcycle(), _i += rdinstret(); \
46 if (coreid == 0) \
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); \
49 } while(0)
50
51
52 //--------------------------------------------------------------------------
53 // Helper functions
54
55 void printArray( char name[], int n, data_t arr[] )
56 {
57 int i;
58 if (coreid != 0)
59 return;
60
61 printf( " %10s :", name );
62 for ( i = 0; i < n; i++ )
63 printf( " %3ld ", (long) arr[i] );
64 printf( "\n" );
65 }
66
67 void __attribute__((noinline)) verify(size_t n, const data_t* test, const data_t* correct)
68 {
69 if (coreid != 0)
70 return;
71
72 size_t i;
73 for (i = 0; i < n; i++)
74 {
75 if (test[i] != correct[i])
76 {
77 printf("FAILED test[%d]= %3ld, correct[%d]= %3ld\n",
78 i, (long)test[i], i, (long)correct[i]);
79 exit(-1);
80 }
81 }
82
83 return;
84 }
85
86 //--------------------------------------------------------------------------
87 // matmul function
88
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[] )
91 {
92 int i, j, k;
93
94 if (coreid > 0)
95 return;
96
97 for ( i = 0; i < lda; i++ )
98 for ( j = 0; j < lda; j++ )
99 {
100 for ( k = 0; k < lda; k++ )
101 {
102 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
103 }
104 }
105
106 }
107
108
109
110 void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
111 {
112 /*size_t i;
113 size_t j;
114 size_t k;
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 for (i=coreid*max_dim/ncores; i<(max_dim/ncores+coreid*max_dim/ncores); i+=8){
118 data_t element=A[i];
119 data_t element2 = A[i+1];
120 data_t element3 = A[i+2];
121 data_t element4 = A[i+3];
122 data_t element5 = A[i+4];
123 data_t element6 = A[i+5];
124 data_t element7 = A[i+6];
125 data_t element8 = A[i+7];
126 int row= (int)(i/32)*32;
127 int column = i%32*32;
128 int column2 = (i+1)%32*32;
129 int column3 = (i+2)%32*32;
130 int column4 = (i+3)%32*32;
131 int column5 = (i+4)%32*32;
132 int column6 = (i+5)%32*32;
133 int column7 = (i+6)%32*32;
134 int column8 = (i+7)%32*32;
135
136 for (j=0; j<32; j++){
137 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];
138 }
139 if (i%32==24){
140 for(k=0; k<32; k++){
141 C[row+k]=temp_mat[k];
142 temp_mat[k]=0;
143 }
144 }
145 }*/
146 int i,j,k,l;
147 //data_t element11, element12, element13, element14, element21, element22, element23, element24;
148 data_t element1, element2, element3, element4, element5, element6, element7, element8;
149 int row, row2;
150 //int column11, column12, column13, column14, column21, column22, column23, column24;
151 int column1, column2, column3, column4, column5, column6, column7, column8;
152 data_t temp[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};
153 //data_t temp2[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};
154 if (coreid == 0){
155 for (i=0; i<32; i++){
156 if (i==15){
157 for (j=0; j<32; j+=4){
158 row=15*32;
159 element1 = A[row+j];
160 element2 = A[row+j+1];
161 element3 = A[row+j+2];
162 element4 = A[row+j+3];
163 column1 = j*32;
164 column2 = (j+1)*32;
165 column3 = (j+2)*32;
166 column4 = (j+3)*32;
167 for (k=0;k<32; k++){
168 temp[k]+=element1*B[column1+k]+element2*B[column2+k]+element3*B[column3+k]+element4*B[column4+k];
169 }
170 if (j==28){
171 for (l=0; l<32; l++){
172 C[row+l]=temp[l];
173 temp[l]=0;
174 }
175 }
176 }
177 }
178 else{
179 row = i*32;
180 for (j=0; j<16; j+=4){
181 element1 = A[i*32+j];
182 element2 = A[i*32+j+1];
183 element3 = A[i*32+j+2];
184 element4 = A[i*32+j+3];
185 column1 = j*32;
186 column2 = (j+1)*32;
187 column3 = (j+2)*32;
188 column4 = (j+3)*32;
189 for (k=0; k<32; k++){
190 temp[k]+=element1*B[column1+k]+element2*B[column2+k]+element3*B[column3+k]+element4*B[column4+k];
191 }
192 if (j==12){
193 for (l=0; l<32; l++){
194 C[row+l]+=temp[l];
195 temp[l]=0;
196 }
197 }
198 }
199 }
200 }
201 }
202 else if (coreid==1){
203 for (i=0; i<32; i++){
204 row = (31-i)*32;
205 if (row/32 != 15){
206 for (j=16; j<32; j+=4){
207 element1 = A[(31-i)*32+j];
208 element2 = A[(31-i)*32+j+1];
209 element3 = A[(31-i)*32+j+2];
210 element4 = A[(31-i)*32+j+3];
211 column1 = j*32;
212 column2 = (j+1)*32;
213 column3 = (j+2)*32;
214 column4 = (j+3)*32;
215 for (k=0; k<32; k++){
216 temp[k]+=element1*B[column1+k]+element2*B[column2+k]+element3*B[column3+k]+element4*B[column4+k];
217 }
218 if (j==28){
219 for (l=0; l<32; l++){
220 C[row+l]+=temp[l];
221 temp[l]=0;
222 }
223 }
224 }
225 }
226 }
227 }
228 // ***************************** //
229 // **** ADD YOUR CODE HERE ***** //
230 // ***************************** //
231 //
232 // feel free to make a separate function for MI and MSI versions.
233
234 }
235
236 //--------------------------------------------------------------------------
237 // Main
238 //
239 // all threads start executing thread_entry(). Use their "coreid" to
240 // differentiate between threads (each thread is running on a separate core).
241
242 void thread_entry(int cid, int nc)
243 {
244 coreid = cid;
245 ncores = nc;
246
247 // static allocates data in the binary, which is visible to both threads
248 static data_t results_data[ARRAY_SIZE];
249
250
251 // Execute the provided, naive matmul
252 barrier();
253 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
254
255
256 // verify
257 verify(ARRAY_SIZE, results_data, verify_data);
258
259 // clear results from the first trial
260 size_t i;
261 if (coreid == 0)
262 for (i=0; i < ARRAY_SIZE; i++)
263 results_data[i] = 0;
264 barrier();
265
266
267 // Execute your faster matmul
268 barrier();
269 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
270
271 #ifdef DEBUG
272 printArray("results:", ARRAY_SIZE, results_data);
273 printArray("verify :", ARRAY_SIZE, verify_data);
274 #endif
275
276 // verify
277 verify(ARRAY_SIZE, results_data, verify_data);
278 barrier();
279
280 exit(0);
281 }
282