1 | #include "benchmark.h" |
---|
2 | #include "system.h" |
---|
3 | #include "stdlib.h" |
---|
4 | #include "stdio.h" |
---|
5 | #include "stdint.h" |
---|
6 | #include "../matrix_multiplication/matrix_multiplication.h" |
---|
7 | #include "../common/common.h" |
---|
8 | |
---|
9 | static uint32_t matrix_lock; |
---|
10 | |
---|
11 | void benchmark_matrix_malloc (int ** matrix_a, |
---|
12 | int ** matrix_b, |
---|
13 | int ** matrix_d, |
---|
14 | int size) |
---|
15 | { |
---|
16 | int i; |
---|
17 | |
---|
18 | for (i=0; i<size; ++i) |
---|
19 | { |
---|
20 | matrix_a[i] = (int*) malloc(size*sizeof(int)); |
---|
21 | matrix_b[i] = (int*) malloc(size*sizeof(int)); |
---|
22 | matrix_d[i] = (int*) malloc(size*sizeof(int)); |
---|
23 | } |
---|
24 | } |
---|
25 | |
---|
26 | void benchmark_matrix_free (int ** matrix_a, |
---|
27 | int ** matrix_b, |
---|
28 | int ** matrix_d, |
---|
29 | int size) |
---|
30 | { |
---|
31 | int i; |
---|
32 | |
---|
33 | for (i=0; i<size; ++i) |
---|
34 | { |
---|
35 | free(matrix_a[i]); |
---|
36 | free(matrix_b[i]); |
---|
37 | free(matrix_d[i]); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | void benchmark_matrix_init (int ** matrix_a, |
---|
42 | int ** matrix_b, |
---|
43 | int size) |
---|
44 | { |
---|
45 | int i,j,x; |
---|
46 | |
---|
47 | x = 0; |
---|
48 | for (i=0; i<size; ++i) |
---|
49 | for (j=0; j<size; ++j) |
---|
50 | { |
---|
51 | matrix_a [i][j] = x; |
---|
52 | matrix_b [j][i] = x; |
---|
53 | x++; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | void benchmark_matrix_validation (int ** matrix_d, |
---|
58 | int size) |
---|
59 | { |
---|
60 | int i,j,x; |
---|
61 | int error = 0; |
---|
62 | |
---|
63 | for (i=0; (i<size)&&(error==0); ++i) |
---|
64 | for (j=0; (j<size)&&(error==0); ++j) |
---|
65 | { |
---|
66 | int val = 0; |
---|
67 | for (x=0; x<size; ++x) |
---|
68 | val += (i*size+x)*(j*size+x); |
---|
69 | |
---|
70 | if (matrix_d[i][j] != val) |
---|
71 | { |
---|
72 | error = 1; |
---|
73 | printf("KO !!! (matrix_d[%d][%d] = %d (!= %d))\n",i,j,matrix_d[i][j],val); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | if (error == 0) |
---|
78 | printf("OK\n"); |
---|
79 | } |
---|
80 | |
---|
81 | int _benchmark_matrix_multiplication_st (unsigned int size) |
---|
82 | { |
---|
83 | printf("\n"); |
---|
84 | printf("================================\n"); |
---|
85 | printf("Benchmark Matrix Multiplication (ST)\n"); |
---|
86 | printf("================================\n"); |
---|
87 | printf("\n"); |
---|
88 | |
---|
89 | printf(" * Size : %d\n",size); |
---|
90 | |
---|
91 | int cycle_begin, result; |
---|
92 | |
---|
93 | result = 0; |
---|
94 | |
---|
95 | printf(" * Malloc...\n"); |
---|
96 | |
---|
97 | lock_lock(&matrix_lock); |
---|
98 | |
---|
99 | int ** matrix_local_a = (int**) malloc(size*sizeof(int*)); |
---|
100 | int ** matrix_local_b = (int**) malloc(size*sizeof(int*)); |
---|
101 | int ** matrix_local_d = (int**) malloc(size*sizeof(int*)); |
---|
102 | |
---|
103 | benchmark_matrix_malloc (matrix_local_a, |
---|
104 | matrix_local_b, |
---|
105 | matrix_local_d, |
---|
106 | size); |
---|
107 | lock_unlock(&matrix_lock); |
---|
108 | |
---|
109 | printf(" * Init...\n"); |
---|
110 | |
---|
111 | benchmark_matrix_init (matrix_local_a, |
---|
112 | matrix_local_b, |
---|
113 | size); |
---|
114 | |
---|
115 | /* matrix_multiplication_print(matrix_local_a, size); */ |
---|
116 | /* matrix_multiplication_print(matrix_local_b, size); */ |
---|
117 | |
---|
118 | printf(" * Matrix multiplication (size : %d)... \n",size); |
---|
119 | |
---|
120 | cycle_begin = cpu_cycles(); |
---|
121 | matrix_multiplication_st(matrix_local_a, |
---|
122 | matrix_local_b, |
---|
123 | matrix_local_d, |
---|
124 | size); |
---|
125 | result += cpu_cycles()-cycle_begin; |
---|
126 | |
---|
127 | /* matrix_multiplication_print(matrix_local_d, size); */ |
---|
128 | |
---|
129 | #if VERIFICATION_MATRIX_MULTIPLICATION |
---|
130 | printf(" * Verification... "); |
---|
131 | |
---|
132 | benchmark_matrix_validation (matrix_local_d, size); |
---|
133 | #endif |
---|
134 | |
---|
135 | printf(" * Free...\n"); |
---|
136 | |
---|
137 | benchmark_matrix_free (matrix_local_a, |
---|
138 | matrix_local_b, |
---|
139 | matrix_local_d, |
---|
140 | size); |
---|
141 | |
---|
142 | free(matrix_local_a); |
---|
143 | free(matrix_local_b); |
---|
144 | free(matrix_local_d); |
---|
145 | |
---|
146 | return result; |
---|
147 | } |
---|
148 | |
---|
149 | static int ** matrix_global_a; |
---|
150 | static int ** matrix_global_b; |
---|
151 | static int ** matrix_global_d; |
---|
152 | |
---|
153 | static int matrix_nb_thread_start; |
---|
154 | static int matrix_nb_thread_stop; |
---|
155 | static int matrix_end; |
---|
156 | |
---|
157 | int _benchmark_matrix_multiplication_mt (int size, int lock_by_line) |
---|
158 | { |
---|
159 | printf("\n"); |
---|
160 | printf("================================\n"); |
---|
161 | printf("Benchmark Matrix Multiplication (MT)\n"); |
---|
162 | printf("================================\n"); |
---|
163 | printf("\n"); |
---|
164 | |
---|
165 | printf(" * Size : %d\n",size); |
---|
166 | printf(" * Lock by line : %d\n",lock_by_line); |
---|
167 | |
---|
168 | int cycle_begin, result; |
---|
169 | |
---|
170 | result = 0; |
---|
171 | |
---|
172 | lock_lock(&matrix_lock); |
---|
173 | |
---|
174 | if (matrix_end != 0) |
---|
175 | { |
---|
176 | printf("Benchmark is already finished\n"); |
---|
177 | |
---|
178 | lock_unlock(&matrix_lock); |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | printf(" * Start number %d\n",matrix_nb_thread_start); |
---|
184 | |
---|
185 | // first thread |
---|
186 | if (matrix_nb_thread_start==0) |
---|
187 | { |
---|
188 | printf(" * Malloc...\n"); |
---|
189 | |
---|
190 | matrix_global_a = (int**) malloc(size*sizeof(int*)); |
---|
191 | matrix_global_b = (int**) malloc(size*sizeof(int*)); |
---|
192 | matrix_global_d = (int**) malloc(size*sizeof(int*)); |
---|
193 | |
---|
194 | benchmark_matrix_malloc (matrix_global_a, |
---|
195 | matrix_global_b, |
---|
196 | matrix_global_d, |
---|
197 | size); |
---|
198 | |
---|
199 | printf(" * Init...\n"); |
---|
200 | |
---|
201 | benchmark_matrix_init (matrix_global_a, |
---|
202 | matrix_global_b, |
---|
203 | size); |
---|
204 | } |
---|
205 | |
---|
206 | matrix_nb_thread_start ++; |
---|
207 | |
---|
208 | lock_unlock(&matrix_lock); |
---|
209 | |
---|
210 | /* matrix_multiplication_print(matrix_global_a, size); */ |
---|
211 | /* matrix_multiplication_print(matrix_global_b, size); */ |
---|
212 | |
---|
213 | printf(" * Matrix multiplication (size : %d)... \n",size); |
---|
214 | |
---|
215 | cycle_begin = cpu_cycles(); |
---|
216 | matrix_multiplication_mt(matrix_global_a, |
---|
217 | matrix_global_b, |
---|
218 | matrix_global_d, |
---|
219 | size, |
---|
220 | lock_by_line); |
---|
221 | result += cpu_cycles()-cycle_begin; |
---|
222 | |
---|
223 | /* matrix_multiplication_print(matrix_global_d, size); */ |
---|
224 | |
---|
225 | lock_lock(&matrix_lock); |
---|
226 | |
---|
227 | printf(" * Stop number %d\n",matrix_nb_thread_stop); |
---|
228 | |
---|
229 | matrix_nb_thread_stop ++; |
---|
230 | |
---|
231 | // first thread |
---|
232 | if (matrix_nb_thread_stop == matrix_nb_thread_start) |
---|
233 | { |
---|
234 | #if VERIFICATION_MATRIX_MULTIPLICATION |
---|
235 | printf(" * Verification... "); |
---|
236 | |
---|
237 | benchmark_matrix_validation (matrix_global_d, size); |
---|
238 | #endif |
---|
239 | |
---|
240 | printf(" * Free...\n"); |
---|
241 | |
---|
242 | benchmark_matrix_free (matrix_global_a, |
---|
243 | matrix_global_b, |
---|
244 | matrix_global_d, |
---|
245 | size); |
---|
246 | |
---|
247 | free(matrix_global_a); |
---|
248 | free(matrix_global_b); |
---|
249 | free(matrix_global_d); |
---|
250 | |
---|
251 | matrix_end = 1; |
---|
252 | } |
---|
253 | |
---|
254 | lock_unlock(&matrix_lock); |
---|
255 | |
---|
256 | return result; |
---|
257 | } |
---|
258 | |
---|
259 | int benchmark_matrix_multiplication_st (void) { return _benchmark_matrix_multiplication_st (MATRIX_MULTIPLICATION_ST_SIZE);} |
---|
260 | int benchmark_matrix_multiplication_mt (void) { return _benchmark_matrix_multiplication_mt (MATRIX_MULTIPLICATION_MT_SIZE,MATRIX_MULTIPLICATION_MT_LOCK_BY_LINE);} |
---|
261 | |
---|
262 | |
---|