OpenScop  0.9.0
pluto_unroll.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** extensions/pluto_unroll.c **
6  **-----------------------------------------------------------------**
7  ** First version: 26/06/2014 **
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) 2014 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 <string.h>
66 
67 #include "../include/osl/extensions/pluto_unroll.h"
68 #include "../include/osl/macros.h"
69 
70 
71 /*+***************************************************************************
72  * Structure display function *
73  *****************************************************************************/
74 
75 
87 void osl_pluto_unroll_idump(FILE * file,
88  osl_pluto_unroll_p pluto_unroll, int level) {
89  int j, first = 1, number=1;
90 
91  // Go to the right level.
92  for (j = 0; j < level; j++)
93  fprintf(file, "|\t");
94 
95  if (pluto_unroll != NULL)
96  fprintf(file, "+-- osl_pluto_unroll_t\n");
97  else
98  fprintf(file, "+-- NULL pluto_unroll\n");
99 
100  while (pluto_unroll != NULL) {
101  // Go to the right level.
102  if (!first) {
103  // Go to the right level.
104  for (j = 0; j < level; j++)
105  fprintf(file, "|\t");
106 
107  fprintf(file, "| osl_pluto_unroll_t (node %d)\n", number);
108  } else {
109  first = 0;
110  }
111 
112  // A blank line.
113  for (j = 0; j <= level+1; j++)
114  fprintf(file, "|\t");
115  fprintf(file, "\n");
116 
117  // Display the names of iterators.
118  for (j = 0; j <= level; j++)
119  fprintf(file, "|\t");
120  fprintf(file, "+--iterator: %s\n", pluto_unroll->iter);
121 
122  // Display the names of iterators.
123  for (j = 0; j <= level; j++)
124  fprintf(file, "|\t");
125  fprintf(file, "+--jam: %i\n", pluto_unroll->jam);
126 
127  // Display the names of iterators.
128  for (j = 0; j <= level; j++)
129  fprintf(file, "|\t");
130  fprintf(file, "+--factor: %i\n", pluto_unroll->factor);
131 
132  pluto_unroll = pluto_unroll->next;
133 
134  // Next line.
135  if (pluto_unroll != NULL) {
136  for (j = 0; j <= level; j++)
137  fprintf(file, "|\t");
138  fprintf(file, "V\n");
139  }
140  }
141 
142  // The last line.
143  for (j = 0; j <= level; j++)
144  fprintf(file, "|\t");
145  fprintf(file, "\n");
146 }
147 
148 
157 void osl_pluto_unroll_dump(FILE * file, osl_pluto_unroll_p pluto_unroll) {
158  osl_pluto_unroll_idump(file, pluto_unroll, 0);
159 }
160 
161 
172 
173  char* string = NULL;
174  char buffer[OSL_MAX_STRING] = { 0 };
175 
176  strcat(buffer, "# Pluto unroll\n");
177  if (pluto_unroll != NULL) {
178  strcat(buffer, "1\n");
179  } else {
180  strcat(buffer, "0\n");
181  }
182 
183  while (pluto_unroll != NULL)
184  {
185  strcat(buffer, "# Iterator name\n");
186  sprintf(buffer + strlen(buffer), "%s\n", pluto_unroll->iter);
187 
188  strcat(buffer, "# Jam\n");
189  sprintf(buffer + strlen(buffer), "%i\n", pluto_unroll->jam);
190 
191  strcat(buffer, "# Factor\n");
192  sprintf(buffer + strlen(buffer), "%i\n", pluto_unroll->factor);
193 
194  pluto_unroll = pluto_unroll->next;
195 
196  strcat(buffer, "# Next\n");
197  if (pluto_unroll != NULL) {
198  strcat(buffer, "1\n");
199  } else {
200  strcat(buffer, "0\n");
201  }
202  }
203 
204  OSL_realloc(string, char*, (strlen(buffer) + 1) * sizeof(char));
205  strcpy(string, buffer);
206 
207  return string;
208 }
209 
210 
211 /*****************************************************************************
212  * Reading function *
213  *****************************************************************************/
214 
215 
230  osl_pluto_unroll_p p = NULL;
231  if (osl_util_read_int(NULL, input) == 1) { p = osl_pluto_unroll_malloc(); }
232  osl_pluto_unroll_p r = p;
233 
234  while (p != NULL)
235  {
236  // iter
237  p->iter = osl_util_read_line(NULL, input);
238  // jam
239  p->jam = osl_util_read_int(NULL, input);
240  // factor
241  p->factor = osl_util_read_int(NULL, input);
242  // Next
243  if (osl_util_read_int(NULL, input) == 1) {
245  }
246  p = p->next;
247  }
248 
249  return r;
250 }
251 
252 
253 /*+***************************************************************************
254  * Memory allocation/deallocation function *
255  *****************************************************************************/
256 
257 
268  osl_pluto_unroll_p pluto_unroll = NULL;
269 
270  OSL_malloc(pluto_unroll, osl_pluto_unroll_p, sizeof(osl_pluto_unroll_t));
271  pluto_unroll->iter = NULL;
272  pluto_unroll->jam = false;
273  pluto_unroll->factor = 0;
274  pluto_unroll->next = NULL;
275 
276  return pluto_unroll;
277 }
278 
279 
288  if (pluto_unroll != NULL) {
289  osl_pluto_unroll_free(pluto_unroll->next);
290 
291  free(pluto_unroll->iter);
292  free(pluto_unroll); pluto_unroll = NULL;
293  }
294 }
295 
296 
297 /*+***************************************************************************
298  * Processing functions *
299  *****************************************************************************/
300 
311  char const * const iterator_name,
312  bool const jam, int const factor) {
313  if (pluto_unroll != NULL) {
314  // iter
315  if (iterator_name != NULL) {
316  OSL_realloc(pluto_unroll->iter,
317  char*, (strlen(iterator_name) + 1) * sizeof(char));
318  strcpy(pluto_unroll->iter, iterator_name);
319  }
320  // jam
321  pluto_unroll->jam = jam;
322  // factor
323  pluto_unroll->factor = factor;
324  } else {
325  OSL_warning("osl_pluto_unroll_fill can not fill NULL osl_pluto_unroll_p");
326  }
327 }
328 
340  osl_pluto_unroll_p p = NULL;
341  if (pluto_unroll != NULL) { p = osl_pluto_unroll_malloc(); }
342  osl_pluto_unroll_p r = p;
343 
344  while (pluto_unroll != NULL)
345  {
346  osl_pluto_unroll_fill(p, pluto_unroll->iter,
347  pluto_unroll->jam,
348  pluto_unroll->factor);
349 
350  pluto_unroll = pluto_unroll->next;
351 
352  if (pluto_unroll != NULL) { p->next = osl_pluto_unroll_malloc(); }
353  p = p->next;
354  }
355 
356  return r;
357 }
358 
372 
373  if (a == b) { return 1; }
374 
375  if (a == NULL && b != NULL) { return 0; }
376  if (a != NULL && b == NULL) { return 0; }
377 
378  while (a != NULL) {
379  // Iter
380  if (a->iter == NULL && b->iter != NULL) { return 0; }
381  if (a->iter != NULL && b->iter == NULL) { return 0; }
382  if (a->iter != NULL) {
383  if (strcmp(a->iter, b->iter) != 0) { return 0; }
384 
385  }
386  // Jam
387  if (a->jam != b->jam) { return 0; }
388  // Factor
389  if (a->factor != b->factor) { return 0; }
390  // Next
391  if (a->next == b->next) { return 1; }
392  if (a->next == NULL && b->next != NULL) { return 0; }
393  if (a->next != NULL && b->next == NULL) { return 0; }
394  a = a->next;
395  b = b->next;
396  }
397 
398  return 1;
399 }
400 
401 
411 
412  OSL_strdup(interface->URI, OSL_URI_PLUTO_UNROLL);
413  interface->idump = (osl_idump_f)osl_pluto_unroll_idump;
414  interface->sprint = (osl_sprint_f)osl_pluto_unroll_sprint;
415  interface->sread = (osl_sread_f)osl_pluto_unroll_sread;
416  interface->malloc = (osl_malloc_f)osl_pluto_unroll_malloc;
417  interface->free = (osl_free_f)osl_pluto_unroll_free;
418  interface->clone = (osl_clone_f)osl_pluto_unroll_clone;
419  interface->equal = (osl_equal_f)osl_pluto_unroll_equal;
420 
421  return interface;
422 }
osl_pluto_unroll_p osl_pluto_unroll_malloc()
Definition: pluto_unroll.c:267
void *(* osl_clone_f)(void *)
Definition: interface.h:80
void osl_pluto_unroll_free(osl_pluto_unroll_p pluto_unroll)
Definition: pluto_unroll.c:287
osl_pluto_unroll_p osl_pluto_unroll_clone(osl_pluto_unroll_p pluto_unroll)
Definition: pluto_unroll.c:339
osl_interface_p osl_interface_malloc()
Definition: interface.c:212
void osl_pluto_unroll_fill(osl_pluto_unroll_p pluto_unroll, char const *const iterator_name, bool const jam, int const factor)
Definition: pluto_unroll.c:310
void *(* osl_sread_f)(char **)
Definition: interface.h:77
unsigned int factor
unroll factor
Definition: pluto_unroll.h:91
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
bool jam
true if jam, false otherwise
Definition: pluto_unroll.h:90
void osl_pluto_unroll_dump(FILE *file, osl_pluto_unroll_p pluto_unroll)
Definition: pluto_unroll.c:157
char * iter
\0 terminated iterator name
Definition: pluto_unroll.h:89
osl_pluto_unroll_p osl_pluto_unroll_sread(char **input)
Definition: pluto_unroll.c:229
int osl_util_read_int(FILE *file, char **str)
Definition: util.c:140
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
struct osl_pluto_unroll * next
next { iter, jam, factor }
Definition: pluto_unroll.h:92
osl_interface_p osl_pluto_unroll_interface()
Definition: pluto_unroll.c:409
void *(* osl_malloc_f)()
Definition: interface.h:78
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
void osl_pluto_unroll_idump(FILE *file, osl_pluto_unroll_p pluto_unroll, int level)
Definition: pluto_unroll.c:87
char * osl_pluto_unroll_sprint(osl_pluto_unroll_p pluto_unroll)
Definition: pluto_unroll.c:171
int osl_pluto_unroll_equal(osl_pluto_unroll_p a, osl_pluto_unroll_p b)
Definition: pluto_unroll.c:371
char * osl_util_read_line(FILE *file, char **str)
Definition: util.c:226
void(* osl_free_f)(void *)
Definition: interface.h:79