OpenScop 0.8.1

relation_list.c

Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                        relation_list.c                          **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 08/10/2010                     **
00008      **-----------------------------------------------------------------**
00009 
00010  
00011  *****************************************************************************
00012  * OpenScop: Structures and formats for polyhedral tools to talk together    *
00013  *****************************************************************************
00014  *    ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__,                *
00015  *    /   / /  //  //  //  // /   / /  //  //   / /  // /  /|,_,             *
00016  *   /   / /  //  //  //  // /   / /  //  //   / /  // /  / / /\             *
00017  *  |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/  \            *
00018  *  | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\  \ /\           *
00019  *  | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\          *
00020  *  | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \  \         *
00021  *  | P |n| l | = | s | t |=| = |d| = | = | = | |   |=| o | | \# \  \        *
00022  *  | H | | y |   | e | o | | = |l|   |   | = | |   | | G | |  \  \  \       *
00023  *  | I | |   |   | e |   | |   | |   |   |   | |   | |   | |   \  \  \      *
00024  *  | T | |   |   |   |   | |   | |   |   |   | |   | |   | |    \  \  \     *
00025  *  | E | |   |   |   |   | |   | |   |   |   | |   | |   | |     \  \  \    *
00026  *  | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | /      \* \  \   *
00027  *  | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/        \  \ /   *
00028  *  '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---'          '--'    *
00029  *                                                                           *
00030  * Copyright (C) 2008 University Paris-Sud 11 and INRIA                      *
00031  *                                                                           *
00032  * (3-clause BSD license)                                                    *
00033  * Redistribution and use in source  and binary forms, with or without       *
00034  * modification, are permitted provided that the following conditions        *
00035  * are met:                                                                  *
00036  *                                                                           *
00037  * 1. Redistributions of source code must retain the above copyright notice, *
00038  *    this list of conditions and the following disclaimer.                  *
00039  * 2. Redistributions in binary form must reproduce the above copyright      *
00040  *    notice, this list of conditions and the following disclaimer in the    *
00041  *    documentation and/or other materials provided with the distribution.   *
00042  * 3. The name of the author may not be used to endorse or promote products  *
00043  *    derived from this software without specific prior written permission.  *
00044  *                                                                           *
00045  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      *
00046  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
00047  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
00048  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          *
00049  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  *
00050  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
00051  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     *
00052  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       *
00053  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  *
00054  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         *
00055  *                                                                           *
00056  * OpenScop Library, a library to manipulate OpenScop formats and data       *
00057  * structures. Written by:                                                   *
00058  * Cedric Bastoul     <Cedric.Bastoul@u-psud.fr> and                         *
00059  * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr>                          *
00060  *                                                                           *
00061  *****************************************************************************/
00062 
00063 
00064 #include <stdlib.h>
00065 #include <stdio.h>
00066 #include <string.h>
00067 #include <ctype.h>
00068 
00069 #include <osl/macros.h>
00070 #include <osl/util.h>
00071 #include <osl/relation.h>
00072 #include <osl/relation_list.h>
00073 
00074 
00075 /*+***************************************************************************
00076  *                          Structure display function                       *
00077  *****************************************************************************/
00078 
00079 
00089 void osl_relation_list_idump(FILE * file, osl_relation_list_p l, int level) {
00090   int j, first = 1;
00091 
00092   // Go to the right level.
00093   for (j = 0; j < level; j++)
00094     fprintf(file,"|\t");
00095 
00096   if (l != NULL)
00097     fprintf(file, "+-- osl_relation_list_t\n");
00098   else
00099     fprintf(file, "+-- NULL relation list\n");
00100 
00101   while (l != NULL) {
00102     if (!first) {
00103       // Go to the right level.
00104       for (j = 0; j < level; j++)
00105         fprintf(file, "|\t");
00106       fprintf(file, "|   osl_relation_list_t\n");
00107     }
00108     else
00109       first = 0;
00110 
00111     // A blank line.
00112     for (j = 0; j <= level+1; j++)
00113       fprintf(file, "|\t");
00114     fprintf(file, "\n");
00115 
00116     // Print a relation.
00117     osl_relation_idump(file, l->elt, level+1);
00118 
00119     l = l->next;
00120 
00121     // Next line.
00122     if (l != NULL) {
00123       for (j = 0; j <= level; j++)
00124         fprintf(file, "|\t");
00125       fprintf(file, "V\n");
00126     }
00127   }
00128 
00129   // The last line.
00130   for (j = 0; j <= level; j++)
00131     fprintf(file, "|\t");
00132   fprintf(file, "\n");
00133 }
00134 
00135 
00143 void osl_relation_list_dump(FILE * file, osl_relation_list_p list) {
00144   osl_relation_list_idump(file, list, 0);
00145 }
00146 
00147 
00158 void osl_relation_list_pprint_elts(FILE * file, osl_relation_list_p list,
00159                                    osl_names_p names) {
00160   int i;
00161   osl_relation_list_p head = list;
00162 
00163   // Count the number of elements in the list with non-NULL content.
00164   i = osl_relation_list_count(list);
00165   
00166   // Print each element of the relation list.
00167   if (i > 0) {
00168     i = 0;
00169     while (head) {
00170       if (head->elt != NULL) {
00171         osl_relation_pprint(file, head->elt, names);
00172         if (head->next != NULL)
00173           fprintf(file, "\n");
00174         i++;
00175       }
00176       head = head->next;
00177     }
00178   }
00179   else {
00180     fprintf(file, "# NULL relation list\n");
00181   }
00182 }
00183 
00184 
00194 void osl_relation_list_pprint(FILE * file, osl_relation_list_p list,
00195                               osl_names_p names) {
00196   int i;
00197 
00198   // Count the number of elements in the list with non-NULL content.
00199   i = osl_relation_list_count(list);
00200   
00201   // Print it.
00202   if (i > 1)
00203     fprintf(file,"# List of %d elements\n%d\n", i, i);
00204   else
00205     fprintf(file,"# List of %d element \n%d\n", i, i);
00206 
00207   // Print each element of the relation list.
00208   osl_relation_list_pprint_elts(file, list, names);
00209 }
00210 
00211 
00220 void osl_relation_list_print(FILE * file, osl_relation_list_p list) {
00221 
00222   osl_relation_list_pprint(file, list, NULL);
00223 }
00224 
00225 /*****************************************************************************
00226  *                               Reading function                            *
00227  *****************************************************************************/
00228 
00229 
00238 osl_relation_list_p osl_relation_list_pread(FILE * file, int precision) {
00239   int i;
00240   osl_relation_list_p list;
00241   osl_relation_list_p res;
00242   int nb_mat;
00243 
00244   // Read the number of relations to read.
00245   nb_mat = osl_util_read_int(file, NULL); 
00246 
00247   if (nb_mat < 0)
00248     OSL_error("negative number of relations");
00249 
00250   // Allocate the header of the list and start reading each element.
00251   res = list = osl_relation_list_malloc();
00252   for (i = 0; i < nb_mat; ++i) {
00253     list->elt = osl_relation_pread(file, precision);
00254     if (i < nb_mat - 1)
00255       list->next = osl_relation_list_malloc();
00256     list = list->next;
00257   }
00258 
00259   return res;
00260 }
00261 
00262 
00270 osl_relation_list_p osl_relation_list_read(FILE * foo) {
00271   int precision = osl_util_get_precision();
00272   return osl_relation_list_pread(foo, precision);
00273 }
00274 
00275 
00276 /*+***************************************************************************
00277  *                    Memory allocation/deallocation function                *
00278  *****************************************************************************/
00279 
00280 
00289 osl_relation_list_p osl_relation_list_malloc() {
00290   osl_relation_list_p res;
00291   
00292   OSL_malloc(res, osl_relation_list_p, sizeof(osl_relation_list_t));
00293   res->elt  = NULL;
00294   res->next = NULL;
00295 
00296   return res;
00297 }
00298 
00299 
00300 
00307 void osl_relation_list_free(osl_relation_list_p list) {
00308   osl_relation_list_p tmp;
00309 
00310   if (list == NULL)
00311     return;
00312 
00313   while (list != NULL) {
00314     if (list->elt != NULL)
00315       osl_relation_free(list->elt);
00316     tmp = list->next;
00317     free(list);
00318     list = tmp;
00319   }
00320 }
00321 
00322 
00323 /*+***************************************************************************
00324  *                            Processing functions                           *
00325  *****************************************************************************/
00326 
00327 
00336 osl_relation_list_p osl_relation_list_node(osl_relation_p r) {
00337   osl_relation_list_p new = NULL;
00338   
00339   if (r != NULL) {
00340     new = osl_relation_list_malloc();
00341     new->elt = osl_relation_clone(r);
00342   }
00343   return new;
00344 }
00345 
00346 
00354 osl_relation_list_p osl_relation_list_clone(osl_relation_list_p list) {
00355   
00356   osl_relation_list_p clone = NULL, node, previous = NULL; 
00357   int first = 1;
00358 
00359   while (list != NULL) {
00360     node      = osl_relation_list_malloc();
00361     node->elt = osl_relation_clone(list->elt);
00362 
00363     if (first) {
00364       first = 0;
00365       clone = node;
00366       previous = node;
00367     }
00368     else {
00369       previous->next = node;
00370       previous = previous->next;
00371     }
00372 
00373     list = list->next;
00374   }
00375 
00376   return clone;
00377 }
00378 
00379 
00389 osl_relation_list_p osl_relation_list_concat(osl_relation_list_p l1,
00390                                              osl_relation_list_p l2) {
00391   osl_relation_list_p new, end;
00392 
00393   if (l1 == NULL)
00394     return osl_relation_list_clone(l2);
00395 
00396   if (l2 == NULL)
00397     return osl_relation_list_clone(l1);
00398 
00399   new = osl_relation_list_clone(l1);
00400   end = new;
00401   while (end->next != NULL)
00402     end = end->next;
00403   end->next = osl_relation_list_clone(l2);
00404 
00405   return new;
00406 }
00407 
00408 
00418 void osl_relation_list_concat_inplace(osl_relation_list_p *l1,
00419                                       osl_relation_list_p l2) {
00420   osl_relation_list_p temp;
00421 
00422   if (*l1 == NULL) {
00423     *l1 = l2;
00424     return;
00425   }
00426   
00427   temp = *l1;
00428   while (temp->next != NULL)
00429     temp = temp->next;
00430   temp->next = l2;
00431 }
00432 
00433 
00442 int osl_relation_list_equal(osl_relation_list_p l1, osl_relation_list_p l2) {
00443   while ((l1 != NULL) && (l2 != NULL)) {
00444     if (l1 == l2)
00445       return 1;
00446     
00447     if (!osl_relation_equal(l1->elt, l2->elt))
00448       return 0;
00449 
00450     l1 = l1->next;
00451     l2 = l2->next;
00452   }
00453 
00454   if (((l1 == NULL) && (l2 != NULL)) || ((l1 != NULL) && (l2 == NULL)))
00455     return 0;
00456   
00457   return 1;
00458 }
00459 
00460 
00476 int osl_relation_list_integrity_check(osl_relation_list_p list,
00477                                       int type,
00478                                       int expected_nb_output_dims,
00479                                       int expected_nb_input_dims,
00480                                       int expected_nb_parameters) {
00481   while (list != NULL) {
00482     // Check the access function.
00483     if (!osl_relation_integrity_check(list->elt,
00484                                       type,
00485                                       expected_nb_output_dims,
00486                                       expected_nb_input_dims,
00487                                       expected_nb_parameters)) {
00488       return 0;
00489     }
00490 
00491     list = list->next;
00492   }
00493 
00494   return 1;
00495 }
00496 
00497 
00505 void osl_relation_list_set_type(osl_relation_list_p list, int type) {
00506 
00507   while (list != NULL) {
00508     if (list->elt != NULL) {
00509       list->elt->type = type;
00510     }
00511     list = list->next;
00512   }
00513 }
00514 
00515 
00525 osl_relation_list_p osl_relation_list_filter(osl_relation_list_p list,
00526                                              int type) {
00527 
00528   osl_relation_list_p copy = osl_relation_list_clone(list);
00529   osl_relation_list_p filtered = NULL;
00530   osl_relation_list_p previous = NULL;
00531   osl_relation_list_p trash;
00532   int first = 1;
00533 
00534   while (copy != NULL) {
00535     if ((copy->elt != NULL) &&
00536         (((type == OSL_TYPE_ACCESS) &&
00537           (osl_relation_is_access(copy->elt))) ||
00538          ((type != OSL_TYPE_ACCESS) &&
00539           (type == copy->elt->type)))) {
00540       if (first) {
00541         filtered = copy;
00542         first = 0;
00543       }
00544       
00545       previous = copy;
00546       copy = copy->next;
00547     }
00548     else {
00549       trash = copy;
00550       if (!first)
00551         previous->next = copy->next;
00552       copy = copy->next;
00553       trash->next = NULL;
00554       osl_relation_list_free(trash);
00555     }
00556   }
00557 
00558   return filtered;
00559 }
00560 
00561 
00569 int osl_relation_list_count(osl_relation_list_p list) {
00570   int i = 0;
00571   
00572   while (list != NULL) {
00573     if (list->elt != NULL)
00574       i++;
00575     list = list->next;
00576   }
00577 
00578   return i;
00579 }
00580   
00581 
00600 void osl_relation_list_get_attributes(osl_relation_list_p list,
00601                                       int * nb_parameters,
00602                                       int * nb_iterators,
00603                                       int * nb_scattdims,
00604                                       int * nb_localdims,
00605                                       int * array_id) {
00606   int local_nb_parameters = OSL_UNDEFINED;
00607   int local_nb_iterators  = OSL_UNDEFINED;
00608   int local_nb_scattdims  = OSL_UNDEFINED;
00609   int local_nb_localdims  = OSL_UNDEFINED;
00610   int local_array_id      = OSL_UNDEFINED;
00611 
00612   while (list != NULL) {
00613     osl_relation_get_attributes(list->elt,
00614                                 &local_nb_parameters,
00615                                 &local_nb_iterators,
00616                                 &local_nb_scattdims,
00617                                 &local_nb_localdims,
00618                                 &local_array_id);
00619     // Update.
00620     *nb_parameters = OSL_max(*nb_parameters, local_nb_parameters);
00621     *nb_iterators  = OSL_max(*nb_iterators,  local_nb_iterators);
00622     *nb_scattdims  = OSL_max(*nb_scattdims,  local_nb_scattdims);
00623     *nb_localdims  = OSL_max(*nb_localdims,  local_nb_localdims);
00624     *array_id      = OSL_max(*array_id,      local_array_id);
00625     list = list->next;
00626   }
00627 }
00628