add cloog_matrix_print_structure
[cloog.git] / source / matrix.c
blobd30c8b53c47b876f0f8238dd91a02e7a46e93313
1 /**-------------------------------------------------------------------**
2 ** CLooG **
3 **-------------------------------------------------------------------**
4 ** cloogmatrix.c **
5 **-------------------------------------------------------------------**/
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 ******************************************************************************/
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include "../include/cloog/cloog.h"
38 /**
39 * cloog_matrix_alloc:
40 * Allocate a CloogMatrix data structure with NbRows rows and NbColumns columns.
41 * All values are initialized to 0.
42 * This method returns a pointer to the data structure if successful or a NULL
43 * pointer otherwise.
45 CloogMatrix *cloog_matrix_alloc(unsigned NbRows, unsigned NbColumns)
47 CloogMatrix *matrix;
48 cloog_int_t **p, *q;
49 int i, j;
51 matrix = (CloogMatrix *)malloc(sizeof(CloogMatrix));
53 if (!matrix)
54 return NULL;
56 matrix->NbRows = NbRows;
57 matrix->NbColumns = NbColumns;
59 if (!NbRows || !NbColumns) {
60 matrix->p = NULL;
61 matrix->p_Init = NULL;
62 return matrix;
65 p = (cloog_int_t **)malloc(NbRows * sizeof(cloog_int_t *));
67 if (p == NULL) {
68 free (matrix);
69 return NULL;
72 q = (cloog_int_t *)malloc(NbRows * NbColumns * sizeof(cloog_int_t));
74 if (q == NULL) {
75 free (matrix);
76 free (p);
77 return NULL;
80 matrix->p = p;
81 matrix->p_Init = q;
83 for (i = 0; i < NbRows; i++) {
84 *p++ = q;
85 for (j = 0; j < NbColumns; j++) {
86 cloog_int_init(*(q+j));
87 cloog_int_set_si(*(q+j), 0);
89 q += NbColumns;
92 return matrix;
95 /**
96 * cloog_matrix_free:
97 * Free matrix.
99 void cloog_matrix_free(CloogMatrix * matrix)
101 int i;
102 cloog_int_t *p;
103 int size = matrix->NbRows * matrix->NbColumns;
105 p = matrix->p_Init;
107 for (i = 0; i < size; i++)
108 cloog_int_clear(*p++);
110 if (matrix) {
111 free(matrix->p_Init);
112 free(matrix->p);
113 free(matrix);
119 * Print the elements of CloogMatrix M to file, with each row prefixed
120 * by prefix and suffixed by suffix.
122 void cloog_matrix_print_structure(FILE *file, CloogMatrix *M,
123 const char *prefix, const char *suffix)
125 int i, j;
127 for (i = 0; i < M->NbRows; ++i) {
128 fprintf(file, "%s", prefix);
129 for (j = 0; j < M->NbColumns; ++j) {
130 cloog_int_print(file, M->p[i][j]);
131 fprintf(file, " ");
133 fprintf(file, "%s\n", suffix);
138 * cloog_matrix_print function:
139 * This function prints the content of a CloogMatrix structure (matrix) into a
140 * file (foo, possibly stdout).
142 void cloog_matrix_print(FILE* foo, CloogMatrix* m)
144 if (!m)
145 fprintf(foo, "(null)\n");
147 fprintf(foo, "%d %d\n", m->NbRows, m->NbColumns);
148 cloog_matrix_print_structure(foo, m, "", "");
149 fflush(foo);