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