Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[smpi,example] an example of matrix multiplication with non contignous memory
[simgrid.git] / examples / smpi / MM / Matrix_init.c
1 #include "Matrix_init.h"
2 #include <math.h>
3 #include "xbt/log.h"
4  XBT_LOG_NEW_DEFAULT_CATEGORY(MM_init,
5                              "Messages specific for this msg example");
6 #define _unused(x) ((void)x)
7
8
9 void matrices_initialisation( double ** p_a, double ** p_b, double ** p_c,
10                               size_t m, size_t k_a, size_t k_b, size_t n,
11                               size_t row, size_t col)
12 {
13
14   size_t x,y,z;
15   size_t lda = k_a;
16   size_t ldb = n;
17   size_t ldc = n;
18   double *a, *b, *c;
19   _unused(row);
20
21   a =  malloc(sizeof(double) * m * k_a);
22
23   if ( a == 0 ){
24     perror("Error allocation Matrix A");
25     exit(-1);
26   }
27
28   b = malloc(sizeof(double) * k_b * n);
29
30   if ( b == 0 ){
31     perror("Error allocation Matrix B");
32     exit(-1);
33   }
34
35   c = malloc(sizeof(double) * m * n);
36   if ( c == 0 ){
37     perror("Error allocation Matrix C");
38     exit(-1);
39   }
40
41   *p_a=a;
42   *p_b =b;
43   *p_c=c;
44
45   // size_tialisation of the matrices
46   for( x=0; x<m; x++){
47     for( z=0; z<k_a; z++){
48 #ifdef SIMPLE_MATRIX
49       a[x*lda+z] = 1;
50 #else
51       a[x*lda+z] = (double)(z+col*n);
52 #endif
53     }
54   }
55   for( z=0; z<k_b; z++){
56     for( y=0; y<n; y++){
57 #ifdef SIMPLE_MATRIX
58       b[z*ldb+y] = 1;
59 #else
60       b[z*ldb+y] = (double)(y);
61 #endif
62     }
63   }
64   for( x=0; x<m; x++){
65     for( y=0; y<n; y++){
66       c[x*ldc+y] = 0;
67     }
68   }
69 }
70
71 void matrices_allocation( double ** p_a, double ** p_b, double ** p_c,
72                           size_t m, size_t k_a, size_t k_b, size_t n)
73 {
74
75   double * a, *b, *c;
76
77   a =  malloc(sizeof(double) * m * k_a);
78
79   if ( a == 0 ){
80     perror("Error allocation Matrix A");
81     exit(-1);
82   }
83
84   b = malloc(sizeof(double) * k_b * n);
85
86   if ( b == 0 ){
87     perror("Error allocation Matrix B");
88     exit(-1);
89   }
90
91   c = malloc(sizeof(double) * m * n);
92   if ( c == 0 ){
93     perror("Error allocation Matrix C");
94     exit(-1);
95   }
96
97   *p_a=a;
98   *p_b =b;
99   *p_c=c;
100
101 }
102
103 void blocks_initialisation( double ** p_a_local, double ** p_b_local,
104                             size_t m, size_t B_k, size_t n)
105 {
106   size_t x,y,z;
107   size_t lda = B_k;
108   size_t ldb = n;
109   double * a_local, *b_local;
110
111   a_local =  malloc(sizeof(double) * m * B_k);
112
113   if ( a_local == 0 ){
114     perror("Error allocation Matrix A");
115     exit(-1);
116   }
117
118   b_local = malloc(sizeof(double) * B_k * n);
119
120   if ( b_local == 0 ){
121     perror("Error allocation Matrix B");
122     exit(-1);
123   }
124
125   *p_a_local = a_local;
126   *p_b_local = b_local;
127
128   // size_tialisation of the matrices
129   for( x=0; x<m; x++){
130     for( z=0; z<B_k; z++){
131       a_local[x*lda+z] = 0.0;
132     }
133   }
134   for( z=0; z<B_k; z++){
135     for( y=0; y<n; y++){
136       b_local[z*ldb+y] = 0.0;
137     }
138   }
139 }
140
141 void check_result(double *c, double *a, double *b,
142                   size_t m, size_t n, size_t k_a, size_t k_b,
143                   size_t row, size_t col,
144                   size_t size_row, size_t size_col)
145 {
146   size_t x,y;
147   size_t ldc = n;
148   _unused(a);
149   _unused(b);
150   _unused(k_b);
151   _unused(k_a);
152   _unused(row);
153   _unused(col);
154   _unused(size_row);
155   /* these variable could be use to check the result in function of the
156    * matrix initialization */
157
158
159   /*Display for checking */
160 #ifdef SIMPLE_MATRIX
161   XBT_INFO("Value get : %f excepted %zu multiply by y\n", c[((int)m/2)*ldc+1],size_row*k_a );
162 #else
163   XBT_INFO("Value get : %f excepted %zu multiply by y\n", c[((int)m/2)*ldc+1], 1*(size_col*m)*((size_col*m)-1)/2) ;
164 #endif
165   for( x=0; x<m; x++){
166     for( y=0; y<n; y++){
167       /* WARNING this could be lead to some errors ( precision with double )*/
168 #ifdef SIMPLE_MATRIX
169       if ( fabs(c[x*ldc + y] - size_row*k_a) > 0.0000001)
170 #else
171       if ( fabs(c[x*ldc + y] - y*(size_col*m)*((size_col*m)-1)/2) > 0.0000001)
172 #endif
173       {
174 #ifdef SIMPLE_MATRIX
175         XBT_INFO( "%f\t%zu, y : %zu x : %zu \n",
176                c[x*ldc+y], size_row*k_a, y, x);
177 #else
178         XBT_INFO( "%f\t%zu, y : %zu x : %zu \n",
179                c[x*ldc+y], y*(size_col*m)*((size_col*m)-1)/2, y, x);
180 #endif
181         goto error_exit;
182       }
183     }
184   }
185   XBT_INFO("result check: ok\n");
186   return;
187 error_exit:
188   XBT_INFO("result check not ok\n"
189          "WARNING the test could be lead to some "
190          "errors ( precision with double )\n");
191   return;
192 }
193
194