/* Vérification du mode: -n : next_fit -f : first_fit -b : best_fit */ void verification_mode(char* mode) { int* tab_objets = (int*) malloc(sizeof(int) * n_objets); if (strcmp(mode, "-n") == 0) { printf("Mode NextFit : OK\n"); next_fit(tab_objets); return; } if (strcmp(mode, "-f") == 0) { printf("Mode FirstFit : OK\n"); first_fit(tab_objets); return; } if (strcmp(mode, "-b") == 0) { printf("Mode BestFit : OK\n"); best_fit(tab_objets); return; } free(tab_objets); perror("Le mode n'existe pas..."); exit(EXIT_FAILURE); }
/** Allocate an extent of the specified size (best fit first) from the extent map, removing the returned extent from the map. */ Extent ExtentMap::allocate_best_fit(rose_addr_t size) { iterator found = best_fit(size, begin()); if (found==end()) throw std::bad_alloc(); Extent retval(found->first.first(), size); erase(retval); return retval; }
void *malloc_intern(size_t size) { t_list *list; list = g_alloc_list; if ((list = best_fit(size)) != NULL) return (alloc_at(list, size)); if ((list = alloc_new(size)) == NULL) return (NULL); if (list->used == TRUE) return (list + 1); return (alloc_at(list, size)); }
void *free_allocation(info_p handler, long n_bytes){ int full_mem=0; if(n_bytes < 0){ printf("Free Allocation Error: requested negative space\n"); return NULL; } if(handler->n_bytes < n_bytes){ printf("Free Allocation Error: requested too much space\n"); return NULL; //User requested more space than permitted } if(n_bytes == handler->n_bytes){ //printf("request:%lu; bytes: %lu; declaring full memory\n", n_bytes, handler->n_bytes); full_mem=1; } if(handler->free_list == NULL){ return NULL; } void *mem = handler->memptr-4; //printf("free list head: %p; size of mem is: %lu\n",handler->free_list, *(long *)mem); switch(handler->flags){ case (FREE1_ALLOC): //first fit return first_fit(handler, handler->free_list, n_bytes,full_mem); break; case (FREE2_ALLOC): //next fit return next_fit(handler, n_bytes); break; case (FREE3_ALLOC): //best fit return best_fit(handler, n_bytes); break; case (FREE4_ALLOC): //worst fit return worst_fit(handler, n_bytes); break; default: //should never happen return NULL; } printf("Not enough memory to allocate\n"); return NULL; }
/* * @brief Détermine la bonne méthode d'allocation * * @param kv descripteur d'accès à la base * @param key clé * @param val valeur * @param offset index dans le .kv modifié par effet de bord */ int kv_put_dkv(KV *kv, const kv_datum *key, const kv_datum *val, len_t *offset) { if(kv->alloc == 0) { return first_fit(kv, key, val, offset); } else if(kv->alloc == 1) { return worst_fit(kv, key, val, offset); } else if(kv->alloc == 2) { return best_fit(kv, key, val, offset); } else { errno = EINVAL; return -1; } }
int main() { printf("请输入内存大小\n"); int n; scanf("%d", &n); NODE *phead; //空闲链 init_list(&phead, 0); init_list_a(phead, n); //init_list_b(phead, n); NODE *head; init_list(&head, 0); NODE *hd; init_list(&hd, 0); printf("首次适应算法-1 循环首次适应算法-2 最佳适应算法-3 最坏适应算法-4\n"); int ch; scanf("%d", &ch); while (1) { if (ch == 1) { first_fit(phead, head, hd); break; } else if (ch == 2) { next_fit(phead, head); break; } else if (ch == 3) { best_fit(phead, head); break; } else if (ch == 4) { best_fit1(phead, head); break; } } return 0; }
//only add function called from SRT or RR. Determines which memory algorithm to use bool Memory::add_memory(char proc, int proc_size, int time){ if(algo == "First-Fit"){ first_fit(proc, proc_size, time); } else if(algo == "Next-Fit"){ next_fit(proc, proc_size, time); } else if(algo == "Best-Fit"){ best_fit(proc, proc_size, time); } else{ return false; } printMem(time); return true; }
static void fit_curves(args_t *args) { int i; for (i=0; i<args->ndist; i++) { dist_t *dist = &args->dist[i]; if ( dist->copy_number!=0 ) { fprintf(args->dat_fp,"CN\t%s\t%.2f\n", dist->chr,(float)dist->copy_number); save_dist(args, i, 0, NULL); continue; } // Parameters (center,scale,sigma) for gaussian peaks. double params_cn2[] = { 1/2.,0.5,0.05 }; double params_cn3[] = { 1/3.,0.5,0.05, 2/3.,0.5,0.05 }; double params_cn4[] = { 1/4.,0.5,0.05, 1/2.,0.5,0.05, 3/4.,0.5,0.05 }; double fit_cn2 = best_fit(args,&args->dist[i],1,params_cn2); double fit_cn3 = best_fit(args,&args->dist[i],2,params_cn3); double fit_cn4 = best_fit(args,&args->dist[i],3,params_cn4); double dx_cn3 = fabs(params_cn3[0] - params_cn3[3]); double dx_cn4 = fabs(params_cn4[0] - params_cn4[6]); double dy_cn3 = params_cn3[1] > params_cn3[4] ? params_cn3[4]/params_cn3[1] : params_cn3[1]/params_cn3[4]; double dy_cn4a = params_cn4[1] > params_cn4[7] ? params_cn4[7]/params_cn4[1] : params_cn4[1]/params_cn4[7]; // side peaks double ymax = params_cn4[1] > params_cn4[7] ? params_cn4[1] : params_cn4[7]; double dy_cn4b = ymax > params_cn4[4] ? params_cn4[4]/ymax : ymax/params_cn4[4]; // middle peak // Three peaks (CN4) are always a better fit than two (CN3) or one (CN2). Therefore // check that peaks are well separated and that the peak sizes are reasonable char cn2_fail = 0, cn3_fail = 0, cn4_fail = 0; if ( fit_cn2 > args->fit_th ) cn2_fail = 'f'; if ( fit_cn3 > args->fit_th ) cn3_fail = 'f'; else if ( dx_cn3 < 0.05 ) cn3_fail = 'x'; // peak separation: at least ~10% of cells else if ( dy_cn3 < args->peak_symmetry ) cn3_fail = 'y'; if ( fit_cn4 > args->fit_th ) cn4_fail = 'f'; else if ( dx_cn4 < 0.1 ) cn4_fail = 'x'; // peak separation else if ( dy_cn4a < args->peak_symmetry ) cn4_fail = 'y'; else if ( dy_cn4b < args->peak_symmetry ) cn4_fail = 'Y'; // Estimate fraction of affected cells. For CN4 we estimate // contamination (the fraction of foreign cells), which is more // common than CN4; hence the value is from the interval [0,0.5]. // CN3 .. f = 2*dx/(1-dx) // CN4 .. f = dx dx_cn3 = 2*dx_cn3 / (1-dx_cn3); double cn = -1, fit = fit_cn2; if ( !cn2_fail ) { cn = 2; fit = fit_cn2; } if ( !cn3_fail && fit_cn3 < args->cn_penalty * fit ) { cn = 3; fit = fit_cn3; } if ( !cn4_fail && fit_cn4 < args->cn_penalty * fit ) { cn = 4; fit = fit_cn4; } if ( cn==-1 ) save_dist(args, i, 0, NULL); else if ( cn==2 ) save_dist(args, i, 1, params_cn2); else if ( cn==3 ) { save_dist(args, i, 2, params_cn3); cn = 2 + dx_cn3; } else if ( cn==4 ) { save_dist(args, i, 3, params_cn4); cn = 3 + dx_cn4; } if ( args->verbose ) { printf("%s: \n", args->dist[i].chr); print_params(&args->dist[i].dat, 1, params_cn2, fit_cn2, 1.0, cn2_fail, cn==2 ? '*' : ' '); print_params(&args->dist[i].dat, 2, params_cn3, fit_cn3, dx_cn3, cn3_fail, cn>2 && cn<=3 ? '*' : ' '); print_params(&args->dist[i].dat, 3, params_cn4, fit_cn4, dx_cn4, cn4_fail, cn>3 ? '*' : ' '); printf("\n"); } fprintf(args->dat_fp,"CN\t%s\t%.2f\t%f\n", dist->chr, cn, fit); } }
RcppExport SEXP FitPhasingBurst(SEXP R_signal, SEXP R_flowCycle, SEXP R_read_sequence, SEXP R_phasing, SEXP R_burstFlows, SEXP R_maxEvalFlow, SEXP R_maxSimFlow) { SEXP ret = R_NilValue; char *exceptionMesg = NULL; try { Rcpp::NumericMatrix signal(R_signal); Rcpp::NumericMatrix phasing(R_phasing); // Standard phasing parameters string flowCycle = Rcpp::as<string>(R_flowCycle); Rcpp::StringVector read_sequences(R_read_sequence); Rcpp::NumericVector phasing_burst(R_burstFlows); Rcpp::NumericVector max_eval_flow(R_maxEvalFlow); Rcpp::NumericVector max_sim_flow(R_maxSimFlow); int window_size = 38; // For normalization ion::FlowOrder flow_order(flowCycle, flowCycle.length()); unsigned int num_flows = flow_order.num_flows(); unsigned int num_reads = read_sequences.size(); // Containers to store results Rcpp::NumericVector null_fit(num_reads); Rcpp::NumericMatrix null_prediction(num_reads, num_flows); Rcpp::NumericVector best_fit(num_reads); Rcpp::NumericVector best_ie_value(num_reads); Rcpp::NumericMatrix best_prediction(num_reads, num_flows); BasecallerRead bc_read; DPTreephaser dpTreephaser(flow_order); DPPhaseSimulator PhaseSimulator(flow_order); vector<double> cf_vec(num_flows, 0.0); vector<double> ie_vec(num_flows, 0.0); vector<double> dr_vec(num_flows, 0.0); // IE Burst Estimation Loop for (unsigned int iRead=0; iRead<num_reads; iRead++) { // Set read object vector<float> my_signal(num_flows); for (unsigned int iFlow=0; iFlow<num_flows; iFlow++) my_signal.at(iFlow) = signal(iRead, iFlow); bc_read.SetData(my_signal, num_flows); string my_sequence = Rcpp::as<std::string>(read_sequences(iRead)); // Default phasing as baseline double my_best_fit, my_best_ie; double base_cf = (double)phasing(iRead, 0); double base_ie = (double)phasing(iRead, 1); double base_dr = (double)phasing(iRead, 2); int burst_flow = (int)phasing_burst(iRead); vector<float> my_best_prediction; cf_vec.assign(num_flows, base_cf); dr_vec.assign(num_flows, base_dr); int my_max_flow = min((int)num_flows, (int)max_sim_flow(iRead)); int my_eval_flow = min(my_max_flow, (int)max_eval_flow(iRead)); PhaseSimulator.SetBaseSequence(my_sequence); PhaseSimulator.SetMaxFlows(my_max_flow); PhaseSimulator.SetPhasingParameters_Basic(base_cf, base_ie, base_dr); PhaseSimulator.UpdateStates(my_max_flow); PhaseSimulator.GetPredictions(bc_read.prediction); dpTreephaser.WindowedNormalize(bc_read, (my_eval_flow/window_size), window_size, true); my_best_ie = base_ie; my_best_prediction = bc_read.prediction; my_best_fit = 0; for (int iFlow=0; iFlow<my_eval_flow; iFlow++) { double residual = bc_read.raw_measurements.at(iFlow) - bc_read.prediction.at(iFlow); my_best_fit += residual*residual; } for (unsigned int iFlow=0; iFlow<num_flows; iFlow++) null_prediction(iRead, iFlow) = bc_read.prediction.at(iFlow); null_fit(iRead) = my_best_fit; // Make sure that there are enough flows to fit a burst if (burst_flow < my_eval_flow-10) { int num_steps = 0; double step_size = 0.0; double step_start = 0.0; double step_end = 0.0; // Brute force phasing burst value estimation using grid search, crude first, then refine for (unsigned int iIteration = 0; iIteration<3; iIteration++) { switch(iIteration) { case 0: step_size = 0.05; step_end = 0.8; break; case 1: step_end = (floor(my_best_ie / step_size)*step_size) + step_size; step_start = max(0.0, (step_end - 2.0*step_size)); step_size = 0.01; break; default: step_end = (floor(my_best_ie / step_size)*step_size) + step_size; step_start = max(0.0, step_end - 2*step_size); step_size = step_size / 10; } num_steps = 1+ ((step_end - step_start) / step_size); for (int iPhase=0; iPhase <= num_steps; iPhase++) { double try_ie = step_start+(iPhase*step_size); ie_vec.assign(num_flows, try_ie); PhaseSimulator.SetBasePhasingParameters(burst_flow, cf_vec, ie_vec, dr_vec); PhaseSimulator.UpdateStates(my_max_flow); PhaseSimulator.GetPredictions(bc_read.prediction); dpTreephaser.WindowedNormalize(bc_read, (my_eval_flow/window_size), window_size, true); double my_fit = 0.0; for (int iFlow=burst_flow+1; iFlow<my_eval_flow; iFlow++) { double residual = bc_read.raw_measurements.at(iFlow) - bc_read.prediction.at(iFlow); my_fit += residual*residual; } if (my_fit < my_best_fit) { my_best_fit = my_fit; my_best_ie = try_ie; my_best_prediction = bc_read.prediction; } } } } // Set output information for this read best_fit(iRead) = my_best_fit; best_ie_value(iRead) = my_best_ie; for (unsigned int iFlow=0; iFlow<num_flows; iFlow++) best_prediction(iRead, iFlow) = my_best_prediction.at(iFlow); } ret = Rcpp::List::create(Rcpp::Named("null_fit") = null_fit, Rcpp::Named("null_prediction") = null_prediction, Rcpp::Named("burst_flow") = phasing_burst, Rcpp::Named("best_fit") = best_fit, Rcpp::Named("best_ie_value") = best_ie_value, Rcpp::Named("best_prediction") = best_prediction); } catch(std::exception& ex) { forward_exception_to_r(ex); } catch(...) { ::Rf_error("c++ exception (unknown reason)"); } if(exceptionMesg != NULL) Rf_error(exceptionMesg); return ret; }
void impl_aux(bip_context* c, int* fixed, int* alpha, int* workplace, int* candidate, int* parents, int level, int* node) { if(level == c->num_vars) { return; } /* Register node num */ int c_node = (*node); (*node) = c_node + 1; parents[level] = c_node; imp_node_open(c, fixed, parents, c_node); /* Calculate best fit and test if performance is improved */ int bf = best_fit(c, fixed, workplace); imp_node_log_bf(c, fixed, workplace, bf, *alpha); /* LOG */ if((c->maximize && (bf <= *alpha)) || (!c->maximize && (bf >= *alpha))) { DEBUG("Node %i: Close node. Doesn't improve performance.\n", c_node); imp_node_close(c, doesnt_improve); /* LOG */ return; } /* Check factibility */ imp_node_log_rc(c); /* LOG */ bool fact = check_restrictions(c, workplace); if(fact) { /* Set the solution as new candidate */ for(int i = 0; i < c->num_vars; i++) { candidate[i] = workplace[i]; } /* Set alpha as the new performance */ (*alpha) = bf; DEBUG("Node %i: Close node. New candidate solution: %i.\n", c_node, bf); imp_node_close(c, new_candidate); /* LOG */ return; } /* Not factible, check possible future factibility */ imp_node_log_ff(c); bool future_fact = check_future_fact(c, fixed, workplace); if(!future_fact) { DEBUG("Node %i: Close node. Not factible.\n", c_node); imp_node_close(c, not_factible); /* LOG */ return; } DEBUG("Node %i: Expand node. Possible future factibility.\n", c_node); imp_node_close(c, expand); /* LOG */ fixed[level] = 0; impl_aux(c, fixed, alpha, workplace, candidate, parents, level + 1, node); for(int i = level + 1; i < c->num_vars; i++) { fixed[i] = -1; parents[i] = -1; } fixed[level] = 1; impl_aux(c, fixed, alpha, workplace, candidate, parents, level + 1, node); for(int i = level + 1; i < c->num_vars; i++) { fixed[i] = -1; parents[i] = -1; } return; }