Package com.hpctoday.fada

Source Code of com.hpctoday.fada.Program

package com.hpctoday.fada;

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

import org.apache.log4j.Logger;

import com.hpctoday.fada.pointers.IntegerPointer;

public class Program {
  private static final Logger log = Logger.getLogger(Program.class.getName());
 
  private String label; // !< the name of the program
  private List<String> global_parameters; // !< all read only scalars
  private Statement syntax_tree; // !< its abstract syntax tree

  private List<References> normalized_stmts; // !< vector of governed references

  private List<String> all_known_parameters; // !< all symbolic constants
  private List<List<Boolean>> sequential_precedence;
  private Options options;
  private LDemonstrator global_properties;

  // ! \brief Build an empty program.
  public Program() {
    global_properties = new LDemonstrator();

    global_parameters = new ArrayList<String>();

    normalized_stmts = new ArrayList<References>();

    all_known_parameters = new ArrayList<String>();
    sequential_precedence = new ArrayList<List<Boolean>>();
   
    options = new Options();
  }
 
  // ! \name Preprocessing
  // ! \brief loops must be identified by their loop counters, renaming can occur
  //private List<String> SingleCounterId() {}

  // ! \brief build governed references from an AST.
  private void NormalizeDomains() {
    syntax_tree.NormalizeConditions(normalized_stmts, global_parameters);
  }

  // private void BuildDomains(){}

  private void ComputeSequentialPrecedence() {
    List<String> common = new ArrayList<String>();
    for (int i = 0; i < normalized_stmts.size(); ++i) {
      List<Boolean> local = new ArrayList<Boolean>();
      for (int j = 0; j <= i; j++)
        local.add(false);
      for (int j = i + 1; j < normalized_stmts.size(); j++)
        local.add(IsSequentiallyBefore(i, j, common));
      sequential_precedence.add(local);
    }
  }

  // private String ToHtml(Options* op);
  // String ToHtml(String dir, String file_name);

  private void CollectPropertiesOnIrregularLoops(LDemonstrator __properties) {
    List<String> empty = new ArrayList<String>();
    syntax_tree.CollectIrregularLoopsProperties(__properties, empty);
  }

  public LDemonstrator GetGlobalProperties() {
    return global_properties;
  }

  // ! \name Getters
  public List<List<Boolean>> GetSequentialPrecedenceMatrix() {
    return sequential_precedence;
  }

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

  // ! \brief A getter for "normalized_stmts"
  public List<References> GetNormalizedStmts() {
    return normalized_stmts;
  }

  // ! \brief A setter for "syntax_tree"
  public void SetSyntaxTree(Statement __main) {
    this.syntax_tree = __main;
  }

  // ! \brief A getter for "syntax_tree"
  public Statement GetSyntaxTree() {
    return this.syntax_tree;
  }

  // ! \brief A getter for "all_known_parameters"
  public List<String> GetAllParameters() {
    return all_known_parameters;
  }

  // ! \brief A getter for "label"
  public String GetLabel() {
    return label;
  }

  public Options GetOptions() {
    return options;
  }

  // ! \name Setters
  public void SetOptions(Options op) {
    options = op;
  }

  // ! \brief A setter for "all_known_parameters"
  public void SetAllParameters(List<String> vect) {
    all_known_parameters = vect;
  }

  // ! \brief A getter for "label"
  public void SetLabel(String str) {
    label = str;
  }

  // ! \brief A setter for "global_parameters"
  public void SetGlobalParameters(List<String> __gp) {
    global_parameters = __gp;
  }

  // ! \name building

  // ! \brief Parsing from a file
  // public boolean Parse(String filename){return false;}

  // ! \brief look for read only scalars from the AST
  // public void LookForGlobalParameters(){}

  // ! \brief look for read only scalars from the AST
  public void LookForParameters(boolean extended) {
    Set<String> read = new HashSet<String>();
    Set<String> written = new HashSet<String>();

    List<Set<String>> read_scalars = new ArrayList<Set<String>>();
    List<Set<String>> written_scalars = new ArrayList<Set<String>>();
    List<Set<String>> written_in_loops_scalars = new ArrayList<Set<String>>();
    Set<String> curr_read = new HashSet<String>();
    Set<String> curr_written = new HashSet<String>();

    /*
     * for(List<References*>::iterator it=normalized_stmts.begin(); it != normalized_stmts.end();){
     *
     * it.Print(0); if(it.GetCounters().size() > 0){ read_scalars.push_back(curr_read); written_scalars.push_back(curr_written);
     * curr_read.clear(); curr_written.clear();
     *
     * while(it!=normalized_stmts.end() && it.GetCounters().size() >0){ it.ReferencedScalars(&curr_written,&curr_read); ++it; }
     *
     * written_in_loops_scalars.push_back(curr_written); read_scalars.push_back(curr_read); written_scalars.push_back(curr_written);
     * curr_read.clear(); curr_written.clear(); } else it.ReferencedScalars(&curr_written,&curr_read); if(it != normalized_stmts.end())
     * ++it; }
     */

    for (Statement it : syntax_tree.GetSurroundedStatements()) {
      if (it.IsControl() && it.GetControl().IsLoop()) {
        read_scalars.add(curr_read);
        written_scalars.add(curr_written);
        curr_read = new HashSet<String>();
        curr_written = new HashSet<String>();
      }
      it.ReferencedScalars(curr_written, curr_read);
      if (it.IsControl() && it.GetControl().IsLoop()) {
        written_in_loops_scalars.add(curr_written);
        read_scalars.add(curr_read);
        written_scalars.add(curr_written);
        curr_read = new HashSet<String>();
        curr_written = new HashSet<String>();
      }
    }

    int itri = 0, itwi = 0;

    Set<String> extra_param = new HashSet<String>();
    while (itri != read_scalars.size()) {
      Set<String> itr = read_scalars.get(itri);
      Set<String> itw = written_scalars.get(itwi);

      for (String it : itr)
        read.add(it);
      for (String it : itw)
        written.add(it);
     
      for (String it : written) {
        int itw2i = itwi;
        ++itw2i;

        while ((itw2i != written_scalars.size()) && written_scalars.get(itw2i).contains(it)) {
          ++itw2i;
        }
        if (itw2i == written_scalars.size())
          extra_param.add(it);
      }
      ++itri;
      ++itwi;
    }

    for (String it : read) {
      if (!written.contains(it))
        extra_param.add(it);
    }

    // Set<String> final_param;
    for (Set<String> it : written_in_loops_scalars) {
      for (String it2 : it) {
        if (extra_param.contains(it2)) {
          extra_param.remove(it2);
        }
      }
    }
    written_in_loops_scalars.add(curr_written);
    List<String> symbolic_constants = new ArrayList<String>();
    // symbolic_constants.resize(extra_param.size());
    // int i = 0;
    // for (Set<String>::iterator it = extra_param.begin(); it
    // != extra_param.end(); ++it, ++i) {
    // symbolic_constants[i] = *it;
    // }
    symbolic_constants.addAll(extra_param);

    SetGlobalParameters(symbolic_constants);
    SetAllParameters(global_parameters);
  }

  // ! \brief Remove loop counters from the read and write references.
  // public void AvoidCounters(List<String> v){}

  // ! \brief Remove loop counters and symbolic constants from the read and write references.
  public void AvoidCountersAndSymbolicConstants() {
    for (References it : normalized_stmts)
      it.AvoidCountersParametersDoubles(global_parameters);
  }

  // public void RemoveTwoAssignments(){}

  // ! \name Dumping
  // ! \brief out write the program in a C format
  public String Generate_C_str() {
    return this.syntax_tree.Regenerate_C_Code("\n");
  }

  // ! \brief out write the program in a C format
  // public void Generate_c(String filename){}

  // ! \brief printing the program
  @Override
  public String toString() {
    return Print_str();
  }

  public String Print_str() {
    String controls = "";

    String result = "";

    String syntaxtree = "\n\n\n\n Abstract Syntax Tree\n" + this.syntax_tree.Print_str("\t");
    return "\n Label = '" + GetLabel() + "'\n\n" + result + "\n\n" + controls + syntaxtree;
  }

  // ! \brief printing the program in the standard output file
  // public void Print(){}

  // ! \brief Dump all properties
  // public String Full_Print_str(){return null;}

  // ! \brief Print the governed references
  //public void PrintNormalized(int i){}

  public String PrintSources(){
    String result = "";
    result = "\n/////////////////////////////////////////////////";
    for(References it: GetNormalizedStmts()){
      result += "\n********************************************";
      result += it.Print_str(1);
    }
    result += "\n/////////////////////////////////////////////////\n";
    return result;
  }

  // ! \brief generate the index for all references.
  // String GenerateHTMLIndex();
  // ! \brief generate html pages with the analysis' results

  // ! \brief generate html pages with the analysis' results
  // String ToHtml() {return ToHtml(GetOptions());}

  /*
   * ! \brief generate a dependence graph in GraphViz Format \param file_name[in] : the file name \param print_rank[in] :print the
   * identifier of statements ? \param print_label[in] : print the statement labels ? \param print_assign[in] : print the original
   * statement code-line ? \param print_condition[in] : print the dependence validity condition ? \param relation[in] : print the entire
   * dependence relation ? \param rename[in] : add the prefix "current_" to all loop counters ? \param bottom[in] : add the node "bottom"
   * for not initialized variables ? \param alphas[in] : hide alphas ? return nothing
   */
  // String Generate_GraphViz_Graph(String file_name, boolean print_rank, boolean print_label, boolean print_assign, boolean
  // print_condition, boolean print_relation, boolean rename,boolean bottom,boolean alphas);
  /*
   * ! \brief generate a dependence graph in GraphViz Format \param print_rank[in] :print the identifier of statements ? \param
   * print_label[in] : print the statement labels ? \param print_assign[in] : print the original statement code-line ? \param
   * print_condition[in] : print the dependence validity condition ? \param relation[in] : print the entire dependence relation ? \param
   * rename[in] : add the prefix "current_" to all loop counters ? \param bottom[in] : add the node "bottom" for not initialized variables
   * ? \param alphas[in] : hide alphas ? \return a graph in GraphViz format.
   */

  // String Generate_GraphViz_Graph(boolean print_rank, boolean print_label, boolean print_assign, boolean print_condition, boolean
  // print_relation,boolean rename,boolean bottom,boolean alphas);

  // ! \name Postprocessing
  /*
   * ! \brief Generate a C program which check wether computed dependence graph is valid. \return the C program
   */
  // public String GenerateOracle(){return null;}
  /*
   * ! \brief Generate a C program which check wether computed dependence graph is valid. \param file_name : the name of the file to write
   * in.
   */
  // public void GenerateOracle(String file_name){}
  /*
   * ! \brief Generate a C program which check wether computed dependence graph is valid. \return the C program
   */
  // public String Generate_C_Test_Code(){return null;}
  /*
   * ! \brief Generate a C program which check wether computed dependence graph is valid. \param file_name : the name of the file to write
   * in.
   */
  // public void Generate_C_Test_Code(String file_name){}

  /*
   * ! \brief rename parameterized loop counters to their original names.
   */
  public void RenameNewParameters() {
    for (References ita : normalized_stmts)
      ita.RenameNewParameters();
  }

  // ! \name Preprocessing
  // ! \brief Map loops to their loop counters
  //public void MapLoopsWithTheirCounters(Statement s) {}

  /*
   * Compute a "plan" the evaluate smartly the definitions of variables read by a statement.
   * \param stmt[in] : the read statement.
   * \param array[in] : conflictual array.
   * \param __i[in] : access function
   */
  public List<ElementaryDependence> GenerateOptimalWayForFlowDependenciesComputing(int stmt, String array, FADA_Index __i) {
    List<ElementaryDependence> result = new ArrayList<ElementaryDependence>();
    References __references = normalized_stmts.get(stmt);
    int common = __references.GetCounters().size();
    Statement statement = __references.GetOrigin();

    //log.trace("Stmt: " + stmt + " common: " + common);
    //log.trace(statement.Regenerate_C_Code(""));
   
    boolean was_while = false;
    Statement parent = statement.GetParent();
    if (statement.IsControl() && statement.GetControl().IsWhileLoop()) {
      parent = statement;
      // common--;
      was_while = true;
    }

    // int regular_begin = stmt;
    // int regular_end;
    int reverse_begin = stmt - 1;
    int reverse_end;

   
    while (!parent.IsProgram()) {
      Map<Integer, Integer> to_be_avoided = new HashMap<Integer, Integer>();
      List<ElementaryDependence> local_if;

      while (parent.IsControl() && (parent.GetControl().IsIf() || parent.GetControl().IsIfElse())) {
        reverse_end = statement.GetID();

        if (parent.GetControl().IsIf())
          reverse_end = parent.GetID();
        else {
          if (!parent.GetSurroundedStatements().contains(statement)) {
            IntegerPointer last = new IntegerPointer();
            IntegerPointer rend = new IntegerPointer();
            parent.GetElseEnclosed(rend, last);

            reverse_end = rend.getValue();

            IntegerPointer _min = new IntegerPointer();
            IntegerPointer _max = new IntegerPointer();
            parent.GetEnclosed(_min, _max);
            to_be_avoided.put(_min.getValue(), _max.getValue());
          } else {
            IntegerPointer _min = new IntegerPointer();
            IntegerPointer _max = new IntegerPointer();
            parent.GetElseEnclosed(_min, _max);
            to_be_avoided.put(_min.getValue(), _max.getValue());
          }
        }

        local_if = Global.ComputeDependenciesConfigs(reverse_begin, reverse_end, stmt, array, __i, common, normalized_stmts);

        reverse_begin = parent.GetID();
        statement = parent;
        parent = statement.GetParent();
      }

      List<ElementaryDependence> valid = Global.EliminateValues2In(result, to_be_avoided);

      for (ElementaryDependence it : valid)
        result.add(it);

      // parent = loop
      if (!parent.IsProgram()) {
        IntegerPointer rbegin = new IntegerPointer();
        IntegerPointer rend = new IntegerPointer();

        parent.GetEnclosed(rend, rbegin);

        reverse_end = rend.getValue();
        reverse_begin = rbegin.getValue();

        List<ElementaryDependence> local = new ArrayList<ElementaryDependence>();
        if (was_while)
          local = Global.ComputeDependenciesConfigs(stmt, reverse_end, stmt, array, __i, common, normalized_stmts);
        else
          local = Global.ComputeDependenciesConfigs(stmt - 1, reverse_end, stmt, array, __i, common, normalized_stmts);

        for (ElementaryDependence it : local)
          result.add(it);

        local = Global.ComputeDependenciesConfigs(reverse_begin, reverse_end, stmt, array, __i, common - 1, normalized_stmts);

        for (ElementaryDependence it : local)
          result.add(it);

        // local=ComputeDependenciesConfigs(stmt-1,reverse_end,stmt,array,__i, common -1,normalized_stmts);
        // for(List<ElementaryDependence*>::iterator it=local.begin(); it != local.end(); ++it)
        // result.push_back(*it);
        //
        // local=ComputeDependenciesConfigs(regular_end,stmt, stmt,array, __i,common -1,normalized_stmts);
        // for(List<ElementaryDependence*>::iterator it=local.begin(); it != local.end(); ++it)
        // result.push_back(*it);

        statement = parent;
        parent = statement.GetParent();
        if (was_while == true)
          was_while = false;

        --common;
        //log.trace("--common");
      }
    }

    if (common != 0) {
      throw new RuntimeException("Program::GenerateOptimalWayForFlowDependenciesComputing stmt: " + stmt + " (common("+common+") != 0)");
    }

    List<ElementaryDependence> local = Global.ComputeDependenciesConfigs(0, statement.GetID(), stmt, array, __i, common,
        normalized_stmts);

    for (ElementaryDependence it : local)
      result.add(it);

    return Global.EliminateElementaryDependenceDoubles(result);
  }

  /*
   * ! \brief Compute a "plan" the evaluate smartly the definitions of variables read by all statements. \see
   * GenerateOptimalWayForFlowDependenciesComputing()
   */
  public void GenerateOptimalWayForFlowDependenciesComputing() {
    for (References it : normalized_stmts){
      it.GenerateOptimalWayForFlowDependenciesComputing(it.GetStmtID(), this);
    }
  }

  /*
   * ! \brief collect the referenced scalars.
   */
  // public void GetAllReferencedVariables(List<Integer> __size, List<String> __id){}

  /*
   * Lower an AST into a vector guarded references.
   */
  public void Normalize() {
    List<String> c = new ArrayList<String>();
    Map<Integer, DomainConstraint> __map = new HashMap<Integer, DomainConstraint>();
    Normalize(syntax_tree, new Condition(new Inequation(true)), __map, c);
  }

  /*
   * Lower a statement into a vector guarded references.
   * __stmt[in] : the concerned statement.
   * __domain[in] : Its iteration domain.
   * __controls[in] : enclosing controls.
   * __counters[in] : enclosing loops.
   */
  public void Normalize(Statement __stmt, Condition __domain, Map<Integer, DomainConstraint> __controls, List<String> counters) {
    List<String> __counters = new ArrayList<String>(counters);
    if (__stmt.IsProgram()) {
      List<String> c = new ArrayList<String>();

      Map<Integer, DomainConstraint> __map = new HashMap<Integer, DomainConstraint>();
      for (Statement it : __stmt.GetSurroundedStatements())
        this.Normalize(it, new Condition(new Inequation(true)), __map, c);

      Statement it = __stmt.GetSurroundedStatements().get(__stmt.GetSurroundedStatements().size() - 1);

      IntegerPointer f = new IntegerPointer();
      IntegerPointer l = new IntegerPointer();
      IntegerPointer useless = new IntegerPointer();
      if (it.IsControl() && it.GetControl().IsIfElse())
        it.GetElseEnclosed(useless, l);
      else
        it.GetEnclosed(useless, l);

      it = __stmt.GetSurroundedStatements().get(0);
      it.GetEnclosed(f, useless);
      __stmt.SetEnclosed(f.getValue(), l.getValue());

      return;
    }

    if (__stmt.IsAssignment()) {

      int __id = normalized_stmts.size();

      List<Read_Reference> vr = new ArrayList<Read_Reference>();
      List<Written_Reference> vw = new ArrayList<Written_Reference>();

      Global.CollectReferences(__stmt.GetAssignment().GetLHSIndex(), vr);
      Global.CollectReferences(__stmt.GetAssignment().GetRHSExpression(), vr);

      Written_Reference wr = new Written_Reference(__stmt.GetAssignment().GetLHSArray(), __stmt.GetAssignment().GetLHSIndex());
      vw.add(wr);

      References new_stmt = new References(__id, __counters, __domain, __controls, vr, vw, __stmt);
      new_stmt.SetPragmaConstraints(__stmt.GetPragmaConstraints());

      __stmt.SetEnclosed(__id, __id);
      Global.Connect(__stmt, new_stmt);
      normalized_stmts.add(new_stmt);

      //log.trace(__id + ": " + __stmt.Regenerate_C_Code(""));
      return;
    }

    if (__stmt.IsControl()) {

      if (__stmt.GetControl().IsWhileLoop()) {
        // cout<<"\nProgram::Normalize, while loops are not handled yet\n";
        // exit(EXIT_FAILURE);

        List<Read_Reference> vr = new ArrayList<Read_Reference>();
        List<Written_Reference> vw = new ArrayList<Written_Reference>();

        Global.CollectReferences(__stmt.GetControl().GetRegularDomain(), vr);

        int __id = normalized_stmts.size();

        __counters.add(__stmt.GetControl().GetCounter());
        Condition new_domain = new Condition(__domain, Condition.Logical_Operator.FADA_AND, __stmt.GetControl().GetRegularDomain());

        List<Expression> iteration = new ArrayList<Expression>();
        for (String it : __counters)
          iteration.add(new Expression(it));

        new_domain.GetRightChild().Instanciate(__id, iteration);
        __controls.put(__id, new DomainConstraint(new_domain.GetRightChild()));

        References new_stmt = new References(__id, __counters, new_domain, __controls, vr, vw, __stmt);
        new_stmt.SetPragmaConstraints(__stmt.GetPragmaConstraints());
        Global.Connect(__stmt, new_stmt);
        normalized_stmts.add(new_stmt);
        //log.trace(__id + ": " + __stmt.Regenerate_C_Code(""));

        for (Statement it : __stmt.GetSurroundedStatements())
          this.Normalize(it, new_domain, __controls, __counters);

        Statement it = __stmt.GetSurroundedStatements().get(__stmt.GetSurroundedStatements().size() - 1);

        IntegerPointer l = new IntegerPointer();
        IntegerPointer useless = new IntegerPointer();

        if (it.IsControl() && it.GetControl().IsIfElse())
          it.GetElseEnclosed(useless, l);
        else
          it.GetEnclosed(useless, l);

        __stmt.SetEnclosed(__id, l.getValue());
        return;
      }

      if (__stmt.GetControl().IsIf()) {
        int __id = normalized_stmts.size();
        List<Read_Reference> vr = new ArrayList<Read_Reference>();
        List<Written_Reference> vw = new ArrayList<Written_Reference>(); // no written variable

        Global.CollectReferences(__stmt.GetControl().GetRegularDomain(), vr);

        References new_stmt = new References(__id, __counters, __domain, __controls, vr, vw, __stmt);
        new_stmt.SetPragmaConstraints(__stmt.GetPragmaConstraints());
        Global.Connect(__stmt, new_stmt);
        normalized_stmts.add(new_stmt);
        //log.trace(__id + ": " + __stmt.Regenerate_C_Code(""));

        Condition new_domain = new Condition(__domain, Condition.Logical_Operator.FADA_AND, __stmt.GetControl().GetRegularDomain());
        List<Expression> iteration = new ArrayList<Expression>();

        for (String it : __counters)
          iteration.add(new Expression(it));

        new_domain.GetRightChild().Instanciate(__id, iteration);

        __controls.put(__id, new DomainConstraint(new_domain.GetRightChild())); // positive

        for (Statement it : __stmt.GetSurroundedStatements())
          this.Normalize(it, new_domain, __controls, __counters);

        Statement it = __stmt.GetSurroundedStatements().get(__stmt.GetSurroundedStatements().size() - 1);

        IntegerPointer l = new IntegerPointer();
        IntegerPointer useless = new IntegerPointer();

        if (it.IsControl() && it.GetControl().IsIfElse())
          it.GetElseEnclosed(useless, l);
        else
          it.GetEnclosed(useless, l);

        __stmt.SetEnclosed(__id, l.getValue());
       
        return;
      }

      if (__stmt.GetControl().IsIfElse()) {

        int __id = normalized_stmts.size();

        List<Read_Reference> vr = new ArrayList<Read_Reference>();
        List<Written_Reference> vw = new ArrayList<Written_Reference>(); // no written reference
        Global.CollectReferences(__stmt.GetControl().GetRegularDomain(), vr);

        References new_stmt = new References(__id, __counters, __domain, __controls, vr, vw, __stmt);
        new_stmt.SetPragmaConstraints(__stmt.GetPragmaConstraints());
        Global.Connect(__stmt, new_stmt);
        normalized_stmts.add(new_stmt);
        //log.trace(__id + ": " + __stmt.Regenerate_C_Code(""));

        Condition new_domain = new Condition(__domain, Condition.Logical_Operator.FADA_AND, __stmt.GetControl().GetRegularDomain());

        List<Expression> iteration = new ArrayList<Expression>();
        for (String it : __counters)
          iteration.add(new Expression(it));

        new_domain.GetRightChild().Instanciate(__id, iteration);

        Map<Integer, DomainConstraint> then = __controls = new HashMap<Integer, DomainConstraint>();
        then.put(__id, new DomainConstraint(new_domain.GetRightChild())); // positive

        for (Statement it : __stmt.GetSurroundedStatements())
          this.Normalize(it, new_domain, then, __counters);

        __controls.put(__id, new DomainConstraint(new_domain.GetRightChild(), false)); // positive

        Condition new_else_domain = new Condition(__domain, Condition.Logical_Operator.FADA_AND, __stmt.GetControl()
            .GetRegularDomain().FastNegation());

        for (Statement it : __stmt.GetElseSurroundedStatements())
          this.Normalize(it, new_else_domain, __controls, __counters);

        Statement it = __stmt.GetElseSurroundedStatements().get(__stmt.GetElseSurroundedStatements().size() - 1);

        IntegerPointer f = new IntegerPointer();
        IntegerPointer l = new IntegerPointer();
        IntegerPointer useless = new IntegerPointer();

        if (it.IsControl() && it.GetControl().IsIfElse())
          it.GetElseEnclosed(useless, l);
        else
          it.GetEnclosed(useless, l);

        it = __stmt.GetElseSurroundedStatements().get(0);

        it.GetEnclosed(f, useless);
        __stmt.SetElseEnclosed(f.getValue(), l.getValue());

        // it=__stmt.GetSurroundedStatements().begin();
        // it.GetEnclosed(&f,&useless);
        it = __stmt.GetSurroundedStatements().get(__stmt.GetSurroundedStatements().size() - 1);

        if (it.IsControl() && it.GetControl().IsIfElse())
          it.GetElseEnclosed(useless, l);
        else
          it.GetEnclosed(useless, l);

        __stmt.SetEnclosed(__id, l.getValue());

        return;
      }

      if (__stmt.GetControl().IsForLoop()) {
        // cout<<"\nProgram::Normalize, it is a for loop\n";
        // exit(EXIT_FAILURE);

        List<Read_Reference> vr = new ArrayList<Read_Reference>();
        List<Written_Reference> vw = new ArrayList<Written_Reference>();

        Global.CollectReferences(__stmt.GetControl().GetForLowerBound(), vr);
        Global.CollectReferences(__stmt.GetControl().GetForUpperBound(), vr);
        int __id = normalized_stmts.size();

        References new_stmt = new References(__id, __counters, __domain, __controls, vr, vw, __stmt);
        new_stmt.SetPragmaConstraints(__stmt.GetPragmaConstraints());
        Global.Connect(__stmt, new_stmt);
        normalized_stmts.add(new_stmt);
        //log.trace(__id + ": " + __stmt.Regenerate_C_Code(""));

        Condition new_domain = new Condition(__domain, Condition.Logical_Operator.FADA_AND, __stmt.GetControl().GetRegularDomain());
        __counters.add(__stmt.GetControl().GetCounter());

        List<Expression> iteration = new ArrayList<Expression>();
        for (String it : __counters)
          iteration.add(new Expression(it));

        new_domain.GetRightChild().Instanciate(__id, iteration);
        __controls.put(__id, new DomainConstraint(new_domain.GetRightChild()));

        for (Statement it : __stmt.GetSurroundedStatements())
          this.Normalize(it, new_domain, __controls, __counters);

        Statement it = __stmt.GetSurroundedStatements().get(__stmt.GetSurroundedStatements().size() - 1);

        IntegerPointer l = new IntegerPointer();
        IntegerPointer useless = new IntegerPointer();

        if (it.IsControl() && it.GetControl().IsIfElse())
          it.GetElseEnclosed(useless, l);
        else
          it.GetEnclosed(useless, l);

        __stmt.SetEnclosed(__id, l.getValue());

        return;
      }

      throw new RuntimeException("Program::Normalize, inappropriate case");
    }
  }

  // preprocessing for FADA analysis
  // void Prepare();
 
  // preprocessing for FADA analysis
  public void Preprocess() {
    if (syntax_tree != null) {
      syntax_tree.Link2Parent();
      syntax_tree.SingleCounterId();
      LookForParameters(true);
      Normalize();
      LookForParameters(true);
      NormalizeDomains();

      ComputeSequentialPrecedence();
      AvoidCountersAndSymbolicConstants();
      GenerateOptimalWayForFlowDependenciesComputing();
      global_properties = new LDemonstrator();
      CollectPropertiesOnIrregularLoops(global_properties);
    }
    PreprocessNormalized();
  }

  public void PreprocessNormalized() {
    for (References it : normalized_stmts){
      it.NormalizeDomainConstraints(global_parameters);
    }
  }

  // ! \name Processing
  // Evaluate the sequential precedence between two statements.
  public boolean IsSequentiallyBefore(int id1, int id2, List<String> common) {
    if (id1 >= id2)
      return false;

    Statement deepest_common_control;
    if (common.isEmpty())
      deepest_common_control = syntax_tree;
    else {
      // List<String>::iterator it=common.end();
      // --it;
      // deepest_common_control=GetLoop(it);
      deepest_common_control = syntax_tree;
    }
    return deepest_common_control.IsSequentiallyBefore(id1, id2);
  }

  // ! \brief THE FADA PROCESSING
  // void ComputeSourcesForAllReadVariables(boolean sa,boolean alphas);
  public void ComputeSourcesForAllReadVariables() {
    Preprocess();
    log.trace("References: size = " + normalized_stmts.size());
    for (References it : normalized_stmts){
      log.trace("*********************************************");
      log.trace("ID: " + it.GetStmtID());
      log.trace("ComputeSourcesForAllReadVariables: " + it);
      it.ComputeDefinitions(global_parameters, normalized_stmts, sequential_precedence, global_properties, options);
      log.trace("*********************************************");
    }
    RenameNewParameters();
  }

  public void ComputeSourcesOnNormalizedReferences() {
    PreprocessNormalized();
    for (References it : normalized_stmts)
      it.ComputeDefinitions(global_parameters, normalized_stmts, sequential_precedence, global_properties, options);
    RenameNewParameters();
  }

  // //! \brief Apply the ietarative analysis.
  // void IterativeAnalysis();

  public void CollectLoops(Set<String> all_loops) {
    for (References it : normalized_stmts) {
      for (String itc : it.GetCounters())
        all_loops.add(itc);
    }
  }

  public List<String> LookForParallelLoops() {
    Set<String> s = new HashSet<String>();
    CollectLoops(s);
    List<String> all_loops = new ArrayList<String>();
    all_loops.addAll(s);

    for (References it : normalized_stmts) {
      // References ref;
      // ref->LookForParallelLoops(&all_loops,*normalized_stmts);
      it.LookForParallelLoops(all_loops, normalized_stmts);
    }

    // cout << " \n================ Parallel Loops Detector:";
    // if (all_loops.size() == 0)
    // cout << "\n      All loops carry dependences";
    // else
    // cout << "\n      Parallel loops are : " << PrintVector(&all_loops);
    // cout << " \n================================";
    return all_loops;
  }
}
TOP

Related Classes of com.hpctoday.fada.Program

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.