OpenScop  0.9.0
pluto_unroll.c
Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                 extensions/pluto_unroll.c                       **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 26/06/2014                     **
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) 2014 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 <string.h>
00066 
00067 #include "../include/osl/extensions/pluto_unroll.h"
00068 #include "../include/osl/macros.h"
00069 
00070 
00071 /*+***************************************************************************
00072  *                          Structure display function                       *
00073  *****************************************************************************/
00074 
00075 
00087 void osl_pluto_unroll_idump(FILE * file,
00088                             osl_pluto_unroll_p pluto_unroll, int level) {
00089   int j, first = 1, number=1;
00090 
00091   // Go to the right level.
00092   for (j = 0; j < level; j++)
00093     fprintf(file, "|\t");
00094 
00095   if (pluto_unroll != NULL)
00096     fprintf(file, "+-- osl_pluto_unroll_t\n");
00097   else
00098     fprintf(file, "+-- NULL pluto_unroll\n");
00099 
00100   while (pluto_unroll != NULL) {
00101     // Go to the right level.
00102     if (!first) {
00103       // Go to the right level.
00104       for (j = 0; j < level; j++)
00105         fprintf(file, "|\t");
00106 
00107       fprintf(file, "|   osl_pluto_unroll_t (node %d)\n", number);
00108     } else {
00109       first = 0;
00110     }
00111 
00112     // A blank line.
00113     for (j = 0; j <= level+1; j++)
00114       fprintf(file, "|\t");
00115     fprintf(file, "\n");
00116   
00117     // Display the names of iterators.
00118     for (j = 0; j <= level; j++)
00119       fprintf(file, "|\t");
00120     fprintf(file, "+--iterator: %s\n", pluto_unroll->iter);
00121   
00122     // Display the names of iterators.
00123     for (j = 0; j <= level; j++)
00124       fprintf(file, "|\t");
00125     fprintf(file, "+--jam: %i\n", pluto_unroll->jam);
00126   
00127     // Display the names of iterators.
00128     for (j = 0; j <= level; j++)
00129       fprintf(file, "|\t");
00130     fprintf(file, "+--factor: %i\n", pluto_unroll->factor);
00131 
00132     pluto_unroll = pluto_unroll->next;
00133 
00134     // Next line.
00135     if (pluto_unroll != NULL) {
00136       for (j = 0; j <= level; j++)
00137         fprintf(file, "|\t");
00138       fprintf(file, "V\n");
00139     }
00140   }
00141 
00142   // The last line.
00143   for (j = 0; j <= level; j++)
00144     fprintf(file, "|\t");
00145   fprintf(file, "\n");
00146 }
00147 
00148 
00157 void osl_pluto_unroll_dump(FILE * file, osl_pluto_unroll_p pluto_unroll) {
00158   osl_pluto_unroll_idump(file, pluto_unroll, 0);
00159 }
00160 
00161 
00171 char * osl_pluto_unroll_sprint(osl_pluto_unroll_p pluto_unroll) {
00172   
00173   char* string = NULL;
00174   char buffer[OSL_MAX_STRING] = { 0 };
00175   
00176   strcat(buffer, "# Pluto unroll\n");
00177   if (pluto_unroll != NULL) {
00178     strcat(buffer, "1\n");
00179   } else {
00180     strcat(buffer, "0\n");
00181   }
00182   
00183   while (pluto_unroll != NULL)
00184   {
00185     strcat(buffer, "# Iterator name\n");
00186     sprintf(buffer + strlen(buffer), "%s\n", pluto_unroll->iter);
00187 
00188     strcat(buffer, "# Jam\n");
00189     sprintf(buffer + strlen(buffer), "%i\n", pluto_unroll->jam);
00190 
00191     strcat(buffer, "# Factor\n");
00192     sprintf(buffer + strlen(buffer), "%i\n", pluto_unroll->factor);
00193 
00194     pluto_unroll = pluto_unroll->next;
00195 
00196     strcat(buffer, "# Next\n");
00197     if (pluto_unroll != NULL) {
00198       strcat(buffer, "1\n");
00199     } else {
00200       strcat(buffer, "0\n");
00201     }
00202   }
00203   
00204   OSL_realloc(string, char*, (strlen(buffer) + 1) * sizeof(char));
00205   strcpy(string, buffer);
00206   
00207   return string;
00208 }
00209 
00210 
00211 /*****************************************************************************
00212  *                               Reading function                            *
00213  *****************************************************************************/
00214 
00215 
00229 osl_pluto_unroll_p osl_pluto_unroll_sread(char** input) {
00230   osl_pluto_unroll_p p = NULL;
00231   if (osl_util_read_int(NULL, input) == 1) { p = osl_pluto_unroll_malloc(); }
00232   osl_pluto_unroll_p r = p;
00233   
00234   while (p != NULL)
00235   {
00236     // iter
00237     p->iter = osl_util_read_line(NULL, input);
00238     // jam
00239     p->jam = osl_util_read_int(NULL, input);
00240     // factor
00241     p->factor = osl_util_read_int(NULL, input);
00242     // Next
00243     if (osl_util_read_int(NULL, input) == 1) {
00244       p->next = osl_pluto_unroll_malloc();
00245     }
00246     p = p->next;
00247   }
00248   
00249   return r;
00250 }
00251 
00252 
00253 /*+***************************************************************************
00254  *                    Memory allocation/deallocation function                *
00255  *****************************************************************************/
00256 
00257 
00267 osl_pluto_unroll_p osl_pluto_unroll_malloc() {
00268   osl_pluto_unroll_p pluto_unroll = NULL;
00269 
00270   OSL_malloc(pluto_unroll, osl_pluto_unroll_p, sizeof(osl_pluto_unroll_t));
00271   pluto_unroll->iter   = NULL;
00272   pluto_unroll->jam    = false;
00273   pluto_unroll->factor = 0;
00274   pluto_unroll->next   = NULL;
00275 
00276   return pluto_unroll;
00277 }
00278 
00279 
00287 void osl_pluto_unroll_free(osl_pluto_unroll_p pluto_unroll) {
00288   if (pluto_unroll != NULL) {
00289     osl_pluto_unroll_free(pluto_unroll->next);
00290 
00291     free(pluto_unroll->iter);
00292     free(pluto_unroll); pluto_unroll = NULL;
00293   }
00294 }
00295 
00296 
00297 /*+***************************************************************************
00298  *                            Processing functions                           *
00299  *****************************************************************************/
00300 
00310 void osl_pluto_unroll_fill(osl_pluto_unroll_p pluto_unroll,
00311                            char const * const iterator_name,
00312                            bool const jam, int const factor) {
00313   if (pluto_unroll != NULL) {
00314     // iter
00315     if (iterator_name != NULL) {
00316       OSL_realloc(pluto_unroll->iter,
00317                   char*, (strlen(iterator_name) + 1) * sizeof(char));
00318       strcpy(pluto_unroll->iter, iterator_name);
00319         }
00320     // jam
00321     pluto_unroll->jam = jam;
00322     // factor
00323     pluto_unroll->factor = factor;
00324   } else {
00325     OSL_warning("osl_pluto_unroll_fill can not fill NULL osl_pluto_unroll_p");
00326   }
00327 }
00328 
00339 osl_pluto_unroll_p osl_pluto_unroll_clone(osl_pluto_unroll_p pluto_unroll) {
00340   osl_pluto_unroll_p p = NULL;
00341   if (pluto_unroll != NULL) { p = osl_pluto_unroll_malloc(); }
00342   osl_pluto_unroll_p r = p;
00343   
00344   while (pluto_unroll != NULL)
00345   {
00346     osl_pluto_unroll_fill(p, pluto_unroll->iter,
00347                              pluto_unroll->jam,
00348                              pluto_unroll->factor);
00349     
00350     pluto_unroll = pluto_unroll->next;
00351 
00352     if (pluto_unroll != NULL) { p->next = osl_pluto_unroll_malloc(); }
00353     p = p->next;
00354   }
00355 
00356   return r;
00357 }
00358 
00371 int osl_pluto_unroll_equal(osl_pluto_unroll_p a, osl_pluto_unroll_p b) {
00372   
00373   if (a == b) { return 1; }
00374   
00375   if (a == NULL && b != NULL) { return 0; }
00376   if (a != NULL && b == NULL) { return 0; }
00377   
00378   while (a != NULL) {
00379     // Iter
00380     if (a->iter == NULL && b->iter != NULL) { return 0; }
00381     if (a->iter != NULL && b->iter == NULL) { return 0; }
00382     if (a->iter != NULL) {
00383       if (strcmp(a->iter, b->iter) != 0) { return 0; }
00384       
00385     }
00386     // Jam
00387     if (a->jam != b->jam) { return 0; }
00388     // Factor
00389     if (a->factor != b->factor) { return 0; }
00390     // Next
00391     if (a->next == b->next) { return 1; }
00392     if (a->next == NULL && b->next != NULL) { return 0; }
00393     if (a->next != NULL && b->next == NULL) { return 0; }
00394     a = a->next;
00395     b = b->next;
00396   }
00397 
00398   return 1;
00399 }
00400 
00401 
00409 osl_interface_p osl_pluto_unroll_interface() {
00410   osl_interface_p interface = osl_interface_malloc();
00411   
00412   OSL_strdup(interface->URI, OSL_URI_PLUTO_UNROLL);
00413   interface->idump  = (osl_idump_f)osl_pluto_unroll_idump;
00414   interface->sprint = (osl_sprint_f)osl_pluto_unroll_sprint;
00415   interface->sread  = (osl_sread_f)osl_pluto_unroll_sread;
00416   interface->malloc = (osl_malloc_f)osl_pluto_unroll_malloc;
00417   interface->free   = (osl_free_f)osl_pluto_unroll_free;
00418   interface->clone  = (osl_clone_f)osl_pluto_unroll_clone;
00419   interface->equal  = (osl_equal_f)osl_pluto_unroll_equal;
00420 
00421   return interface;
00422 }