Logo AND Algorithmique Numérique Distribuée

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