Package com.hpctoday.fada

Source Code of com.hpctoday.fada.ElementaryDependence

package com.hpctoday.fada;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import org.apache.log4j.Logger;

import com.hpctoday.fada.integersolver.Solver;
import com.hpctoday.fada.integersolver.SolverFactory;

/**
* \class ElementaryDependence
*
* \brief It describes a dependence between two statements at one level of precedence. A level of a precedence
* defines one DNF-term in the condition of lexicographic order between the dependent operations.
* The aim of this class is the compute the dependence between two statements in a level of precedence.
* That is done in two step algorithm :
* 1) recover and build appropriate constraints (using properties :"stmt1", "stmt2" and "deep").
* 2) Compute the QUAST describing the dependence. If all built constraints are
*     affine, the exact source can be computed and given in the property "quast". Otherwise, an approximation is computed in the property
*    "quast", and some extra information is filled in properties: "properties" and "parameter_of_maximum".
*/

public class ElementaryDependence {
  private static final Logger log = Logger.getLogger(ElementaryDependence.class.getName());
 
  // ! \name Absolutely required information to build objects
  private int stmt1; /*
             * !< One of the two dependent statements (in the case of flow dependence stmt1 is the read statement). Used to
             * recover and parameterize to context constraints.
             */
  private int stmt2; /*
             * !< The second dependent statement (it is the write statement in the case of flow dependence). Used to recover the
             * domain of the appropriate statement.
             */
  private int deep; /* !< The level of precedence, it serves to build the precedence constraints */
  private FADA_Index array_access_index; /* !< A pointer to the access-index */

  // ! \name Properties can be built using methods, but required by FADA analysis
  private List<String> stmt1_iteration; /* !< List of all enclosing parameterized counter-loops */
  private List<List<Inequation>> affine_environment; /* !< All affine environment-constraints (disjunction of conjunctions) */
  private List<List<Inequation>> non_affine_environment; /* !< Non affine environment-constraints ((disjunction of conjunctions) */

  private List<List<Inequation>> affine_valid_stmt2; /* !< The affine constraints from the domain of "stmt2" */
  private List<List<Inequation>> non_affine_valid_stmt2; /* !< Non affine constraints from the domain of "stmt2" */

  private List<Inequation> precedence; /* !< Lexicographic precedence (between "stmt1" and "stmt2" ) contraints */

  private List<Inequation> affine_index_equality; /* !< Affine inequalities from the equality of access-indexes */
  private List<Inequation> non_affine_index_equality; /* !< Non-affine inequalities from the equality of access-indexes */

  private List<String> stmt2_iteration; /* !< List of enclosing loops (in a good order) */
  private List<String> global_parameters; /* !< List of all symbolic constants */

  // ! \name the result of FADA processing
  private Quast quast; /* !< Quast describing when the dependence can occur */
  private List<String> parameter_of_maximum; /* !< Name of the parameter of the maximum */
  private LDemonstrator properties; /* !< Some properties on the maximum of the parameter */

  // ! \name Setters
  // ! \brief A setter for "properties"
  @SuppressWarnings("unused")
  private void SetProperties(LDemonstrator dem) {
    properties = dem;
  }

  // ! \brief A setter for "stmt1"
  private void SetStmt1(int _1) {
    stmt1 = _1;
  }

  // ! \brief A setter for "stmt2"
  private void SetStmt2(int _2) {
    stmt2 = _2;
  }

  // ! \brief A setter for "deep"
  private void SetDeep(int _d) {
    deep = _d;
  }

  // ! \brief A setter for "array_access_index"
  private void SetIndex(FADA_Index __i) {
    array_access_index = __i;
  }

  // ! \brief A setter for "affine_environment"
  private void SetAffineEnvironment(List<List<Inequation>> __c) {
    affine_environment = __c;
  }

  // ! \brief A setter for "non_affine_environment"
  private void SetNonAffineEnvironment(List<List<Inequation>> __c) {
    non_affine_environment = __c;
  }

  // ! \brief A setter for "affine_valid_stmt2"
  private void SetAffineValidStmt2(List<List<Inequation>> __c) {
    affine_valid_stmt2 = __c;
  }

  // ! \brief A setter for "non_affine_stmt2"
  private void SetNonAffineValidStmt2(List<List<Inequation>> __c) {
    non_affine_valid_stmt2 = __c;
  }

  // ! \brief A setter for "quast"
  private void SetQuast(Quast __q) {
    quast = __q;
  }

  // ! \brief A setter for "affine_index_equality"
  private void SetAffineIndexEquality(List<Inequation> __aie) {
    affine_index_equality = __aie;
  }

  // ! \brief A setter for "non_affine_index_equality"
  private void SetNonAffineIndexEquality(List<Inequation> __naie) {
    non_affine_index_equality = __naie;
  }

  // ! \brief A setter for "precedence"
  private void SetPrecedenceConstraints(List<Inequation> __c) {
    precedence = __c;
  }

  // ! \brief A setter for "parameter_of_maximum"
  private void SetParameterOfTheMaximum(List<String> __p) {
    parameter_of_maximum = __p;
  }

  // ! \brief A setter for "stmt1_iteration"
  private void SetStmt1Iteration(List<String> __c) {
    stmt1_iteration = __c;
  }

  // ! \brief A setter for "stmt2_iteration"
  private void SetStmt2Iteration(List<String> __c) {
    stmt2_iteration = __c;
  }

  // ! \brief A setter for "global_parameters"
  private void SetParameters(List<String> __p) {
    global_parameters = __p;
  }

  // ! \name Advanced Setters
  /*
   * !\brief An advanced setter for the entire object.\param __1 : the identifier of the first statement.\param __2 : the identifier of
   * the second statement. \param __d : the deep of the precedence.\param __i : the second statement access-index for the confictual
   * array. Arguments are mapped to (repectively) : stmt1, stmt2, deep and array_access_index.
   */
  private void Set(int __1, int __2, int __d, FADA_Index __i) {
    SetStmt1(__1);
    SetStmt2(__2);
    SetDeep(__d);
    SetIndex(__i);
  }

  private void Set(int __1, int __2, int __d) {
    SetStmt1(__1);
    SetStmt2(__2);
    SetDeep(__d);
  }

//  /*
//   * ! \brief A setter for all properties.
//   */
//  private void Set(List<String> __stmt1, List<String> __stmt2, List<String> __p, List<List<Inequation>> __ae,
//      List<List<Inequation>> __nae, List<List<Inequation>> __ac, List<List<Inequation>> __nac, List<Inequation> __preced,
//      List<Inequation> __aie, List<Inequation> __anie, Quast __dep) {
//    SetStmt1Iteration(__stmt1);
//    SetStmt2Iteration(__stmt2);
//    SetParameters(__p);
//    SetAffineEnvironment(__ae);
//    SetNonAffineEnvironment(__nae);
//    SetAffineValidStmt2(__ac);
//    SetNonAffineValidStmt2(__nac);
//    SetPrecedenceConstraints(__preced);
//    SetAffineIndexEquality(__aie);
//    SetNonAffineIndexEquality(__anie);
//    SetQuast(__dep);
//  }

  /*
   * ! \brief Set all properties to NULL values.
   */
  private void Initialize() {
    stmt1 = -1;
    stmt2 = -1;
    deep = -1;
    array_access_index = null;
   
    stmt1_iteration = new ArrayList<String>();
    affine_environment = new ArrayList<List<Inequation>>();
    non_affine_environment = new ArrayList<List<Inequation>>();

    affine_valid_stmt2 = new ArrayList<List<Inequation>>();
    non_affine_valid_stmt2 = new ArrayList<List<Inequation>>();

    precedence = new ArrayList<Inequation>();

    affine_index_equality = new ArrayList<Inequation>();
    non_affine_index_equality = new ArrayList<Inequation>();

    stmt2_iteration = new ArrayList<String>();
    global_parameters = new ArrayList<String>();

    parameter_of_maximum = new ArrayList<String>();   
   
    properties = new LDemonstrator();
  }

  // ! \name Getters
  // ! \brief a getter for "stmt1"
  public int GetStmt1() {
    return stmt1;
  }

  // ! \brief a getter for "stmt2"
  public int GetStmt2() {
    return stmt2;
  }

  // ! \brief a getter for "deep"
  public int GetDeep() {
    return deep;
  }

  // ! \brief a getter for "stmt1_iteration"
  public List<String> GetStmt1Iteration() {
    return stmt1_iteration;
  }

  // ! \brief a getter for "stmt2_iteration"
  public List<String> GetStmt2Iteration() {
    return stmt2_iteration;
  }

  // ! \brief a getter for "non_affine_index_equality"
  public List<Inequation> GetNonAffineIndexesEquality() {
    return non_affine_index_equality;
  }

  // ! \brief a getter for "affine_index_equality"
  public List<Inequation> GetAffineIndexesEquality() {
    return affine_index_equality;
  }

  // ! \brief a getter for "affine_environment"
  public List<List<Inequation>> GetAffineEnvironment() {
    return affine_environment;
  }

  // ! \brief a getter for "non_affine_environment"
  public List<List<Inequation>> GetNonAffineEnvironment() {
    return non_affine_environment;
  }

  // ! \brief a getter for "affine_valid_stmt2"
  public List<List<Inequation>> GetAffineValidStmt2() {
    return affine_valid_stmt2;
  }

  // ! \brief a getter for "non_affine_valid_stmt2"
  public List<List<Inequation>> GetNonAffineValidStmt2() {
    return non_affine_valid_stmt2;
  }

  // ! \brief a getter for "precedence"
  public List<Inequation> GetPrecedence() {
    return precedence;
  }

  // ! \brief a getter for "quast"
  public Quast GetQuast() {
    return quast;
  }

  // ! \brief a getter for "parameter_of_maximum"
  public List<String> GetParameterOfTheMaximum() {
    return parameter_of_maximum;
  }

  public LDemonstrator GetProperties() {
    return properties;
  }

  // ! \brief a getter for "index"
  public FADA_Index GetIndex() {
    return array_access_index;
  }

  // ! \brief a getter for "global_parameters"
  public List<String> GetParameters() {
    return global_parameters;
  }

  // ! \name Filling the rest of properties
  /*
   * !\brief This method builds the constraints related to the dependence detween "stmt1" and "stmt2" at the deep "deep".
   * \param __parameterized_iteration : always a fixed (to a parameterized) iteration always for "stmt1".
   * \param __global_parameters :global symbolic constants.
   * \param __references : the entire references. It play the role of a whole program, and it is needed in order to
   *                       recover domain constraints.
   * \return It fills the caller with the approriate constraints. all we need later is to apply the FADA model on it.
   */

  public void BuildConstraints(List<String> __global_parameters, List<References> __references, List<String> __parameterized_iteration,
      List<List<Inequation>> __affine_env, List<List<Inequation>> __non_affine_env, String __conflictual_variables,
      FADA_Index __parameterized_Index) {

    int read_stmt = GetStmt1();
    int write_stmt = GetStmt2();
    int common = GetDeep();
    SetStmt1Iteration(__parameterized_iteration);
    SetStmt2Iteration(__references.get(write_stmt).GetCounters());
    SetParameters(__global_parameters);

    // recover the write index
    FADA_Index write_index = null;
    List<Written_Reference> written_variables_ = (__references.get(write_stmt).GetWV());

    for (Written_Reference it : written_variables_) {
      if (it.GetName().equals(__conflictual_variables)) {
        write_index = it.GetIndex();
        break;
      }
    }
    if (write_index == null) {
      throw new RuntimeException("ElementaryDependence::BuildConstraints2 ... FATAL ERROR (inapropriate case [variable"
          + __conflictual_variables + " is not written by statment #" + write_stmt + " ])");
    }

    // compute common loops
    List<String> read_loops, write_loops, common_loops;
    read_loops = __references.get(read_stmt).GetCounters();
    write_loops = __references.get(write_stmt).GetCounters();

    common_loops = Global.Common(read_loops, write_loops);

    if (common_loops.size() < common) {
      throw new RuntimeException(
          "ElementaryDependence::BuildConstraints2 ... FATAL ERROR (inappropriate case [common_loops < common])");
    }

    // Compute precedence constraints (always affine)
    List<Inequation> __precedence = new ArrayList<Inequation>();
    __precedence.addAll(Global.Precedence(write_loops, __parameterized_iteration, common_loops.size(), common));
    SetPrecedenceConstraints(__precedence); // lexicographic precedence

    // Set the environment
    SetAffineEnvironment(__affine_env);
    SetNonAffineEnvironment(__non_affine_env);

    // valid write (domain)

    SetAffineValidStmt2(__references.get(write_stmt).GetAffineConstraints());
    SetNonAffineValidStmt2(__references.get(write_stmt).GetNonAffineConstraints());

    // indexes equality

    Condition __same_cell = __parameterized_Index.Equal(write_index);
    List<Inequation> all_eq = new ArrayList<Inequation>(__same_cell.GetTermInequations());
    List<Inequation> affine_eq = new ArrayList<Inequation>();
    List<Inequation> non_affine_eq = new ArrayList<Inequation>();
    Condition.SortInequations(all_eq, affine_eq, non_affine_eq, write_loops, GetParameters());
    SetAffineIndexEquality(affine_eq);
    SetNonAffineIndexEquality(non_affine_eq);

    // inject pragma-constraints into properties stack
    // if(GetDeep()>=stmt2_iteration->size()){
    // local pragma constraints are applyied only for current iteration.

    List<Inequation> empty = new ArrayList<Inequation>();
    LC_RHS_Term __false = new LC_RHS_Term(new Inequation(false));
    List<LC_RHS_Term> ___false = new ArrayList<LC_RHS_Term>();
    ___false.add(__false);
    List<Inequation> write_pragma_constraints = new ArrayList<Inequation>(__references.get(write_stmt).GetPragmaConstraints());
    if (write_pragma_constraints.size() > 0){
      log.trace("properties.PUSHBACK");
      properties.PUSHBACK(new LogicalClause(stmt2_iteration, empty, write_pragma_constraints, ___false));
    }
  }

  public void ComputeTheMaximum(LDemonstrator __global_properties, Options op) {
    // this part is to be removed when the structural analysis will be able to recover exact values for the parameter of the maximum.
    if (op.processing.apply_structural_analysis) {
      log.trace("apply_structural_analysis");

      List<Inequation> non_affine_env = new ArrayList<Inequation>();
      List<Inequation> non_affine_valid2 = new ArrayList<Inequation>();

      if (non_affine_environment.size() > 0)
        non_affine_env.addAll(non_affine_environment.get(0));

      if (non_affine_valid_stmt2.size() > 0)
        non_affine_valid2.addAll(non_affine_valid_stmt2.get(0));

      // cout<<"\n ENV NON AFFINE "<<PrintInequations(&non_affine_env);
      // cout<<"\n ENV NON AFFINE VALID STMT2 "<<PrintInequations(&non_affine_valid2);

      List<Inequation> result = new ArrayList<Inequation>();

      for (Inequation it2 : non_affine_valid2) {
        boolean insert = true;
        Inequation __ineq2 = (it2);
        for (Inequation it : non_affine_env) {
          Inequation __ineq = it.Negate(true);
          List<String> empty = new ArrayList<String>();
          List<Inequation> res = new ArrayList<Inequation>();

          if (LogicalClause.__Unify(__ineq2, stmt2_iteration, __ineq, empty, GetParameters(), res)) {
            /*
             * cout<<"\n UNIFY \n"; getchar();
             */
            LDemonstrator local = new LDemonstrator();
            local = __global_properties;
            List<Inequation> empty1 = new ArrayList<Inequation>();
            List<String> empty3 = new ArrayList<String>();
            List<Inequation> non_affine = new ArrayList<Inequation>();
            List<LC_RHS_Term> empty2 = new ArrayList<LC_RHS_Term>();

            non_affine.add(__ineq2);
            local.PUSHINFRONT(new LogicalClause(empty3, empty1, non_affine, empty2));
            local.Inference(GetParameters());
            local.POPAffines();
            non_affine.clear();
            non_affine.add(__ineq);
            local.PUSHINFRONT(new LogicalClause(stmt2_iteration, empty1, non_affine, empty2));
            local.Inference(GetParameters());
            local = local.POPAffines();
            // local.Print();
            if (local.IsAccurate(precedence, GetParameters())) {
              // cout<<"\nHi............";
              insert = false;
              break;
            }
          }
        }
        if (insert)
          result.add(it2);
      }

      List<List<Inequation>> non_aff = new ArrayList<List<Inequation>>();
      if (result.size() > 0)
        non_aff.add(result);
      SetNonAffineValidStmt2(non_aff);

      /*
       * cout<<"\ncontraintes restantes sont \n"<<PrintInequations(&non_aff); getchar();
       */
    }

    if (IsThereNonAffineConstraints()) {
      log.trace("IsThereNonAffineConstraints");
     
      // parameterization
      List<String> __new_variables = new ArrayList<String>();
      List<String> __local_parameters = new ArrayList<String>(GetParameters());

      List<String> __the_parameter_of_the_maximum = new ArrayList<String>();
      Map<String, String> __mapping_counters_alphas = new HashMap<String, String>();
      Map<String, Expression> __hard_map_counters_alphas = new HashMap<String, Expression>();
      int i = 0;
      List<Inequation> added_constraints = new ArrayList<Inequation>();

      for (String it : stmt2_iteration) {
        StringBuilder nv = new StringBuilder();
        StringBuilder alpha = new StringBuilder();

        nv.append(Global.FADA_New_var).append(i);
        if (!op.printing.human_friendly)
          alpha.append(Global.FADA_Alpha).append("_").append(GetStmt2()).append("_").append(GetDeep()).append("_").append(i);
        else if (!op.printing.command_line_printing && op.printing.tex_printing)
          alpha.append(it).append("_{").append(GetStmt2()).append("}^{").append(GetDeep()).append("}");
        else
          alpha.append(it).append("_").append(GetStmt2()).append("_").append(GetDeep());

        __mapping_counters_alphas.put(it, alpha.toString());
        __hard_map_counters_alphas.put(it, new Expression(alpha.toString()));
        added_constraints.add(new Inequation(new Expression(it), Inequation.Predicate.FADA_EQ, new Expression(nv.toString())));
        __new_variables.add(nv.toString());
        __local_parameters.add(it);
        __local_parameters.add(alpha.toString());
        GetParameters().add(alpha.toString());
        __the_parameter_of_the_maximum.add(alpha.toString());
        ++i;
      }
      StringBuilder _alpha = new StringBuilder();
      _alpha.append(Global.FADA_Alpha).append("_").append(GetStmt2()).append("_").append(GetDeep());

      SetParameterOfTheMaximum(__the_parameter_of_the_maximum);

      //List<List<Inequation>> constraints_on_parameter = new ArrayList<List<Inequation>>();

      Quast result = new Quast();
      // build affine systems

      for (List<Inequation> itw : affine_valid_stmt2) {
        // valid read
        List<Inequation> system = new ArrayList<Inequation>(); // =*itr;

        // valid write
        for (Inequation iti : itw) {
          system.add(iti);
        }

        // precedence
        for (Inequation itp : precedence) {
          system.add(itp);
        }

        // same variable
        for (Inequation itsc : affine_index_equality) {
          system.add(itsc);
        }

        system = Global.EliminateTrueAndDoubles(system);

        // /////////////////////////////////////////////

        LexicographicInequation lex = new LexicographicInequation(stmt2_iteration,
            LexicographicInequation.Lexicographic_Operator.LEX_LESS, stmt2_iteration.size(), parameter_of_maximum);
        // if(parameter_of_maximum.size()<=0){
        // cout<<"\nElementaryDependence::ComputeTheMaximum .......FATAL ERROR : got non affine constraints from statement 'ID#"<<GetStmt2()<<"' supposed to be invariant\n";
        // throw(INAPPROPRIATE_CASE);
        // exit(EXIT_FAILURE);
        // }

        Inequation ineq = new Inequation(new Expression(_alpha.toString()), Inequation.Predicate.FADA_GREATER, new Expression(
            Global.FADA_Bottom));
        LC_RHS_Term rhs = new LC_RHS_Term(lex, ineq);
        List<LC_RHS_Term> vect = new ArrayList<LC_RHS_Term>();
        vect.add(rhs);

        if (non_affine_valid_stmt2.size() > 0) {
          for (List<Inequation> itnaff : non_affine_valid_stmt2) {
            List<Inequation> non_affine_constraints = Global.MergeInequations(itnaff, non_affine_index_equality);

            LogicalClause __clause = new LogicalClause(stmt2_iteration,system, non_affine_constraints, vect);
            properties.PUSHBACK(__clause);
            log.trace("properties.PUSHBACK 01");
            log.trace(__clause);
          }
        } else {
          LogicalClause __clause = new LogicalClause(stmt2_iteration, system, non_affine_index_equality, vect);
          properties.PUSHBACK(__clause);
          log.trace("properties.PUSHBACK 02");
          log.trace(__clause);
        }
        Inequation no_bottom = new Inequation(new Expression(_alpha.toString()), Inequation.Predicate.FADA_LESS_EQ, new Expression(
            Global.FADA_Bottom));
        List<Inequation> no_bottom_vect = new ArrayList<Inequation>();
        no_bottom_vect.add(no_bottom);
        List<Inequation> __v = new ArrayList<Inequation>();
        __v.addAll(affine_valid_stmt2.get(0));
        __v = Global.EliminateTrueAndDoubles(__v);

        for (Inequation it : __v) {
          LC_RHS_Term rhs2 = new LC_RHS_Term(it.Negate(true).SubstituteByExpression(__hard_map_counters_alphas));
          List<LC_RHS_Term> vect2 = new ArrayList<LC_RHS_Term>();
          List<String> var = new ArrayList<String>();
          List<Inequation> non_aff = new ArrayList<Inequation>();
          vect2.add(rhs2);
          LogicalClause __clause = new LogicalClause(var, no_bottom_vect, non_aff, vect2);
          properties.PUSHBACK(__clause);
          log.trace("properties.PUSHBACK 03");
          log.trace(__clause);
        }

        Inequation bottom = new Inequation(new Expression(_alpha.toString()), Inequation.Predicate.FADA_GREATER, new Expression(
            Global.FADA_Bottom));
        List<Inequation> bottom_vect = new ArrayList<Inequation>();
        bottom_vect.add(bottom);
        for (Inequation it : __v) {
          LC_RHS_Term rhs2 = new LC_RHS_Term(it.SubstituteByExpression(__hard_map_counters_alphas));
          List<LC_RHS_Term> vect2 = new ArrayList<LC_RHS_Term>();
          List<String> var = new ArrayList<String>();
          List<Inequation> non_aff = new ArrayList<Inequation>();
          vect2.add(rhs2);
          LogicalClause __clause = new LogicalClause(var, bottom_vect, non_aff, vect2);
          properties.PUSHBACK(__clause);
          log.trace("properties.PUSHBACK 04");
          log.trace(__clause);
        }
        // /////////////////////////////////////////////

        // added constraints

        for (Inequation ita : added_constraints)
          system.add(ita);

        log.trace("Solver 01");
        Solver solver = SolverFactory.createSolver();
        solver.set(GetStmt2(),GetDeep(),__new_variables,__local_parameters,system);
        /*
         * cout<<"\n Solver = \n"; solver.Print();
         */
        Quast local__result = solver.Max();
        /*
         * cout<<"\n local = \n"; local__result.Print();
         */
        local__result.SubstituteByString(__mapping_counters_alphas);
        result = result.Max(local__result);
        // cout<<"\n final = \n";
        // result.Print();
        // getchar();
      }
      parameter_of_maximum.add(_alpha.toString());
      result.SubstituteByString(__mapping_counters_alphas);
      result = result.EliminateRedondantLeaves(false);
      result = result.Simplify(affine_environment, GetParameters());
      // result=result.Compress();
      SetQuast(result);
    } else {
      log.trace("Not IsThereNonAffineConstraints");
     
      // all constraints are affine ==> compute the exact definition
      List<String> __local_parameters = GetParameters();
      Quast result = new Quast(); // _|_
      // build affine systems
      //int i = 0;
     
      log.trace("AffineValidStmt2: " + affine_valid_stmt2.size());
     
      for (List<Inequation> itw : affine_valid_stmt2) {

        // valid read
        List<Inequation> system = new ArrayList<Inequation>();// =*itr;
        // if(affine_environment.size()>0);
        // system=*affine_environment.begin();

        // valid write
        for (Inequation iti : itw)
          system.add(iti);

        // lexicographic precedence
        for (Inequation itp : precedence)
          system.add(itp);

        // same variable
        for (Inequation itsc : affine_index_equality)
          system.add(itsc);

        log.trace("Solver 02");
        Solver solver = SolverFactory.createSolver();
        solver.set(GetStmt2(),GetDeep(),stmt2_iteration,__local_parameters,system);
        Quast local_result = solver.Max();
        log.trace("Local Result: " + local_result);
        result = result.Max(local_result);
      }

     
      log.trace("***************************************************************************");
      log.trace("Before Simplify: " + result.Print_str("\n"));
      result = result.EliminateRedondantLeaves(false);
      result = result.Simplify(affine_environment, GetParameters());
      log.trace("\nGetAffineEnvironment:\n" + Global.PrintListInequations(affine_environment));
      log.trace("\nAfter Simplify: \n" + result.Print_str("\n"));
      log.trace("***************************************************************************");
      SetQuast(result);
    }
  }

  /*
   * !\brief This method build and apply the FADA processing.\param __parameterized_iteration : always a fixed (to a parameterized)
   * iteration always for "stmt1".\param __global_parameters :global symbolic constants.\param __pgm : the entire AST of the program. It
   * is needed to solve the textual order between statements.\return It fills the "quast" property. May be "properties" and
   * "parameter_of_maximum" are updated if some non-affine constraints occur.\see BuildConstraints()
   */
  // void BuildAndComputeMax(List<String>* __parameterized_iteration,List<String>* __global_parameters, Program* __pgm,bool
  // alphas,bool sa);

  // ! \name Frequently Asked Questions
  /*
   * ! \brief Checks whether it is some non affine constraints.
   */

  public boolean IsThereNonAffineConstraints() {
    return ((!non_affine_index_equality.isEmpty()) || !non_affine_valid_stmt2.isEmpty());
  }

  /*
   * ! \brief Checks whether two instances are semantically equal.
   */
  public boolean Equal(ElementaryDependence ed) {
    return GetStmt1() == ed.GetStmt1() && GetStmt2() == ed.GetStmt2() && GetDeep() == ed.GetDeep();
  }

  // ! \name Building
  /*
   * ! \param __1[in] : first dependent statement id. \param __2[in] : second dependent statement id. \param __d[in] : the deep of the
   * dependence precedence. \param __i[in] : the index for the first statement access. \remarks These are the basic information, required
   * properties by FADA processing can be built using "BuildConstraints". The FADA processing is done by "BuildAndComputeMax". \see
   * Set(),BuildAndComputeMax(), BuildConstraints()
   */
  public ElementaryDependence(int __1, int __2, int __d, FADA_Index __i) {
    Initialize();
    Set(__1, __2, __d, __i);
  }

  public ElementaryDependence(int __1, int __2, int __d) {
    Initialize();
    Set(__1, __2, __d);
  }

    @Override
  public String toString() {
    return Print_str(0);
  }

  // ! \name Printing
  public String Print_str(int level) {
    StringBuilder out = new StringBuilder();
    out.append("( r: ").append(GetStmt1()).append(" , w : ").append(GetStmt2()).append(" , d : ").append(GetDeep()).append(" )");
    if (level == 0)
      return out.toString();

    out.append("\nSTMT 1 iteration vector ...");
    for (String it : stmt1_iteration) {
      out.append("\t").append(it);
    }

    out.append("\nSTMT 2 iteration vector...");
    for (String it : stmt2_iteration) {
      out.append("\t").append(it);
    }

    out.append("\nAffine Environment ...");
    for (List<Inequation> it : affine_environment) {
      out.append("\n");
      for (Inequation iti : it)
        out.append("\t").append(iti);
    }

    out.append("\nNON-Affine Environment ...");

    for (List<Inequation> it : non_affine_environment) {
      out.append("\n");
      for (Inequation iti : it)
        out.append("\t").append(iti);
    }

    out.append("\nAffine inequation from \"valid(statement2)\"  ...");
    for (List<Inequation> it : affine_valid_stmt2) {
      out.append("\n");
      for (Inequation iti : it)
        out.append("\t").append(iti);
    }

    out.append("\nNon-affine inequations from \"valid(statement2)\"  ... ");
    out.append("\n");
    for (List<Inequation> it : non_affine_valid_stmt2) {
      out.append("\n");
      for (Inequation iti : it)
        out.append("\t").append(iti);
    }

    out.append("\nAffine inequations from 'indexes equality'....");
    out.append("\n");
    for (Inequation it : affine_index_equality)
      out.append("\t").append(it);

    out.append("\nNon-affine inequations from 'indexes equality'");
    out.append("\n");
    for (Inequation it : non_affine_index_equality)
      out.append("\t").append(it);

    out.append("\nPrecedence");
    out.append("\n");
    for (Inequation it : precedence)
      out.append("\t").append(it);

    return out.toString();
  }

  // public String Full_Tex_Print_str() {}

  public String PrintProperties() {
    Map<String, Expression> var = new HashMap<String, Expression>();
    Map<String, Expression> param = new HashMap<String, Expression>();
    List<String> vector2 = new ArrayList<String>();
    List<String> param2 = new ArrayList<String>();

    for (String it : stmt2_iteration) {
      var.put(it, new Expression(it + "_w"));
      vector2.add((it + "_w"));
    }
    for (String it : stmt1_iteration) {
      String current = "current_";

      String str = it.substring(current.length());
      param.put(it, new Expression(str));
      param2.add(str);
    }
    if (parameter_of_maximum.size() > 0) {
      String result = "";
      result += Global.PrintVector(parameter_of_maximum);
      result += "= Max_lex { " + Global.PrintVector(vector2) + " \\ ";
      result += Global.PrintInequations(Global.Substitute1(Global.Substitute1(precedence, var), param));
      if (precedence.size() > 0)
        result += "  , ";

      result += Global.PrintListInequations(Global.Substitute2(Global.Substitute2(non_affine_valid_stmt2, var), param)/*
                                                               * ,&vector2,&param2
                                                               */);
      if (non_affine_valid_stmt2.size() > 0 && non_affine_index_equality.size() > 0)
        result += "  , ";

      result += Global.PrintInequations(Global.Substitute1(Global.Substitute1(non_affine_index_equality, var), param)/*
                                                                 * ,&vector2,
                                                                 * &param2
                                                                 */);

      result += " } ";

      return result;
    }
    return "";
  }

  public void CollectMaximumOfParameterValues(Map<String, String> map, List<String> loops) {
    if (GetDeep() > 0 && quast != null && parameter_of_maximum.size() > 0) {
      if (GetDeep() > loops.size() && GetDeep() > parameter_of_maximum.size()) {
        throw new RuntimeException("ElementaryDependence::CollectMaximumOfParameterValues .... FATAL ERROR [INAPPROPRIATE CASE]");
      }

      for (int i = 0; i < GetDeep(); ++i)
        map.put(parameter_of_maximum.get(i), loops.get(i));
    }
  }

  // //!\name Miscellaneous

}
TOP

Related Classes of com.hpctoday.fada.ElementaryDependence

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.