Logo AND Algorithmique Numérique Distribuée

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