void ppl_min_for_le_pointset (ppl_Pointset_Powerset_C_Polyhedron_t ps, ppl_Linear_Expression_t le, Value res) { ppl_Coefficient_t num, denom; Value dv, nv; int minimum, err; value_init (nv); value_init (dv); ppl_new_Coefficient (&num); ppl_new_Coefficient (&denom); err = ppl_Pointset_Powerset_C_Polyhedron_minimize (ps, le, num, denom, &minimum); if (err > 0) { ppl_Coefficient_to_mpz_t (num, nv); ppl_Coefficient_to_mpz_t (denom, dv); gcc_assert (value_notzero_p (dv)); value_division (res, nv, dv); } value_clear (nv); value_clear (dv); ppl_delete_Coefficient (num); ppl_delete_Coefficient (denom); }
static double compute_enode(enode *p, Value *list_args) { int i; Value m, param; double res=0.0; if (!p) return(0.); value_init(m); value_init(param); if (p->type == polynomial) { if (p->size > 1) value_assign(param,list_args[p->pos-1]); /* Compute the polynomial using Horner's rule */ for (i=p->size-1;i>0;i--) { res +=compute_evalue(&p->arr[i],list_args); res *=VALUE_TO_DOUBLE(param); } res +=compute_evalue(&p->arr[0],list_args); } else if (p->type == periodic) { value_assign(m,list_args[p->pos-1]); /* Choose the right element of the periodic */ value_set_si(param,p->size); value_pmodulus(m,m,param); res = compute_evalue(&p->arr[VALUE_TO_INT(m)],list_args); } value_clear(m); value_clear(param); return res; } /* compute_enode */
/** * Computes the overall period of the variables I for (MI) mod |d|, where M is * a matrix and |d| a vector. Produce a diagonal matrix S = (s_k) where s_k is * the overall period of i_k * @param M the set of affine functions of I (row-vectors) * @param d the column-vector representing the modulos */ Matrix * affine_periods(Matrix * M, Matrix * d) { Matrix * S; unsigned int i,j; Value tmp; Value * periods = (Value *)malloc(sizeof(Value) * M->NbColumns); value_init(tmp); for(i=0; i< M->NbColumns; i++) { value_init(periods[i]); value_set_si(periods[i], 1); } for (i=0; i<M->NbRows; i++) { for (j=0; j< M->NbColumns; j++) { value_gcd(tmp, d->p[i][0], M->p[i][j]); value_divexact(tmp, d->p[i][0], tmp); value_lcm(periods[j], periods[j], tmp); } } value_clear(tmp); /* 2- build S */ S = Matrix_Alloc(M->NbColumns, M->NbColumns); for (i=0; i< M->NbColumns; i++) for (j=0; j< M->NbColumns; j++) if (i==j) value_assign(S->p[i][j],periods[j]); else value_set_si(S->p[i][j], 0); /* 3- clean up */ for(i=0; i< M->NbColumns; i++) value_clear(periods[i]); free(periods); return S; } /* affine_periods */
int test_memory() { int i, j; value var, ten = value_set_long(10); value container = value_init_nil(); printf("testing memory\n"); var.type = VALUE_VAR; var.core.u_var = "x"; for (i = 1; i < 10000000; i *= 2) { container = value_init(VALUE_MPZ); size_t start = usec(); for (j = 0; j < i; ++j) { value_add_now(&container, ten); } value_clear(&container); size_t finish = usec(); printf("time to do and clear %d elements: %ld usec\n", i, (unsigned long) finish - start); } // size_t time = usec(); // while (usec() - time < 5000000) ; return 0; }
/*--------------------------------------------------------------*/ int Polyhedron_Not_Empty(Polyhedron *P,Polyhedron *C,int MAXRAYS) { int res,i; Value *context; Polyhedron *L; POL_ENSURE_FACETS(P); POL_ENSURE_VERTICES(P); POL_ENSURE_FACETS(C); POL_ENSURE_VERTICES(C); /* Create a context vector size dim+2 and set it to all zeros */ context = (Value *) malloc((P->Dimension+2)*sizeof(Value)); /* Initialize array 'context' */ for (i=0;i<(P->Dimension+2);i++) value_init(context[i]); Vector_Set(context,0,(P->Dimension+2)); /* Set context[P->Dimension+1] = 1 (the constant) */ value_set_si(context[P->Dimension+1],1); L = Polyhedron_Scan(P,C,MAXRAYS); res = exist_points(1,L,context); Domain_Free(L); /* Clear array 'context' */ for (i=0;i<(P->Dimension+2);i++) value_clear(context[i]); free(context); return res; }
/* * Given matrices 'Mat1' and 'Mat2', compute the matrix product and store in * matrix 'Mat3' */ void Matrix_Product(Matrix *Mat1,Matrix *Mat2,Matrix *Mat3) { int Size, i, j, k; unsigned NbRows, NbColumns; Value **q1, **q2, *p1, *p3,sum; NbRows = Mat1->NbRows; NbColumns = Mat2->NbColumns; Size = Mat1->NbColumns; if(Mat2->NbRows!=Size||Mat3->NbRows!=NbRows||Mat3->NbColumns!=NbColumns) { fprintf(stderr, "? Matrix_Product : incompatable matrix dimension\n"); return; } value_init(sum); p3 = Mat3->p_Init; q1 = Mat1->p; q2 = Mat2->p; /* Mat3[i][j] = Sum(Mat1[i][k]*Mat2[k][j] where sum is over k = 1..nbrows */ for (i=0;i<NbRows;i++) { for (j=0;j<NbColumns;j++) { p1 = *(q1+i); value_set_si(sum,0); for (k=0;k<Size;k++) { value_addmul(sum, *p1, *(*(q2+k)+j)); p1++; } value_assign(*p3,sum); p3++; } } value_clear(sum); return; } /* Matrix_Product */
/* * Return the component of 'p' with minimum non-zero absolute value. 'index' * points to the component index that has the minimum value. If no such value * and index is found, Value 1 is returned. */ void Vector_Min_Not_Zero(Value *p,unsigned length,int *index,Value *min) { Value aux; int i; i = First_Non_Zero(p, length); if (i == -1) { value_set_si(*min,1); return; } *index = i; value_absolute(*min, p[i]); value_init(aux); for (i = i+1; i < length; i++) { if (value_zero_p(p[i])) continue; value_absolute(aux, p[i]); if (value_lt(aux,*min)) { value_assign(*min,aux); *index = i; } } value_clear(aux); } /* Vector_Min_Not_Zero */
int main(int argc, const char *argv[]) { init_tools(); init_values(); init_evaluator(); init_interpreter(); init_tests(); init_sexp_to_c(); // TO TRY: Add an -O3 flag to Xcode's compile. Then compile and profile, and see if it runs faster. // run_tests(); // run_benchmarks(); if (argc > 1) { if (streq(argv[1], "test")) { run_tests(); } else if (streq(argv[1], "benchmark")) { run_benchmarks(); } else { value str = value_set_str(argv[1]); value_import(str); value_clear(&str); } } else { run_interpreter(); } return 0; }
static ppl_Pointset_Powerset_C_Polyhedron_t dr_equality_constraints (graphite_dim_t dim, graphite_dim_t pos, graphite_dim_t nb_subscripts) { ppl_Polyhedron_t subscript_equalities; ppl_Pointset_Powerset_C_Polyhedron_t res; Value v, v_op; graphite_dim_t i; value_init (v); value_init (v_op); value_set_si (v, 1); value_set_si (v_op, -1); ppl_new_C_Polyhedron_from_space_dimension (&subscript_equalities, dim, 0); for (i = 0; i < nb_subscripts; i++) { ppl_Linear_Expression_t expr; ppl_Constraint_t cstr; ppl_Coefficient_t coef; ppl_new_Coefficient (&coef); ppl_new_Linear_Expression_with_dimension (&expr, dim); ppl_assign_Coefficient_from_mpz_t (coef, v); ppl_Linear_Expression_add_to_coefficient (expr, pos + i, coef); ppl_assign_Coefficient_from_mpz_t (coef, v_op); ppl_Linear_Expression_add_to_coefficient (expr, pos + i + nb_subscripts, coef); ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL); ppl_Polyhedron_add_constraint (subscript_equalities, cstr); ppl_delete_Linear_Expression (expr); ppl_delete_Constraint (cstr); ppl_delete_Coefficient (coef); } ppl_new_Pointset_Powerset_C_Polyhedron_from_C_Polyhedron (&res, subscript_equalities); value_clear (v); value_clear (v_op); ppl_delete_Polyhedron (subscript_equalities); return res; }
/* * Compute GCD of 'a' and 'b' */ void Gcd(Value a,Value b,Value *result) { Value acopy, bcopy; value_init(acopy); value_init(bcopy); value_assign(acopy,a); value_assign(bcopy,b); while(value_notzero_p(acopy)) { value_modulus(*result,bcopy,acopy); value_assign(bcopy,acopy); value_assign(acopy,*result); } value_absolute(*result,bcopy); value_clear(acopy); value_clear(bcopy); } /* Gcd */
void count(mpz_t states,int argc, char **argv, int lanes[NLANES][L], int filaments, int noconsts) { int i,j; //int temp[500]; int** constraints; Value *test; char* total; unsigned NbRows, NbColumns; Value cb; Polyhedron *A; Matrix *M; struct barvinok_options *options = barvinok_options_new_with_defaults(); NbRows=0; argc = barvinok_options_parse(options, argc, argv, ISL_ARG_ALL); //printf("length 1st lane: %d\n", lanes[0][1]); constraints= malloc(noconsts*sizeof(int *)); //printf("%lu\n",(filaments+2)*(noconsts)); for (i=0;i<noconsts;i++){ constraints[i]=(int*)malloc(sizeof(int)*(filaments+2)); for(j=0;j<filaments+2;j++){ constraints[i][j]=0; //printf("%d %d %d\n",i,j,constraints[i][j]); } } //printf("from count: fils %d noconsts %d\n",filaments,noconsts); gen_constraints(constraints,lanes,filaments,noconsts); dump_to_file(constraints,filaments,noconsts); value_init(cb); total=malloc(sizeof(char)*500); get_popen_data(total); //A = Constraints2Polyhedron(M, options->MaxRays); //value_print(stdout, P_VALUE_FMT, cb); mpz_set_str(cb,total,10); free(total); //printf("%s\n%d\n",total,strlen(total)); //gmp_printf("%Zd\n",states); //printf("%d\n",tot_int); if (options->print_stats) barvinok_stats_print(options->stats, stdout); value_clear(cb); //value_clear(test); barvinok_options_free(options); for (i=0;i<noconsts;i++){ free(constraints[i]); } free(constraints); }
enum lp_result PL_polyhedron_opt(Polyhedron *P, Value *obj, Value denom, enum lp_dir dir, Value *opt) { int i; int first = 1; Value val, d; enum lp_result res = lp_empty; POL_ENSURE_VERTICES(P); if (emptyQ(P)) return res; value_init(val); value_init(d); for (i = 0; i < P->NbRays; ++ i) { Inner_Product(P->Ray[i]+1, obj, P->Dimension+1, &val); if (value_zero_p(P->Ray[i][0]) && value_notzero_p(val)) { res = lp_unbounded; break; } if (value_zero_p(P->Ray[i][1+P->Dimension])) { if ((dir == lp_min && value_neg_p(val)) || (dir == lp_max && value_pos_p(val))) { res = lp_unbounded; break; } } else { res = lp_ok; value_multiply(d, denom, P->Ray[i][1+P->Dimension]); if (dir == lp_min) mpz_cdiv_q(val, val, d); else mpz_fdiv_q(val, val, d); if (first || (dir == lp_min ? value_lt(val, *opt) : value_gt(val, *opt))) value_assign(*opt, val); first = 0; } } value_clear(d); value_clear(val); return res; }
/* * Free the memory space occupied by Vector */ void Vector_Free(Vector *vector) { int i; if (!vector) return; for(i=0;i<vector->Size;i++) value_clear(vector->p[i]); free(vector->p); free(vector); } /* Vector_Free */
int ppl_lexico_compare_linear_expressions (ppl_Linear_Expression_t a, ppl_Linear_Expression_t b) { ppl_dimension_type min_length, length1, length2; ppl_dimension_type i; ppl_Coefficient_t c; int res; Value va, vb; ppl_Linear_Expression_space_dimension (a, &length1); ppl_Linear_Expression_space_dimension (b, &length2); ppl_new_Coefficient (&c); value_init (va); value_init (vb); if (length1 < length2) min_length = length1; else min_length = length2; for (i = 0; i < min_length; i++) { ppl_Linear_Expression_coefficient (a, i, c); ppl_Coefficient_to_mpz_t (c, va); ppl_Linear_Expression_coefficient (b, i, c); ppl_Coefficient_to_mpz_t (c, vb); res = value_compare (va, vb); if (res == 0) continue; value_clear (va); value_clear (vb); ppl_delete_Coefficient (c); return res; } value_clear (va); value_clear (vb); ppl_delete_Coefficient (c); return length1 - length2; }
void item_destroy(item_t *item) { assert(item); assert(item->value); value_clear(item->value); free(item->value); item->value = NULL; free(item); }
value value_drop(value op, value n) { if (op.type == VALUE_NIL) { return value_init_nil(); } else if (op.type == VALUE_ARY) { if (n.type == VALUE_MPZ) { value length = value_set_long(op.core.u_a.length); value res = value_range(op, n, length); value_clear(&length); return res; } } else if (op.type == VALUE_LST) { if (n.type == VALUE_MPZ) { if (value_lt(n, value_zero)) { value_error(1, "Domain Error: drop() is undefined where n is %s (>= 0 expected).", n); return value_init_error(); } else if (value_gt(n, value_int_max)) { value_error(1, "Domain Error: drop() is undefined where n is %s (<= %s expected).", n, value_int_max); return value_init_error(); } size_t i, max = value_get_long(n); value ptr = op; for (i = 0; i < max && ptr.type == VALUE_LST; ++i) { ptr = ptr.core.u_l[1]; } return value_set(ptr); } } else if (op.type == VALUE_PAR) { if (n.type == VALUE_MPZ) { if (value_lt(n, value_zero)) { value_error(1, "Domain Error: drop() is undefined where n is %s (>= 0 expected).", n); return value_init_error(); } else if (value_gt(n, value_int_max)) { value_error(1, "Domain Error: drop() is undefined where n is %s (<= %s expected).", n, value_int_max); return value_init_error(); } size_t i, max = value_get_long(n); value ptr = op; for (i = 0; i < max && ptr.type == VALUE_PAR; ++i) { ptr = ptr.core.u_p->tail; } return value_set(ptr); } } else { value_error(1, "Type Error: drop() is undefined where op1 is %ts (array or list expected).", op); if (n.type == VALUE_MPZ) return value_init_error(); } value_error(1, "Type Error: drop() is undefined where op2 is %ts (integer expected).", n); return value_init_error(); }
int in_domain(Polyhedron *P, Value *list_args) { int col,row; Value v; /* value of the constraint of a row when parameters are instanciated*/ if( !P ) return( 0 ); POL_ENSURE_INEQUALITIES(P); value_init(v); /* P->Constraint constraint matrice of polyhedron P */ for(row=0;row<P->NbConstraints;row++) { value_assign(v,P->Constraint[row][P->Dimension+1]); /*constant part*/ for(col=1;col<P->Dimension+1;col++) { value_addmul(v, P->Constraint[row][col], list_args[col-1]); } if (value_notzero_p(P->Constraint[row][0])) { /*if v is not >=0 then this constraint is not respected */ if (value_neg_p(v)) { value_clear(v); return( in_domain(P->next, list_args) ); } } else { /*if v is not = 0 then this constraint is not respected */ if (value_notzero_p(v)) { value_clear(v); return( in_domain(P->next, list_args) ); } } } /* if not return before this point => all the constraints are respected */ value_clear(v); return 1; } /* in_domain */
void ppl_set_coef_gmp (ppl_Linear_Expression_t e, ppl_dimension_type i, Value x) { Value v0, v1; ppl_Coefficient_t c; value_init (v0); value_init (v1); ppl_new_Coefficient (&c); ppl_Linear_Expression_coefficient (e, i, c); ppl_Coefficient_to_mpz_t (c, v1); value_oppose (v1, v1); value_assign (v0, x); value_addto (v0, v0, v1); ppl_assign_Coefficient_from_mpz_t (c, v0); ppl_Linear_Expression_add_to_coefficient (e, i, c); value_clear (v0); value_clear (v1); ppl_delete_Coefficient (c); }
void ppl_set_inhomogeneous_gmp (ppl_Linear_Expression_t e, Value x) { Value v0, v1; ppl_Coefficient_t c; value_init (v0); value_init (v1); ppl_new_Coefficient (&c); ppl_Linear_Expression_inhomogeneous_term (e, c); ppl_Coefficient_to_mpz_t (c, v1); value_oppose (v1, v1); value_assign (v0, x); value_addto (v0, v0, v1); ppl_assign_Coefficient_from_mpz_t (c, v0); ppl_Linear_Expression_add_to_inhomogeneous (e, c); value_clear (v0); value_clear (v1); ppl_delete_Coefficient (c); }
/* * Reduce a vector by dividing it by GCD and making sure its pos-th * element is positive. */ void Vector_Normalize_Positive(Value *p,int length,int pos) { Value gcd; int i; value_init(gcd); Vector_Gcd(p,length,&gcd); if (value_neg_p(p[pos])) value_oppose(gcd,gcd); if(value_notone_p(gcd)) Vector_AntiScale(p, p, gcd, length); value_clear(gcd); } /* Vector_Normalize_Positive */
value value_tail_now(value *op) { if (op->type == VALUE_NIL) { value_error(1, "Error: cannot find tail!() of an empty list."); return value_init_error(); } else if (op->type == VALUE_ARY) { size_t i, length = value_length(*op); if (length == 0) { value_error(1, "Error: cannot find tail!() of an empty array."); return value_init_error(); } value_clear(&op->core.u_a.a[0]); for (i = 0; i < length-1; ++i) op->core.u_a.a[i] = op->core.u_a.a[i+1]; --op->core.u_a.length; } else if (op->type == VALUE_LST) { if (value_empty_p(*op)) { value_error(1, "Error: cannot find tail!() of an empty list."); return value_init_error(); } else { value_clear(&op->core.u_l[0]); value_free(op->core.u_l); *op = op->core.u_l[1]; } } else if (op->type == VALUE_PAR) { value tmp = op->core.u_p->tail; value_clear(&op->core.u_p->head); value_free(op->core.u_p); *op = tmp; } else { value_error(1, "Type Error: tail!() is undefined where op is %ts (array or list expected).", *op); return value_init_error(); } return value_init_nil(); }
static ppl_Constraint_t build_pairwise_constraint (graphite_dim_t dim, graphite_dim_t pos1, graphite_dim_t pos2, int c, enum ppl_enum_Constraint_Type cstr_type) { ppl_Linear_Expression_t expr; ppl_Constraint_t cstr; ppl_Coefficient_t coef; Value v, v_op, v_c; value_init (v); value_init (v_op); value_init (v_c); value_set_si (v, 1); value_set_si (v_op, -1); value_set_si (v_c, c); ppl_new_Coefficient (&coef); ppl_new_Linear_Expression_with_dimension (&expr, dim); ppl_assign_Coefficient_from_mpz_t (coef, v); ppl_Linear_Expression_add_to_coefficient (expr, pos1, coef); ppl_assign_Coefficient_from_mpz_t (coef, v_op); ppl_Linear_Expression_add_to_coefficient (expr, pos2, coef); ppl_assign_Coefficient_from_mpz_t (coef, v_c); ppl_Linear_Expression_add_to_inhomogeneous (expr, coef); ppl_new_Constraint (&cstr, expr, cstr_type); ppl_delete_Linear_Expression (expr); ppl_delete_Coefficient (coef); value_clear (v); value_clear (v_op); value_clear (v_c); return cstr; }
/* * Compute n! */ void Factorial(int n, Value *fact) { int i; Value tmp; value_init(tmp); value_set_si(*fact,1); for (i=1;i<=n;i++) { value_set_si(tmp,i); value_multiply(*fact,*fact,tmp); } value_clear(tmp); } /* Factorial */
/* * Reduce a vector by dividing it by GCD. There is no restriction on * components of Vector 'p'. Making the last element positive is *not* OK * for equalities. */ void Vector_Normalize(Value *p,unsigned length) { Value gcd; int i; value_init(gcd); Vector_Gcd(p,length,&gcd); if (value_notone_p(gcd)) Vector_AntiScale(p, p, gcd, length); value_clear(gcd); } /* Vector_Normalize */
/* * Compute n!/(p!(n-p)!) */ void Binomial(int n, int p, Value *result) { int i; Value tmp; Value f; value_init(tmp);value_init(f); if (n-p<p) p=n-p; if (p!=0) { value_set_si(*result,(n-p+1)); for (i=n-p+2;i<=n;i++) { value_set_si(tmp,i); value_multiply(*result,*result,tmp); } Factorial(p,&f); value_division(*result,*result,f); } else value_set_si(*result,1); value_clear(f);value_clear(tmp); } /* Binomial */
/* * Return the number of ways to choose 'b' items from 'a' items, that is, * return a!/(b!(a-b)!) */ void CNP(int a,int b, Value *result) { int i; Value tmp; value_init(tmp); value_set_si(*result,1); /* If number of items is less than the number to be choosen, return 1 */ if(a <= b) { value_clear(tmp); return; } for(i=a;i>b;--i) { value_set_si(tmp,i); value_multiply(*result,*result,tmp); } for(i=1;i<=(a-b);++i) { value_set_si(tmp,i); value_division(*result,*result,tmp); } value_clear(tmp); } /* CNP */
/* * Given Vectors 'p1' and 'p2', return Vector 'p3' = lambda * p1 + mu * p2. */ void Vector_Combine(Value *p1, Value *p2, Value *p3, Value lambda, Value mu, unsigned length) { Value tmp; int i; value_init(tmp); for (i = 0; i < length; i++) { value_multiply(tmp, lambda, p1[i]); value_addmul(tmp, mu, p2[i]); value_assign(p3[i], tmp); } value_clear(tmp); return; } /* Vector_Combine */
void value_free(Value *p, int size) { int i; if (cache_size < MAX_CACHE_SIZE) { cache[cache_size].p = p; cache[cache_size].size = size; ++cache_size; return; } for (i=0; i < size; i++) value_clear(p[i]); free(p); }
/* * Return the GCD of components of Vector 'p' */ void Vector_Gcd(Value *p,unsigned length,Value *min) { Value *q,*cq, *cp; int i, Not_Zero, Index_Min=0; q = (Value *)malloc(length*sizeof(Value)); /* Initialize all the 'Value' variables */ for(i=0;i<length;i++) value_init(q[i]); /* 'cp' points to vector 'p' and cq points to vector 'q' that holds the */ /* absolute value of elements of vector 'p'. */ cp=p; for (cq = q,i=0;i<length;i++) { value_absolute(*cq,*cp); cq++; cp++; } do { Vector_Min_Not_Zero(q,length,&Index_Min,min); /* if (*min != 1) */ if (value_notone_p(*min)) { cq=q; Not_Zero=0; for (i=0;i<length;i++,cq++) if (i!=Index_Min) { /* Not_Zero |= (*cq %= *min) */ value_modulus(*cq,*cq,*min); Not_Zero |= value_notzero_p(*cq); } } else break; } while (Not_Zero); /* Clear all the 'Value' variables */ for(i=0;i<length;i++) value_clear(q[i]); free(q); } /* Vector_Gcd */
int test_list() { value list = value_init_nil(); value num = value_set_long(10); long i, count; size_t start = usec(); for (count = 0; count < 5; ++count) { for (i = 0; i < 10000000; ++i) { value_cons_now(num, &list); } value_clear(&list); } size_t finish = usec(); printf("time to do %ld iterations: %ld\n", i, (long) (finish - start) / count); return 0; }