Updated mt tests
[riscv-tests.git] / mt / af_matmul / failedattempt2.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
110 void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
111 {
112 size_t i;
113 size_t i2;
114 size_t j;
115 size_t j2;
116 size_t k;
117 size_t k2;
118 size_t max_dim = lda*lda;
119 size_t block_size = lda/2;
120 int result = 0;
121 data_t temp_mat1[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};
122 if (coreid == 0) {
123 for (k = 0; k < lda/2; k++) {
124 int columnIndex = 32*k;
125
126 //temp_mat1 will store the kth column of B
127 for (i = 0; i < lda; i++) {
128 temp_mat1[i] = B[32*i + k];
129 }
130
131 for (j =0; j < lda; j++) {
132 int rowIndex = 32*j;
133 //iterate through each element of A in row J and accumulate result
134 for (i2 = 0; i2 <lda; i2 += 4) {
135 int elementA = A[rowIndex+i2];
136 int elementA2 = A[rowIndex+i2+1];
137 int elementA3 = A[rowIndex+i2+2];
138 int elementA4 = A[rowIndex+i2+3];
139 result += elementA*temp_mat1[i2] + elementA2*temp_mat1[i2+1] + elementA3*temp_mat1[i2+2] + elementA4*temp_mat1[i2+3] ;
140 }
141 C[k+rowIndex] = result;
142 result = 0;
143 }
144
145 }
146 } else {
147 for (k = lda/2; k < lda; k++) {
148 int columnIndex = 32*k;
149
150 //temp_mat1 will store the kth column of B
151 for (i = 0; i < lda; i++) {
152 temp_mat1[i] = B[32*i + k];
153 }
154
155 for (j =0; j < lda; j++) {
156 int rowIndex = 32*j;
157 //iterate through each element of A in row J and accumulate result
158 for (i2 = 0; i2 <lda; i2 += 4) {
159 int elementA = A[rowIndex+i2];
160 int elementA2 = A[rowIndex+i2+1];
161 int elementA3 = A[rowIndex+i2+2];
162 int elementA4 = A[rowIndex+i2+3];
163 result += elementA*temp_mat1[i2] + elementA2*temp_mat1[i2+1] + elementA3*temp_mat1[i2+2] + elementA4*temp_mat1[i2+3] ;
164 }
165 C[k+rowIndex] = result;
166 result = 0;
167 }
168
169 }
170
171 }
172
173
174
175
176 // ***************************** //
177 // **** ADD YOUR CODE HERE ***** //
178 // ***************************** //
179 //
180 // feel free to make a separate function for MI and MSI versions.
181
182 }
183
184 //--------------------------------------------------------------------------
185 // Main
186 //
187 // all threads start executing thread_entry(). Use their "coreid" to
188 // differentiate between threads (each thread is running on a separate core).
189
190 void thread_entry(int cid, int nc)
191 {
192 coreid = cid;
193 ncores = nc;
194
195 // static allocates data in the binary, which is visible to both threads
196 static data_t results_data[ARRAY_SIZE];
197
198
199 // Execute the provided, naive matmul
200 barrier(nc);
201 stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
202
203
204 // verify
205 verifyMT(ARRAY_SIZE, results_data, verify_data);
206
207 // clear results from the first trial
208 size_t i;
209 if (coreid == 0)
210 for (i=0; i < ARRAY_SIZE; i++)
211 results_data[i] = 0;
212 barrier(nc);
213
214
215 // Execute your faster matmul
216 barrier(nc);
217 stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier(nc));
218
219 #ifdef DEBUG
220 printArrayMT("results:", ARRAY_SIZE, results_data);
221 printArrayMT("verify :", ARRAY_SIZE, verify_data);
222 #endif
223
224 // verify
225 verifyMT(ARRAY_SIZE, results_data, verify_data);
226 barrier(nc);
227
228 exit(0);
229 }