Updated mt tests
[riscv-tests.git] / mt / ay_matmul / ay_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 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 void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
109 {
110 static __thread int i, j, k;
111 static __thread data_t tempA0, tempA1, tempA2, tempA3, tempA4, tempA5, tempA6, tempA7;
112 static __thread data_t tempC0, tempC1, tempC2, tempC3, tempC4, tempC5, tempC6, tempC7, tempC8, tempC9, tempC10, tempC11, tempC12, tempC13, tempC14, tempC15;
113
114 static __thread int start, end, jStride, jToRow, jToCol;
115
116 start = coreid << 9;
117 end = (coreid+1) << 9;
118 jStride = 8;
119
120 for (j=start; j < end; j+=jStride) {
121 jToRow = (j>>5)<<5;
122 jToCol = j%32;
123 tempC0 = 0;
124 tempC1 = 0;
125 tempC2 = 0;
126 tempC3 = 0;
127 tempC4 = 0;
128 tempC5 = 0;
129 tempC6 = 0;
130 tempC7 = 0;
131 for ( i=0; i < lda; i+=2 ) {
132 tempA0 = A[i + jToRow];
133 tempA1 = A[i+1 + jToRow];
134 tempC0 += tempA0 * B[(jToCol ) + (i<<5)];
135 tempC1 += tempA0 * B[(jToCol+1 ) + (i<<5)];
136 tempC2 += tempA0 * B[(jToCol+2 ) + (i<<5)];
137 tempC3 += tempA0 * B[(jToCol+3 ) + (i<<5)];
138 tempC4 += tempA0 * B[(jToCol+4 ) + (i<<5)];
139 tempC5 += tempA0 * B[(jToCol+5 ) + (i<<5)];
140 tempC6 += tempA0 * B[(jToCol+6 ) + (i<<5)];
141 tempC7 += tempA0 * B[(jToCol+7 ) + (i<<5)];
142 tempC0 += tempA1 * B[(jToCol ) + ((i+1)<<5)];
143 tempC1 += tempA1 * B[(jToCol+1 ) + ((i+1)<<5)];
144 tempC2 += tempA1 * B[(jToCol+2 ) + ((i+1)<<5)];
145 tempC3 += tempA1 * B[(jToCol+3 ) + ((i+1)<<5)];
146 tempC4 += tempA1 * B[(jToCol+4 ) + ((i+1)<<5)];
147 tempC5 += tempA1 * B[(jToCol+5 ) + ((i+1)<<5)];
148 tempC6 += tempA1 * B[(jToCol+6 ) + ((i+1)<<5)];
149 tempC7 += tempA1 * B[(jToCol+7 ) + ((i+1)<<5)];
150 }
151 C[j] =tempC0;
152 C[j + 1 ]=tempC1;
153 C[j + 2 ]=tempC2;
154 C[j + 3 ]=tempC3;
155 C[j + 4 ]=tempC4;
156 C[j + 5 ]=tempC5;
157 C[j + 6 ]=tempC6;
158 C[j + 7 ]=tempC7;
159 }
160
161 }
162
163 //--------------------------------------------------------------------------
164 // Main
165 //
166 // all threads start executing thread_entry(). Use their "coreid" to
167 // differentiate between threads (each thread is running on a separate core).
168
169 void thread_entry(int cid, int nc)
170 {
171 coreid = cid;
172 ncores = nc;
173
174 // static allocates data in the binary, which is visible to both threads
175 static data_t results_data[ARRAY_SIZE];
176
177
178 //// Execute the provided, naive matmul
179 //barrier(nc);
180 //stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
181
182 //
183 //// verify
184 //verifyMT(ARRAY_SIZE, results_data, verify_data);
185 //
186 //// clear results from the first trial
187 //size_t i;
188 //if (coreid == 0)
189 // for (i=0; i < ARRAY_SIZE; i++)
190 // results_data[i] = 0;
191 //barrier(nc);
192
193
194 // Execute your faster matmul
195 barrier(nc);
196 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
197
198 #ifdef DEBUG
199 printArrayMT("results:", ARRAY_SIZE, results_data);
200 printArrayMT("verify :", ARRAY_SIZE, verify_data);
201 #endif
202
203 // verify
204 verifyMT(ARRAY_SIZE, results_data, verify_data);
205 barrier(nc);
206
207 exit(0);
208 }
209
210