1 /**-------------------------------------------------------------------**
3 **-------------------------------------------------------------------**
5 **-------------------------------------------------------------------**/
8 /******************************************************************************
9 * CLooG : the Chunky Loop Generator (experimental) *
10 ******************************************************************************
12 * Copyright (C) 2001-2005 Cedric Bastoul *
14 * This library is free software; you can redistribute it and/or *
15 * modify it under the terms of the GNU Lesser General Public *
16 * License as published by the Free Software Foundation; either *
17 * version 2.1 of the License, or (at your option) any later version. *
19 * This library is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Lesser General Public License for more details. *
24 * You should have received a copy of the GNU Lesser General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27 * Boston, MA 02110-1301 USA *
29 * CLooG, the Chunky Loop Generator *
30 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
32 ******************************************************************************/
37 #include "../include/cloog/cloog.h"
41 * Allocate a CloogMatrix data structure with NbRows rows and NbColumns columns.
42 * All values are initialized to 0.
43 * This method returns a pointer to the data structure if successful or a NULL
46 CloogMatrix
*cloog_matrix_alloc(unsigned NbRows
, unsigned NbColumns
)
52 matrix
= (CloogMatrix
*)malloc(sizeof(CloogMatrix
));
57 matrix
->NbRows
= NbRows
;
58 matrix
->NbColumns
= NbColumns
;
60 if (!NbRows
|| !NbColumns
) {
62 matrix
->p_Init
= NULL
;
66 p
= (cloog_int_t
**)malloc(NbRows
* sizeof(cloog_int_t
*));
73 q
= (cloog_int_t
*)malloc(NbRows
* NbColumns
* sizeof(cloog_int_t
));
84 for (i
= 0; i
< NbRows
; i
++) {
86 for (j
= 0; j
< NbColumns
; j
++) {
87 cloog_int_init(*(q
+j
));
88 cloog_int_set_si(*(q
+j
), 0);
100 void cloog_matrix_free(CloogMatrix
* matrix
)
104 int size
= matrix
->NbRows
* matrix
->NbColumns
;
108 for (i
= 0; i
< size
; i
++)
109 cloog_int_clear(*p
++);
112 free(matrix
->p_Init
);
120 * Print the elements of CloogMatrix M to file, with each row prefixed
121 * by prefix and suffixed by suffix.
123 void cloog_matrix_print_structure(FILE *file
, CloogMatrix
*M
,
124 const char *prefix
, const char *suffix
)
128 for (i
= 0; i
< M
->NbRows
; ++i
) {
129 fprintf(file
, "%s", prefix
);
130 for (j
= 0; j
< M
->NbColumns
; ++j
) {
131 cloog_int_print(file
, M
->p
[i
][j
]);
134 fprintf(file
, "%s\n", suffix
);
139 * cloog_matrix_print function:
140 * This function prints the content of a CloogMatrix structure (matrix) into a
141 * file (foo, possibly stdout).
143 void cloog_matrix_print(FILE* foo
, CloogMatrix
* m
)
146 fprintf(foo
, "(null)\n");
148 fprintf(foo
, "%d %d\n", m
->NbRows
, m
->NbColumns
);
149 cloog_matrix_print_structure(foo
, m
, "", "");
154 static char *next_line(FILE *input
, char *line
, unsigned len
)
159 if (!(p
= fgets(line
, len
, input
)))
161 while (isspace(*p
) && *p
!= '\n')
163 } while (*p
== '#' || *p
== '\n');
169 * Read a matrix in PolyLib format from input.
171 CloogMatrix
*cloog_matrix_read(FILE *input
)
175 unsigned n_row
, n_col
;
180 if (!next_line(input
, line
, sizeof(line
)))
181 cloog_die("Input error.\n");
182 if (sscanf(line
, "%u %u", &n_row
, &n_col
) != 2)
183 cloog_die("Input error.\n");
184 M
= cloog_matrix_alloc(n_row
, n_col
);
186 cloog_die("memory overflow.\n");
187 for (i
= 0; i
< n_row
; ++i
) {
191 p
= next_line(input
, line
, sizeof(line
));
193 cloog_die("Input error.\n");
194 for (j
= 0; j
< n_col
; ++j
) {
195 n
= sscanf(p
, "%s%n", val
, &offset
);
197 cloog_die("Input error.\n");
198 cloog_int_read(M
->p
[i
][j
], val
);