static CloogMatrix *
new_Cloog_Matrix_from_ppl_Constraint_System (ppl_const_Constraint_System_t pcs)
{
  CloogMatrix *matrix;
  ppl_Constraint_System_const_iterator_t cit, end;
  ppl_dimension_type dim;
  int rows;
  int row = 0;

  rows = ppl_Constrain_System_number_of_constraints (pcs);
  ppl_Constraint_System_space_dimension (pcs, &dim);
  matrix = cloog_matrix_alloc (rows, dim + 2);
  ppl_new_Constraint_System_const_iterator (&cit);
  ppl_new_Constraint_System_const_iterator (&end);

  for (ppl_Constraint_System_begin (pcs, cit),
        ppl_Constraint_System_end (pcs, end);
       !ppl_Constraint_System_const_iterator_equal_test (cit, end);
       ppl_Constraint_System_const_iterator_increment (cit))
    {
      ppl_const_Constraint_t c;
      ppl_Constraint_System_const_iterator_dereference (cit, &c);
      insert_constraint_into_matrix (matrix, row, c);
      row++;
    }

  ppl_delete_Constraint_System_const_iterator (cit);
  ppl_delete_Constraint_System_const_iterator (end);

  return matrix;
}
static int
ppl_Constrain_System_number_of_constraints (ppl_const_Constraint_System_t pcs)
{
  ppl_Constraint_System_const_iterator_t cit, end;
  int num = 0;

  ppl_new_Constraint_System_const_iterator (&cit);
  ppl_new_Constraint_System_const_iterator (&end);

  for (ppl_Constraint_System_begin (pcs, cit),
        ppl_Constraint_System_end (pcs, end);
       !ppl_Constraint_System_const_iterator_equal_test (cit, end);
       ppl_Constraint_System_const_iterator_increment (cit))
    num++;

  ppl_delete_Constraint_System_const_iterator (cit);
  ppl_delete_Constraint_System_const_iterator (end);
  return num;
}
示例#3
0
文件: ppl_lpsol.c 项目: hnxiao/ppl
static int
solve_with_simplex(ppl_const_Constraint_System_t cs,
                   ppl_const_Linear_Expression_t objective,
                   ppl_Coefficient_t optimum_n,
                   ppl_Coefficient_t optimum_d,
                   ppl_Generator_t point) {
  ppl_MIP_Problem_t ppl_mip;
  int optimum_found = 0;
  int pricing = 0;
  int status = 0;
  int satisfiable = 0;
  ppl_dimension_type space_dim;
  ppl_const_Constraint_t c;
  ppl_const_Generator_t g;
  ppl_Constraint_System_const_iterator_t i;
  ppl_Constraint_System_const_iterator_t iend;
  int counter;
  int mode = maximize
    ? PPL_OPTIMIZATION_MODE_MAXIMIZATION
    : PPL_OPTIMIZATION_MODE_MINIMIZATION;

  ppl_Constraint_System_space_dimension(cs, &space_dim);
  ppl_new_MIP_Problem_from_space_dimension(&ppl_mip, space_dim);
  switch (pricing_method) {
  case 0:
    pricing = PPL_MIP_PROBLEM_CONTROL_PARAMETER_PRICING_STEEPEST_EDGE_FLOAT;
    break;
  case 1:
    pricing = PPL_MIP_PROBLEM_CONTROL_PARAMETER_PRICING_STEEPEST_EDGE_EXACT;
    break;
  case 2:
    pricing = PPL_MIP_PROBLEM_CONTROL_PARAMETER_PRICING_TEXTBOOK;
    break;
  default:
    fatal("ppl_lpsol internal error");
  }
  ppl_MIP_Problem_set_control_parameter(ppl_mip, pricing);
  ppl_MIP_Problem_set_objective_function(ppl_mip, objective);
  ppl_MIP_Problem_set_optimization_mode(ppl_mip, mode);
  if (!no_mip)
    ppl_MIP_Problem_add_to_integer_space_dimensions(ppl_mip, integer_variables,
                                                    glpk_lp_num_int);
  if (incremental) {
    /* Add the constraints of `cs' one at a time. */
    ppl_new_Constraint_System_const_iterator(&i);
    ppl_new_Constraint_System_const_iterator(&iend);
    ppl_Constraint_System_begin(cs, i);
    ppl_Constraint_System_end(cs, iend);

    counter = 0;
    while (!ppl_Constraint_System_const_iterator_equal_test(i, iend)) {
      ++counter;
      if (verbosity >= 4)
        fprintf(output_file, "\nSolving constraint %d\n", counter);
      ppl_Constraint_System_const_iterator_dereference(i, &c);
      ppl_MIP_Problem_add_constraint(ppl_mip, c);

      if (no_optimization) {
        satisfiable = ppl_MIP_Problem_is_satisfiable(ppl_mip);
        if (!satisfiable)
          break;
      }
      else
        status = ppl_MIP_Problem_solve(ppl_mip);
      ppl_Constraint_System_const_iterator_increment(i);
    }
    ppl_delete_Constraint_System_const_iterator(i);
    ppl_delete_Constraint_System_const_iterator(iend);
  }

  else {
    ppl_MIP_Problem_add_constraints(ppl_mip, cs);
    if (no_optimization)
      satisfiable = ppl_MIP_Problem_is_satisfiable(ppl_mip);
    else
      status = ppl_MIP_Problem_solve(ppl_mip);
  }

#ifdef PPL_LPSOL_SUPPORTS_TIMINGS

  if (print_timings) {
    fprintf(stderr, "Time to solve the problem: ");
    print_clock(stderr);
    fprintf(stderr, " s\n");
    start_clock();
  }

#endif /* defined(PPL_LPSOL_SUPPORTS_TIMINGS) */

  if ((no_optimization && !satisfiable)
      || (!no_optimization && status == PPL_MIP_PROBLEM_STATUS_UNFEASIBLE)) {
    if (verbosity >= 1)
      fprintf(output_file, "Unfeasible problem.\n");
    maybe_check_results(status, 0.0);
    goto exit;
  }
  else if (no_optimization && satisfiable) {
    if (verbosity >= 1)
      fprintf(output_file, "Feasible problem.\n");
    /* Kludge: let's pass PPL_MIP_PROBLEM_STATUS_OPTIMIZED,
       to let work `maybe_check_results'. */
    maybe_check_results(PPL_MIP_PROBLEM_STATUS_OPTIMIZED, 0.0);
    goto exit;
  }
  else if (status == PPL_MIP_PROBLEM_STATUS_UNBOUNDED) {
    if (verbosity >= 1)
      fprintf(output_file, "Unbounded problem.\n");
    maybe_check_results(status, 0.0);
    goto exit;
  }
  else if (status == PPL_MIP_PROBLEM_STATUS_OPTIMIZED) {
    ppl_MIP_Problem_optimal_value(ppl_mip, optimum_n, optimum_d);
    ppl_MIP_Problem_optimizing_point(ppl_mip, &g);
    ppl_assign_Generator_from_Generator(point, g);
    optimum_found = 1;
    goto exit;
  }
  else
    fatal("internal error");

 exit:
  ppl_delete_MIP_Problem(ppl_mip);
  return optimum_found;
}
ppl_Polyhedron_t
ppl_strip_loop (ppl_Polyhedron_t ph, ppl_dimension_type loop, int stride)
{
  ppl_const_Constraint_System_t pcs;
  ppl_Constraint_System_const_iterator_t cit, end;
  ppl_const_Constraint_t cstr;
  ppl_Linear_Expression_t expr;
  int v;
  ppl_dimension_type dim;
  ppl_Polyhedron_t res;
  ppl_Coefficient_t c;
  Value val;

  value_init (val);
  ppl_new_Coefficient (&c);

  ppl_Polyhedron_space_dimension (ph, &dim);
  ppl_Polyhedron_get_constraints (ph, &pcs);

  /* Start from a copy of the constraints.  */
  ppl_new_C_Polyhedron_from_space_dimension (&res, dim + 1, 0);
  ppl_Polyhedron_add_constraints (res, pcs);

  /* Add an empty dimension for the strip loop.  */
  ppl_insert_dimensions (res, loop, 1);

  /* Identify the constraints that define the lower and upper bounds
     of the strip-mined loop, and add them to the strip loop.  */
  {
    ppl_Polyhedron_t tmp;

    ppl_new_C_Polyhedron_from_space_dimension (&tmp, dim + 1, 0);
    ppl_new_Constraint_System_const_iterator (&cit);
    ppl_new_Constraint_System_const_iterator (&end);

    for (ppl_Constraint_System_begin (pcs, cit),
	   ppl_Constraint_System_end (pcs, end);
	 !ppl_Constraint_System_const_iterator_equal_test (cit, end);
	 ppl_Constraint_System_const_iterator_increment (cit))
      {
	ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
	ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
	ppl_Linear_Expression_coefficient (expr, loop, c);
	ppl_delete_Linear_Expression (expr);
	ppl_Coefficient_to_mpz_t (c, val);
	v = value_get_si (val);

	if (0 < v || v < 0)
	  ppl_Polyhedron_add_constraint (tmp, cstr);
      }
    ppl_delete_Constraint_System_const_iterator (cit);
    ppl_delete_Constraint_System_const_iterator (end);

    ppl_insert_dimensions (tmp, loop + 1, 1);
    ppl_Polyhedron_get_constraints (tmp, &pcs);
    ppl_Polyhedron_add_constraints (res, pcs);
    ppl_delete_Polyhedron (tmp);
  }

  /* Lower bound of a tile starts at "stride * outer_iv".  */
  {
    ppl_Constraint_t new_cstr;
    ppl_new_Linear_Expression_with_dimension (&expr, dim + 1);

    ppl_set_coef (expr, loop + 1, 1);
    ppl_set_coef (expr, loop, -1 * stride);

    ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
    ppl_delete_Linear_Expression (expr);
    ppl_Polyhedron_add_constraint (res, new_cstr);
    ppl_delete_Constraint (new_cstr);
  }

  /* Upper bound of a tile stops at "stride * outer_iv + stride - 1",
     or at the old upper bound that is not modified.  */
  {
    ppl_Constraint_t new_cstr;
    ppl_new_Linear_Expression_with_dimension (&expr, dim + 1);

    ppl_set_coef (expr, loop + 1, -1);
    ppl_set_coef (expr, loop, stride);
    ppl_set_inhomogeneous (expr, stride - 1);

    ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
    ppl_delete_Linear_Expression (expr);
    ppl_Polyhedron_add_constraint (res, new_cstr);
    ppl_delete_Constraint (new_cstr);
  }

  value_clear (val);
  ppl_delete_Coefficient (c);
  return res;
}