source: trunk/platforms/caba-ring-ccxcachev4_memcachev4-mips32el/soft/benchmark/benchmark_matrix_multiplication.c @ 134

Last change on this file since 134 was 134, checked in by kane, 13 years ago

add multi write buffer in cc_xcache_v4

File size: 6.4 KB
Line 
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
9static uint32_t matrix_lock;
10
11void 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
26void 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
41void 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
57void 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
81int _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  printf(" * Verification... ");
130 
131  benchmark_matrix_validation (matrix_local_d, size);
132 
133  printf(" * Free...\n");
134 
135  benchmark_matrix_free (matrix_local_a,
136                         matrix_local_b,
137                         matrix_local_d,
138                         size);
139 
140  free(matrix_local_a);
141  free(matrix_local_b);
142  free(matrix_local_d);
143
144  return result;
145}
146
147static int ** matrix_global_a;
148static int ** matrix_global_b;
149static int ** matrix_global_d;
150
151static int matrix_nb_thread_start;
152static int matrix_nb_thread_stop;
153static int matrix_end;
154
155int _benchmark_matrix_multiplication_mt (int size, int lock_by_line)
156{
157  printf("\n");
158  printf("================================\n");
159  printf("Benchmark Matrix Multiplication (MT)\n");
160  printf("================================\n");
161  printf("\n");
162 
163  printf(" * Size         : %d\n",size);
164  printf(" * Lock by line : %d\n",lock_by_line);
165 
166  int cycle_begin, result;
167
168  result = 0;
169
170  lock_lock(&matrix_lock);
171
172  if (matrix_end != 0)
173    {
174      printf("Benchmark is already finished\n");
175
176      lock_unlock(&matrix_lock);
177     
178      return 0;
179    }
180     
181  printf(" * Start number %d\n",matrix_nb_thread_start);
182 
183  // first thread
184  if (matrix_nb_thread_start==0)
185    {
186      printf(" * Malloc...\n");
187
188      matrix_global_a = (int**) malloc(size*sizeof(int*));
189      matrix_global_b = (int**) malloc(size*sizeof(int*));
190      matrix_global_d = (int**) malloc(size*sizeof(int*));
191
192      benchmark_matrix_malloc (matrix_global_a,
193                               matrix_global_b,
194                               matrix_global_d,
195                               size);
196           
197      printf(" * Init...\n");
198
199      benchmark_matrix_init (matrix_global_a,
200                             matrix_global_b,
201                             size);
202    }
203
204  matrix_nb_thread_start ++;
205
206  lock_unlock(&matrix_lock);
207 
208  /* matrix_multiplication_print(matrix_global_a, size); */
209  /* matrix_multiplication_print(matrix_global_b, size); */
210 
211  printf(" * Matrix multiplication (size : %d)... \n",size);
212
213  cycle_begin = cpu_cycles();
214  matrix_multiplication_mt(matrix_global_a,
215                           matrix_global_b,
216                           matrix_global_d,
217                           size,
218                           lock_by_line);
219  result += cpu_cycles()-cycle_begin;
220
221  /* matrix_multiplication_print(matrix_global_d, size); */
222
223  lock_lock(&matrix_lock);
224
225  printf(" * Stop number %d\n",matrix_nb_thread_stop);
226 
227  matrix_nb_thread_stop ++;
228   
229  // first thread
230  if (matrix_nb_thread_stop == matrix_nb_thread_start)
231    {
232      printf(" * Verification... ");
233
234      benchmark_matrix_validation (matrix_global_d, size);
235     
236      printf(" * Free...\n");
237   
238      benchmark_matrix_free (matrix_global_a,
239                             matrix_global_b,
240                             matrix_global_d,
241                             size);
242     
243      free(matrix_global_a);
244      free(matrix_global_b);
245      free(matrix_global_d);
246
247      matrix_end = 1;
248    }
249
250  lock_unlock(&matrix_lock);
251 
252  return result;
253}
254
255int benchmark_matrix_multiplication_st (void) { return _benchmark_matrix_multiplication_st (MATRIX_MULTIPLICATION_ST_SIZE);}
256int benchmark_matrix_multiplication_mt (void) { return _benchmark_matrix_multiplication_mt (MATRIX_MULTIPLICATION_MT_SIZE,MATRIX_MULTIPLICATION_MT_LOCK_BY_LINE);}
257
258
Note: See TracBrowser for help on using the repository browser.