Logo AND Algorithmique Numérique Distribuée

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