void quicksort(void *closure, struct scheduler *s) { struct quicksort_args *args = (struct quicksort_args *)closure; int *a = args->a; int lo = args->lo; int hi = args->hi; int p; int rc; free(closure); if(lo >= hi) return; if(hi - lo <= 128) { quicksort_serial(a, lo, hi); return; } p = partition(a, lo, hi); rc = sched_spawn(quicksort, new_args(a, lo, p), s); assert(rc >= 0); rc = sched_spawn(quicksort, new_args(a, p + 1, hi), s); assert(rc >= 0); }
expr_ref mk_array_instantiation::create_head(app* old_head) { expr_ref_vector new_args(m); for(unsigned i=0;i<old_head->get_num_args();i++) { expr*arg = old_head->get_arg(i); if(m_a.is_array(get_sort(arg))) { for(unsigned k=0; k< m_ctx.get_params().xform_instantiate_arrays_nb_quantifier();k++) { expr_ref_vector dummy_args(m); dummy_args.push_back(arg); for(unsigned i=0;i<get_array_arity(get_sort(arg));i++) { dummy_args.push_back(m.mk_var(cnt, get_array_domain(get_sort(arg), i))); cnt++; } expr_ref select(m); select = m_a.mk_select(dummy_args.size(), dummy_args.c_ptr()); new_args.push_back(select); selects.insert_if_not_there(arg, ptr_vector<expr>()); selects[arg].push_back(select); } if(!m_ctx.get_params().xform_instantiate_arrays_enforce()) new_args.push_back(arg); } else new_args.push_back(arg); } return create_pred(old_head, new_args); }
int read_and_exec(int socket) { struct buf_t* buf = buf_new(8049); int pos = buf_readuntil(socket, buf, '\n'); if (pos == -2) return 0; char* buffer = buf->data; buffer[pos] = '\0'; struct execargs_t* arguments[1024]; int k = 0; while (1) { char delim; int argc = count_words(buffer, '|'); if (argc == 0) break; char* argv[argc]; int shift; int i = 0; for (i = 0; i < argc; i++) { delim = '|'; argv[i] = get_word(buffer, &delim, &shift); buffer += shift; } arguments[k] = (struct execargs_t*) malloc(sizeof(struct execargs_t)); *arguments[k] = new_args(argc, argv); shift = get_delim(buffer, '|'); buffer+=shift; k++; } buf->size -= (buffer - (char*) buf->data + 1); runpiped(arguments, k, socket); buf_free(buf); return 0; }
void mk_new_rule_tail(ast_manager & m, app * pred, var_idx_set const & non_local_vars, unsigned & next_idx, varidx2var_map & varidx2var, sort_ref_buffer & new_rule_domain, expr_ref_buffer & new_rule_args, app_ref & new_pred) { expr_ref_buffer new_args(m); unsigned n = pred->get_num_args(); for (unsigned i = 0; i < n; i++) { expr * arg = pred->get_arg(i); if (m.is_value(arg)) { new_args.push_back(arg); } else { SASSERT(is_var(arg)); int vidx = to_var(arg)->get_idx(); var * new_var = 0; if (!varidx2var.find(vidx, new_var)) { new_var = m.mk_var(next_idx, to_var(arg)->get_sort()); next_idx++; varidx2var.insert(vidx, new_var); if (non_local_vars.contains(vidx)) { // other predicates used this variable... so it should be in the domain of the filter new_rule_domain.push_back(to_var(arg)->get_sort()); new_rule_args.push_back(new_var); } } SASSERT(new_var != 0); new_args.push_back(new_var); } } new_pred = m.mk_app(pred->get_decl(), new_args.size(), new_args.c_ptr()); }
void bool_rewriter::mk_and_as_or(unsigned num_args, expr * const * args, expr_ref & result) { expr_ref_buffer new_args(m()); for (unsigned i = 0; i < num_args; i++) { expr_ref tmp(m()); mk_not(args[i], tmp); new_args.push_back(tmp); } expr_ref tmp(m()); mk_or(new_args.size(), new_args.c_ptr(), tmp); mk_not(tmp, result); }
expr_ref_vector mk_array_instantiation::instantiate_pred(app*old_pred) { unsigned nb_old_args=old_pred->get_num_args(); //Stores, for each old position, the list of a new possible arguments vector<expr_ref_vector> arg_correspondance; for(unsigned i=0;i<nb_old_args;i++) { expr_ref arg(old_pred->get_arg(i), m); if(m_a.is_array(get_sort(arg))) { vector<expr_ref_vector> arg_possibilities(m_ctx.get_params().xform_instantiate_arrays_nb_quantifier(), retrieve_all_selects(arg)); arg_correspondance.append(arg_possibilities); if(!m_ctx.get_params().xform_instantiate_arrays_enforce()) { expr_ref_vector tmp(m); tmp.push_back(arg); arg_correspondance.push_back(tmp); } } else { expr_ref_vector tmp(m); tmp.push_back(arg); arg_correspondance.push_back(tmp); } } //Now, we need to deal with every combination expr_ref_vector res(m); svector<unsigned> chosen(arg_correspondance.size(), 0u); while(1) { expr_ref_vector new_args(m); for(unsigned i=0;i<chosen.size();i++) { new_args.push_back(arg_correspondance[i][chosen[i]].get()); } res.push_back(create_pred(old_pred, new_args)); unsigned pos=-1; do { pos++; if(pos==chosen.size()) { return res; } }while(chosen[pos]+1>=arg_correspondance[pos].size()); chosen[pos]++; } }
void distribute_forall::reduce1_quantifier(quantifier * q) { // This transformation is applied after skolemization/quantifier elimination. So, all quantifiers are universal. SASSERT(q->is_forall()); // This transformation is applied after basic pre-processing steps. // So, we can assume that // 1) All (and f1 ... fn) are already encoded as (not (or (not f1 ... fn))) // 2) All or-formulas are flat (or f1 (or f2 f3)) is encoded as (or f1 f2 f3) expr * e = get_cached(q->get_expr()); if (m_manager.is_not(e) && m_manager.is_or(to_app(e)->get_arg(0))) { // found target for simplification // (forall X (not (or F1 ... Fn))) // --> // (and (forall X (not F1)) // ... // (forall X (not Fn))) app * or_e = to_app(to_app(e)->get_arg(0)); unsigned num_args = or_e->get_num_args(); expr_ref_buffer new_args(m_manager); for (unsigned i = 0; i < num_args; i++) { expr * arg = or_e->get_arg(i); expr_ref not_arg(m_manager); // m_bsimp.mk_not applies basic simplifications. For example, if arg is of the form (not a), then it will return a. m_bsimp.mk_not(arg, not_arg); quantifier_ref tmp_q(m_manager); tmp_q = m_manager.update_quantifier(q, not_arg); expr_ref new_q(m_manager); elim_unused_vars(m_manager, tmp_q, new_q); new_args.push_back(new_q); } expr_ref result(m_manager); // m_bsimp.mk_and actually constructs a (not (or ...)) formula, // it will also apply basic simplifications. m_bsimp.mk_and(new_args.size(), new_args.c_ptr(), result); cache_result(q, result); } else { cache_result(q, m_manager.update_quantifier(q, e)); } }
void BD_Factory::add_gaol(double f_RES) { Array<const ExprNode> new_args(sip.n_arg+sip.p_arg); int I=0; int J=0; for (int K=0; K<sip.n_arg+sip.p_arg; K++) { if (!sip.is_param[K]) new_args.set_ref(K,new_vars[I++]); else // necessary for applying goal function but ignored at the end // (there is no parameter in the goal function): new_args.set_ref(K,ExprConstant::new_(sip.p_domain[J++],true)); } const ExprNode* goal_node=&((*sip.sys.goal)(new_args)); if (problem==ORA) { const ExprConstant& f_RES_node=ExprConstant::new_scalar(f_RES); goal_node = &((*goal_node) - f_RES); add_ctr(ExprCtr(*goal_node,LEQ)); const ExprNode& minus_eta=-new_vars[sip.n_arg]; add_goal(minus_eta); delete &minus_eta; } else { add_goal(*goal_node); } // cleanup for (int K=0; K<sip.n_arg+sip.p_arg; K++) { if (sip.is_param[K]) { if (!new_args[K].fathers.is_empty()) ibex_error("parameters in the objective"); delete &new_args[K]; } } cleanup(*goal_node,false); f_ctrs_copy.clone.clean(); }
expr_ref mk_array_instantiation::create_pred(app*old_pred, expr_ref_vector& n_args) { expr_ref_vector new_args(m); new_args.append(n_args); new_args.append(getId(old_pred, n_args)); for(unsigned i=0;i<new_args.size();i++) { if(m_a.is_select(new_args[i].get())) { new_args[i] = mk_select_var(new_args[i].get()); } } sort_ref_vector new_sorts(m); for(unsigned i=0;i<new_args.size();i++) new_sorts.push_back(get_sort(new_args[i].get())); expr_ref res(m); func_decl_ref fun_decl(m); fun_decl = m.mk_func_decl(symbol((old_pred->get_decl()->get_name().str()+"!inst").c_str()), new_sorts.size(), new_sorts.c_ptr(), old_pred->get_decl()->get_range()); m_ctx.register_predicate(fun_decl, false); if(src_set->is_output_predicate(old_pred->get_decl())) dst->set_output_predicate(fun_decl); res=m.mk_app(fun_decl,new_args.size(), new_args.c_ptr()); return res; }
/** \brief Apply local context simplification at (OR args[0] ... args[num_args-1]) Basic idea: - Replace args[i] by false in the other arguments - If args[i] is of the form (not t), then replace t by true in the other arguments. To make sure the simplification is efficient we bound the depth. */ bool bool_rewriter::local_ctx_simp(unsigned num_args, expr * const * args, expr_ref & result) { expr_ref_vector old_args(m()); expr_ref_vector new_args(m()); expr_ref new_arg(m()); expr_fast_mark1 neg_lits; expr_fast_mark2 pos_lits; bool simp = false; bool modified = false; bool forward = true; unsigned rounds = 0; while (true) { rounds++; #if 0 if (rounds > 10) verbose_stream() << "rounds: " << rounds << "\n"; #endif #define PUSH_NEW_ARG(ARG) { \ new_args.push_back(ARG); \ if (m().is_not(ARG)) \ neg_lits.mark(to_app(ARG)->get_arg(0)); \ else \ pos_lits.mark(ARG); \ } #define PROCESS_ARG() \ { \ expr * arg = args[i]; \ if (m().is_not(arg) && m().is_or(to_app(arg)->get_arg(0)) && \ simp_nested_not_or(to_app(to_app(arg)->get_arg(0))->get_num_args(), \ to_app(to_app(arg)->get_arg(0))->get_args(), \ neg_lits, \ pos_lits, \ new_arg)) { \ modified = true; simp = true; \ arg = new_arg; \ } \ if (simp_nested_eq_ite(arg, neg_lits, pos_lits, new_arg)) { \ modified = true; simp = true; \ arg = new_arg; \ } \ if (m().is_false(arg)) \ continue; \ if (m().is_true(arg)) { \ result = arg; \ return true; \ } \ if (m_flat && m().is_or(arg)) { \ unsigned sz = to_app(arg)->get_num_args(); \ for (unsigned j = 0; j < sz; j++) { \ expr * arg_arg = to_app(arg)->get_arg(j); \ PUSH_NEW_ARG(arg_arg); \ } \ } \ else { \ PUSH_NEW_ARG(arg); \ } \ } m_local_ctx_cost += 2*num_args; #if 0 static unsigned counter = 0; counter++; if (counter % 10000 == 0) verbose_stream() << "local-ctx-cost: " << m_local_ctx_cost << "\n"; #endif if (forward) { for (unsigned i = 0; i < num_args; i++) { PROCESS_ARG(); } forward = false; } else { unsigned i = num_args; while (i > 0) { --i; PROCESS_ARG(); } if (!modified) { if (simp) { result = mk_or_app(num_args, args); return true; } return false; // didn't simplify } // preserve the original order... std::reverse(new_args.c_ptr(), new_args.c_ptr() + new_args.size()); modified = false; forward = true; } pos_lits.reset(); neg_lits.reset(); old_args.reset(); old_args.swap(new_args); SASSERT(new_args.empty()); args = old_args.c_ptr(); num_args = old_args.size(); } }
void BD_Factory::add_discretized_ctr(double eps_g) { std::vector<double>* samples; switch (problem) { case LBD : samples = sip.LBD_samples; break; case UBD : samples = sip.UBD_samples; break; case ORA : samples = sip.ORA_samples; break; } // For each constraint (even non-quantified) for (int c=0; c<sip.sys.nb_ctr; c++) { //cout << "Constraint: " << sip.sys.ctrs[c] << endl; // build an initial set of (all) parameters values. // (includes those not involved in the constraint but these // values will be ignored anyway) Vector p_box(sip.p); for (int j=0; j<sip.p; j++) p_box[j]=samples[j].front(); // push this initial combination in a list list<Vector> p_boxes; //cout << " add p_box:" << p_box << endl; p_boxes.push_back(p_box); // We then create all combinations of LBD/UBD_samples for the parameters // involved in the constraint c, via a recursive Cartesian product // with all previously existing combinations. for (int j=0; j<sip.p; j++) { //cout << " param n°" << j << "=" << sip.varset.param(j); if (sip.sys.ctrs[c].f.used(sip.varset.param(j))) { //cout << " **used**" << endl; // for each box already inside the list... for (list<Vector>::iterator it2=p_boxes.begin(); it2!=p_boxes.end(); it2=p_boxes.erase(it2)) { p_box=*it2; // ... we instantiate the domain of the parameter to each of its current // sampled values for (vector<double>::iterator it=samples[j].begin(); it!=samples[j].end(); it++) { p_box[j]=*it; p_boxes.push_front(p_box); // pushed at the beginning -> no impact for the enclosing loop //cout << " add p_box:" << p_box << endl; } } } else { //cout << " (unused)" << endl; } } // all the constraints generated with constraint n°c // (stored in an array for cleanup) Array<const ExprNode> exprs_ctr(p_boxes.size()); int i=0; // We generate one constraint for each p_box for (list<Vector>::iterator it=p_boxes.begin(); it!=p_boxes.end(); it++) { // transform the "flat" box into a list of domains // (which is necessary for composition of functions expression) load(sip.p_domain, *it); Array<const ExprNode> new_args(sip.n_arg+sip.p_arg); int I=0; int J=0; for (int K=0; K<sip.n_arg+sip.p_arg; K++) { if (!sip.is_param[K]) // a variable x in the original system is a variable in // the LBD/UBD problem new_args.set_ref(K,new_vars[I++]); else // a parameter becomes a constant // (note: can have a reference because the expression is copied afterward) new_args.set_ref(K,ExprConstant::new_(sip.p_domain[J++],true)); } const ExprNode* expr_ctr_tmp=&sip.sys.ctrs[c].f(new_args); if (problem==UBD) { // In the UBD problem, shift the constraint with eps_g const ExprConstant& eps_node=ExprConstant::new_scalar(sip.sys.ctrs[c].op==LEQ ? eps_g : -eps_g); expr_ctr_tmp = & (*expr_ctr_tmp + eps_node); } else if (problem==ORA) { // in the ORA problem, shift the constraint with eta const ExprNode& eta=new_vars[sip.n_arg]; const ExprNode& eta_node=sip.sys.ctrs[c].op==LEQ ? eta : -eta; expr_ctr_tmp = & (*expr_ctr_tmp + eta_node); } // cleanup the constants created which // do not appear in the constraint expression for (int K=0; K<sip.n_arg+sip.p_arg; K++) { if (sip.is_param[K] && new_args[K].fathers.is_empty()) delete &new_args[K]; } const ExprNode& expr_ctr=expr_ctr_tmp->simplify(); ExprCtr ctr(expr_ctr, sip.sys.ctrs[c].op); //cout << " generated constraint:" << ctr << endl; add_ctr(ctr); exprs_ctr.set_ref(i++,expr_ctr); } cleanup(exprs_ctr,false); f_ctrs_copy.clone.clean(); //cout << endl; } }
int main(int argc, char **argv) { int *a; struct timespec begin, end; double delay; int rc; int n = 10 * 1024 * 1024; int nthreads = -1; int serial = 0; while(1) { int opt = getopt(argc, argv, "sn:t:"); if(opt < 0) break; switch(opt) { case 's': serial = 1; break; case 'n': n = atoi(optarg); if(n <= 0) goto usage; break; case 't': nthreads = atoi(optarg); if(nthreads <= 0) goto usage; break; default: goto usage; } } a = (int *) malloc(n * sizeof(int)); int i = 0; for(i = 0; i < n; i++) { if(i % 2 == 0) a[i] = i; else a[i] = -i; } clock_gettime(CLOCK_MONOTONIC, &begin); if(serial) { quicksort_serial(a, 0, n - 1); } else { rc = sched_init(nthreads, (n + 127) / 128, quicksort, new_args(a, 0, n - 1)); assert(rc >= 0); } clock_gettime(CLOCK_MONOTONIC, &end); delay = end.tv_sec + end.tv_nsec / 1000000000.0 - (begin.tv_sec + begin.tv_nsec / 1000000000.0); printf("Done in %lf seconds.\n", delay); i = 0; for(i = 0; i < n - 1; i++) { assert(a[i] <= a[i + 1]); } printf("aborded \n"); free(a); return 0; usage: printf("quicksort [-n size] [-t threads] [-s]\n"); return 1; }
inline bool zsuper(STATE, CallFrame* call_frame, intptr_t literal) { Object* block = stack_pop(); Object* const recv = call_frame->self(); VariableScope* scope = call_frame->method_scope(state); interp_assert(scope); MachineCode* mc = scope->method()->machine_code(); Object* splat_obj = 0; Array* splat = 0; size_t arg_count = mc->total_args; if(mc->splat_position >= 0) { splat_obj = scope->get_local(state, mc->splat_position); splat = try_as<Array>(splat_obj); if(splat) { arg_count += splat->size(); } else { arg_count++; } } Tuple* tup = Tuple::create(state, arg_count); native_int tup_index = 0; native_int fixed_args; if(splat) { fixed_args = mc->splat_position; } else if(mc->keywords) { fixed_args = mc->total_args - 1; } else { fixed_args = mc->total_args; } for(native_int i = 0; i < fixed_args; i++) { tup->put(state, tup_index++, scope->get_local(state, i)); } if(splat) { for(native_int i = 0; i < splat->size(); i++) { tup->put(state, tup_index++, splat->get(state, i)); } } else if(splat_obj) { tup->put(state, tup_index++, splat_obj); } if(mc->post_args) { native_int post_position = mc->splat_position + 1; for(native_int i = post_position; i < post_position + mc->post_args; i++) { tup->put(state, tup_index++, scope->get_local(state, i)); } } if(mc->keywords) { native_int placeholder_position = splat_obj ? mc->total_args : mc->total_args - 1; native_int keywords_position = placeholder_position + 1; Object* placeholder = scope->get_local(state, placeholder_position); Array* ary = Array::create(state, 2); for(native_int i = keywords_position; i <= mc->keywords_count; i++) { ary->set(state, 0, as<Symbol>(call_frame->compiled_code->local_names()->at(state, i))); ary->set(state, 1, scope->get_local(state, i)); placeholder->send(state, state->symbol("[]="), ary); } tup->put(state, tup_index++, scope->get_local(state, placeholder_position)); } CallSite* call_site = reinterpret_cast<CallSite*>(literal); Arguments new_args(call_site->name(), recv, block, arg_count, 0); new_args.use_tuple(tup, arg_count); Object* ret; Symbol* current_name = call_frame->original_name(); if(call_site->name() != current_name) { call_site->name(state, current_name); } ret = call_site->execute(state, new_args); state->vm()->checkpoint(state); CHECK_AND_PUSH(ret); }
static void low_accept_loop(struct args *arg) { struct args *arg2 = new_args(); ACCEPT_SIZE_T len = sizeof(arg->from); while(1) { MEMCPY(arg2, arg, sizeof(struct args)); arg2->fd = fd_accept(arg->fd, (struct sockaddr *)&arg2->from, &len); if(arg2->fd != -1) { th_farm((void (*)(void *))aap_handle_connection, arg2); arg2 = new_args(); arg2->res.leftovers = 0; } else { if(errno == EBADF) { int i; struct cache_entry *e, *t; struct cache *c, *p = NULL; struct log *l, *n = NULL; /* oups. */ low_mt_lock_interpreter(); /* Can run even if threads_disabled. */ for(i=0; i<CACHE_HTABLE_SIZE; i++) { e = arg->cache->htable[i]; while(e) { t = e; e = e->next; t->next = 0; free_string(t->data); aap_free(t->url); aap_free(t); } } while(arg->log->log_head) { struct log_entry *l = arg->log->log_head->next; aap_free(arg->log->log_head); arg->log->log_head = l; } c = first_cache; while(c && c != arg->cache) { p=c; c = c->next; } if(c) { if(p) p->next = c->next; else first_cache = c->next; c->gone = 1; aap_free(c); } l = aap_first_log; while(l && l != arg->log) { n=l; l = l->next; } if(l) { if(n) n->next = l->next; else aap_first_log = l->next; aap_free(l); } mt_unlock_interpreter(); aap_free(arg2); aap_free(arg); return; /* No more accept here.. */ } } } }