update isl for improved piplib efficiency
[cloog/uuh.git] / doc / cloog.texi
blobe882949a4070541973c4791ae260faf3fc6863fc
1 \input texinfo
2 @c %
3 @c %  /**-----------------------------------------------------------------**
4 @c %   **                              CLooG                              **
5 @c %   **-----------------------------------------------------------------**
6 @c %   **                            cloog.texi                           **
7 @c %   **-----------------------------------------------------------------**
8 @c %   **                   First version: july 6th 2002                  **
9 @c %   **-----------------------------------------------------------------**/
10 @c %
11 @c % release 1.0: september 17th 2002
12 @c % release 1.1: december   5th 2002
13 @c % release 1.2: april     22th 2003
14 @c % release 2.0: november  21th 2005 (and now in texinfo instead of LaTeX)
15 @c % release 2.1: october   15th 2007
16 @c %
17 @c %/**************************************************************************
18 @c % *               CLooG : the Chunky Loop Generator (experimental)         *
19 @c % **************************************************************************/
20 @c %/* CAUTION: the english used is probably the worst you ever read, please
21 @c % *          feel free to correct and improve it !
22 @c % */
24 @c %\textit{"I found the ultimate transformation functions, optimization for
25 @c %static control programs is now a closed problem, I have \textnormal{just}
26 @c %to generate the target code !"} 
30 @c % /*************************************************************************
31 @c %  *                              PART I: HEADER                           *
32 @c %  *************************************************************************/
33 @c %**start of header
34 @setfilename cloog.info
35 @settitle CLooG - a loop generator for scanning polyhedra
37 @set EDITION 2.1
38 @include gitversion.texi
39 @set UPDATED October 15th 2007
40 @setchapternewpage odd
42 @c %**end of header
44 @c % /*************************************************************************
45 @c %  *                 PART II: SUMMARY DESCRIPTION AND COPYRIGHT            *
46 @c %  *************************************************************************/
48 @copying
49 This manual is for CLooG version @value{VERSION}, a software
50 which generates loops for scanning Z-polyhedra. That is, CLooG produces a
51 code visiting each integral point of a union of parametrized
52 polyhedra. CLooG is designed to avoid control overhead and to produce a very
53 efficient code.
55 It would be quite kind to refer the following paper in any publication that
56 results from the use of the CLooG software or its library:
58 @example
59 @@InProceedings@{Bas04,
60 @ @ author =@ @ @ @ @{C. Bastoul@},
61 @ @ title =@ @ @ @ @ @{Code Generation in the Polyhedral Model
62 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ Is Easier Than You Think@},
63 @ @ booktitle = @{PACT'13 IEEE International Conference on
64 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ Parallel Architecture and Compilation Techniques@},
65 @ @ year =@ @ @ @ @ @ 2004,
66 @ @ pages =@ @ @ @ @ @{7--16@},
67 @ @ month =@ @ @ @ @ @{september@},
68 @ @ address =@ @ @ @{Juan-les-Pins@}
70 @end example
72 Copyright @copyright{} 2002-2005 C@'edric Bastoul.
74 @c quotation
75 Permission is granted to copy, distribute and/or modify this document under
76 the terms of the GNU Free Documentation License, Version 1.2 
77 published by the Free Software Foundation. To receive a copy of the
78 GNU Free Documentation License, write to the Free Software Foundation, Inc.,
79 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
80 @c end quotation
81 @end copying
83 @c % /*************************************************************************
84 @c %  *                 PART III: TITLEPAGE, CONTENTS, COPYRIGHT              *
85 @c %  *************************************************************************/
86 @titlepage
87 @title CLooG
88 @subtitle A Loop Generator For Scanning Polyhedra
89 @subtitle Edition @value{EDITION}, for CLooG @value{VERSION}
90 @subtitle @value{UPDATED}
91 @author C@'edric Bastoul
92      
93 @c The following two commands start the copyright page.
94 @page
95 @noindent (September 2001)
96 @table @code
97 @item C@'edric Bastoul
98 SCHEDULES GENERATE !!! I just need to apply them now, where can I find
99 a good code generator ?!
100      
101 @item Paul Feautrier
102 Hmmm. I fear that if you want something powerful enough, you'll have to
103 write it yourself !
104 @end table
106 @vskip 0pt plus 1filll
107 @insertcopying
108 @end titlepage
109      
110 @c Output the table of contents at the beginning.
111 @contents
113 @c % /*************************************************************************
114 @c %  *                     PART IV: TOP NODE AND MASTER MENU                 *
115 @c %  *************************************************************************/
116 @ifnottex
117 @node Top
118 @top CLooG
119      
120 @insertcopying
121 @end ifnottex
123 @menu
124 * Introduction::
125 * CLooG Software::
126 * CLooG Library::
127 @c * Hacking::
128 * Installing::
129 * Documentation::
130 * References::
131 @end menu
135 @c % /*************************************************************************
136 @c %  *                       PART V: BODY OF THE DOCUMENT                    *
137 @c %  *************************************************************************/
139 @c %  ****************************** INTRODUCTION ******************************
140 @node Introduction
141 @chapter Introduction
142 CLooG is a free software and library generating loops for scanning Z-polyhedra.
143 That is, it finds a code (e.g. in C, FORTRAN...) that reaches each integral
144 point of one or more parameterized polyhedra. CLooG has been originally
145 written to solve the code generation problem for optimizing compilers based on
146 the polytope model. Nevertheless it is used now in various area, e.g., to build
147 control automata for high-level synthesis or to find the best polynomial
148 approximation of a function. CLooG may help in any situation where scanning
149 polyhedra matters. It uses the best state-of-the-art code generation
150 algorithm known as the Quiller@'e et al. algorithm (@pxref{Qui00})
151 with our own improvements and extensions (@pxref{Bas04}).
152 The user has full control on generated code quality.
153 On one hand, generated code size has to be tuned for sake of
154 readability or instruction cache use. On the other hand, we must ensure that
155 a bad control management does not hamper performance of the generated code,
156 for instance by producing redundant guards or complex loop bounds.
157 CLooG is specially designed to avoid control overhead and to produce a very
158 efficient code.
160 CLooG stands for @emph{Chunky Loop Generator}: it is a part of the Chunky
161 project, a research tool for data locality improvement (@pxref{Bas03a}).
162 It is designed
163 also to be the back-end of automatic parallelizers like LooPo (@pxref{Gri04}).
164 Thus it is very
165 compilable code oriented and provides powerful program transformation
166 facilities. Mainly, it allows the user to specify very general schedules where, 
167 e.g., unimodularity or invertibility of the transformation doesn't matter.
169 The current version is still under
170 evaluation, and there is no guarantee that the upward compatibility
171 will be respected (but the previous API has been stable for two years,
172 we hope this one will be as successful -and we believe it-).
173 A lot of reports are necessary to freeze the library
174 API and the input file shape. Most API changes from 0.12.x to 0.14.x
175 have been requested by the users themselves.
176 Thus you are very welcome and encouraged
177 to post reports on bugs, wishes, critics, comments, suggestions or
178 successful experiences in the forum of @code{http://www.CLooG.org}
179 or to send them to cedric.bastoul@@inria.fr directly.
181 @menu
182 * Basics::
183 * Scattering::
184 @end menu
186 @node Basics
187 @section Basically, what's the point ?
188 If you want to use CLooG, this is because you want to scan or to find
189 something inside the integral points of a set of polyhedra. There are many
190 reasons for that. Maybe you need the generated code itself because it
191 actually implements a very smart program transformation you found.
192 Maybe you want to use the generated code
193 because you know that the solution of your problem belongs to the integral
194 points of those damned polyhedra and you don't know which one. Maybe you just
195 want to know if a polyhedron has integral points depending on some parameters,
196 which is the lexicographic minimum, maximum, the third on the basis of the
197 left etc. Probably you have your own reasons to use CLooG.
199 Let us illustrate a basic use of CLooG. Suppose we have a set of affine
200 constraints that describes a part of a whatever-dimensional space,
201 called a @strong{domain}, and we
202 want to scan it. Let us consider for instance the following set of constraints
203 where @samp{i}
204 and @samp{j} are the unknown (the two dimensions of the space) and
205 @samp{m} and @samp{n} are the parameters (some symbolic constants):
206 @example
207 @group
208 2<=i<=n
209 2<=j<=m
210 j<=n+2-i
211 @end group
212 @end example
213 Let us also consider that we have a partial knowledge of the parameter values,
214 called the @strong{context}, expressed as affine constraints as well,
215 for instance:
216 @example
217 @group
218 m>=2
219 n>=2
220 @end group
221 @end example
222 Note that using parameters is optional, if you are not comfortable with
223 parameter manipulation, just replace them with any scalar value that fits
224 @code{m>=2} and @code{n>=2}.
225 A graphical representation of this part of the 2-dimensional space, where
226 the integral points are represented using heavy dots would be for instance:
228 @image{images/basic,6cm}
230 The affine constraints of both the domain and the context are what we will
231 provide to CLooG as input (in a particular shape that will be described later).
232 The output of CLooG is a pseudo-code to scan the integral points of the
233 input domain according to the context:
234 @example
235 @group
236 for (i=2;i<=n;i++) @{
237   for (j=2;j<=min(m,-i+n+2);j++) @{    
238     S1(i,j) ;
239   @}
241 @end group
242 @end example
243 If you felt such a basic example is yet interesting, there is a good chance
244 that CLooG is appropriate for you. CLooG can do much more: scanning several
245 polyhedra or unions of polyhedra at the same time, applying general affine
246 transformations to the polyhedra, generate compilable code etc. Welcome
247 to the CLooG's user's guide !
249 @node Scattering
250 @section Defining a Scanning Order: Scattering Functions
251 In CLooG, domains only define the set of integral points to scan and their
252 coordinates. In particular, CLooG is free to choose the scanning order for
253 generating the most efficient code. This means, for optimizing/parallelizing
254 compiler people, that CLooG doesn't make any speculation on dependences on and
255 between statements (by the way, it's not its job !).
256 For instance, if an user give to
257 CLooG only two domains @code{S1:1<=i<=n}, @code{S2:1<=i<=n} and the context
258 @code{n>=1}, the following pseudo-codes are considered to be equivalent:
260 @example
261 @group
262 /* A convenient target pseudo-code. */
263 for (i=1;i<=N;i++) @{
264  S1(i) ;
266 for (i=1;i<=N;i++) @{
267  S2(i) ;
269 @end group
270 @end example
272 @example
273 @group
274 /* Another convenient target pseudo-code. */
275 for (i=1;i<=N;i++) @{
276  S1(i) ;
277  S2(i) ;
279 @end group
280 @end example
282 The default behaviour
283 of CLooG is to generate the second one, since it is optimized in control. 
284 It is right if there are no data dependences
285 between @code{S1} and @code{S2}, but wrong otherwise. 
287 Thus it is often useful to force scanning to respect a given order. This can be
288 done in CLooG by using @strong{scattering functions}. Scattering is a
289 shortcut for scheduling, allocation, chunking functions and the like we can
290 find in the restructuring compilation litterature. There are a lot of reasons
291 to scatter the integral points of the domains (i.e. the statement instances
292 of a program, for compilation people), parallelization or optimization are good
293 examples. For instance, if the user wants for any reason to set some
294 precedence constraints between the statements of our example above
295 in order to force the generation of the
296 first code, he can do it easily by setting (for example) the following
297 scheduling functions:
299 @tex
300 $$\theta _{S1}(i) =  (1)$$
301 $$\theta _{S2}(j) =  (2)$$
302 @end tex
304 @ifnottex
305 @example
306 @group
307 T_S1(i) = (1)
308 T_S2(j) = (2)
309 @end group
310 @end example
311 @end ifnottex
313 This scattering means that each integral point of the domain @code{S1}
314 is scanned at logical date @code{1} while each integral point of the domain
315 @code{S2} is scanned at logical date @code{2}. As a result, the whole
316 domain @code{S1} is scanned before domain @code{S2} and the first code in our
317 example is generated.
319 The user can set every kind of affine scanning order thanks to the
320 scattering functions. Each domain has its own scattering function and
321 each scattering function may be multi-dimensional. A multi-dimentional logical
322 date may be seen as classical date (year,month,day,hour,minute,etc.) where
323 the first dimensions are the most significant. Each scattering dimension
324 may depend linearly on the original dimensions (e.g., @code{i}), the
325 parameters (e.g., @code{n}) ans scalars (e.g., @code{2}).
327 A very useful example of multi-dimensional scattering functions is, for
328 compilation people, the scheduling of the original program.
329 The basic data to use for code generation are statement iteration domains.
330 As we saw, these data are not sufficient to rebuild the original
331 program (what is the ordering between instances of different statements ?).
332 The missing data can be put in the scattering functions as the original
333 scheduling. The method to compute it is quite simple (@pxref{Fea92}). The idea is to
334 build an abstract syntax tree of the program and to read the scheduling for
335 each statement. For instance, let us consider the following implementation of
336 a Cholesky factorization:
338 @example
339 @group
340 /* A Cholesky factorization kernel. */
341 for (i=1;i<=N;i++) @{
342   for (j=1;j<=i-1;j++) @{
343     a[i][i] -= a[i][j] ;           /* S1 */
344   @}
345   a[i][i] = sqrt(a[i][i]) ;        /* S2 */
346   for (j=i+1;j<=N;j++) @{
347     for (k=1;k<=i-1;k++) @{
348       a[j][i] -= a[j][k]*a[i][k] ; /* S3 */
349     @}
350     a[j][i] /= a[i][i] ;           /* S4 */
351     @}
352   @}
354 @end group
355 @end example
357 The corresponding abstract syntax tree is given in the following figure.
358 It directly gives the scattering functions (schedules) for all the
359 statements of the program.
361 @image{images/tree,6cm}
363 @tex
365 \hbox{$ \cases{ \theta _{S1}(i,j)^T    &$=  (0,i,0,j,0)^T$\cr
366                 \theta _{S2}(i)        &$=  (0,i,1)^T$\cr
367                 \theta _{S3}(i,j,k)^T  &$=  (0,i,2,j,0,k,0)^T$\cr
368                 \theta _{S4}(i,j)^T    &$=  (0,i,2,j,1)^T$}$}
370 @end tex
372 @ifnottex
373 @example
374 @group
375 T_S1(i,j)^T   = (0,i,0,j,0)^T
376 T_S2(i)       = (0,i,1)^T
377 T_S3(i,j,k)^T = (0,i,2,j,0,k,0)^T
378 T_S4(i,j)^T   = (0,i,2,j,1)^T
379 @end group
380 @end example
381 @end ifnottex
383 These schedules depend on the iterators and give for each instance of each
384 statement a unique execution date. Using such scattering functions allow
385 CLooG to re-generate the input code. 
391 @c %  ***********************Using the CLooG Software **************************
392 @node CLooG Software
393 @chapter Using the CLooG Software
396 @menu
397 * A First Example::
398 * Writing The Input File::
399 * Calling CLooG::
400 * CLooG Options::
401 * Full Example::
402 @end menu
404 @c %/*************************************************************************
405 @c % *                              A FIRST EXAMPLE                          *
406 @c % *************************************************************************/
407 @node A First Example
408 @section A First Example
409 CLooG takes as input a file that must be written accordingly to a grammar
410 described in depth in a further section (@pxref{Writing The Input File}). 
411 Moreover it supports many options to tune the target code presentation or
412 quality as discussed in a dedicated section (@pxref{Calling CLooG}).
413 However, a basic use
414 of CLooG is not very complex and we present in this section how to generate the
415 code corresponding to a basic example discussed earlier (@pxref{Basics}).
417 The problem is to find the code that scans a 2-dimensional polyhedron
418 where @samp{i} and @samp{j} are the unknown (the two dimensions of the space)
419 and @samp{m} and @samp{n} are the parameters (the symbolic constants),
420 defined by the following set of constraints:
421 @example
422 @group
423 2<=i<=n
424 2<=j<=m
425 j<=n+2-i
426 @end group
427 @end example
428 @noindent We also consider a partial knowledge of the parameter values,
429 expressed thanks to the following affine constraints:
430 @example
431 @group
432 m>=2
433 n>=2
434 @end group
435 @end example
437 An input file that corresponds to this problem, and asks for a generated
438 code in C, may be the following. Note that we do not describe here precisely
439 the structure and the components of this file (@pxref{Writing The Input File}
440  for such information, if you feel it necessary):
442 @example
443 # ---------------------- CONTEXT ----------------------
444 c # language is C
446 # Context (constraints on two parameters)
447 2 4                   # 2 lines and 4 columns
448 # eq/in m  n  1         eq/in: 1 for inequality >=0, 0 for equality =0
449     1   1  0 -2       # 1*m + 0*n -2*1 >= 0, i.e. m>=2
450     1   0  1 -2       # 0*m + 1*n -2*1 >= 0, i.e. n>=2
452 1 # We want to set manually the parameter names
453 m n                   # parameter names
455 # --------------------- STATEMENTS --------------------
456 1 # Number of statements
458 1 # First statement: one domain
459 # First domain
460 5 6                   # 5 lines and 6 columns
461 # eq/in i  j  m  n  1 
462     1   1  0  0  0 -2 # i >= 2
463     1  -1  0  0  1  0 # i <= n
464     1   0  1  0  0 -2 # j >= 2
465     1   0 -1  1  0  0 # j <= m
466     1  -1 -1  0  1  2 # n+2-i>=j
467 0  0  0               # for future options
469 1 # We want to set manually the iterator names
470 i j                   # iterator names
472 # --------------------- SCATTERING --------------------
473 0 # No scattering functions
474 @end example
476 This file may be called @samp{basic.cloog}
477 (this example is provided in the CLooG distribution as
478 @code{test/manual_basic.cloog}) and we can ask CLooG to process it
479 and to generate the code by a simple calling to CLooG with this file as input:
480 @samp{cloog basic.cloog}. By default, CLooG will print the generated code in
481 the standard output:
483 @example
484 @group
485 /* Generated by CLooG v@value{VERSION} in 0.00s. */
486 for (i=2;i<=n;i++) @{
487   for (j=2;j<=min(m,-i+n+2);j++) @{    
488     S1(i,j) ;
489   @}
491 @end group
492 @end example
494 @c %/*************************************************************************
495 @c % *                                Input file                             *
496 @c % *************************************************************************/
497 @node Writing The Input File
498 @section Writing The Input File
499 The input text file contains a problem description, i.e. the context,
500 the domains and the scattering functions.
501 Because CLooG is very 'compilable code generation oriented', we can associate
502 some additional informations to each domain. We call this association a
503 @emph{statement}. The set of all informations is 
504 called a @emph{program}. The input file respects the grammar below
505 (terminals are preceeded by "_"):
507 @example
508 File             ::= Program
509 Program          ::= Context Statements Scattering
510 Context          ::= Language      Domain         Naming
511 Statements       ::= Nb_statements Statement_list Naming
512 Scattering       ::= Nb_functions  Domain_list    Naming
513 Naming           ::= Option Name_list
514 Name_list        ::= _String   Name_list      | (void)
515 Statement_list   ::= Statement Statement_list | (void)
516 Domain_list      ::= _Domain   Domain_list    | (void)
517 Statement        ::= Iteration_domain 0 0 0
518 Iteration_domain ::= Domain_union
519 Domain_union     ::= Nb_domains Domain_list
520 Option           ::= 0 | 1
521 Language         ::= c | f
522 Nb_statements    ::= _Integer
523 Nb_domains       ::= _Integer
524 Nb_functions     ::= _Integer
525 @end example
527 @itemize @bullet
528 @item  @samp{Context} represents the informations that are
529        shared by all the statements. It consists on
530        the language used (which can be @samp{c} for C or @samp{f} for FORTRAN 90)
531        and the global constraints on parameters.
532        These constraints are essential
533        since they give to CLooG the number of parameters. If there is no
534        parameter or no constraints on parameters, just give a constraint
535        always satisfied like @math{1 \geq 0}. @samp{Naming} sets the parameter
536        names.
537        If the naming option @samp{Option} is 1, parameter names will be read
538        on the next line. There must be exactly as many names as parameters.
539        If the naming option @samp{Option} is 0, parameter names are
540        automatically generated. The name of the first parameter will
541        be @samp{M}, and the name of the @math{(n+1)^{th}} parameter directly
542        follows the name of the @math{n^{th}} parameter in ASCII code.
543        It is the user responsibility to ensure that parameter names,
544        iterators and scattering dimension names are different. 
545 @item  @samp{Statements} represents the informations on the statements.
546        @samp{Nb_statements} is the number of statements in the program, 
547        i.e. the number of @samp{Statement} items in the @samp{Statement_list}.
548        @samp{Statement} represents the informations on a given statement.
549        To each statement is associated a domain
550        (the statement iteration domain: @samp{Iteration_domain}) and three
551        zeroes that represents future options.
552        @samp{Naming} sets the iterator names. If the naming option
553        @samp{Option} is 1, the iterator names
554        will be read on the next line. There must be exactly as many names as
555        nesting level in the deepest iteration domain. If the naming option
556        @samp{Option} is 0, iterator names are automatically generated.
557        The iterator name of the outermost loop will be @samp{i}, and the
558        iterator name of the loop at level @math{n+1} directly follows the 
559        iterator name of the loop at level @math{n} in ASCII code. 
560 @item  @samp{Scattering} represents the informations on scattering functions.
561        @samp{Nb_functions} is the number of functions (it must be
562        equal to the number of statements or 0 if there is no scattering
563        function). The function themselves are represented through
564        @samp{Domain_list}.
565        @samp{Naming} sets the scattering dimension names. If the naming option
566        @samp{Option} is 1, the scattering dimension names will be read on the
567        next line.
568        There must be exactly as many names as scattering dimensions. If the
569        naming option @samp{Option} is 0, scattering dimension names are automatically
570        generated. The name of the @math{n^{th}} scattering dimention
571        will be @samp{cn}.
572 @end itemize
574 @menu
575 * Domain Representation::
576 * Scattering Representation::
577 @end menu
579 @node Domain Representation
580 @subsection Domain Representation
581 As shown by the grammar, the input file describes the various informations
582 thanks to characters, integers and domains. Each domain is defined by a set of
583 constraints in the PolyLib format (@pxref{Wil93}). They have the
584 following syntax:
585 @enumerate
586 @item some optional comment lines beginning with @samp{#},
587 @item the row and column numbers, possibly followed by comments,
588 @item the constraint rows, each row corresponds to a constraint the
589       domain have to satisfy. Each row must be on a single line and is possibly
590       followed by comments. The constraint is an equality @math{p(x) = 0} if the
591       first element is 0, an inequality  @math{p(x) \geq 0} if the first element
592       is 1. The next elements are the unknown coefficients, followed by
593       the parameter coefficients. The last element is the constant factor.
594 @end enumerate
595 For instance, assuming that @samp{i}, @samp{j} and @samp{k} are iterators and
596 @samp{m} and @samp{n} are parameters, the domain defined by the following
597 constraints :
599 @tex
601 \hbox{$ \cases{ -i     + m &$\geq 0$\cr
602                     -j + n &$\geq 0$\cr
603                  i + j - k &$\geq 0$}$}
605 @end tex
607 @ifnottex
608 @example
609 @group
610    -i + m >= 0
611    -j + n >= 0
612 i + j - k >= 0
613 @end group
614 @end example
615 @end ifnottex
617 @noindent can be written in the input file as follows :
619 @example
620 @group
621 # This is the domain
622 3 7                      # 3 lines and 7 columns
623 # eq/in i  j  k  m  n  1 
624     1  -1  0  0  1  0  0 #    -i + m >= 0
625     1   0 -1  0  0  1  0 #    -j + n >= 0
626     1   1  1 -1  0  0  0 # i + j - k >= 0
627 @end group
628 @end example
630 Each iteration domain @samp{Iteration_domain} of a given statement
631 is a union of polyhedra
632 @samp{Domain_union}. A union is defined by its number of elements
633 @samp{Nb_domains} and the elements themselves @samp{Domain_list}.
634 For instance, let us consider the following pseudo-code:
636 @example
637 @group
638 for (i=1;i<=n;i++) @{
639   if ((i >= m) || (i <= 2*m))
640     S1 ;
641   for (j=i+1;j<=m;j++)
642     S2 ;
643 @} 
644 @end group
645 @end example
647 @noindent The iteration domain of @samp{S1} can be divided into two
648 polyhedra and written in the input file as follows:
650 @example
651 @group
652 2 # Number of polyhedra in the union
653 # First domain
654 3 5                # 3 lines and 5 columns
655 # eq/in i  m  n  1 
656     1   1  0  0 -1 #  i >= 1
657     1  -1  0  1  0 #  i <= n
658     1   1 -1  0  0 #  i >= m
659 # Second domain
660 3 5                # 3 lines and 5 columns
661 # eq/in i  m  n  1 
662     1   1  0  0 -1 #  i >= 1
663     1  -1  0  1  0 #  i <= n
664     1  -1  2  0  0 #  i <= 2*m
665 @end group
666 @end example
668 @node Scattering Representation
669 @subsection Scattering Function Representation
670 Scattering functions are depicted in the input file thanks a representation
671 very close to the domain one.
672 An integer gives the number of functions @samp{Nb_functions} and each function
673 is represented by a domain. Each line of the domain corresponds to an equality
674 defining a dimension of the function. Note that at present
675 (CLooG @value{VERSION})
676 @strong{all functions must have the same scattering dimension number}. If a
677 user wants to set scattering functions with different dimensionality, he has
678 to complete the smaller one with zeroes to reach the maximum dimensionality.
679 For instance, let us consider the following code and
680 scheduling functions:
682 @example
683 @group
684 for (i=1;i<=n;i++) @{
685   if ((i >= m) || (i <= 2*m))
686     S1 ;
687   for (j=i+1;j<=m;j++)
688     S2 ;
689 @} 
690 @end group
691 @end example
693 @tex
695 \hbox{$ \cases{ \theta _{S1}(i)      &$=  (i,0)^T$\cr
696                 \theta _{S2}(i,j)^T  &$=  (n,i+j)^T$}$}
698 @end tex
700 @ifnottex
701 @example
702 @group
703 T_S1(i)     = (i,0)^T
704 T_S2(i,j)^T = (n,i+j)^T
705 @end group
706 @end example
707 @end ifnottex
710 @noindent This scheduling can be written in the input file as follows:
712 @example
713 @group
714 2 # Number of scattering functions
715 # First function
716 2 7                          # 2 lines and 7 columns
717 # eq/in c1 c2  i  m  n  1 
718     0    1  0 -1  0  0  0    #  c1 = i
719     0    0  1  0  0  0  0    #  c2 = 0
720 # Second function
721 2 8                          # 2 lines and 8 columns
722 # eq/in c1 c2  i  j  m  n  1 
723     0    1  0  0  0  0 -1  0 #  c1 = n
724     0    0  1 -1 -1  0  0  0 #  c2 = i+j
725 @end group
726 @end example
727 The complete input file for the user who wants to generate the code for this
728 example with the preceding scheduling would be
729 (this file is provided in the CLooG distribution
730 as @code{test/manual_scattering.cloog}:
732 @example
733 # ---------------------- CONTEXT ----------------------
734 c # language is C
736 # Context (no constraints on two parameters)
737 1 4                   # 1 lines and 4 columns
738 # eq/in m  n  1
739     1   0  0  0       # 0 >= 0, always true
741 1 # We want to set manually the parameter names
742 m n                   # parameter names
744 # --------------------- STATEMENTS --------------------
745 2 # Number of statements
747 2 # First statement: two domains
748 # First domain
749 3 5                   # 3 lines and 5 columns
750 # eq/in i  m  n  1
751     1   1  0  0 -1    # i >= 1
752     1  -1  0  1  0    # i <= n
753     1   1 -1  0  0    # i >= m
754 # Second domain
755 3 5                   # 3 lines and 5 columns
756 # eq/in i  m  n  1 
757     1   1  0  0 -1    # i >= 1
758     1  -1  0  1  0    # i <= n
759     1  -1  2  0  0    # i <= 2*m
760 0  0  0               # for future options
762 1 # Second statement: one domain
763 4 6                   # 4 lines and 6 columns
764 # eq/in i  j  m  n  1 
765     1   1  0  0  0 -1 # i >= 1
766     1  -1  0  0  1  0 # i <= n
767     1  -1  1  0  0 -1 # j >= i+1
768     1   0 -1  1  0  0 # j <= m
769 0  0  0               # for future options
771 1 # We want to set manually the iterator names
772 i j                   # iterator names
774 # --------------------- SCATTERING --------------------
775 2 # Scattering functions
776 # First function
777 2 7                   # 2 lines and 7 columns
778 # eq/in p1 p2  i  m  n  1 
779     0    1  0 -1  0  0  0    # p1 = i
780     0    0  1  0  0  0  0    # p2 = 0
781 # Second function
782 2 8                   # 2 lines and 8 columns
783 # eq/in p1 p2  i  j  m  n  1 
784     0    1  0  0  0  0 -1  0 # p1 = n
785     0    0  1 -1 -1  0  0  0 # p2 = i+j
787 1 # We want to set manually the scattering dimension names
788 p1 p2                 # scattering dimension names
789 @end example
792 @c %/*************************************************************************
793 @c % *                             Calling CLooG                             *
794 @c % *************************************************************************/
795 @node Calling CLooG
796 @section Calling CLooG
797 CLooG is called by the following command:
798 @example
799        cloog [ options | file ]
800 @end example
801 The default behavior of CLooG is to read the input informations from a file and
802 to print the generated code or pseudo-code on the standard output.
803 CLooG's behavior and the output code shape is under the user control thanks
804 to many options which are detailed a further section (@pxref{CLooG Options}).
805 @code{file} is the input file. @code{stdin} is a special value: when used,
806 input is standard input. For instance, we can call CLooG to treat the
807 input file @code{basic.cloog} with default options by typing:
808 @code{cloog basic.cloog} or @code{more basic.cloog | cloog stdin}.
810 @c %/*************************************************************************
811 @c % *                             CLooG Options                             *
812 @c % *************************************************************************/
813 @node CLooG Options
814 @section CLooG Options
816 @menu
817 * Last Depth to Optimize Control::
818 * First Depth to Optimize Control::
819 * Simplify Convex Hull::
820 * Once Time Loop Elimination::
821 * Equality Spreading::
822 * Constant Spreading::
823 * First Level for Spreading::
824 * C PreProcessor Friendly::
825 * Statement Block::
826 * Loop Strides::
827 * Compilable Code::
828 * Output::
829 * Help::
830 * Version ::
831 * Quiet ::
832 @end menu
834 @node Last Depth to Optimize Control
835 @subsection Last Depth to Optimize Control @code{-l <depth>}
837 @code{-l <depth>}: this option sets the last loop depth to be optimized in
838 control. The higher this depth, the less control overhead.
839 For instance, with some input file, a user can generate
840 different pseudo-codes with different @code{depth} values as shown below.
841 @example
842 @group
843 /* Generated using a given input file and @strong{option -l 1} */
844 for (i=0;i<=M;i++) @{
845   S1 ;
846   for (j=0;j<=N;j++) @{
847     S2 ;
848   @}
849   for (j=0;j<=N;j++) @{
850     S3 ;
851   @}
852   S4 ;
854 @end group
855 @end example
856 @example
857 @group
858 /* Generated using the same input file but @strong{option -l 2} */
859 for (i=0;i<=M;i++) @{
860   S1 ;
861   for (j=0;j<=N;j++) @{
862     S2 ;
863     S3 ;
864   @}
865   S4 ;
867 @end group
868 @end example
869      In this example we can see that this option can change the operation
870      execution order between statements. Let us remind that CLooG does not
871      make any speculation on dependences between statements
872      (@pxref{Scattering}). Thus if nothing (i.e. scattering functions)
873      forbids this, CLooG considers the above codes to be equivalent.
874      If there is no scattering functions, the minimum value for @code{depth}
875      is 1 (in the case of 0, the user doesn't really need a loop generator !),
876      and the number of scattering dimensions otherwise (CLooG will warn the
877      user if he doesn't respect such constraint).
878      The maximum value for depth is -1 (infinity).
879      Default value is infinity.
881 @node First Depth to Optimize Control
882 @subsection First Depth to Optimize Control @code{-f <depth>}
884      @code{-f <depth>}: this option sets the first loop depth to be optimized
885      in control. The lower this depth, the less control overhead (and the longer
886      the generated code). For instance, with some input file, a user
887      can generate different pseudo-codes with different @code{depth} values
888      as shown below.
889      The minimum value for @code{depth} is 1, and the
890      maximum value is -1 (infinity).
891      Default value is 1.
892 @example
893 @group
894 /* Generated using a given input file and @strong{option -f 3} */
895 for (i=1;i<=N;i++) @{
896   for (j=1;j<=M;j++) @{
897     S1 ;
898     if (j >= 10) @{
899       S2 ;
900     @}
901   @}
903 @end group
904 @end example
905 @example
906 @group
907 /* Generated using the same input file but @strong{option -f 2} */
908 for (i=1;i<=N;i++) @{
909   for (j=1;j<=9;j++) @{
910     S1 ;
911   @}
912   for (j=10;j<=M;j++) @{
913     S1 ;
914     S2 ;
915   @}
917 @end group
918 @end example
920 @node Simplify Convex Hull
921 @subsection  Simplify Convex Hull @code{-sh <boolean>}
923      @code{-sh <boolean>}: this option enables (@code{boolean=1})
924      or forbids (@code{boolean=0}) a simplification step
925      that may simplify some constraints.
926      This option works only for generated code without
927      code duplication (it means, you have to tune @code{-f} and
928      @code{-l} options first to generate only a loop nest with internal
929      guards). For instance, with the input file @code{test/union.cloog}, a user
930      can generate different pseudo-codes  as shown below.
931      Default value is 0.
932 @example
933 @group
934 /* Generated using test/union.cloog and @strong{option -f -1 -l 2 -override} */
935 for (i=0;i<=11;i++) @{
936   for (j=max(0,5*i-50);j<=min(15,5*i+10);j++) @{
937     if ((i <= 10) && (j <= 10)) @{
938       S1 ;
939     @}
940     if ((i >= 1) && (j >= 5)) @{
941       S2 ;
942     @}
943   @}
945 @end group
946 @end example
947 @example
948 @group
949 /* Generated using the same input file but @strong{option -sh 1 -f -1 -l 2 -override} */
950 for (i=0;i<=11;i++) @{
951   for (j=0;j<=15;j++) @{
952     if ((i <= 10) && (j <= 10)) @{
953       S1 ;
954     @}
955     if ((i >= 1) && (j >= 5)) @{
956       S2 ;
957     @}
958   @}
960 @end group
961 @end example
963 @node Once Time Loop Elimination
964 @subsection Once Time Loop Elimination @code{-otl <boolean>}
966      @code{-otl <boolean>}: this option allows (@code{boolean=1}) or
967      forbids (@code{boolean=0}) the simplification of loops running
968      once. Default value is 1.
969 @example
970 @group
971 /* Generated using a given input file and @strong{option -otl 0} */
972 for (j=i+1;j<=i+1;j++) @{
973   S1 ;
975 @end group
976 @end example
977 @example
978 @group
979 /* Generated using the same input file but @strong{option -otl 1} */
980 j = i+1 ;
981 S1 ;
982 @end group
983 @end example
986 @node Equality Spreading 
987 @subsection Equality Spreading @code{-esp <boolean>}
989      @code{-esp <boolean>}: this option allows (@code{boolean=1}) or
990      forbids (@code{boolean=0}) values spreading when there
991      are equalities. Default value is 0.
992 @example
993 @group
994 /* Generated using a given input file and @strong{option -esp 0} */
995 i = M+2 ;
996 j = N ;
997 for (k=i;k<=j+M;k++) @{
998   S1 ;
1000 @end group
1001 @end example
1002 @example
1003 @group
1004 /* Generated using the same input file but @strong{option -esp 1} */
1005 for (k=M+2;k<=N+M;k++) @{
1006   S1(i = M+2, j = N) ;
1008 @end group
1009 @end example
1012 @node Constant Spreading 
1013 @subsection Constant Spreading @code{-csp <boolean>}
1015      @code{-csp <boolean>}: this option allows (@code{boolean=1}) or
1016      forbids (@code{boolean=0}) values spreading when
1017      there are @emph{constant} equalities. That is, when the right member
1018      of the equality is a constant term. Default value is 1.
1019 @example
1020 @group
1021 /* Generated using a given input file and @strong{option -csp 0} */
1022 i = M+2 ;
1023 j = N ;
1024 for (k=i;j<=j+M;j++) @{
1025   S1 ;
1027 @end group
1028 @end example
1029 @example
1030 @group
1031 /* Generated using the same input file but @strong{option -csp 1} */
1032 i = M+2 ;
1033 for (k=i;k<=N+M;k++) @{
1034   S1(j = N) ;
1036 @end group
1037 @end example
1040 @node First Level for Spreading 
1041 @subsection First Level for Spreading @code{-fsp <level>}
1043      @code{-fsp <level>}: it can be useful to set a
1044      first level to begin equality spreading. Particularly when using
1045      scattering functions, the user may want to see the scattering dimension
1046      values instead of spreading or hiding them. If user has set a
1047      spreading, @code{level} is
1048      the first level to start it. Default value is 1.
1049 @example
1050 @group
1051 /* Generated using a given input file and @strong{option -fsp 1} */
1052 for (j=0;j<=N+M;j++) @{
1053   S1(i = N) ;
1055 for (j=0;j<=N+M;j++) @{
1056   S1(i = M) ;
1058 @end group
1059 @end example
1060 @example
1061 @group
1062 /* Generated using the same input file but @strong{option -fsp 2} */
1063 c1 = N ;
1064 for (j=0;j<=c1+M;j++) @{
1065   S1(i = c1) ;
1067 c1 = M ;
1068 for (j=0;j<=N+c1;j++) @{
1069   S1(i = c1) ;
1071 @end group
1072 @end example
1075 @node C PreProcessor Friendly 
1076 @subsection C PreProcessor Friendly @code{-cpp <boolean>}
1078      @code{-cpp <boolean>}: this option ask CLooG for printing a less
1079      human-readable but compilable code by using the C preprocessor
1080      (@code{boolean=1}). In this case each statement is written as a
1081      function of the iterators corresponding to its domain dimensions:
1082      @code{Si(value_of_iterator_1,...,value_of_iterator_n)}. It follows
1083      that the user can easily add preprocessor macros to define each
1084      statement and use the generated textual code directly for compilation.
1085      When @code{boolean} is set to 0, the pretty printer has the default
1086      behaviour. Default value is 0.
1087 @example
1088 @group
1089 /* Generated using a given input file and @strong{option -cpp 0} */
1090 for (j=0;j<=N+M;j++) @{
1091   S1(i = N) ;
1093 @end group
1094 @end example
1095 @example
1096 @group
1097 /* Generated using the same input file but @strong{option -cpp 1} */
1098 /* and a preprocessor macro set by the user */
1100 #define S1(i,j) A[(j)]=3*(i)
1102 for (j=0;j<=N+M;j++) @{
1103   S1(N,j) ;
1105 @end group
1106 @end example
1108 @node Statement Block  
1109 @subsection Statement Block @code{-block <boolean>}
1111      @code{-block <boolean>}: this option allows (@code{boolean=1}) to
1112      create a statement block for each new iterator, even if there is only
1113      an equality. This can be useful in order to parse the generated
1114      pseudo-code. When @code{boolean} is set to 0 or when the generation
1115      language is FORTRAN, this feature is disabled. Default value is 0.
1116 @example
1117 @group
1118 /* Generated using a given input file and @strong{option -block 0} */
1119 i = M+2 ;
1120 j = N ;
1121 S1 ;
1122 @end group
1123 @end example
1124 @example
1125 @group
1126 /* Generated using the same input file but @strong{option -block 1} */
1127 @{ i = M+2 ;
1128   @{ j = N ;
1129     S1 ;
1130   @}
1132 @end group
1133 @end example
1136 @node Loop Strides 
1137 @subsection Loop Strides @code{-strides <boolean>}
1139      @code{-strides <boolean>}: this options allows (@code{boolean=1}) to
1140      handle non-unit strides for loop increments. This can remove a lot of
1141      guards and make the generated code more efficient. Default value is 0.
1142 @example
1143 @group
1144 /* Generated using a given input file and @strong{option -strides 0} */
1145 for (i=1;i<=n;i++) @{
1146   if (i%2 == 0) @{
1147     S1(j = i/2) ;
1148   @}
1149   if (i%4 == 0) @{
1150     S2(j = i/4) ;
1151   @}
1153 @end group
1154 @end example
1155 @example
1156 @group
1157 /* Generated using the same input file but @strong{option -strides 1} */
1158 for (i=2;i<=n;i+=2) @{
1159   S1(j = i/2) ;
1160   if (i%4 == 0) @{
1161     S2(j = i/4) ;
1162   @}
1164 @end group
1165 @end example
1167 @node Compilable Code
1168 @subsection Compilable Code @code{-compilable <value>}
1170      @code{-compilable <value>}: this options allows (@code{value} is not 0)
1171      to generate a compilable code where all parameters have the integral value
1172      @code{value}. This option creates a macro for each statement. Since
1173      CLooG do not know anything about the statement sources, it fills the
1174      macros with a basic increment that computes the total number of
1175      scanned integral points. The user may change easily the macros according
1176      to his own needs. This option is possible only if the generated code is
1177      in C. Default value is 0.
1178 @example
1179 @group
1180 /* Generated using a given input file and @strong{option -compilable 0} */
1181 for (i=0;i<=n;i++) @{
1182   for (j=0;j<=n;j++) @{
1183     S1 ;
1184     S2 ;
1185   @}
1186   S3 ;
1188 @end group
1189 @end example
1190 @example
1191 /* Generated using the same input file but @strong{option -compilable 10} */
1192 /* DON'T FORGET TO USE -lm OPTION TO COMPILE. */
1194 /* Useful headers. */
1195 #include <stdio.h>
1196 #include <stdlib.h>
1197 #include <math.h>
1199 /* Parameter value. */
1200 #define PARVAL 10
1202 /* Statement macros (please set). */
1203 #define S1(i,j) @{total++;@}
1204 #define S2(i,j) @{total++;@}
1205 #define S3(i)   @{total++;@}
1207 int main() @{
1208   /* Original iterators. */
1209   int i, j ;
1210   /* Parameters. */
1211   int n=PARVAL, total=0 ;
1213   for (i=0;i<=n;i++) @{
1214     for (j=0;j<=n;j++) @{
1215       S1(i,j) ;
1216       S2(i,j) ;
1217     @}
1218     S3(i) ;
1219   @}
1221   printf("Number of integral points: %d.\n",total) ;
1222   return 0 ;
1224 @end example
1226 @node Callable Code
1227 @subsection Callable Code @code{-callable <boolean>}
1229      @code{-callable <boolean>}: if @code{boolean=1}, then a @code{test}
1230      function will be generated that has the parameters as arguments.
1231      Similarly to the @code{-compilable} option,
1232      a macro for each statement is generated.  The generated definitions of
1233      these macros are as used during the correctness testing, but they
1234      can easily be changed by the user to suit her own needs.
1235      This option is only available if the target language is C.
1236      The default value is 0.
1238 @example
1239 /* Generated from double.cloog with @strong{option -callable 0} */
1240 for (i=0;i<=M;i++) @{
1241   S1 ;
1242   for (j=0;j<=N;j++) @{
1243     S2 ;
1244     S3 ;
1245   @}
1246   S4 ;
1248 @end example
1249 @example
1250 /* Generated from double.cloog with @strong{option -callable 1} */
1251 extern void hash(int);
1253 /* Useful macros. */
1254 #define floord(n,d) (((n)<0) ? ((n)-(d)+1)/(d) : (n)/(d))
1255 #define ceild(n,d)  (((n)<0) ? (n)/(d) : ((n)+(d)+1)/(d))
1256 #define max(x,y)    ((x) > (y) ? (x) : (y))
1257 #define min(x,y)    ((x) < (y) ? (x) : (y))
1259 #define S1(i) @{ hash(1); hash(i); @}
1260 #define S2(i,j) @{ hash(2); hash(i); hash(j); @}
1261 #define S3(i,j) @{ hash(3); hash(i); hash(j); @}
1262 #define S4(i) @{ hash(4); hash(i); @}
1264 void test(int M, int N)
1266   /* Original iterators. */
1267   int i, j;
1268   for (i=0;i<=M;i++) @{
1269     S1(i) ;
1270     for (j=0;j<=N;j++) @{
1271       S2(i,j) ;
1272       S3(i,j) ;
1273     @}
1274     S4(i) ;
1275   @}
1277 @end example
1279 @node Output
1280 @subsection Output @code{-o <output>}
1282      @code{-o <output>}: this option sets the output file. @code{stdout} is a
1283      special value: when used, output is standard output.
1284      Default value is @code{stdout}.
1286 @node Help
1287 @subsection Help @code{--help} or @code{-h}
1289      @code{--help} or @code{-h}: this option ask CLooG to print a short help.
1291 @node Version
1292 @subsection Version @code{--version} or @code{-v}
1294      @code{--version} or @code{-v}: this option ask CLooG to print some version
1295      informations.
1297 @node Quiet
1298 @subsection Quiet @code{--quiet} or @code{-q}
1300      @code{--quiet} or @code{-q}: this option tells CLooG not to print
1301      any informational messages.
1304 @c %/*************************************************************************
1305 @c % *                           A Full Example                              *
1306 @c % *************************************************************************/
1307 @node Full Example
1308 @section A Full Example
1310 Let us consider the allocation problem of a Gaussian elimination, i.e. we want
1311 to distribute the various statement instances of the compute kernel onto
1312 different processors. The original code is the following:
1313 @example
1314 @group
1315 for (i=1;j<=N-1;i++) @{
1316   for (j=i+1;j<=N;j++) @{
1317     c[i][j] = a[j][i]/a[i][i] ;    /* S1 */
1318     for (k=i+1;k<=N;k++) @{
1319       a[j][k] -= c[i][j]*a[i][k] ; /* S2 */
1320     @}
1321   @}
1323 @end group
1324 @end example
1326 @noindent The best affine allocation functions can be found by any good automatic
1327 parallelizer like LooPo (@pxref{Gri04}):
1329 @tex
1331 \hbox{$ \cases{ \theta _{S1}(i,j)^T    &$=  (i)$\cr
1332                 \theta _{S2}(i,j,k)^T  &$=  (k)$}$}
1334 @end tex
1336 @ifnottex
1337 @example
1338 @group
1339 T_S1(i,j)^T   = (i)
1340 T_S2(i,j,k)^T = (k)
1341 @end group
1342 @end example
1343 @end ifnottex
1345 @noindent To ensure that on each processor, the set of statement instances is
1346 executed according to the original ordering, we add as minor scattering
1347 dimensions the original scheduling (@pxref{Scattering}):
1349 @tex
1351 \hbox{$ \cases{ \theta _{S1}(i,j)^T    &$=  (i,0,i,0,j,0)^T$\cr
1352                 \theta _{S2}(i,j,k)^T  &$=  (k,0,i,0,j,1,k,0)^T$}$}
1354 @end tex
1356 @ifnottex
1357 @example
1358 @group
1359 T_S1(i,j)^T   = (i,0,i,0,j,0)^T
1360 T_S2(i,j,k)^T = (k,0,i,0,j,1,k,0)^T
1361 @end group
1362 @end example
1363 @end ifnottex
1365 @noindent To ensure that the scattering functions have the same dimensionality, we
1366 complete the first function with zeroes
1367 (this is a CLooG @value{VERSION} and previous versions requirement,
1368 it should be removed in a future version, don't worry it's absolutly legal !):
1370 @tex
1372 \hbox{$ \cases{ \theta _{S1}(i,j)^T    &$=  (i,0,i,0,j,0,0,0)^T$\cr
1373                 \theta _{S2}(i,j,k)^T  &$=  (k,0,i,0,j,1,k,0)^T$}$}
1375 @end tex
1377 @ifnottex
1378 @example
1379 @group
1380 T_S1(i,j)^T   = (i,0,i,0,j,0,0,0)^T
1381 T_S2(i,j,k)^T = (k,0,i,0,j,1,k,0)^T
1382 @end group
1383 @end example
1384 @end ifnottex
1386 @noindent The input file corresponding to this code generation problem
1387 could be (this file is provided in the CLooG distribution
1388 as @code{test/manual_gauss.cloog}:
1390 @example
1391 # ---------------------- CONTEXT ----------------------
1392 c # language is C
1394 # Context (no constraints on one parameter)
1395 1 3                     # 1 line and 3 columns    
1396 # eq/in n  1
1397     1   0  0            # 0 >= 0, always true
1399 1 # We want to set manually the parameter name
1400 n                       # parameter name
1402 # --------------------- STATEMENTS --------------------
1403 2 # Number of statements
1405 1 # First statement: one domain
1406 4 5                     # 4 lines and 3 columns
1407 # eq/in i  j  n  1
1408     1   1  0  0 -1      # i >= 1
1409     1  -1  0  1 -1      # i <= n-1
1410     1  -1  1  0 -1      # j >= i+1
1411     1   0 -1  1  0      # j <= n
1412 0  0  0                 # for future options
1415 # Second statement: one domain
1416 6 6                     # 6 lines and 3 columns
1417 # eq/in i  j  k  n  1
1418     1   1  0  0  0 -1   # i >= 1
1419     1  -1  0  0  1 -1   # i <= n-1
1420     1  -1  1  0  0 -1   # j >= i+1
1421     1   0 -1  0  1  0   # j <= n
1422     1  -1  0  1  0 -1   # k >= i+1
1423     1   0  0 -1  1  0   # k <= n
1424 0  0  0                 # for future options
1426 0 # We let CLooG set the iterator names
1428 # --------------------- SCATTERING --------------------
1429 2 # Scattering functions
1430 # First function
1431 8 13                    # 3 lines and 3 columns
1432 # eq/in p1 p2 p3 p4 p5 p6 p7 p8  i  j  n  1
1433     0    1  0  0  0  0  0  0  0 -1  0  0  0     # p1 = i
1434     0    0  1  0  0  0  0  0  0  0  0  0  0     # p2 = 0
1435     0    0  0  1  0  0  0  0  0 -1  0  0  0     # p3 = i
1436     0    0  0  0  1  0  0  0  0  0  0  0  0     # p4 = 0
1437     0    0  0  0  0  1  0  0  0  0 -1  0  0     # p5 = j
1438     0    0  0  0  0  0  1  0  0  0  0  0  0     # p6 = 0
1439     0    0  0  0  0  0  0  1  0  0  0  0  0     # p7 = 0
1440     0    0  0  0  0  0  0  0  1  0  0  0  0     # p8 = 0
1441 # Second function
1442 8 14                    # 3 lines and 3 columns
1443 # eq/in p1 p2 p3 p4 p5 p6 p7 p8  i  j  k  n  1
1444     0    1  0  0  0  0  0  0  0  0  0 -1  0  0  # p1 = k
1445     0    0  1  0  0  0  0  0  0  0  0  0  0  0  # p2 = 0
1446     0    0  0  1  0  0  0  0  0 -1  0  0  0  0  # p3 = i
1447     0    0  0  0  1  0  0  0  0  0  0  0  0  0  # p4 = 0
1448     0    0  0  0  0  1  0  0  0  0 -1  0  0  0  # p5 = j
1449     0    0  0  0  0  0  1  0  0  0  0  0  0 -1  # p6 = 1
1450     0    0  0  0  0  0  0  1  0  0  0 -1  0  0  # p7 = k
1451     0    0  0  0  0  0  0  0  1  0  0  0  0  0  # p8 = 0
1453 1 # We want to set manually the scattering dimension names
1454 p1 p2 p3 p4 p5 p6 p7 p8 # scattering dimension names
1455 @end example
1457 Calling CLooG, with for instance the command line
1458 @code{cloog -fsp 2 gauss.cloog} for a better view
1459 of the allocation (the processor number is given by @code{p1}),
1460 will result on the following target code that actually implements
1461 the transformation. A minor processing on the dimension @code{p1}
1462 to implement, e.g., MPI calls, which is not shown here may
1463 result in dramatic speedups !
1465 @example
1466 if (n >= 2) @{
1467   p1 = 1 ;
1468   for (p5=2;p5<=n;p5++) @{
1469     S1(i = 1,j = p5) ;
1470   @}
1472 for (p1=2;p1<=n-1;p1++) @{
1473   for (p3=1;p3<=p1-1;p3++) @{
1474     for (p5=p3+1;p5<=n;p5++) @{
1475       S2(i = p3,j = p5,k = p1) ;
1476     @}
1477   @}
1478   for (p5=p1+1;p5<=n;p5++) @{
1479     S1(i = p1,j = p5) ;
1480   @}
1482 if (n >= 2) @{
1483   p1 = n ;
1484   for (p3=1;p3<=n-1;p3++) @{
1485     for (p5=p3+1;p5<=n;p5++) @{
1486       S2(i = p3,j = p5,k = n) ;
1487     @}
1488   @}
1490 @end example
1493 @c %/*************************************************************************
1494 @c % *                           A Full Example                              *
1495 @c % *************************************************************************/
1496 @node CLooG Library
1497 @chapter Using the CLooG Library
1498 The CLooG Library was implemented to allow the user to call CLooG
1499 directly from his programs, without file accesses or system calls. The
1500 user only needs to link his programs with C libraries. The CLooG
1501 library mainly provides one function (@code{cloog_program_generate})
1502 which takes as input the problem
1503 description with some options, and returns the data structure corresponding
1504 to the generated code (a @code{CloogProgram} structure) which is more or less
1505 an abstract syntax tree.
1506 The user can work with this data structure and/or use
1507 our pretty printing function to write the final code in either C or FORTRAN.
1508 Some other functions are provided for convenience reasons.
1509 These functions as well as the data structures are described in this section.
1511 @menu
1512 * CLooG Data Structures::
1513 * CLooG Functions::
1514 * Example of Library Utilization::
1515 @end menu
1518 @node CLooG Data Structures
1519 @section CLooG Data Structures Description
1520 In this section, we describe the data structures used by the loop
1521 generator to represent and to process a code generation problem.
1523 @menu
1524 * CloogMatrix::
1525 * CloogDomain::
1526 * CloogDomainList::
1527 * CloogStatement::
1528 * CloogBlock::
1529 * CloogBlockList::
1530 * CloogLoop::
1531 * CloogNames::
1532 * CloogProgram::
1533 * CloogOptions::
1534 @end menu
1537 @node CloogMatrix
1538 @subsection CloogMatrix
1539 @example
1540 @group
1541 #define CloogMatrix Matrix
1542 @end group
1543 @end example
1545 @noindent The @code{CloogMatrix} structure is directly the PolyLib
1546 @code{Matrix} data structure (@pxref{Wil93}). This structure is devoted to
1547 represent a set of constraints. It is 
1548 defined in @code{polylib/types.h} as the following:
1550 @example
1551 @group
1552 struct matrix
1553 @{ unsigned NbRows ;    /* Number of rows. */
1554   unsigned NbColumns ; /* Number of columns. */
1555   Value ** p ;         /* Array of pointers to the matrix rows. */
1556   Value * p_Init ;     /* Matrix rows contiguously in memory. */
1557   int p_Init_size ;    /* For internal use. */
1559 typedef struct matrix Matrix;
1560 @end group
1561 @end example
1563 @noindent The whole matrix is stored in memory row after row at the
1564 @code{p_Init} address. @code{p} is an array of pointers where
1565 @code{p[i]} points to the first element of the @math{i^{th}} row.
1566 @code{NbRows} and @code{NbColumns} are respectively the number of
1567 rows and columns of the matrix. 
1568 Each row corresponds to a constraint. The first element of each row is an
1569 equality/inequality tag. The
1570 constraint is an equality @math{p(x) = 0} if the first element is 0, but it is
1571 an inequality @math{p(x) \geq 0} if the first element is 1.
1572 The next elements are the unknown coefficients, followed by the parameter
1573 coefficients, then the scalar coefficient.
1574 For instance, the following three constraints:
1576 @tex
1578 \hbox{$ \cases{ -i + m       &$= 0$\cr
1579                 -j + n       &$\geq 0$\cr
1580                  j + i - k   &$\geq 0$}$}
1582 @end tex
1584 @ifnottex
1585 @example
1586 @group
1587     -i + m  = 0
1588     -j + n >= 0
1589  i + j - k >= 0
1590 @end group
1591 @end example
1592 @end ifnottex
1594 @noindent would be represented by the following rows:
1596 @example
1597 @group
1598 # eq/in  i   j   k   m   n   cst
1599     0    0  -1   0   1   0    0 
1600     1   -1   0   0   0   1    0 
1601     1    1   1  -1   0   0    0 
1602 @end group
1603 @end example
1605 @noindent To be able to provide different precision version (CLooG
1606 supports 32 bits, 64 bits and arbitrary precision through the GMP library),
1607 the @code{Value} type depends on the configuration options (it may be
1608 @code{long int} for 32 bits version, @code{long long int} for 64 bits version,
1609 and @code{mpz_t} for multiple precision version).
1610 The @code{p_Init_size} field is needed by the PolyLib to free
1611 the memory allocated by @code{mpz_init} in the multiple precision release.
1612 Set this field to 0 if you are @emph{not} using multiple precision.
1613 Set this field to the size of the @code{p_Init} array if you
1614 initialized it yourself and if you are using the multiple precision version.
1617 @node CloogDomain
1618 @subsection CloogDomain
1619 @example
1620 @group
1621 struct cloogdomain
1622 @{ Polyhedron * polyhedron ;  /* The polyhedral domain. */
1623 @} ;
1624 typedef struct cloogdomain CloogDomain ;
1625 @end group
1626 @end example
1628 @noindent The @code{CloogDomain} structure contains a PolyLib
1629 @code{Polyhedron} data structure which represents a polyhedral domain
1630 (a union of polyhedra) in both constraint representation and its dual
1631 ray representation (@pxref{Wil93}).
1632 It is defined in @code{polylib/types.h} as the following:
1634 @example
1635 @group
1636 struct polyhedron
1637 @{ unsigned Dimension,        /* Number of dimensions. */
1638            NbConstraints,    /* Number of constraints. */
1639            NbRays,           /* Number of rays. */
1640            NbEq,             /* Number of vertices (?). */
1641            NbBid ;           /* Number of extremal rays (?). */
1642   Value ** Constraint ;      /* Pointers to constraints. */
1643   Value ** Ray ;             /* Pointers to rays. */
1644   Value * p_Init ;           /* Constraints and rays contiguously. */
1645   int p_Init_size ;          /* For internal use. */
1646   struct polyhedron * next ; /* Next component of the union. */
1648 typedef struct polyhedron Polyhedron;
1649 @end group
1650 @end example
1652 @noindent The constraint representation is quite the same as in
1653 the @code{Matrix} data structure (@pxref{CloogMatrix}). The number of
1654 rows is @code{NbConstraints} and the
1655 number of columns in the @code{Polyhedron} structure is
1656 @code{Dimension+2} (the @math{+2} comes from the equality/inequality
1657 tag and the scalar coefficient). As in the @code{Matrix} structure,
1658 The data are stored in memory contiguously at the
1659 @code{p_Init} address and the @code{p_Init_size} field is used for
1660 memory deallocation in the multiple precision case (@pxref{CloogMatrix}).
1661 For a better understanding of the
1662 dual ray representation, the user may refer to the PolyLib documentation.
1665 @node CloogDomainList
1666 @subsection CloogDomainList
1667 @example
1668 @group
1669 struct cloogdomainlist
1670 @{ CloogDomain * domain ;
1671   struct cloogdomainlist * next ;
1672 @} ;
1673 typedef struct cloogdomainlist CloogDomainList ;
1674 @end group
1675 @end example
1677 @noindent The CloogDomainList structure represents a @code{NULL} terminated linked list
1678 of domains.
1681 @node CloogStatement
1682 @subsection CloogStatement
1683 @example
1684 @group
1685 struct cloogstatement
1686 @{ int number ;                  /* The statement unique number. */
1687   void * usr ;                  /* Pointer for user's convenience. */
1688   struct cloogstatement * next ;/* Next element of the linked list. */
1689 @} ;
1690 typedef struct cloogstatement CloogStatement ;
1691 @end group
1692 @end example
1694 @noindent The @code{CloogStatement} structure represents a @code{NULL}
1695 terminated linked
1696 list of statements. In CLooG, a statement is only defined by its unique
1697 number (@code{number}). The user can use the pointer @code{usr} for his
1698 own convenience to link his own statement representation to the
1699 corresponding @code{CloogStatement} structure. The whole management of the
1700 @code{usr} pointer is under the responsibility of the user, in particular,
1701 CLooG never tries to print, to allocate or to free a memory block pointed
1702 by @code{usr}. 
1705 @node CloogBlock
1706 @subsection CloogBlock
1707 @example
1708 @group
1709 struct cloogblock
1710 @{ CloogStatement * statement ; /* Statement list of the block. */
1711   CloogMatrix * scattering ;   /* Scattering function of the block. */
1712   int depth ;                  /* Original block depth.*/
1713   void * usr;                  /* Pointer for user's convenience. */
1714 @} ;
1715 typedef struct cloogblock CloogBlock ;
1716 @end group
1717 @end example
1719 @noindent The @code{CloogBlock} structure represents a statement block.
1720 In a statement block, every statements have the same iteration
1721 domain and the same scattering function (actually, the scattering
1722 functions may differ only by a scalar
1723 coefficient if it just precises the ordering of the statements within
1724 the block). @code{statement} is the statement list where the
1725 statement order matters, @code{scattering} is one of
1726 the statement scattering functions and
1727 @code{depth} is the number of dimensions of the
1728 iteration domain (only the unknown, not the tag/parameters/scalar).
1729 @code{usr} is a pointer for library user's convenience. Note this pointer
1730 is never allocated, freed or printed by CLooG.
1732 @node CloogBlockList
1733 @subsection CloogBlockList
1734 @example
1735 @group
1736 struct cloogdblocklist
1737 @{ CloogBlock * block ;
1738   struct cloogblocklist * next ;
1739 @} ;
1740 typedef struct cloogblocklist CloogBlockList ;
1741 @end group
1742 @end example
1744 @noindent The CloogBlockList structure represents a @code{NULL} terminated linked list
1745 of blocks.
1748 @node CloogLoop
1749 @subsection CloogLoop 
1750 @example
1751 @group
1752 struct cloogloop
1753 @{ CloogDomain * domain;       /* Iteration domain. */
1754   Value stride ;               /* Loop stride. */
1755   CloogBlock * block ;         /* Included statement block.*/
1756   void * usr;                  /* Pointer for user's convenience. */
1757   struct cloogloop * inner ;   /* Loop at the next level. */
1758   struct cloogloop * next ;    /* Next loop at the same level. */
1759 @} ;
1760 typedef struct cloogloop CloogLoop ;
1761 @end group
1762 @end example
1764 @noindent The @code{CloogLoop} structure represents a loop.
1765 First of all, a
1766 loop has an iteration domain (@code{domain}). The iterator's stride for loop
1767 increment is @code{stride}. The loop can include a statement block
1768 in the field @code{block}. If there is no included statement block,
1769 @code{block} is set to @code{NULL}. @code{usr} is a pointer for library
1770 user's convenience. Note that this pointer is never allocated, freed or
1771 printed by CLooG. @code{inner} is a pointer to the inner
1772 loop, and @code{next} a pointer to the next loop in the textual order. If
1773 there are no inner loop or no next loop, the corresponding pointer is set
1774 to @code{NULL}.
1777 @node CloogNames
1778 @subsection CloogNames
1779 @example
1780 @group
1781 struct cloognames
1782 @{ int nb_scattering ;         /* Scattering dimension number. */
1783   int nb_iterators ;          /* Iterator number. */
1784   int nb_parameters ;         /* Parameter number. */
1785   char ** scattering ;        /* The scattering dimension names. */
1786   char ** iterators ;         /* The iterator names. */
1787   char ** parameters ;        /* The parameter names. */
1788 @} ;
1789 typedef struct cloognames CloogNames ;
1790 @end group
1791 @end example
1793 @noindent The @code{CloogNames} structure represents the scattering dimension,
1794 the iterator and the parameter names in the final program.
1795 @code{nb_scattering} 
1796 (respectively @code{nb_iterators} and @code{nb_parameters})
1797 is the number of scattering dimensions number
1798 (respectively the iterator and parameter number)
1799 and of elements in the corresponding array of strings
1800 @code{scattering}
1801 (respectively @code{iterators} and @code{parameters}).
1802 The @math{i^{th}} scattering dimension name will be associated with the
1803 to the dimension @math{i} of the scattering function.
1804 The @math{i^{th}} iterator name will be associated with the
1805 dimension @math{i} of the iteration domain. 
1806 The @math{i^{th}} parameter name will be associated with the
1807 dimension @math{i} of the context polyhedron.
1808 The user has to ensure that there are
1809 enough scattering dimension, iterator and parameter names. 
1812 @node CloogProgram
1813 @subsection CloogProgram
1814 @example
1815 @group
1816 struct cloogprogram
1817 @{ char language ;              /* The language of the program. */
1818   int  nb_scattdims ;          /* Scattering dimension number. */
1819   CloogNames  * names ;        /* Iterators and parameters names. */
1820   CloogDomain * context ;      /* The context of the program. */
1821   CloogLoop   * loop ;         /* The loops of the program. */
1822   CloogBlockList * blocklist ; /* The statement block list. */
1823   void * usr;                  /* For library user's convenience. */
1824 @} ;
1825 typedef struct cloogprogram CloogProgram ;
1826 @end group
1827 @end example
1829 @noindent The @code{CloogProgram} structure represents a static control program kernel.
1830 @code{language} precises the language (@code{c} for C or @code{f} for FORTRAN).
1831 @code{nb_scattdims} gives the number of scattering dimensions.
1832 @code{context} is a pointer to the constraints on the program parameters,
1833 it can't be the
1834 @code{NULL} pointer even if there are no constraints on parameters. In such a
1835 case, set a polyhedron with as many dimensions as there are parameters, with
1836 an @emph{always true} constraint like @math{1 \geq 0} (this is necessary
1837 since the number of parameters is deduced from the dimension number of
1838 the context constraints). @code{loop} is a pointer
1839 to the first loop of the program. @code{names} is a pointer to the various
1840 element names (scattering dimension, iterators, parameters)
1841 of the final program. @code{names} can be the @code{NULL}
1842 pointer if the user do not want to use our pretty printing function.
1843 @code{blocklist} is the linked list of all the statement block structures.
1844 @code{usr} is a pointer for library user's convenience. Note that this pointer
1845 is never allocated, freed or printed by CLooG.
1846 As an example, let us consider the following loop nest:
1847 @example
1848 @group
1849 for (i=0; i<=n; i++) @{ 
1850   for (j=0; j<=n; j++) @{
1851     S1 ;
1852     S2 ;
1853   @}
1854   for (j=n+1; j<=2*n; j++) @{
1855     S3 ;
1856   @}
1857 @}  
1858 @end group
1859 @end example
1860 @noindent The next figure gives a possible representation in memory for this
1861 program thanks to the CLooG data structures (it has been actually printed
1862 by the @code{cloog_program_print} function). In this figure,
1863 @samp{+-- CloogLoop} denotes an @samp{inner} loop, while a @samp{CloogLoop}
1864 on the same column pointed by an arrow denotes a @samp{next} loop:
1866 @smallexample
1867 +-- CloogProgram
1868 |       |       
1869 |       Language: c
1870 |       |       
1871 |       Scattering dimension number: 0
1872 |       |       
1873 |       +-- CloogNames
1874 |       |       |       
1875 |       |       Scattering dimension number: 0
1876 |       |       |       
1877 |       |       +-- No scattering string
1878 |       |       |       
1879 |       |       Iterator number -----------: 2
1880 |       |       |       
1881 |       |       +-- Iterator strings ------: i j
1882 |       |       |       
1883 |       |       Parameter number ----------: 1
1884 |       |       |       
1885 |       |       +-- Parameter strings -----: n
1886 |       |       
1887 |       +-- Context
1888 |       |       [   1    1   -2  ]
1889 |       |       [   1    0    1  ]
1890 |       |       
1891 |       +-- CloogLoop
1892 |       |       |       
1893 |       |       +-- CloogDomain
1894 |       |       |       [   1   -1    1    0  ]
1895 |       |       |       [   1    1    0    0  ]
1896 |       |       |       [   1    0    0    1  ]
1897 |       |       |       
1898 |       |       Stride: 1
1899 |       |       |       
1900 |       |       +-- Null CloogBlock
1901 |       |       |       
1902 |       |       +-- CloogLoop
1903 |       |       |       |       
1904 |       |       |       +-- CloogDomain
1905 |       |       |       |       [   1    0    1    0    0  ]
1906 |       |       |       |       [   1    0   -1    1    0  ]
1907 |       |       |       |       [   1    0    0    0    1  ]
1908 |       |       |       |       
1909 |       |       |       Stride: 1
1910 |       |       |       |       
1911 |       |       |       +-- Null CloogBlock
1912 |       |       |       |       
1913 |       |       |       +-- CloogLoop
1914 |       |       |       |       |       
1915 |       |       |       |       +-- CloogDomain
1916 |       |       |       |       |       [   1    0    0    0    1  ]
1917 |       |       |       |       |       
1918 |       |       |       |       Stride: 1
1919 |       |       |       |       |       
1920 |       |       |       |       +-- CloogBlock
1921 |       |       |       |       |       |       
1922 |       |       |       |       |       +-- CloogStatement 1 
1923 |       |       |       |       |       |          |   
1924 |       |       |       |       |       |          V   
1925 |       |       |       |       |       |   CloogStatement 2 
1926 |       |       |       |       |       |       
1927 |       |       |       |       |       +-- Null scattering function
1928 |       |       |       |       |       |       
1929 |       |       |       |       |       Depth: 2
1930 |       |       |       |       |       
1931 |       |       |       |       
1932 |       |       |       V
1933 |       |       |   CloogLoop
1934 |       |       |       |       
1935 |       |       |       +-- CloogDomain
1936 |       |       |       |       [   1    0   -1    2    0  ]
1937 |       |       |       |       [   1    0    1   -1   -1  ]
1938 |       |       |       |       [   1    0    0    0    1  ]
1939 |       |       |       |       
1940 |       |       |       Stride: 1
1941 |       |       |       |       
1942 |       |       |       +-- Null CloogBlock
1943 |       |       |       |       
1944 |       |       |       +-- CloogLoop
1945 |       |       |       |       |       
1946 |       |       |       |       +-- CloogDomain
1947 |       |       |       |       |       [   1    0    0    0    1  ]
1948 |       |       |       |       |       
1949 |       |       |       |       Stride: 1
1950 |       |       |       |       |       
1951 |       |       |       |       +-- CloogBlock
1952 |       |       |       |       |       |       
1953 |       |       |       |       |       +-- CloogStatement 3 
1954 |       |       |       |       |       |       
1955 |       |       |       |       |       +-- Null scattering function
1956 |       |       |       |       |       |       
1957 |       |       |       |       |       Depth: 2
1958 |       |       |       |       |       
1959 |       |       |       |       
1960 |       |       |       
1961 |       |       
1962 |       
1963 @end smallexample
1966 @node CloogOptions
1967 @subsection CloogOptions
1968 @example
1969 @group
1970 struct cloogoptions
1971 @{ int l ;                     /* -l option.          */
1972   int f ;                     /* -f option.          */
1973   int strides ;               /* -strides option.    */
1974   int sh ;                    /* -sh option.         */
1975   int esp ;                   /* -esp option.        */
1976   int csp ;                   /* -csp option.        */
1977   int fsp ;                   /* -fsp option.        */
1978   int otl ;                   /* -otl option.        */
1979   int block ;                 /* -block option.      */
1980   int cpp ;                   /* -cpp option.        */
1981   int compilable ;            /* -compilable option. */
1982 @} ;
1983 typedef struct cloogoptions CloogOptions ;
1984 @end group
1985 @end example
1987 @noindent The @code{CloogOptions} structure contains all the possible options to
1988 rule CLooG's behaviour (@pxref{Calling CLooG}).
1989 As a reminder, the default values are:
1990 @itemize @bullet
1991 @item @math{l = -1} (optimize control until the innermost loops),
1992 @item @math{f = 1} (optimize control from the outermost loops),
1993 @item @math{strides = 0} (use only unit strides),
1994 @item @math{sh = 0} (do not simplify convex hulls),
1995 @item @math{esp = 0} (do not spread complex equalities),
1996 @item @math{csp = 1} (spread constant values),
1997 @item @math{fsp = 1} (start to spread from the first iterators),
1998 @item @math{otl = 1} (simplify loops running only once).
1999 @item @math{block = 0} (do not make statement blocks when not necessary).
2000 @item @math{cpp = 0} (do not generate a compilable part of code using preprocessor).
2001 @item @math{compilable = 0} (do not generate a compilable code).
2002 @end itemize 
2005 @node CLooG Functions
2006 @section CLooG Functions Description
2008 @menu
2009 * cloog_program_generate::
2010 * cloog_program_scatter::
2011 * cloog_program_pprint::
2012 * cloog_program_read::
2013 * From Matrices to Domains and Conversely::
2014 * Allocation and Initialization Functions::
2015 * Memory Deallocation Functions::
2016 * Printing Functions::
2017 @end menu
2020 @node cloog_program_generate
2021 @subsection cloog_program_generate
2022 @example
2023 @group
2024 CloogProgram   * cloog_program_generate
2025 ( CloogProgram * program,       /* Input program. */
2026   CloogOptions * options        /* Options. */
2027 ) ;
2028 @end group
2029 @end example
2031 @noindent The @code{cloog_program_generate} function generates
2032 the data structure of the source code that scans the input
2033 polyhedra pointed by @code{program}
2034 according to the options pointed by @code{options}.
2035 The process is made directly on the input structure pointed by
2036 @code{program}, thus the original structure is no longer available
2037 after a call to this function. It returns a pointer to a
2038 @code{CloogProgram} structure containing the
2039 solution in CLooG structures.
2041 The input @code{CloogProgram} structure must have only one loop level
2042 (no inner loops): there is one loop per statement block. For a given block,
2043 the corresponding loop carries the iteration domain, the statement block,
2044 and a loop stride initialized to 1. For instance, the input @code{CloogProgram} structure
2045 that have been sent to @code{cloog_program_generate} to achieve the final
2046 structure and code shown as example in the @code{CloogProgram} structure
2047 description (@pxref{CloogProgram}) was the following one:
2049 @smallexample
2050 +-- CloogProgram
2051 |       |       
2052 |       Language: c
2053 |       |       
2054 |       Scattering dimension number: 0
2055 |       |       
2056 |       +-- CloogNames
2057 |       |       |       
2058 |       |       Scattering dimension number: 0
2059 |       |       |       
2060 |       |       +-- No scattering string
2061 |       |       |       
2062 |       |       Iterator number -----------: 2
2063 |       |       |       
2064 |       |       +-- Iterator strings ------: i j
2065 |       |       |       
2066 |       |       Parameter number ----------: 1
2067 |       |       |       
2068 |       |       +-- Parameter strings -----: n
2069 |       |       
2070 |       +-- Context
2071 |       |       [    1     1    -2  ]
2072 |       |       
2073 |       +-- CloogLoop
2074 |       |       |       
2075 |       |       +-- CloogDomain
2076 |       |       |       [    1     1     0     0     0  ]
2077 |       |       |       [    1    -1     0     1     0  ]
2078 |       |       |       [    1     0     1     0     0  ]
2079 |       |       |       [    1     0    -1     1     0  ]
2080 |       |       |       
2081 |       |       Stride: 1
2082 |       |       |       
2083 |       |       +-- CloogBlock
2084 |       |       |       |       
2085 |       |       |       +-- CloogStatement 1 
2086 |       |       |       |          |
2087 |       |       |       |          V
2088 |       |       |       |   CloogStatement 2 
2089 |       |       |       |       
2090 |       |       |       +-- Null scattering function
2091 |       |       |       |       
2092 |       |       |       Depth: 2
2093 |       |       |       
2094 |       |       V
2095 |       |   CloogLoop
2096 |       |       |       
2097 |       |       +-- CloogDomain
2098 |       |       |       [    1     1     0     0     0  ]
2099 |       |       |       [    1    -1     0     1     0  ]
2100 |       |       |       [    1     0     1    -1    -1  ]
2101 |       |       |       [    1     0    -1     2     0  ]
2102 |       |       |       
2103 |       |       Stride: 1
2104 |       |       |       
2105 |       |       +-- CloogBlock
2106 |       |       |       |       
2107 |       |       |       +-- CloogStatement 3 
2108 |       |       |       |       
2109 |       |       |       +-- Null scattering function
2110 |       |       |       |       
2111 |       |       |       Depth: 2
2112 |       |       |       
2113 |       |       
2114 |       
2115 @end smallexample
2118 @node cloog_program_pprint
2119 @subsection cloog_program_pprint
2120 @example
2121 @group
2122 void cloog_program_pprint
2123 ( FILE * file,                  /* Output file. */
2124   CloogProgram * program,       /* Program to print. */
2125   CloogOptions * options        /* Options. */
2126 ) ;
2127 @end group
2128 @end example
2130 @noindent The function @code{cloog_program_pprint} is a pretty printer for
2131 @code{CloogProgram} structures when it is a solution provided by
2132 the @code{cloog_program_generate} function. It prints the code or pseudo-code in the
2133 file pointed by @code{file} (possibly @code{stdout}) with regards to the
2134 options pointed by @code{options}.
2137 @node cloog_program_scatter
2138 @subsection cloog_program_scatter
2139 @example
2140 @group
2141 void cloog_program_scatter
2142 ( CloogProgram * program,       /* Input program. */
2143   CloogDomainList * scattering, /* Additional scattering functions. */
2144   char ** names ;               /* Additional dimension names. */
2145 ) ;
2146 @end group
2147 @end example
2149 @noindent The function @code{cloog_program_scatter} applies scattering
2150 functions to the @code{CloogProgram} structure pointed by @code{program}.
2151 Original domains of @code{program} are freed. Scattering functions
2152 are inside the @code{CloogDomainList} structure pointed by @code{scattering}.
2153 There must be as many scattering functions in the @code{CloogDomainList}
2154 structure as loops (i.e. iteration domains) in the @code{CloogProgram}
2155 structure. The first scattering function of the list will be applied to the
2156 iteration domain of the first loop in the program, and so on.
2157 @code{names} gives the scattering dimension names as an array of strings. If
2158 @code{names} is @code{NULL}, names are automatically generated: the name of
2159 the @math{n^{th}} scattering dimension will be @code{cn}.
2162 @node cloog_program_read
2163 @subsection cloog_program_read
2164 @example
2165 CloogProgram * cloog_program_read(FILE *) ;
2166 @end example
2167 @noindent The @code{cloog_program_read} function
2168 reads the program data from a CLooG input file
2169 (@pxref{Writing The Input File}). It takes
2170 as input a pointer to the file it has to read (possibly @code{stdin}), and
2171 return a pointer to the read @code{CloogProgram} structure.
2174 @node From Matrices to Domains and Conversely
2175 @subsection From Matrices to Domains and Conversely
2176 @example
2177 CloogMatrix * cloog_domain_domain2matrix(CloogDomain *) ;
2178 CloogDomain * cloog_domain_matrix2domain(CloogMatrix *) ;
2179 @end example
2180 @noindent Two functions are provided to translate a @code{CloogDomain}
2181 data structure to a @code{CloogMatrix} data structure and conversely.
2182 Each function takes as input a pointer to the data structure to translate
2183 and returns as output a pointer to the translated data structure. The
2184 input data structure if neither modified nor freed. They
2185 may be quite useful for e.g. pretty printing since it is more convenient
2186 in constraint (matrix) representation.
2189 @node Allocation and Initialization Functions
2190 @subsection Allocation and Initialization Functions
2191 @example
2192 CloogStructure * cloog_structure_malloc() ;
2193 @end example
2194 @noindent Each CLooG data structure has an allocation and initialization
2195 function as shown above, where @code{Structure} and @code{structure} have to
2196 be replaced by the name of the convenient structure (without @samp{Cloog} prefix) for
2197 instance @code{CloogLoop * cloog_loop_malloc() ;}. These functions return
2198 pointers to an allocated structure with fields set to convenient default
2199 values. @strong{Using those functions is mandatory} to support internal
2200 management fields and to avoid upward compatibility problems if
2201 new fields appear. An exception is @code{cloog_matrix_malloc} since the
2202 @code{CloogMatrix} comes directly from the PolyLib. It takes two parameters:
2203 the number of rows and columns of the matrix we want to allocate:
2204 @example
2205 CloogMatrix * cloog_matrix_malloc(unsigned nbrows, unsigned nbcolumns);
2206 @end example
2209 @node Memory Deallocation Functions
2210 @subsection Memory Deallocation Functions
2211 @example
2212 void cloog_structure_free(CloogStructure *) ;
2213 @end example
2214 @noindent Each CLooG data structure has a deallocation function as shown above,
2215  where @code{Structure} and @code{structure} have to
2216 be replaced by the name of the convenient structure (without @samp{Cloog} prefix) for
2217 instance @code{void cloog_loop_free(CloogLoop *) ;}. These functions
2218 free the allocated memory for the structure provided as input. They free
2219 memory recursively, i.e. they also free the allocated memory for the internal
2220 structures.
2221 @strong{Using those functions is mandatory} to avoid memory leaks on internal
2222 management fields and to avoid upward compatibility problems if
2223 new fields appear.
2226 @node Printing Functions
2227 @subsection Printing Functions
2228 @example
2229 void cloog_structure_print(FILE *, CloogStructure *) ;
2230 @end example
2231 @noindent Each CLooG data structure has a printing function as shown above,
2232  where @code{Structure} and @code{structure} have to
2233 be replaced by the name of the convenient structure (without @samp{Cloog} prefix) for
2234 instance @code{void cloog_loop_print(FILE *, CloogLoop *) ;}. These functions
2235 print the pointed structure (and its fields recursively) to the file provided
2236 as input (possibly @code{stdout}).
2239 @node Example of Library Utilization
2240 @section Example of Library Utilization
2241 Here is a basic example showing how it is possible to use the CLooG library,
2242 assuming that a standard installation has been done.
2243 The following C program reads a CLooG input file on the standard input,
2244 then prints the solution on the standard output.
2245 Options are preselected to the default values of the CLooG software.
2246 This example is provided in the @code{example} directory of the
2247 CLooG distribution.
2248 @example
2249 /* example.c */
2250 # include <stdio.h>
2251 # include <cloog/cloog.h>
2253 int main()
2254 @{ CloogProgram * program ;
2255   CloogOptions * options ;
2256   
2257   /* Setting options and reading program informations. */
2258   options = cloog_options_malloc() ;
2259   program = cloog_program_read(stdin,options) ;
2261   /* Generating and printing the code. */
2262   program = cloog_program_generate(program,options) ;
2263   cloog_program_pprint(stdout,program,options) ;
2265   cloog_options_free(options) ;
2266   cloog_program_free(program) ;
2267   return 0;
2269 @end example
2271 @noindent The compilation command could be:
2272 @example
2273 gcc example.c -lcloog -o example
2274 @end example
2275 @noindent A calling command with the input file test.cloog could be:
2276 @example
2277 more test.cloog | ./example
2278 @end example
2281 @c %  ******************************** HACKING *********************************
2282 @c @node Hacking
2283 @c @chapter Hacking CLooG
2285 @c @menu
2286 @c * Program organization::
2287 @c * Special Options::
2288 @c * CLooG Coding Standards::
2289 @c @end menu
2291 @c @node Program organization
2292 @c @section Program organization
2294 @c @node Special Options
2295 @c @section Special Options
2297 @c @node CLooG Coding Standards
2298 @c @section CLooG Coding Standards
2301 @c %  ****************************** INSTALLING ********************************
2302 @node Installing
2303 @chapter Installing CLooG
2305 @menu
2306 * License::
2307 * Requirements::
2308 * Basic Installation::
2309 * Optional Features::
2310 * Uninstallation::
2311 @end menu
2313 @node License
2314 @section License
2315 First of all, it would be very kind to refer the following paper in any
2316 publication that result from the use of the CLooG software or its library,
2317 @pxref{Bas04} (a bibtex entry is provided behind the title page of this
2318 manual, along with copyright notice, and in the CLooG home
2319 @code{http://www.CLooG.org}.
2321 This program is free software; you can redistribute it and/or
2322 modify it under the terms of the GNU General Public License version 2
2323 as published by the Free Software Foundation.
2324 This program is distributed in the hope that it will be useful,
2325 but WITHOUT ANY WARRANTY; without even the implied warranty of
2326 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2327 GNU General Public License for more details.
2328 @code{http://www.gnu.org/copyleft/gpl.html}
2331 @node Requirements
2332 @section Requirements
2334 @menu
2335 * PolyLib::
2336 * GMP Library::
2337 @end menu
2340 @node PolyLib
2341 @subsection PolyLib (mandatory)
2342 To successfully install CLooG, the user need firstly to install PolyLib
2343 version 5.22.1 or above (default 64 bits version is satisfying
2344 as well as 32 bits or GMP multiple precision version).
2345 Polylib can be downloaded freely
2346 at @code{http://icps.u-strasbg.fr/PolyLib/} or
2347 @code{http://www.irisa.fr/polylib/}. Once downloaded and unpacked
2348 (e.g. using the @samp{tar -zxvf polylib-5.22.1.tar.gz} command),
2349 the user can compile
2350 it by typing the following commands on the PolyLib's root directory:
2352 @itemize @bullet
2353 @item @code{./configure}
2354 @item @code{make}
2355 @item And as root: @code{make install}
2356 @end itemize
2358 The PolyLib default installation is @code{/usr/local}. This directory may
2359 not be inside your library path. To fix the problem, the user should set
2360 @example
2361 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
2362 @end example
2363 @noindent if your shell is, e.g., bash or
2364 @example
2365 setenv LD_LIBRARY_PATH $LD_LIBRARY_PATH:/usr/local/lib
2366 @end example
2367 @noindent if your shell is, e.g., tcsh. Add the line to your .bashrc or .tcshrc (or
2368 whatever convenient file) to make this change permanent. Another solution
2369 is to ask PolyLib to install in the standard path by using the prefix
2370 option of the configure script:
2371 @samp{./configure --prefix=/usr}.
2373 CLooG makes intensive calls to polyhedral operations, and PolyLib
2374 functions do the job. Polylib is a free library written in C for the
2375 manipulation of polyhedra. The library is operating on objects like
2376 vectors, matrices, lattices, polyhedra, Z-polyhedra, unions of
2377 polyhedra and a lot of other intermediary structures. It provides
2378 functions for all the important operations on these structures. 
2380 @node GMP Library
2381 @subsection GMP Library (optional)
2383 To be able to deal with insanely large coefficient, the user will need to
2384 install the GNU Multiple Precision Library (GMP for short) version 4.1.4
2385 or above. It can be freely downloaded from @code{http://www.swox.com/gmp}.
2386 The user can compile it by typing the following commands on the GMP root
2387 directory:
2389 @itemize @bullet
2390 @item @code{./configure}
2391 @item @code{make}
2392 @item And as root: @code{make install}
2393 @end itemize
2395 The GMP default installation is @code{/usr/local}, the same method to
2396 fix a library path problem applies as with PolyLib (@pxref{PolyLib}).
2398 The PolyLib has to be built using the GMP library by specifying the option
2399 @samp{--with-libgmp=PATH_TO_GMP} to the PolyLib configure script
2400 (where @code{PATH_TO_GMP} is @code{/usr/local} if you did not change the GMP
2401 installation directory). Then you have to set the convenient CLooG configure
2402 script options to buid the GMP version (@pxref{Optional Features}).
2405 @node Basic Installation
2406 @section CLooG Basic Installation
2408 Once downloaded and unpacked
2409 (e.g. using the @samp{tar -zxvf cloog-@value{VERSION}.tar.gz} command),
2410 you can compile CLooG by typing the following commands on the CLooG's root
2411 directory:
2413 @itemize @bullet
2414 @item @code{./configure}
2415 @item @code{make}
2416 @item And as root: @code{make install}
2417 @end itemize
2419 Depending on the location of the PolyLib, you may need to set the
2420 option @code{--with-polylib} of the configure script
2421 (e.g. @samp{./configure --with-polylib=/usr/local} with a default PolyLib
2422 installation).
2424 The program binaries and object files can be removed from the
2425 source code directory by typing @code{make clean}. To also remove the
2426 files that the @code{configure} script created (so you can compile the
2427 package for a different kind of computer) type @code{make distclean}.
2429 Both the CLooG software and library have been successfully compiled
2430 on the following systems:
2431 @itemize @bullet
2432 @item PC's under Linux, with the @code{gcc} compiler,
2433 @item PC's under Windows (Cygwin), with the @code{gcc} compiler,
2434 @item Sparc and UltraSparc Stations, with the @code{gcc} compiler.
2435 @end itemize
2437 @node Optional Features 
2438 @section Optional Features  
2439 The @code{configure} shell script attempts to guess correct values for
2440 various system-dependent variables and user options used during compilation.
2441 It uses those values to create the @code{Makefile}. Various user options
2442 are provided by the CLooG's configure script. They are summarized in the
2443 following list and may be printed by typing @code{./configure --help} in the
2444 CLooG top-level directory.
2446 @itemize @bullet
2447 @item By default, the installation directory is @code{/usr/local}:
2448 @code{make install} will install the package's files in
2449 @code{/usr/local/bin}, @code{/usr/local/lib} and @code{/usr/local/include}.
2450 The user can specify an installation prefix other than @code{/usr/local} by
2451 giving @code{configure} the option @code{--prefix=PATH}.
2453 @item By default, @code{configure} will look for the PolyLib in standard
2454 locations. If necessary, the user can specify the PolyLib path by giving
2455 @code{configure} the option @code{--with-polylib=PATH}.
2457 @item By default, both CLooG software and library are compiled and installed.
2458 By giving  @code{configure} the option @code{--without-cloog} the user
2459 disable the compilation and installation of the CLooG software.
2460 By giving @code{configure} the option
2461 @code{--without-lib} the user disable the compilation and installation of the
2462 CLooG library.
2464 @item By default, CLooG is built in 64bits version if such version of the
2465 PolyLib is found by @code{configure}. If the only existing version of the
2466 PolyLib is the 32bits or if the user give to @code{configure} the option
2467 @code{--with-bits=32}, the 32bits version of CLooG will be compiled. In the
2468 same way, the option @code{--with-bits=gmp} have to be used to build
2469 the multiple precision version.
2471 @item By default, @code{configure} will look for the GMP library
2472 (necessary to build the multiple precision version) in standard
2473 locations. If necessary, the user can specify the GMP path by giving
2474 @code{configure} the option @code{--with-gmp=PATH}.
2475 @end itemize
2477 @node Uninstallation 
2478 @section Uninstallation  
2479 The user can easily remove the CLooG software and library from his system
2480 by typing (as root if necessary) from the CLooG top-level directory
2481 @code{make uninstall}.
2483 @c %  **************************** DOCUMENTATION ******************************
2484 @node Documentation
2485 @chapter Documentation
2486 The CLooG distribution provides several documentation sources. First, the
2487 source code itself is as documented as possible. The code comments use a
2488 Doxygen-compatible presentation (something similar to what JavaDoc does for
2489 JAVA). The user may install Doxygen
2490 (see @code{http://www.stack.nl/~dimitri/doxygen}) to automatically
2491 generate a technical documentation by typing @code{make doc} or
2492 @code{doxygen ./autoconf/Doxyfile} at the CLooG top-level directory after
2493 running the configure script (@pxref{Installing}). Doxygen will generate
2494 documentation sources (in HTML, LaTeX and man) in the @code{doc/source}
2495 directory of the CLooG distribution.
2497 The Texinfo sources of the present document are also provided in the @code{doc}
2498 directory. You can build it in either DVI format (by typing
2499 @code{texi2dvi cloog.texi}) or PDF format
2500 (by typing @code{texi2pdf cloog.texi}) or HTML format
2501 (by typing @code{makeinfo --html cloog.texi}, using @code{--no-split}
2502 option to generate a single HTML file) or info format
2503 (by typing @code{makeinfo cloog.texi}).
2505 @c %  ****************************** REFERENCES ********************************
2506 @node References
2507 @chapter References
2509 @itemize
2510 @item
2511 @anchor{Bas03a}[Bas03a] C. Bastoul, P. Feautrier. Improving data locality
2512 by chunking. CC'12 International Conference on Compiler Construction,
2513 LNCS 2622, pages 320-335, Warsaw, april 2003. 
2515 @item
2516 @anchor{Bas03b}[Bas03b] C. Bastoul. Efficient code generation for automatic
2517 parallelization and optimization. ISPDC'03 IEEE International Symposium on
2518 Parallel and Distributed Computing, pages 23-30, Ljubljana, october 2003. 
2520 @item
2521 @anchor{Bas04}[Bas04] C. Bastoul. Code Generation in the Polyhedral Model
2522 Is Easier Than You Think. PACT'13 IEEE International Conference on Parallel
2523 Architecture and Compilation Techniques, pages 7-16, Juan-les-Pins,
2524 september 2004.
2526 @item
2527 @anchor{Fea92}[Fea92] P. Feautrier Some efficient solutions to the affine
2528 scheduling problem, part II: multidimensional time.
2529 International Journal of Parallel Programming, 21(6):389--420, December 1992.
2531 @item
2532 @anchor{Gri04}[Gri04] M. Griebl. Automatic parallelization of loop programs
2533 for distributed memory architectures. Habilitation Thesis. Facult@"at f@"ur
2534 Mathematik und Informatik, Universit@"at Passau, 2004.
2535 @emph{http://www.infosun.fmi.uni-passau.de/cl/loopo/}
2537 @item
2538 @anchor{Qui00}[Qui00] F. Quiller@'e, S. Rajopadhye, and D. Wilde.
2539 Generation of efficient nested loops from polyhedra.
2540 International Journal of Parallel Programming, 28(5):469-498,
2541 october 2000.
2543 @item
2544 @anchor{Wil93}[Wil93] Doran K. Wilde.
2545 A library for doing polyhedral operations.
2546 Technical Report 785, IRISA, Rennes, France, 1993.
2548 @end itemize
2553 @c % /*************************************************************************
2554 @c %  *                       PART VI: END OF THE DOCUMENT                    *
2555 @c %  *************************************************************************/
2556 @c @unnumbered Index
2557      
2558 @c @printindex cp
2559      
2560 @bye