//****************************************************************************** static void tmr_handler(void) { uint32_t term_cnt32 = US_TIMER->term_cnt32; US_TIMER->term_cnt32 = 0xFFFFFFFF; // reset to max value to prevent further interrupts US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt NVIC_ClearPendingIRQ(US_TIMER_IRQn); inc_current_cnt(term_cnt32); if (event_passed(current_cnt + US_TIMER->count32, event_cnt )) { // the timestamp has expired event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value us_ticker_irq_handler(); } else { uint64_t diff = event_diff(current_cnt, event_cnt); if (diff < (uint64_t)0xFFFFFFFF) { // the event occurs before the next overflow US_TIMER->term_cnt32 = diff; // Since the timer keeps counting after the terminal value is reached, it is possible that the new // terminal value is in the past. if (US_TIMER->term_cnt32 < US_TIMER->count32) { // the timestamp has expired US_TIMER->term_cnt32 = 0xFFFFFFFF; // reset to max value to prevent further interrupts US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt NVIC_ClearPendingIRQ(US_TIMER_IRQn); event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value us_ticker_irq_handler(); } } } }
//****************************************************************************** void us_ticker_set_interrupt(timestamp_t timestamp) { // Note: interrupts are disabled before this function is called. US_TIMER->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; // disable timer if (US_TIMER->intfl) { US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt NVIC_ClearPendingIRQ(US_TIMER_IRQn); inc_current_cnt(US_TIMER->term_cnt32); } // add and reset the current count value inc_current_cnt(US_TIMER->count32); US_TIMER->count32 = 0; // add the number of cycles that the timer is disabled here for inc_current_cnt(200); event_cnt = (uint64_t)timestamp * ticks_per_us; // Check to see if the event has already passed if (!event_passed(current_cnt, event_cnt)) { uint64_t diff = event_diff(current_cnt, event_cnt); if (diff < (uint64_t)0xFFFFFFFF) { // the event occurs before the next overflow US_TIMER->term_cnt32 = diff; } else { // the event occurs after the next overflow US_TIMER->term_cnt32 = 0xFFFFFFFF; // set to max } } else { // the requested timestamp occurs in the past // set the timer up to immediately expire US_TIMER->term_cnt32 = 1; } US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer }
void run_gp(multipop *mpop, int startgen, event *t_eval, event *t_breed, int startfromcheckpoint) { char *param; int gen; int maxgen; int exch_gen; int i, j; int checkinterval; char *checkfileformat; char *checkfilename = NULL; event start, end, diff; int term = 0; termination_override =0; int stt_interval; int bestn; if (!startfromcheckpoint) { /* get the number of top individuals to track. */ bestn = atoi(get_parameter("output.bestn")); if (bestn < 1) { error( E_WARNING, "\"output.bestn\" must be at least 1. defaulting to 1."); bestn = 1; } /* allocate statistics for overall run. */ run_stats = (popstats *) MALLOC((mpop->size + 1) * sizeof(popstats)); for (i = 0; i < mpop->size + 1; ++i) { run_stats[i].bestn = bestn; run_stats[i].size = -1; } /* initialize the linked list of saved individuals. */ saved_head = (saved_ind *) MALLOC(sizeof(saved_ind)); saved_head->ind = NULL; saved_head->refcount = 0; saved_head->next = NULL; saved_tail = saved_head; } /* get the maximum number of generations. */ param = get_parameter("max_generations"); if (param == NULL) error( E_FATAL_ERROR, "no value specified for \"max_generations\"."); maxgen = atoi(param); if (maxgen <= 0) error( E_FATAL_ERROR, "\"max_generations\" must be greater than zero."); /* get the interval for subpopulation exchanges, if there is more than one subpopulation. */ if (mpop->size > 1) { param = get_parameter("multiple.exch_gen"); if (param == NULL) error( E_FATAL_ERROR, "no value specified for \"multiple.exch_gen\"."); exch_gen = atoi(param); if (exch_gen <= 0) error( E_FATAL_ERROR, "\"multiple.exch_gen\" must be greater than zero."); } /* get the interval for doing checkpointing. */ param = get_parameter("checkpoint.interval"); if (param == NULL) /* checkpointing disabled. */ checkinterval = -1; else checkinterval = atoi(param); /* get the format string for the checkpoint filenames. */ checkfileformat = get_parameter("checkpoint.filename"); checkfilename = (char *) MALLOC(strlen(checkfileformat) + 50); /* get the interval for writing information to the .stt file. */ stt_interval = atoi(get_parameter("output.stt_interval")); if (stt_interval < 1) error( E_FATAL_ERROR, "\"output.stt_interval\" must be greater than zero."); oputs( OUT_SYS, 10, "\n\nstarting evolution.\n"); /* print out how often we'll be doing checkpointing. */ if (checkinterval > 0) oprintf( OUT_SYS, 20, "checkpointing will be done every %d generations and " "after the last generation.\n", checkinterval); else if (checkinterval == 0) oprintf( OUT_SYS, 20, "checkpointing will be done only after the last " "generation.\n"); else oprintf( OUT_SYS, 20, "no checkpointing will be done.\n"); /* the big loop. */ for (gen = startgen; gen < maxgen && !term; ++gen) { oprintf( OUT_SYS, 20, "=== generation %d.\n", gen); generation_No = gen; /* unless this is the first generation after loading a checkpoint file... */ if (!(startfromcheckpoint && gen == startgen)) { /* evaluate the population. */ event_mark(&start); for (i = 0; i < mpop->size; ++i) { //generation_No = i; evaluate_pop(mpop->pop[i]); } event_mark(&end); event_diff(&diff, &start, &end); #ifdef TIMING_AVAILABLE oprintf( OUT_SYS, 40, " evaluation complete. (%s)\n", event_string(&diff)); #else oprintf ( OUT_SYS, 40, " evaluation complete.\n" ); #endif event_accum(t_eval, &diff); /* calculate and print statistics. returns 1 if user termination criterion was met, 0 otherwise. */ term = generation_information(gen, mpop, stt_interval, run_stats[0].bestn); if (term) { //oprintf( OUT_SYS, 30, "user termination criterion met.\n"); /*extern float *optimal_in_generation; extern int *optimal_index_in_generation; extern int same_optimal_count; int i; for (i = 0; i < generationSIZE; i++) { if ((int) optimal_in_generation[i] == -1) { printf("tried to Break"); break; } printf("Index: %d ERR : %f -Index %d Same : %i\n", i, optimal_in_generation[i], optimal_index_in_generation[i], same_optimal_count); }*/ } flush_output_streams(); } /** write a checkpoint file if checkinterval is non-negative and: we've reached the last generation, or the user termination criterion has been met, or we've reached the specified checkpoint interval. **/ if (checkinterval >= 0 && (gen == maxgen || term || (checkinterval > 0 && gen > startgen && (gen % checkinterval) == 0))) { sprintf(checkfilename, checkfileformat, gen); write_checkpoint(gen, mpop, checkfilename); } /** if this is not the last generation and the user criterion hasn't been met, then do breeding. **/ if (gen != maxgen && !term) { /** exchange subpops if it's time. **/ if (mpop->size > 1 && gen && (gen % exch_gen) == 0) { exchange_subpopulations(mpop); oprintf( OUT_SYS, 10, " subpopulation exchange complete.\n"); } /* breed the new population. */ event_mark(&start); for (i = 0; i < mpop->size; ++i) mpop->pop[i] = change_population(mpop->pop[i], mpop->bpt[i]); event_mark(&end); event_diff(&diff, &start, &end); /* call the application end-of-breeding callback. */ app_end_of_breeding(gen, mpop); #ifdef TIMING_AVAILABLE oprintf( OUT_SYS, 30, " breeding complete. (%s)\n", event_string(&diff)); #else oprintf ( OUT_SYS, 30, " breeding complete.\n" ); #endif event_accum(t_breed, &diff); } /* free unused ERCs. */ ephem_const_gc(); flush_output_streams(); } /** free up a lot of stuff before returning. */ if (checkfilename) FREE(checkfilename); ephem_const_gc(); for (i = 0; i < mpop->size + 1; ++i) { for (j = 0; j < run_stats[i].bestn; ++j) --run_stats[i].best[j]->refcount; FREE(run_stats[i].best); } FREE(run_stats); saved_individual_gc(); FREE(saved_head); }