From fb0e7a6b1c01001cc4ac07fd3c4a84915db7f13d Mon Sep 17 00:00:00 2001 From: Cedric Bastoul Date: Wed, 28 Mar 2012 17:47:06 +0200 Subject: [PATCH] Update library usage examples Slightly update the library usage example (in the documentation and in the ./example directory) and add a new (much probably more useful) example about building an input from isl sets and maps, and generating a clast/code corresponding to this input. --- doc/cloog.texi | 103 +++++++++++++++++++++++++++++++++++++++-- examples/README | 17 ++++--- examples/example/Makefile | 20 ++++---- examples/example/example-isl.c | 66 ++++++++++++++++++++++++++ 4 files changed, 185 insertions(+), 21 deletions(-) create mode 100644 examples/example/example-isl.c diff --git a/doc/cloog.texi b/doc/cloog.texi index 4a78d14..fc2b68b 100644 --- a/doc/cloog.texi +++ b/doc/cloog.texi @@ -2178,6 +2178,13 @@ to match CLooG's header version with the library's version. @node Example of Library Utilization @section Example of Library Utilization +@menu +* Basic Library Utilization:: +* Scanning isl Sets:: +@end menu + +@node Basic Library Utilization +@subsection Basic Library Utilization Here is a basic example showing how it is possible to use the CLooG library, assuming that a standard installation has been done. The following C program reads a CLooG input file on the standard input, @@ -2190,11 +2197,10 @@ CLooG distribution. # include # include -int main() -@{ +int main() @{ CloogState *state; CloogInput *input; - CloogOptions * options ; + CloogOptions *options ; struct clast_stmt *root; /* Setting options and reading program informations. */ @@ -2213,15 +2219,102 @@ int main() @} @end example -@noindent The compilation command could be: +@noindent The compilation (with default isl/GMP version installed) +command could be: @example -gcc example.c -lcloog -o example +gcc -DCLOOG_INT_GMP example.c -lcloog-isl -o example @end example @noindent A calling command with the input file test.cloog could be: @example more test.cloog | ./example @end example +@node Scanning isl Sets +@subsection Scanning isl Sets +Here is an isl-level example to prepare a convenient input, to generate the +Clast of the scanning code for this input, to pretty-print the code and to +de-allocate memory in a clean way. This example is provided in the +@code{example} directory of the CLooG distribution. + +@example +/* example-isl.c */ +#include +#include + +/* Input problem */ +int nb_parameters = 1; +char *parameter_name[] = @{"N"@}; +char *iterator_name[] = @{"i", "j"@}; +char *scattering_name[] = @{"t0", "t1", "t2"@}; +char *str_context = "[N] -> @{ : N > 0@}"; +char *str_domain1 = "[N] -> @{[i, j] : 0 <= i < N and 0 <= j < N@}"; +char *str_domain2 = "[N] -> @{[i, j] : 0 <= i < N and 0 <= j < N@}"; +char *str_scattering1 = "[N] -> @{[i, j] -> [0, i + j, j]@}"; +char *str_scattering2 = "[N] -> @{[i, j] -> [1, i, -j]@}"; + +int main() @{ + isl_ctx *ctx; + isl_set *set_context, *set1, *set2; + isl_map *map1, *map2; + CloogDomain *context, *domain1, *domain2; + CloogScattering *scattering1, *scattering2; + CloogUnionDomain *domains; + CloogInput *input; + CloogState *state; + CloogOptions *options; + struct clast_stmt *root; + + /* Build isl structures for context, sets and mapping */ + ctx = isl_ctx_alloc(); + set_context = isl_set_read_from_str(ctx, str_context); + set1 = isl_set_read_from_str(ctx, str_domain1); + set2 = isl_set_read_from_str(ctx, str_domain2); + map1 = isl_map_read_from_str(ctx, str_scattering1); + map2 = isl_map_read_from_str(ctx, str_scattering2); + + /* Translate them to CLooG context, domains and scattering */ + context = cloog_domain_from_isl_set(set_context); + domain1 = cloog_domain_from_isl_set(set1); + domain2 = cloog_domain_from_isl_set(set2); + scattering1 = cloog_scattering_from_isl_map(map1); + scattering2 = cloog_scattering_from_isl_map(map2); + + /* Prepare the list of domains to scan */ + domains = cloog_union_domain_alloc(nb_parameters); + cloog_union_domain_add_domain(domains,"S1",domain1,scattering1,NULL); + cloog_union_domain_add_domain(domains,"S2",domain2,scattering2,NULL); + cloog_union_domain_set_name(domains,CLOOG_PARAM,0,parameter_name[0]); + cloog_union_domain_set_name(domains,CLOOG_ITER, 0,iterator_name[0]); + cloog_union_domain_set_name(domains,CLOOG_ITER, 1,iterator_name[1]); + cloog_union_domain_set_name(domains,CLOOG_SCAT, 0,scattering_name[0]); + cloog_union_domain_set_name(domains,CLOOG_SCAT, 1,scattering_name[1]); + cloog_union_domain_set_name(domains,CLOOG_SCAT, 2,scattering_name[2]); + + /* Build the input, generate a scanning code AST and print the code */ + input = cloog_input_alloc(context, domains); + state = cloog_isl_state_malloc(ctx); + options = cloog_options_malloc(state); + root = cloog_clast_create_from_input(input, options); + clast_pprint(stdout, root, 0, options); + + /* Recycle allocated memory */ + cloog_clast_free(root); + cloog_options_free(options); + cloog_state_free(state); + isl_ctx_free(ctx); +@} +@end example + +@noindent The compilation (with default isl/GMP version installed) +command could be: +@example +gcc -DCLOOG_INT_GMP example-isl.c -lcloog-isl -o example-isl +@end example +@noindent A calling command could be: +@example +./example-isl +@end example + @c % ******************************** HACKING ********************************* @c @node Hacking diff --git a/examples/README b/examples/README index 1a9881e..03948fb 100644 --- a/examples/README +++ b/examples/README @@ -19,18 +19,23 @@ thus you need to set LD_LIBRARY_PATH thanks to one of the following command: 'setenv LD_LIBRARY_PATH /usr/local/lib' for tcsh-like shells, or 'export LD_LIBRARY_PATH=/usr/local/lib' for bash-like shells. -For any problem: . +For any problem: . # **-------------------------------------------------------------------** -# ** I. Example ** +# ** I. example ** # **-------------------------------------------------------------------**/ -This example program creates a simple CLooG-like loop generation program in a -few lines. The input problem has to be given on standard input. We can test it -for instance by typing 'more FILE.cloog | ./example' (or example.exe under -Cygwin). +This directory includes two basic examples: +- The first example program creates a simple CLooG-like loop generation +program in a few lines. The input problem has to be given on standard input. +Users can test it for instance by typing 'more FILE.cloog | ./example' (or +example.exe under Cygwin) after compilation. +- The second example shows how to build a CLooG input from isl sets, +generate the scanning code AST and pretty-printing it to the standard +output. Users can test it by typing './example-isl' (or example-isl.exe +under Cygwin) after compilation. # **-------------------------------------------------------------------** diff --git a/examples/example/Makefile b/examples/example/Makefile index 9b6150b..88c665e 100644 --- a/examples/example/Makefile +++ b/examples/example/Makefile @@ -1,21 +1,21 @@ -# Please enter here the locations for CloogLib include and libraries if they +# Please enter here the locations for the CLooG include and library if they # aren't the default values (/usr/lib and /usr/include). -CLOOG_INC = $(HOME)/progs/linux/include -CLOOG_LIB = $(HOME)/progs/linux/lib +CLOOG_INC = $(HOME)/usr/include +CLOOG_LIB = $(HOME)/usr/lib CC = gcc -LDLIBS= -lcloog -CFLAGS= -I $(CLOOG_INC) -L $(CLOOG_LIB) +LDLIBS= -lcloog-isl +CFLAGS= -DCLOOG_INT_GMP -I $(CLOOG_INC) -L $(CLOOG_LIB) - -example: example.c +example: example.c example-isl.c @echo " /*-----------------------------------------------*" - @echo " * Making example *" + @echo " * Making examples *" @echo " *-----------------------------------------------*/" $(CC) example.c -o example $(CFLAGS) $(LDLIBS) + $(CC) example-isl.c -o example-isl $(CFLAGS) $(LDLIBS) clean: @echo " /*-----------------------------------------------*" - @echo " * Cleaning example *" + @echo " * Cleaning examples *" @echo " *-----------------------------------------------*/" - -rm -f example example.exe core + -rm -f example example.exe example-isl example-isl.exe core diff --git a/examples/example/example-isl.c b/examples/example/example-isl.c new file mode 100644 index 0000000..6dede4c --- /dev/null +++ b/examples/example/example-isl.c @@ -0,0 +1,66 @@ +/* example-isl.c */ +#include +#include + +/* Input problem */ +int nb_parameters = 1; +char *parameter_name[] = {"N"}; +char *iterator_name[] = {"i", "j"}; +char *scattering_name[] = {"t0", "t1", "t2"}; +char *str_context = "[N] -> { : N > 0}"; +char *str_domain1 = "[N] -> {[i, j] : 0 <= i < N and 0 <= j < N}"; +char *str_domain2 = "[N] -> {[i, j] : 0 <= i < N and 0 <= j < N}"; +char *str_scattering1 = "[N] -> {[i, j] -> [0, i + j, j]}"; +char *str_scattering2 = "[N] -> {[i, j] -> [1, i, -j]}"; + +int main() { + isl_ctx *ctx; + isl_set *set_context, *set1, *set2; + isl_map *map1, *map2; + CloogDomain *context, *domain1, *domain2; + CloogScattering *scattering1, *scattering2; + CloogUnionDomain *domains; + CloogInput *input; + CloogState *state; + CloogOptions *options; + struct clast_stmt *root; + + /* Build isl structures for context, sets and mapping */ + ctx = isl_ctx_alloc(); + set_context = isl_set_read_from_str(ctx, str_context); + set1 = isl_set_read_from_str(ctx, str_domain1); + set2 = isl_set_read_from_str(ctx, str_domain2); + map1 = isl_map_read_from_str(ctx, str_scattering1); + map2 = isl_map_read_from_str(ctx, str_scattering2); + + /* Translate them to CLooG context, domains and scattering */ + context = cloog_domain_from_isl_set(set_context); + domain1 = cloog_domain_from_isl_set(set1); + domain2 = cloog_domain_from_isl_set(set2); + scattering1 = cloog_scattering_from_isl_map(map1); + scattering2 = cloog_scattering_from_isl_map(map2); + + /* Prepare the list of domains to scan */ + domains = cloog_union_domain_alloc(nb_parameters); + cloog_union_domain_add_domain(domains, "S1", domain1, scattering1, NULL); + cloog_union_domain_add_domain(domains, "S2", domain2, scattering2, NULL); + cloog_union_domain_set_name(domains, CLOOG_PARAM, 0, parameter_name[0]); + cloog_union_domain_set_name(domains, CLOOG_ITER, 0, iterator_name[0]); + cloog_union_domain_set_name(domains, CLOOG_ITER, 1, iterator_name[1]); + cloog_union_domain_set_name(domains, CLOOG_SCAT, 0, scattering_name[0]); + cloog_union_domain_set_name(domains, CLOOG_SCAT, 1, scattering_name[1]); + cloog_union_domain_set_name(domains, CLOOG_SCAT, 2, scattering_name[2]); + + /* Build the input, generate the AST of the scanning code and print it */ + input = cloog_input_alloc(context, domains); + state = cloog_isl_state_malloc(ctx); + options = cloog_options_malloc(state); + root = cloog_clast_create_from_input(input, options); + clast_pprint(stdout, root, 0, options); + + /* Recycle allocated memory */ + cloog_clast_free(root); + cloog_options_free(options); + cloog_state_free(state); + isl_ctx_free(ctx); +} -- 2.11.4.GIT