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