multithreading tests from 152 lab 5
[riscv-tests.git] / mt / bt_matmul / bt_matmul.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
113 // ***************************** //
114 // **** ADD YOUR CODE HERE ***** //
115 // ***************************** //
116 //
117 // feel free to make a separate function for MI and MSI versions.
118
119 int i, j, k , jj , kk;
120 int start_i = coreid*lda/2;
121 int end_i = start_i + lda/2;
122 int step_j, step_k;
123 int start_k, end_k, start_j, end_j;
124 int j_lda;
125 int pos_A , pos_B, pos_C;
126 data_t temp00, temp01,temp02,temp03,temp04,temp05,temp06,temp07;
127 data_t temp10, temp11,temp12,temp13,temp14,temp15,temp16,temp17;
128 data_t temp_A0, temp_A1, temp_A2, temp_A3, temp_A4, temp_A5, temp_A6, temp_A7;
129
130 if (coreid == 0)
131 {
132 step_k = 1;
133 start_k= 0;
134 end_k = lda;
135
136 step_j = 2;
137 start_j= 0;
138 end_j = lda;
139
140 }else
141 {
142
143 step_k = -1;
144 start_k = lda-1;
145 end_k = -1;
146
147 step_j = -2;
148 start_j= lda-2;
149 end_j = -2;
150 }
151
152 for( kk = start_k ; kk!= end_k ; kk+=(step_k*16) )
153 {
154 for( jj = start_j ; jj!= end_j ; jj+=(step_j*8) )
155 {
156 for ( i = start_i; i < end_i; i+=8 )
157 {
158 //pos_C = i + jj*lda;
159 for ( j = jj; j != (jj+(step_j*8)) ; j+=step_j )
160 {
161
162 pos_C = i + j*lda;
163 temp00 = C[(pos_C + 0)];
164 temp01 = C[(pos_C + 1)];
165 temp02 = C[(pos_C + 2)];
166 temp03 = C[(pos_C + 3)];
167 temp04 = C[(pos_C + 4)];
168 temp05 = C[(pos_C + 5)];
169 temp06 = C[(pos_C + 6)];
170 temp07 = C[(pos_C + 7)];
171
172 //pos_C += lda;
173 pos_C = i + (j+1)*lda;
174
175 temp10 = C[(pos_C + 0)];
176 temp11 = C[(pos_C + 1)];
177 temp12 = C[(pos_C + 2)];
178 temp13 = C[(pos_C + 3)];
179 temp14 = C[(pos_C + 4)];
180 temp15 = C[(pos_C + 5)];
181 temp16 = C[(pos_C + 6)];
182 temp17 = C[(pos_C + 7)];
183
184 pos_B = kk*lda + i;
185 pos_A = j*lda + kk;
186 for ( k = kk; k != (kk+(step_k*16)) ; k+=step_k )
187 {
188 temp_A0 = A[ pos_A ] ;
189 temp_A1 = A[pos_A +lda];
190
191 temp00 += temp_A0 * B[(pos_B + 0)];
192 temp01 += temp_A0 * B[(pos_B + 1)];
193 temp02 += temp_A0 * B[(pos_B + 2)];
194 temp03 += temp_A0 * B[(pos_B + 3)];
195 temp04 += temp_A0 * B[(pos_B + 4)];
196 temp05 += temp_A0 * B[(pos_B + 5)];
197 temp06 += temp_A0 * B[(pos_B + 6)];
198 temp07 += temp_A0 * B[(pos_B + 7)];
199
200 temp10 += temp_A1 * B[(pos_B + 0)];
201 temp11 += temp_A1 * B[(pos_B + 1)];
202 temp12 += temp_A1 * B[(pos_B + 2)];
203 temp13 += temp_A1 * B[(pos_B + 3)];
204 temp14 += temp_A1 * B[(pos_B + 4)];
205 temp15 += temp_A1 * B[(pos_B + 5)];
206 temp16 += temp_A1 * B[(pos_B + 6)];
207 temp17 += temp_A1 * B[(pos_B + 7)];
208
209 pos_B += (lda*step_k) ;
210 pos_A += step_k;
211 }
212 //barrier();
213
214 C[(pos_C + 0)] = temp10;
215 C[(pos_C + 1)] = temp11;
216 C[(pos_C + 2)] = temp12;
217 C[(pos_C + 3)] = temp13;
218 C[(pos_C + 4)] = temp14;
219 C[(pos_C + 5)] = temp15;
220 C[(pos_C + 6)] = temp16;
221 C[(pos_C + 7)] = temp17;
222 //barrier();
223
224 pos_C = i + j*lda;
225 //pos_C -= lda;
226 C[(pos_C + 0)] = temp00;
227 C[(pos_C + 1)] = temp01;
228 C[(pos_C + 2)] = temp02;
229 C[(pos_C + 3)] = temp03;
230 C[(pos_C + 4)] = temp04;
231 C[(pos_C + 5)] = temp05;
232 C[(pos_C + 6)] = temp06;
233 C[(pos_C + 7)] = temp07;
234 //barrier();
235 //pos_C += step_j * lda;
236 }
237 //barrier();
238 }
239 //barrier();
240
241 }
242 //barrier();
243 }
244 }
245
246 //--------------------------------------------------------------------------
247 // Main
248 //
249 // all threads start executing thread_entry(). Use their "coreid" to
250 // differentiate between threads (each thread is running on a separate core).
251
252 void thread_entry(int cid, int nc)
253 {
254 coreid = cid;
255 ncores = nc;
256
257 // static allocates data in the binary, which is visible to both threads
258 static data_t results_data[ARRAY_SIZE];
259
260 /*
261 // Execute the provided, naive matmul
262 barrier();
263 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
264
265
266 // verify
267 verify(ARRAY_SIZE, results_data, verify_data);
268
269 // clear results from the first trial
270 size_t i;
271 if (coreid == 0)
272 for (i=0; i < ARRAY_SIZE; i++)
273 results_data[i] = 0;
274 barrier();
275
276 */
277 // Execute your faster matmul
278 barrier();
279 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
280
281
282
283 #ifdef DEBUG
284 printArray("results:", ARRAY_SIZE, results_data);
285 printArray("verify :", ARRAY_SIZE, verify_data);
286 #endif
287
288 // verify
289 verify(ARRAY_SIZE, results_data, verify_data);
290 barrier();
291
292
293 //printf("input1_data");
294 exit(0);
295
296 }