OpenScop  0.9.0
relation_list.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** relation_list.c **
6  **-----------------------------------------------------------------**
7  ** First version: 08/10/2010 **
8  **-----------------------------------------------------------------**
9 
10 
11  *****************************************************************************
12  * OpenScop: Structures and formats for polyhedral tools to talk together *
13  *****************************************************************************
14  * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15  * / / / // // // // / / / // // / / // / /|,_, *
16  * / / / // // // // / / / // // / / // / / / /\ *
17  * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18  * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19  * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20  * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21  * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22  * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23  * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24  * | T | | | | | | | | | | | | | | | | | \ \ \ *
25  * | E | | | | | | | | | | | | | | | | | \ \ \ *
26  * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27  * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28  * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29  * *
30  * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31  * *
32  * (3-clause BSD license) *
33  * Redistribution and use in source and binary forms, with or without *
34  * modification, are permitted provided that the following conditions *
35  * are met: *
36  * *
37  * 1. Redistributions of source code must retain the above copyright notice, *
38  * this list of conditions and the following disclaimer. *
39  * 2. Redistributions in binary form must reproduce the above copyright *
40  * notice, this list of conditions and the following disclaimer in the *
41  * documentation and/or other materials provided with the distribution. *
42  * 3. The name of the author may not be used to endorse or promote products *
43  * derived from this software without specific prior written permission. *
44  * *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55  * *
56  * OpenScop Library, a library to manipulate OpenScop formats and data *
57  * structures. Written by: *
58  * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59  * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60  * *
61  *****************************************************************************/
62 
63 
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <ctype.h>
68 
69 #include <osl/macros.h>
70 #include <osl/util.h>
71 #include <osl/relation.h>
72 #include <osl/relation_list.h>
73 
74 
75 /*+***************************************************************************
76  * Structure display function *
77  *****************************************************************************/
78 
79 
89 void osl_relation_list_idump(FILE * file, osl_relation_list_p l, int level) {
90  int j, first = 1;
91 
92  // Go to the right level.
93  for (j = 0; j < level; j++)
94  fprintf(file,"|\t");
95 
96  if (l != NULL)
97  fprintf(file, "+-- osl_relation_list_t\n");
98  else
99  fprintf(file, "+-- NULL relation list\n");
100 
101  while (l != NULL) {
102  if (!first) {
103  // Go to the right level.
104  for (j = 0; j < level; j++)
105  fprintf(file, "|\t");
106  fprintf(file, "| osl_relation_list_t\n");
107  }
108  else
109  first = 0;
110 
111  // A blank line.
112  for (j = 0; j <= level+1; j++)
113  fprintf(file, "|\t");
114  fprintf(file, "\n");
115 
116  // Print a relation.
117  osl_relation_idump(file, l->elt, level+1);
118 
119  l = l->next;
120 
121  // Next line.
122  if (l != NULL) {
123  for (j = 0; j <= level; j++)
124  fprintf(file, "|\t");
125  fprintf(file, "V\n");
126  }
127  }
128 
129  // The last line.
130  for (j = 0; j <= level; j++)
131  fprintf(file, "|\t");
132  fprintf(file, "\n");
133 }
134 
135 
144  osl_relation_list_idump(file, list, 0);
145 }
146 
147 
159  osl_names_p names) {
160  size_t i;
161  osl_relation_list_p head = list;
162 
163  // Count the number of elements in the list with non-NULL content.
164  i = osl_relation_list_count(list);
165 
166  // Print each element of the relation list.
167  if (i > 0) {
168  i = 0;
169  while (head) {
170  if (head->elt != NULL) {
171  osl_relation_pprint(file, head->elt, names);
172  if (head->next != NULL)
173  fprintf(file, "\n");
174  i++;
175  }
176  head = head->next;
177  }
178  }
179  else {
180  fprintf(file, "# NULL relation list\n");
181  }
182 }
183 
184 
197  osl_relation_list_p list, osl_names_p names, int add_fakeiter) {
198  size_t i;
199  int nb_rows_read = 0, nb_columns_read = 0;
200  int nb_rows_write = 0, nb_columns_write = 0;
201  int nb_rows_may_write = 0, nb_columns_may_write = 0;
202  osl_relation_list_p head ;
203 
204  // Count the number of elements in the list with non-NULL content.
205  i = osl_relation_list_count(list);
206 
207  // Print each element of the relation list.
208  if (i > 0) {
209 
210  // Read/Write arrays size
211  head = list;
212  while (head) {
213  if (head->elt != NULL) {
214  if (head->elt->type == OSL_TYPE_READ) {
215  if (head->elt->nb_rows == 1)
216  nb_rows_read++;
217  else
218  nb_rows_read += head->elt->nb_rows - 1; // remove the 'Arr'
219 
220  nb_columns_read = head->elt->nb_columns - head->elt->nb_output_dims;
221 
222  } else if (head->elt->type == OSL_TYPE_WRITE) {
223  if (head->elt->nb_rows == 1)
224  nb_rows_write++;
225  else
226  nb_rows_write += head->elt->nb_rows - 1; // remove the 'Arr'
227 
228  nb_columns_write = head->elt->nb_columns - head->elt->nb_output_dims;
229 
230  } else if (head->elt->type == OSL_TYPE_MAY_WRITE) {
231  if (head->elt->nb_rows == 1)
232  nb_rows_may_write++;
233  else
234  nb_rows_may_write += head->elt->nb_rows - 1; // remove the 'Arr'
235 
236  nb_columns_may_write = head->elt->nb_columns -
237  head->elt->nb_output_dims;
238  }
239  }
240  head = head->next;
241  }
242 
243  if (add_fakeiter) {
244  nb_columns_read++;
245  nb_columns_write++;
246  nb_columns_may_write++;
247  }
248 
249  fprintf(file, "# Read access informations\n%d %d\n",
250  nb_rows_read, nb_columns_read);
251  head = list;
252  while (head) {
253  if (head->elt != NULL && head->elt->type == OSL_TYPE_READ) {
254  osl_relation_pprint_scoplib(file, head->elt, names, 0, add_fakeiter);
255  }
256  head = head->next;
257  }
258 
259  fprintf(file, "# Write access informations\n%d %d\n",
260  nb_rows_write, nb_columns_write);
261  head = list;
262  while (head) {
263  if (head->elt != NULL && head->elt->type == OSL_TYPE_WRITE) {
264  osl_relation_pprint_scoplib(file, head->elt, names, 0, add_fakeiter);
265  }
266  head = head->next;
267  }
268 
269  if (nb_rows_may_write > 0) {
270  fprintf(file, "# May Write access informations\n%d %d\n",
271  nb_rows_may_write, nb_columns_may_write);
272  head = list;
273  while (head) {
274  if (head->elt != NULL && head->elt->type == OSL_TYPE_MAY_WRITE) {
275  osl_relation_pprint_scoplib(file, head->elt, names, 0, add_fakeiter);
276  }
277  head = head->next;
278  }
279  }
280  }
281  else {
282  fprintf(file, "# NULL relation list\n");
283  }
284 }
285 
286 
297  osl_names_p names) {
298  size_t i;
299 
300  // Count the number of elements in the list with non-NULL content.
301  i = osl_relation_list_count(list);
302 
303  // Print it.
304  if (i > 1)
305  fprintf(file,"# List of %lu elements\n%lu\n", i, i);
306  else
307  fprintf(file,"# List of %lu element \n%lu\n", i, i);
308 
309  // Print each element of the relation list.
310  osl_relation_list_pprint_elts(file, list, names);
311 }
312 
313 
323 
324  osl_relation_list_pprint(file, list, NULL);
325 }
326 
327 /*****************************************************************************
328  * Reading function *
329  *****************************************************************************/
330 
331 
341  int i;
342  osl_relation_list_p list;
344  int nb_mat;
345 
346  // Read the number of relations to read.
347  nb_mat = osl_util_read_int(file, NULL);
348 
349  if (nb_mat < 0)
350  OSL_error("negative number of relations");
351 
352  // Allocate the header of the list and start reading each element.
353  res = list = osl_relation_list_malloc();
354  for (i = 0; i < nb_mat; ++i) {
355  list->elt = osl_relation_pread(file, precision);
356  if (i < nb_mat - 1)
357  list->next = osl_relation_list_malloc();
358  list = list->next;
359  }
360 
361  return res;
362 }
363 
364 
374  return osl_relation_list_pread(foo, precision);
375 }
376 
377 
378 /*+***************************************************************************
379  * Memory allocation/deallocation function *
380  *****************************************************************************/
381 
382 
393 
394  OSL_malloc(res, osl_relation_list_p, sizeof(osl_relation_list_t));
395  res->elt = NULL;
396  res->next = NULL;
397 
398  return res;
399 }
400 
401 
402 
411 
412  if (list == NULL)
413  return;
414 
415  while (list != NULL) {
416  if (list->elt != NULL)
417  osl_relation_free(list->elt);
418  tmp = list->next;
419  free(list);
420  list = tmp;
421  }
422 }
423 
424 
425 /*+***************************************************************************
426  * Processing functions *
427  *****************************************************************************/
428 
429 
439  osl_relation_list_p new = NULL;
440 
441  if (r != NULL) {
442  new = osl_relation_list_malloc();
443  new->elt = osl_relation_clone(r);
444  }
445  return new;
446 }
447 
448 
457 
458  osl_relation_list_p clone = NULL, node, previous = NULL;
459  int first = 1;
460 
461  while (list != NULL) {
462  node = osl_relation_list_malloc();
463  node->elt = osl_relation_clone(list->elt);
464 
465  if (first) {
466  first = 0;
467  clone = node;
468  previous = node;
469  }
470  else {
471  previous->next = node;
472  previous = previous->next;
473  }
474 
475  list = list->next;
476  }
477 
478  return clone;
479 }
480 
481 
492  osl_relation_list_p l2) {
493  osl_relation_list_p new, end;
494 
495  if (l1 == NULL)
496  return osl_relation_list_clone(l2);
497 
498  if (l2 == NULL)
499  return osl_relation_list_clone(l1);
500 
501  new = osl_relation_list_clone(l1);
502  end = new;
503  while (end->next != NULL)
504  end = end->next;
505  end->next = osl_relation_list_clone(l2);
506 
507  return new;
508 }
509 
510 
521  while (*l1 != NULL)
522  l1 = &((*l1)->next);
523 
524  *l1 = l2;
525 }
526 
527 
537  osl_relation_list_p node) {
538  if (node != NULL) {
539  node->next = *head;
540  *head = node;
541  }
542 }
543 
544 
555  osl_relation_list_p top = NULL;
556 
557  if (*head != NULL) {
558  top = *head;
559  *head = (*head)->next;
560  top->next = NULL;
561  }
562 
563  return top;
564 }
565 
566 
578  osl_relation_list_push(head, top);
579 }
580 
581 
594 }
595 
596 
606 
607  while (*head != NULL)
609 }
610 
611 
621  while ((l1 != NULL) && (l2 != NULL)) {
622  if (l1 == l2)
623  return 1;
624 
625  if (!osl_relation_equal(l1->elt, l2->elt))
626  return 0;
627 
628  l1 = l1->next;
629  l2 = l2->next;
630  }
631 
632  if (((l1 == NULL) && (l2 != NULL)) || ((l1 != NULL) && (l2 == NULL)))
633  return 0;
634 
635  return 1;
636 }
637 
638 
655  int type,
656  int expected_nb_output_dims,
657  int expected_nb_input_dims,
658  int expected_nb_parameters) {
659  while (list != NULL) {
660  // Check the access function.
662  type,
663  expected_nb_output_dims,
664  expected_nb_input_dims,
665  expected_nb_parameters)) {
666  return 0;
667  }
668 
669  list = list->next;
670  }
671 
672  return 1;
673 }
674 
675 
684 
685  while (list != NULL) {
686  if (list->elt != NULL) {
687  list->elt->type = type;
688  }
689  list = list->next;
690  }
691 }
692 
693 
704  int type) {
705 
707  osl_relation_list_p filtered = NULL;
708  osl_relation_list_p previous = NULL;
709  osl_relation_list_p trash;
710  int first = 1;
711 
712  while (copy != NULL) {
713  if ((copy->elt != NULL) &&
714  (((type == OSL_TYPE_ACCESS) &&
715  (osl_relation_is_access(copy->elt))) ||
716  ((type != OSL_TYPE_ACCESS) &&
717  (type == copy->elt->type)))) {
718  if (first) {
719  filtered = copy;
720  first = 0;
721  }
722 
723  previous = copy;
724  copy = copy->next;
725  }
726  else {
727  trash = copy;
728  if (!first)
729  previous->next = copy->next;
730  copy = copy->next;
731  trash->next = NULL;
732  osl_relation_list_free(trash);
733  }
734  }
735 
736  return filtered;
737 }
738 
739 
748  size_t i = 0;
749 
750  while (list != NULL) {
751  if (list->elt != NULL)
752  i++;
753  list = list->next;
754  }
755 
756  return i;
757 }
758 
759 
779  int * nb_parameters,
780  int * nb_iterators,
781  int * nb_scattdims,
782  int * nb_localdims,
783  int * array_id) {
784  int local_nb_parameters = OSL_UNDEFINED;
785  int local_nb_iterators = OSL_UNDEFINED;
786  int local_nb_scattdims = OSL_UNDEFINED;
787  int local_nb_localdims = OSL_UNDEFINED;
788  int local_array_id = OSL_UNDEFINED;
789 
790  while (list != NULL) {
792  &local_nb_parameters,
793  &local_nb_iterators,
794  &local_nb_scattdims,
795  &local_nb_localdims,
796  &local_array_id);
797  // Update.
798  *nb_parameters = OSL_max(*nb_parameters, local_nb_parameters);
799  *nb_iterators = OSL_max(*nb_iterators, local_nb_iterators);
800  *nb_scattdims = OSL_max(*nb_scattdims, local_nb_scattdims);
801  *nb_localdims = OSL_max(*nb_localdims, local_nb_localdims);
802  *array_id = OSL_max(*array_id, local_array_id);
803  list = list->next;
804  }
805 }
806 
void osl_relation_free(osl_relation_p relation)
Definition: relation.c:1731
void osl_relation_list_pprint_elts(FILE *file, osl_relation_list_p list, osl_names_p names)
osl_relation_list_p osl_relation_list_read(FILE *foo)
void osl_relation_list_push(osl_relation_list_p *head, osl_relation_list_p node)
int nb_columns
Definition: relation.h:108
void osl_relation_list_get_attributes(osl_relation_list_p list, int *nb_parameters, int *nb_iterators, int *nb_scattdims, int *nb_localdims, int *array_id)
void osl_relation_list_print(FILE *file, osl_relation_list_p list)
osl_relation_list_p osl_relation_list_pop(osl_relation_list_p *head)
int nb_parameters
Definition: relation.h:113
int nb_output_dims
Definition: relation.h:109
osl_relation_list_p osl_relation_list_filter(osl_relation_list_p list, int type)
void osl_relation_list_free(osl_relation_list_p list)
void osl_relation_pprint(FILE *file, osl_relation_p relation, osl_names_p names)
Definition: relation.c:1242
osl_relation_list_p osl_relation_list_node(osl_relation_p r)
int osl_relation_is_access(osl_relation_p relation)
Definition: relation.c:2820
int osl_relation_equal(osl_relation_p r1, osl_relation_p r2)
Definition: relation.c:2435
void osl_relation_idump(FILE *file, osl_relation_p relation, int level)
Definition: relation.c:164
osl_relation_list_p osl_relation_list_malloc()
int osl_util_get_precision()
Definition: util.c:518
void osl_relation_list_dup(osl_relation_list_p *head)
int precision
Definition: relation.h:106
void osl_relation_list_add(osl_relation_list_p *l1, osl_relation_list_p l2)
void osl_relation_pprint_scoplib(FILE *file, osl_relation_p relation, osl_names_p names, int print_nth_part, int add_fakeiter)
Definition: relation.c:1260
void osl_relation_list_destroy(osl_relation_list_p *head)
osl_relation_list_p osl_relation_list_clone(osl_relation_list_p list)
void osl_relation_get_attributes(osl_relation_p relation, int *nb_parameters, int *nb_iterators, int *nb_scattdims, int *nb_localdims, int *array_id)
Definition: relation.c:2853
void osl_relation_list_pprint_access_array_scoplib(FILE *file, osl_relation_list_p list, osl_names_p names, int add_fakeiter)
int osl_relation_integrity_check(osl_relation_p relation, int expected_type, int expected_nb_output_dims, int expected_nb_input_dims, int expected_nb_parameters)
Definition: relation.c:2540
int osl_relation_list_equal(osl_relation_list_p l1, osl_relation_list_p l2)
osl_relation_list_p osl_relation_list_concat(osl_relation_list_p l1, osl_relation_list_p l2)
int osl_util_read_int(FILE *file, char **str)
Definition: util.c:140
void osl_relation_list_set_type(osl_relation_list_p list, int type)
osl_relation_p osl_relation_clone(osl_relation_p relation)
Definition: relation.c:1863
void osl_relation_list_drop(osl_relation_list_p *head)
int osl_relation_list_integrity_check(osl_relation_list_p list, int type, int expected_nb_output_dims, int expected_nb_input_dims, int expected_nb_parameters)
osl_relation_list_p osl_relation_list_pread(FILE *file, int precision)
osl_relation_p elt
Definition: relation_list.h:82
void osl_relation_list_dump(FILE *file, osl_relation_list_p list)
void osl_relation_list_idump(FILE *file, osl_relation_list_p l, int level)
Definition: relation_list.c:89
osl_relation_p osl_relation_pread(FILE *foo, int precision)
Definition: relation.c:1382
struct osl_relation_list * next
Definition: relation_list.h:83
size_t osl_relation_list_count(osl_relation_list_p list)
void osl_relation_list_pprint(FILE *file, osl_relation_list_p list, osl_names_p names)