Logo AND Algorithmique Numérique Distribuée

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