From 780f5365b12e7e3042982c4e33f7f4f1895d6334 Mon Sep 17 00:00:00 2001 From: Tobias Grosser Date: Wed, 23 Sep 2009 21:35:48 +0200 Subject: [PATCH] add backend independent functions for creating CloogDomains and CloogScatterings Start a generic library interface that reads from CloogMatrix to generate Domain and Scattering. This interface does not require the user of libcloog to use the api of any specific polytop library backend. Signed-off-by: Tobias Grosser Signed-off-by: Sven Verdoolaege --- doc/cloog.texi | 29 ++++++++++--- include/cloog/domain.h | 5 +++ source/isl/domain.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ source/polylib/domain.c | 43 +++++++++++++++++++ 4 files changed, 180 insertions(+), 5 deletions(-) diff --git a/doc/cloog.texi b/doc/cloog.texi index 82e6ff8..108179d 100644 --- a/doc/cloog.texi +++ b/doc/cloog.texi @@ -1566,6 +1566,8 @@ and @code{mpz_t} for multiple precision version). @group CloogDomain *cloog_domain_union_read(CloogState *state, FILE *input, int nb_parameters); +CloogDomain *cloog_domain_from_cloog_matrix(CloogState *state, + CloogMatrix *matrix, int nb_par); void cloog_domain_free(CloogDomain *domain); @end group @end example @@ -1573,8 +1575,15 @@ void cloog_domain_free(CloogDomain *domain); @noindent @code{CloogDomain} is an opaque type representing a polyhedral domain (a union of polyhedra). A @code{CloogDomain} can be read -from a file using @code{cloog_domain_union_read}. -The input format is that of @ref{Domain Representation}. +from a file using @code{cloog_domain_union_read} or +converted from a @code{CloogMatrix}. +The input format for @code{cloog_domain_union_read} +is that of @ref{Domain Representation}. +The function @code{cloog_domain_from_cloog_matrix} takes a @code{CloogState}, a +@code{CloogMatrix} and @code{int} as input and returns a pointer to a +@code{CloogDomain}. @code{matrix} describes the domain and @code{nb_par} is the +number of parameters in this domain. The input data structures are neiter +modified nor freed. The @code{CloogDomain} can be freed using @code{cloog_domain_free}. There are also some backend dependent functions for creating @code{CloogDomain}s. @@ -1617,6 +1626,8 @@ The function consumes a reference to the given @code{struct isl_set}. @group CloogScattering *cloog_domain_read_scattering(CloogDomain *domain, FILE *foo); +CloogScattering *cloog_scattering_from_cloog_matrix(CloogState *state, + CloogMatrix *matrix, int nb_scat, int nb_par); void cloog_scattering_free(CloogScattering *); @end group @end example @@ -1624,8 +1635,17 @@ void cloog_scattering_free(CloogScattering *); @noindent The @code{CloogScattering} type represents a scattering function. A @code{CloogScattering} for a given @code{CloogDomain} can be read -from a file using @code{cloog_scattering_read}. -It can be freed using @code{cloog_scattering_free}. +from a file using @code{cloog_scattering_read} or converted +from a @code{CloogMatrix} using @code{cloog_scattering_from_cloog_matrix}. +The function @code{cloog_scattering_from_cloog_matrix} takes a +@code{CloogState}, a @code{CloogMatrix} and two @code{int}s as input and +returns a +pointer to a @code{CloogScattering}. +@code{matrix} describes the scattering, while @code{nb_scat} and +@code{nb_par} are the number of scattering dimensions and +the number of parameters, respectively. The input data structures are +neiter modified nor freed. +A @code{CloogScattering} can be freed using @code{cloog_scattering_free}. There are also some backend dependent functions for creating @code{CloogScattering}s. @@ -1634,7 +1654,6 @@ There are also some backend dependent functions for creating * CloogScattering/isl:: @end menu - @node CloogScattering/PolyLib @subsubsection PolyLib diff --git a/include/cloog/domain.h b/include/cloog/domain.h index afccd76..cb04a6e 100644 --- a/include/cloog/domain.h +++ b/include/cloog/domain.h @@ -97,6 +97,11 @@ CloogDomain * cloog_domain_read_context(CloogState *state, FILE * foo); CloogDomain * cloog_domain_union_read(CloogState *state, FILE *foo, int nb_par); CloogScattering *cloog_domain_read_scattering(CloogDomain *domain, FILE *foo); +CloogDomain * cloog_domain_from_cloog_matrix(CloogState *state, + CloogMatrix *matrix, int nb_par); +CloogScattering * cloog_scattering_from_cloog_matrix(CloogState *state, + CloogMatrix *matrix, int nb_scat, int nb_par); + /****************************************************************************** * Processing functions * diff --git a/source/isl/domain.c b/source/isl/domain.c index 1433621..5ab6cfd 100644 --- a/source/isl/domain.c +++ b/source/isl/domain.c @@ -398,6 +398,114 @@ CloogScattering *cloog_domain_read_scattering(CloogDomain *domain, FILE *input) return cloog_scattering_from_isl_map(isl_map_from_basic_map(scat)); } +/****************************************************************************** + * CloogMatrix Reading function * + ******************************************************************************/ + +/** + * isl_constraint_read_from_matrix: + * Convert a single line of a matrix to a isl_constraint. + * Returns a pointer to the constraint if successful; NULL otherwise. + */ +static struct isl_constraint *isl_constraint_read_from_matrix( + struct isl_dim *dim, cloog_int_t *row) +{ + struct isl_constraint *constraint; + int j; + int nvariables = isl_dim_size(dim, isl_dim_set); + int nparam = isl_dim_size(dim, isl_dim_param); + + if (cloog_int_is_zero(row[0])) + constraint = isl_equality_alloc(dim); + else + constraint = isl_inequality_alloc(dim); + + for (j = 0; j < nvariables; ++j) + isl_constraint_set_coefficient(constraint, isl_dim_out, j, + row[1 + j]); + + for (j = 0; j < nparam; ++j) + isl_constraint_set_coefficient(constraint, isl_dim_param, j, + row[1 + nvariables + j]); + + isl_constraint_set_constant(constraint, row[1 + nvariables + nparam]); + + return constraint; +} + +/** + * isl_basic_set_read_from_matrix: + * Convert matrix to basic_set. The matrix contains nparam parameter columns. + * Returns a pointer to the basic_set if successful; NULL otherwise. + */ +static struct isl_basic_set *isl_basic_set_read_from_matrix(struct isl_ctx *ctx, + CloogMatrix* matrix, int nparam) +{ + struct isl_dim *dim; + struct isl_basic_set *bset; + int i; + unsigned nrows, ncolumns; + + nrows = matrix->NbRows; + ncolumns = matrix->NbColumns; + int nvariables = ncolumns - 2 - nparam; + + dim = isl_dim_set_alloc(ctx, nparam, nvariables); + + bset = isl_basic_set_universe(isl_dim_copy(dim)); + + for (i = 0; i < nrows; ++i) { + cloog_int_t *row = matrix->p[i]; + struct isl_constraint *constraint = + isl_constraint_read_from_matrix(isl_dim_copy(dim), row); + bset = isl_basic_set_add_constraint(bset, constraint); + } + + isl_dim_free(dim); + + return bset; +} + +/** + * cloog_domain_from_cloog_matrix: + * Create a CloogDomain containing the constraints described in matrix. + * nparam is the number of parameters contained in the domain. + * Returns a pointer to the CloogDomain if successful; NULL otherwise. + */ +CloogDomain *cloog_domain_from_cloog_matrix(CloogState *state, + CloogMatrix *matrix, int nparam) +{ + struct isl_ctx *ctx = state->backend->ctx; + struct isl_basic_set *bset; + + bset = isl_basic_set_read_from_matrix(ctx, matrix, nparam); + + return cloog_domain_from_isl_set(isl_set_from_basic_set(bset)); +} + +/** + * cloog_scattering_from_cloog_matrix: + * Create a CloogScattering containing the constraints described in matrix. + * nparam is the number of parameters contained in the domain. + * Returns a pointer to the CloogScattering if successful; NULL otherwise. + */ +CloogScattering *cloog_scattering_from_cloog_matrix(CloogState *state, + CloogMatrix *matrix, int nb_scat, int nb_par) +{ + struct isl_ctx *ctx = state->backend->ctx; + struct isl_basic_set *bset; + struct isl_basic_map *scat; + struct isl_dim *dims; + unsigned dim; + + bset = isl_basic_set_read_from_matrix(ctx, matrix, nb_par); + dim = isl_basic_set_n_dim(bset) - nb_scat; + dims = isl_dim_alloc(ctx, nb_par, nb_scat, dim); + + scat = isl_basic_map_from_basic_set(bset, dims); + return cloog_scattering_from_isl_map(isl_map_from_basic_map(scat)); +} + /****************************************************************************** * Processing functions * diff --git a/source/polylib/domain.c b/source/polylib/domain.c index 750eb16..6a14070 100644 --- a/source/polylib/domain.c +++ b/source/polylib/domain.c @@ -788,6 +788,49 @@ CloogScattering *cloog_domain_read_scattering(CloogDomain *domain, FILE *foo) /****************************************************************************** + * CloogMatrix Reading function * + ******************************************************************************/ + +/** + * Create a CloogDomain containing the constraints described in matrix. + * nb_par is the number of parameters contained in the domain. + * Returns a pointer to the CloogDomain if successful; NULL otherwise. + */ +CloogDomain *cloog_domain_from_cloog_matrix(CloogState *state, + CloogMatrix *matrix, int nb_par) +{ + int i, j; + Matrix *pmatrix; + Value **p; + + pmatrix = cloog_polylib_matrix_alloc(matrix->NbRows,matrix->NbColumns); + + if (!pmatrix) + return NULL; + + p = pmatrix->p; + + for (i = 0; i < pmatrix->NbRows; i++) + for (j = 0; j < pmatrix->NbColumns; j++) + cloog_int_set(p[i][j], matrix->p[i][j]); + + return cloog_domain_polylib_matrix2domain(state, pmatrix, nb_par); +} + +/** + * Create a CloogScattering containing the constraints described in matrix. + * nb_par is the number of parameters contained in the domain. + * Returns a pointer to the CloogScattering if successful; NULL otherwise. + */ +CloogScattering *cloog_scattering_from_cloog_matrix(CloogState *state, + CloogMatrix *matrix, int nb_scat, int nb_par) +{ + CloogDomain *domain = cloog_domain_from_cloog_matrix(state, matrix, nb_par); + return (CloogScattering *)domain; +} + + +/****************************************************************************** * Processing functions * ******************************************************************************/ -- 2.11.4.GIT