1   /**-------------------------------------------------------------------**
2    **                              CLooG                                **
3    **-------------------------------------------------------------------**
4    **                           cloogmatrix.c                           **
5    **-------------------------------------------------------------------**/
6
7
8/******************************************************************************
9 *               CLooG : the Chunky Loop Generator (experimental)             *
10 ******************************************************************************
11 *                                                                            *
12 * Copyright (C) 2001-2005 Cedric Bastoul                                     *
13 *                                                                            *
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.         *
18 *                                                                            *
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.                            *
23 *                                                                            *
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                                                *
28 *                                                                            *
29 * CLooG, the Chunky Loop Generator                                           *
30 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr                         *
31 *                                                                            *
32 ******************************************************************************/
33
34#include <ctype.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include "../include/cloog/cloog.h"
38
39/**
40 * cloog_matrix_alloc:
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
44 * pointer otherwise.
45 */
46CloogMatrix *cloog_matrix_alloc(unsigned NbRows, unsigned NbColumns)
47{
48  CloogMatrix *matrix;
49  cloog_int_t **p, *q;
50  int i, j;
51
52  matrix = (CloogMatrix *)malloc(sizeof(CloogMatrix));
53
54  if (!matrix)
55    return NULL;
56
57  matrix->NbRows = NbRows;
58  matrix->NbColumns = NbColumns;
59
60  if (!NbRows || !NbColumns) {
61    matrix->p = NULL;
62    matrix->p_Init = NULL;
63    return matrix;
64  }
65
66  p = (cloog_int_t **)malloc(NbRows * sizeof(cloog_int_t *));
67
68  if (p == NULL) {
69    free (matrix);
70    return NULL;
71  }
72
73  q = (cloog_int_t *)malloc(NbRows * NbColumns * sizeof(cloog_int_t));
74
75  if (q == NULL) {
76    free (matrix);
77    free (p);
78    return NULL;
79  }
80
81  matrix->p = p;
82  matrix->p_Init = q;
83
84  for (i = 0; i < NbRows; i++) {
85    *p++ = q;
86    for (j = 0; j < NbColumns; j++) {
87      cloog_int_init(*(q+j));
88      cloog_int_set_si(*(q+j), 0);
89    }
90    q += NbColumns;
91  }
92
93  return matrix;
94}
95
96/**
97 * cloog_matrix_free:
98 * Free matrix.
99 */
100void cloog_matrix_free(CloogMatrix * matrix)
101{
102  int i;
103  cloog_int_t *p;
104  int size = matrix->NbRows * matrix->NbColumns;
105
106  p = matrix->p_Init;
107
108  for (i = 0; i < size; i++)
109    cloog_int_clear(*p++);
110
111  if (matrix) {
112    free(matrix->p_Init);
113    free(matrix->p);
114    free(matrix);
115  }
116}
117
118
119/**
120 * Print the elements of CloogMatrix M to file, with each row prefixed
121 * by prefix and suffixed by suffix.
122 */
123void cloog_matrix_print_structure(FILE *file, CloogMatrix *M,
124		const char *prefix, const char *suffix)
125{
126    int i, j;
127
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]);
132	    fprintf(file, " ");
133	}
134	fprintf(file, "%s\n", suffix);
135    }
136}
137
138/**
139 * cloog_matrix_print function:
140 * This function prints the content of a CloogMatrix structure (matrix) into a
141 * file (foo, possibly stdout).
142 */
143void cloog_matrix_print(FILE* foo, CloogMatrix* m)
144{
145  if (!m)
146    fprintf(foo, "(null)\n");
147
148  fprintf(foo, "%d %d\n", m->NbRows, m->NbColumns);
149  cloog_matrix_print_structure(foo, m, "", "");
150  fflush(foo);
151}
152
153
154static char *next_line(FILE *input, char *line, unsigned len)
155{
156	char *p;
157
158	do {
159		if (!(p = fgets(line, len, input)))
160			return NULL;
161		while (isspace(*p) && *p != '\n')
162			++p;
163	} while (*p == '#' || *p == '\n');
164
165	return p;
166}
167
168CloogMatrix *cloog_matrix_read(FILE *input)
169{
170	unsigned n_row, n_col;
171	char line[1024];
172
173	if (!next_line(input, line, sizeof(line)))
174		cloog_die("Input error.\n");
175	if (sscanf(line, "%u %u", &n_row, &n_col) != 2)
176		cloog_die("Input error.\n");
177
178	return cloog_matrix_read_of_size(input, n_row, n_col);
179}
180
181/**
182 * Read a matrix in PolyLib format from input.
183 */
184CloogMatrix *cloog_matrix_read_of_size(FILE *input,
185	unsigned n_row, unsigned n_col)
186{
187	CloogMatrix *M;
188	int i, j;
189	char line[1024];
190	char val[1024];
191	char *p;
192
193	M = cloog_matrix_alloc(n_row, n_col);
194	if (!M)
195		cloog_die("memory overflow.\n");
196	for (i = 0; i < n_row; ++i) {
197		int offset;
198		int n;
199
200		p = next_line(input, line, sizeof(line));
201		if (!p)
202			cloog_die("Input error.\n");
203		for (j = 0; j < n_col; ++j) {
204			n = sscanf(p, "%s%n", val, &offset);
205			if (!n)
206				cloog_die("Input error.\n");
207			cloog_int_read(M->p[i][j], val);
208			p += offset;
209		}
210	}
211
212	return M;
213}
214