Logo AND Algorithmique Numérique Distribuée

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