Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change free be xbt_free_f
[simgrid.git] / src / xbt / xbt_matrix.c
1 /* $Id$ */
2
3 /* xbt_matrix_t management functions                                        */
4
5 /* Copyright (c) 2006 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/matrix.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_matrix,xbt,"2D data storage");
15
16 /** \brief constructor */
17 xbt_matrix_t xbt_matrix_new(int lines, int rows, 
18                             const unsigned long elmsize,
19                             void_f_pvoid_t * const free_f)  {
20    xbt_matrix_t res=xbt_new(s_xbt_matrix_t, 1);
21    res->lines   = lines;
22    res->rows    = rows;
23    res->elmsize = elmsize;
24    res->free_f  = free_f;
25    res->data    = xbt_malloc(elmsize*lines*rows);
26    return res;
27 }
28
29 /** \brief Creates a matrix being a submatrix of another one */
30 xbt_matrix_t xbt_matrix_new_sub(xbt_matrix_t from, 
31                                 int lsize, int rsize,
32                                 int lpos, int rpos,
33                                 pvoid_f_pvoid_t *const cpy_f) {
34    
35    xbt_matrix_t res=xbt_matrix_new(lsize,rsize,
36                                    from->elmsize, from->free_f);
37    xbt_matrix_copy_values(res,from,lsize,rsize,0,0,lpos,rpos,cpy_f);
38    return res;
39 }
40
41 /** \brief destructor */
42 void xbt_matrix_free(xbt_matrix_t mat) {
43   int i;
44   if (mat) {
45     if (mat->free_f) {
46       for (i=0; i < (mat->lines * mat->rows) ; i++) {
47         (*(mat->free_f))( (void*)& (mat->data[i*mat->elmsize]) );
48       }
49     }
50     free(mat->data);
51     free(mat);
52   }
53 }
54
55 /** \brief Freeing function for containers of xbt_matrix_t */
56 void xbt_matrix_free_voidp(void *d) {
57    xbt_matrix_free( (xbt_matrix_t) *(void**)d );
58 }
59
60
61 /** \brief Display the content of a matrix (debugging purpose) 
62  *  \param coords: boolean indicating whether we should add the coords of each cell to the output*/
63 void xbt_matrix_dump(xbt_matrix_t matrix, const char*name, int coords,
64                      void_f_pvoid_t display_fun) {
65   int i,j;
66
67   fprintf(stderr,">>> Matrix %s dump (%d x %d)\n",name,matrix->lines,matrix->rows);
68   for (i=0; i<matrix->lines; i++) {
69     fprintf(stderr,"  ");
70     for (j=0; j<matrix->rows; j++) {
71        if (coords)
72          fprintf(stderr," (%d,%d)=",i,j);
73        else 
74          fprintf(stderr," ");
75        display_fun(xbt_matrix_get_ptr(matrix,i,j));
76     }
77     fprintf(stderr,"\n");
78   }
79   fprintf(stderr,"<<< end_of_matrix %s dump\n",name);
80 }
81
82 void xbt_matrix_dump_display_double(void*d) {
83   fprintf(stderr,"%.2f",*(double*)d);
84 }
85 /** \brief Copy the values from the matrix src into the matrix dst
86  * \param dest: destination
87  * \param src: source
88  * \param lsize: number of lines to copy
89  * \param rsize: number of rows to copy
90  * \param lpos_dst: line offset on destination matrix
91  * \param rpos_dst: row offset on destination matrix
92  * \param lpos_src: line offset on destination matrix
93  * \param rpos_src: row offset on destination matrix
94  */
95 void xbt_matrix_copy_values(xbt_matrix_t dst, xbt_matrix_t src,
96                             int lsize, int rsize,
97                             int lpos_dst,int rpos_dst,
98                             int lpos_src,int rpos_src,
99                             pvoid_f_pvoid_t *const cpy_f) {
100    int i,j;
101    
102    DEBUG10("Copy a %dx%d submatrix from %dx%d(of %dx%d) to %dx%d (of %dx%d)",
103            lsize,rsize,
104            lpos_src,rpos_src,src->lines,src->rows,
105            lpos_dst,rpos_dst,dst->lines,dst->rows);
106    
107    /* everybody knows that issue is between the chair and the screen (particulary in my office) */
108    xbt_assert(src->elmsize == dst->elmsize);
109    /* don't check free_f since the user may play weird games with this */
110    
111    xbt_assert(lpos_src+lsize <= src->lines);
112    xbt_assert(rpos_src+rsize <= src->rows);
113
114    xbt_assert(lpos_dst+lsize <= dst->lines);
115    xbt_assert(rpos_dst+rsize <= dst->rows);
116    
117    /* Lets get serious here */
118    for (i=0;i<rsize;i++) {
119       if (cpy_f) {
120          for (j=0;j<lsize;j++)
121            xbt_matrix_get_as(dst,j+lpos_dst,i+rpos_dst,void*) = cpy_f(xbt_matrix_get_ptr(src,j+rpos_src,i+lpos_src));
122       } else {
123          memcpy(xbt_matrix_get_ptr(dst,lpos_dst,i+rpos_dst),
124                 xbt_matrix_get_ptr(src,lpos_src,i+rpos_src),
125                 dst->elmsize*lsize);
126       }
127    }
128
129 }
130
131 /** \brief Creates a new matrix of double filled with zeros */
132 xbt_matrix_t xbt_matrix_double_new_zeros(int lines, int rows) {
133   xbt_matrix_t res = xbt_matrix_new(lines, rows,sizeof(double),NULL);
134    
135   memset(res->data,0, res->elmsize * res->lines * res->rows);
136   return res;
137 }
138
139 /** \brief Creates a new matrix of double being the identity matrix */
140 xbt_matrix_t xbt_matrix_double_new_id(int lines, int rows) {
141   xbt_matrix_t res = xbt_matrix_double_new_zeros(lines, rows);
142   int i;
143    
144   for (i=0; i<lines; i++) 
145     xbt_matrix_get_as(res,i,i,double) = 1;
146   return res;
147 }
148
149 /** \brief Creates a new matrix of double randomly filled */
150 xbt_matrix_t xbt_matrix_double_new_rand(int lines, int rows) {
151   xbt_matrix_t res = xbt_matrix_new(lines, rows,sizeof(double),NULL);
152   int i,j;
153    
154   for (i=0; i<lines; i++) 
155     for (j=0; j<rows; j++)
156       xbt_matrix_get_as(res,i,j,double) = (double)rand();
157   return res;
158 }
159 /** \brief Creates a new matrix of double randomly by subsequent numbers */
160 xbt_matrix_t xbt_matrix_double_new_seq(int lines, int rows) {
161   xbt_matrix_t res = xbt_matrix_new(lines, rows,sizeof(double),NULL);
162   int i;
163    
164   for (i=0; i<lines*rows; i++)
165      *(double*)&res->data[i*res->elmsize] = i;
166
167   return res;
168 }
169
170 /** \brief Creates a new matrix being the multiplication of two others */
171 xbt_matrix_t xbt_matrix_double_new_mult(xbt_matrix_t A,xbt_matrix_t B) {
172   xbt_matrix_t result = xbt_matrix_double_new_zeros(A->lines, B->rows);
173
174   xbt_matrix_double_addmult(A,B,result);
175   return result;
176 }
177
178 /** \brief add to C the result of A*B */
179 void xbt_matrix_double_addmult(xbt_matrix_t A,xbt_matrix_t B,
180                        /*OUT*/ xbt_matrix_t C) {
181   int i,j,k;
182
183   xbt_assert2(A->lines == C->lines,
184               "A->lines != C->lines (%d vs %d)",A->lines,C->lines);
185   xbt_assert(B->rows == C->rows);
186    
187   for (i=0; i<C->lines; i++) 
188     for (j=0; j<C->rows; j++) 
189        for (k=0; k<B->lines; k++) 
190          xbt_matrix_get_as(C,i,j,double) +=
191             xbt_matrix_get_as(A,i,k,double) * xbt_matrix_get_as(B,k,j,double);
192 }
193
194    
195