multithreading tests from 152 lab 5
[riscv-tests.git] / mt / af_matmul / keeptrying3.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 data_t mult(data_t x, data_t y)
87 { data_t result = 0;
88 size_t i;
89 for (i=0; i < x; i++) {
90 result += y;
91 }
92 return result;
93 }
94 //--------------------------------------------------------------------------
95 // matmul function
96
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[] )
99 {
100 int i, j, k;
101
102 if (coreid > 0)
103 return;
104
105 for ( i = 0; i < lda; i++ )
106 for ( j = 0; j < lda; j++ )
107 {
108 for ( k = 0; k < lda; k++ )
109 {
110 C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
111 }
112 }
113
114 }
115
116
117 void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
118 {
119 size_t i, j, k, l;
120 int row,row2, row3, row4, column, column2, column3, column4, column5, column6, column7, column8;
121 data_t element, element2, element3, element4, element5, element6, element7, element8;
122 data_t element9, element10, element11, element12, element13, element14, element15, element16;
123 data_t elementB1,elementB2,elementB3,elementB4;
124 data_t temp_mat[128]={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,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,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,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};
126 //for (i=coreid*max_dim/ncores; i<(max_dim/ncores+coreid*max_dim/ncores); i+=8){
127 for (l=coreid*32/ncores; l<32*(1+coreid)/ncores; l+=4){
128 row=l*lda;
129 row2=(l+1)*lda;
130 row3=(l+2)*lda;
131 row4=(l+3)*lda;
132 for (i=0; i<lda; i+=4){
133 element = A[row+i];
134 element2 = A[row+i+1];
135 element3 = A[row+i+2];
136 element4 = A[row+i+3];
137
138 element5 = A[row2+i];
139 element6 = A[row2+i+1];
140 element7 = A[row2+i+2];
141 element8 = A[row2+i+3];
142
143 element9 = A[row3+i];
144 element10 = A[row3+i+1];
145 element11 = A[row3+i+2];
146 element12 = A[row3+i+3];
147
148 element13 = A[row4+i];
149 element14 = A[row4+i+1];
150 element15 = A[row4+i+2];
151 element16 = A[row4+i+3];
152
153 column=i*lda;
154 column2=(i+1)*lda;
155 column3=(i+2)*lda;
156 column4=(i+3)*lda;
157
158
159 for (j=0; j<lda; j+=4){
160
161 temp_mat[j]+=element*B[column+j]+element2*B[column2+j]+element3*B[column3+j]+element4*B[column4+j];
162 temp_mat[j+1]+=element*B[column+j+1]+element2*B[column2+j+1]+element3*B[column3+j+1]+element4*B[column4+j+1];
163 temp_mat[j+2]+=element*B[column+j+2]+element2*B[column2+j+2]+element3*B[column3+j+2]+element4*B[column4+j+2];
164 temp_mat[j+3]+=element*B[column+j+3]+element2*B[column2+j+3]+element3*B[column3+j+3]+element4*B[column4+j+3];
165
166 temp_mat[j+lda]+=element5*B[column+j]+element6*B[column2+j]+element7*B[column3+j]+element8*B[column4+j];
167 temp_mat[j+1+lda]+=element5*B[column+j+1]+element6*B[column2+j+1]+element7*B[column3+j+1]+element8*B[column4+j+1];
168 temp_mat[j+2+lda]+=element5*B[column+j+2]+element6*B[column2+j+2]+element7*B[column3+j+2]+element8*B[column4+j+2];
169 temp_mat[j+3+lda]+=element5*B[column+j+3]+element6*B[column2+j+3]+element7*B[column3+j+3]+element8*B[column4+j+3];
170
171 temp_mat[j+2*lda]+=element9*B[column+j]+element10*B[column2+j]+element11*B[column3+j]+element12*B[column4+j];
172 temp_mat[j+1+2*lda]+=element9*B[column+j+1]+element10*B[column2+j+1]+element11*B[column3+j+1]+element12*B[column4+j+1];
173 temp_mat[j+2+2*lda]+=element9*B[column+j+2]+element10*B[column2+j+2]+element11*B[column3+j+2]+element12*B[column4+j+2];
174 temp_mat[j+3+2*lda]+=element9*B[column+j+3]+element10*B[column2+j+3]+element11*B[column3+j+3]+element12*B[column4+j+3];
175
176 temp_mat[j+3*lda]+=element13*B[column+j]+element14*B[column2+j]+element15*B[column3+j]+element16*B[column4+j];
177 temp_mat[j+1+3*lda]+=element13*B[column+j+1]+element14*B[column2+j+1]+element15*B[column3+j+1]+element16*B[column4+j+1];
178 temp_mat[j+2+3*lda]+=element13*B[column+j+2]+element14*B[column2+j+2]+element15*B[column3+j+2]+element16*B[column4+j+2];
179 temp_mat[j+3+3*lda]+=element13*B[column+j+3]+element14*B[column2+j+3]+element15*B[column3+j+3]+element16*B[column4+j+3];
180
181
182 }
183
184 }
185
186 for(k=0; k<32; k++){
187 C[row+k]=temp_mat[k];
188 temp_mat[k]=0;
189 C[row2+k]=temp_mat[k+lda];
190 temp_mat[k+lda]=0;
191 C[row3+k]=temp_mat[k+2*lda];
192 temp_mat[k+2*lda]=0;
193 C[row4+k]=temp_mat[k+3*lda];
194 temp_mat[k+3*lda]=0;
195
196
197 }
198
199
200 }
201 // ***************************** //
202 // **** ADD YOUR CODE HERE ***** //
203 // ***************************** //
204 //
205 // feel free to make a separate function for MI and MSI versions.
206
207 }
208 //--------------------------------------------------------------------------
209 // Main
210 //
211 // all threads start executing thread_entry(). Use their "coreid" to
212 // differentiate between threads (each thread is running on a separate core).
213
214 void thread_entry(int cid, int nc)
215 {
216 coreid = cid;
217 ncores = nc;
218
219 // static allocates data in the binary, which is visible to both threads
220 static data_t results_data[ARRAY_SIZE];
221
222
223 // Execute the provided, naive matmul
224 barrier();
225 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
226
227
228 // verify
229 verify(ARRAY_SIZE, results_data, verify_data);
230
231 // clear results from the first trial
232 size_t i;
233 if (coreid == 0)
234 for (i=0; i < ARRAY_SIZE; i++)
235 results_data[i] = 0;
236 barrier();
237
238
239 // Execute your faster matmul
240 barrier();
241 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
242
243 #ifdef DEBUG
244 printArray("results:", ARRAY_SIZE, results_data);
245 printArray("verify :", ARRAY_SIZE, verify_data);
246 #endif
247
248 // verify
249 verify(ARRAY_SIZE, results_data, verify_data);
250 barrier();
251
252 exit(0);
253 }