char *elapsed_remaining (ELAPSED *e, unsigned long current, unsigned long maxval, const char *suffix) { double percent = (double)current * 100.0 / (double)maxval; double rem; char bb[64]; unsigned long dval; double dd; elapsed_stop (e); dval = current - e->prevVal; rem = diff_us (&e->EndPoint, &e->StartPoint); dd = diff_us (&e->EndPoint, &e->prevPoint); dd = (double)dval*1.0e6 / dd; rem = rem * (100.0 - percent) / percent; rem /= 1.0e6; remaining_out (e, percent, (unsigned long)rem); if (dd > 1000) sprintf (bb, " %s %s ", print_big_number((unsigned long)dd), suffix ? suffix : "op/s"); else sprintf (bb, " %.0f %s ", dd, suffix ? suffix : "op/s"); strcat (e->buf, bb); e->prevVal = current; memcpy (&e->prevPoint, &e->EndPoint, sizeof(struct timeval)); return e->buf; }
int cmd_client(int argc, char *argv[]) { void *context = zmq_init(1); assert(context); void *subscriber = zmq_socket(context, ZMQ_SUB); assert(subscriber); int rc; rc = zmq_connect(subscriber, "tcp://localhost:50001"); assert_rc; rc = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "", 0); assert_rc; timeval tv_start; gettimeofday_exn(&tv_start); long long num_msgs = 0; long long total_latency = 0; // in us while (1) { zmq_msg_t message; rc = zmq_msg_init(&message); assert_rc; rc = zmq_recv(subscriber, &message, 0); if (0 != rc) break; long long sec; long long usec; sscanf(zmq_msg_data(&message), "%lld %lld ", &sec, &usec); timeval tv_source_sent; tv_source_sent.tv_sec = sec; tv_source_sent.tv_usec = usec; ignore(zmq_msg_close(&message)); ++num_msgs; timeval tv_interval_end; gettimeofday_exn(&tv_interval_end); total_latency += diff_us(&tv_interval_end, &tv_source_sent); if (num_msgs % 100000 == 0) { printf("\rMsgs: %lld; Freq: %lld msgs/sec; Ltncy: %lld; ", num_msgs, ((num_msgs * 1000000) / diff_us(&tv_interval_end, &tv_start)), (total_latency / num_msgs)); fflush(stdout); } } printf("\nExited cleanly\n"); ignore(zmq_close(subscriber)); ignore(zmq_term(context)); return 0; }
double CFS::cost (ElementSubset * X) { timeval begin, end; gettimeofday (& begin, NULL); double cost; number_of_calls_of_cost_function++; if (set->get_set_cardinality () == 0) cost = 0; else if (X->get_subset_cardinality () == 0) cost = INFTY; else cost = - evaluateSubset (X); gettimeofday (& end, NULL); elapsed_time_of_cost_function_calls += diff_us (end, begin); // Threshold is a maximum number of calls of the cost function // if ((has_max_number_of_calls_set) && (number_of_calls_of_cost_function >= max_number_of_calls_of_cost_function)) reached_threshold = true; return cost; }
double Explicit::cost (ElementSubset * X) { timeval begin, end; gettimeofday (& begin, NULL); double cost = 0; number_of_calls_of_cost_function++; unsigned int i; string key = "_"; // example of a key: _010101 if (! (X->get_set_cardinality() == 0)) { for (i = 0; i < set->get_set_cardinality (); i++) if (X->has_element (i)) key.append ("1"); else key.append ("0"); cost = set->get_explicit_cost (key); } gettimeofday (& end, NULL); elapsed_time_of_cost_function_calls += diff_us (end, begin); // Threshold is a maximum number of calls of the cost function // if ((has_max_number_of_calls_set) && (number_of_calls_of_cost_function >= max_number_of_calls_of_cost_function)) reached_threshold = true; return cost; }
void RBM::get_minima_list (unsigned int max_size_of_minima_list) { timeval begin_program, end_program; gettimeofday (& begin_program, NULL); // unsigned int direction; Collection * L = new Collection (); bool search_space_is_empty = false; ElementSubset * X, Y ("", set); do { // at each iteration it is called either minimal_element or maximal_element direction = rand () % 2; if (direction == 0) { search_space_is_empty = down_up_direction (L, lower_restriction, upper_restriction, cost_function, set); } else { search_space_is_empty = up_down_direction (L, lower_restriction, upper_restriction, cost_function, set); } while (L->size() > 0) { X = L->remove_last_subset (); list_of_minima.push_back (X); if (store_visited_subsets) list_of_visited_subsets->add_subset (X); } clean_list_of_minima (max_size_of_minima_list); } while ((! search_space_is_empty) && (! cost_function->has_reached_threshold ())); delete L; // number_of_visited_subsets = cost_function->get_number_of_calls_of_cost_function (); clean_list_of_minima (max_size_of_minima_list); gettimeofday (& end_program, NULL); elapsed_time_of_the_algorithm = diff_us (end_program, begin_program); }
void CHCGA::get_minima_list (unsigned int max_size_of_minima_list) { timeval begin_program, end_program; gettimeofday (& begin_program, NULL); unsigned int cataclysms = 20; list<ElementSubset *> population; list<ElementSubset *> offspring; list<ElementSubset *>::iterator it; // CHCGAPopulation pop (set, cost_function); pop.start_population (50); population = pop.get_population (); for (it = population.begin (); it != population.end (); it++) { list_of_visited_subsets->add_subset (*it); list_of_minima.push_back (new ElementSubset (*it)); } if (!cost_function->has_reached_threshold ()) { while (cataclysms > 0 && !cost_function->has_reached_threshold ()) { offspring = pop.recombine (); if (pop.fittest_survival (offspring)) cataclysms--; population = pop.get_population (); for (it = population.begin (); it != population.end (); it++) { list_of_visited_subsets->add_subset (*it); list_of_minima.push_back (new ElementSubset (*it)); } clean_list_of_minima (max_size_of_minima_list); } } number_of_visited_subsets = cost_function->get_number_of_calls_of_cost_function (); clean_list_of_minima (max_size_of_minima_list); gettimeofday (& end_program, NULL); elapsed_time_of_the_algorithm = diff_us (end_program, begin_program); }
double SubsetSum::cost (ElementSubset * X) { timeval begin, end; gettimeofday (& begin, NULL); double cost = 0; number_of_calls_of_cost_function++; // unsigned int i, j; Element * elem; if (! (X->get_set_cardinality () == 0)) { for (i = 0; i < set->get_set_cardinality (); i++) if (X->has_element (i)) { elem = set->get_element (i); for (j = 0; j < elem->get_number_of_values (); j++) cost += elem->get_element_value (j); } cost = fabs (t - cost); } // gettimeofday (& end, NULL); elapsed_time_of_cost_function_calls += diff_us (end, begin); // Threshold is a maximum number of calls of the cost function // if ((has_max_number_of_calls_set) && (number_of_calls_of_cost_function >= max_number_of_calls_of_cost_function)) reached_threshold = true; return cost; }
int cmd_server(int argc, char *argv[]) { void *context = zmq_init(1); assert(context); void *subscriber = zmq_socket(context, ZMQ_SUB); assert(subscriber); int rc; rc = zmq_connect(subscriber, "tcp://localhost:50000"); assert_rc; rc = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "", 0); assert_rc; void *publisher = zmq_socket(context, ZMQ_PUB); assert(publisher); rc = zmq_bind(publisher, "tcp://*:50001"); assert_rc; timeval tv_start; gettimeofday_exn(&tv_start); long long num_msgs = 0; catch_signals(); while (!interrupted) { zmq_msg_t in_message; rc = zmq_msg_init(&in_message); assert_rc; rc = zmq_recv(subscriber, &in_message, 0); if (0 != rc) break; int size = zmq_msg_size(&in_message); zmq_msg_t out_message; rc = zmq_msg_init_size(&out_message, size); assert_rc; memcpy(zmq_msg_data(&out_message), zmq_msg_data(&in_message), size); rc = zmq_send(publisher, &out_message, 0); assert_rc; ignore(zmq_msg_close(&out_message)); ignore(zmq_msg_close(&in_message)); ++num_msgs; timeval tv_interval_end; gettimeofday_exn(&tv_interval_end); if (num_msgs % 100000 == 0) { printf("\rMsgs: %lld; Freq: %lld msgs/sec; ", num_msgs, ((num_msgs * 1000000) / diff_us(&tv_interval_end, &tv_start))); fflush(stdout); } } printf("\nExited cleanly\n"); ignore(zmq_close(subscriber)); ignore(zmq_close(publisher)); ignore(zmq_term(context)); return 0; }
void SBFS::get_minima_list (unsigned int max_size_of_minima_list) { timeval begin_program, end_program; gettimeofday (&begin_program, NULL); ElementSubset X ("X", set), Y ("Y", set), * Z; unsigned int k, best, worst, number_of_exclusions; float * J; J = new (nothrow) float [set->get_set_cardinality () + 1]; if (J == NULL) cout << endl << "SBFS: error allocating array J!" << endl; X.set_complete_subset (); // Add the empty set // k = 0; J[k] = X.cost = cost_function->cost (&X); Z = new ElementSubset ("", set); Z->copy (&X); list_of_minima.push_back (Z); if (store_visited_subsets) list_of_visited_subsets->add_subset (&X); while (((best = get_best_element (&X)) < set->get_set_cardinality ()) && (! cost_function->has_reached_threshold ())) { // Update the current best subset // X.remove_element (best); k++; J[k] = X.cost = cost_function->cost (&X); if (cost_function->has_reached_threshold ()) break; Z = new ElementSubset ("", set); Z->copy (&X); list_of_minima.push_back (Z); if (store_visited_subsets) list_of_visited_subsets->add_subset (&X); // Tries to remove subsets (conditional exclusion) // in order to restart the inclusion from a better smaller subset // worst = get_worst_element (&X); if (cost_function->has_reached_threshold ()) break; Y.copy (&X); Y.add_element (worst); Y.cost = cost_function->cost (&Y); if (cost_function->has_reached_threshold ()) break; if (J[k-1] <= Y.cost) { number_of_exclusions = 0; while ((k > 2) && ((Y.cost) > J[k-1]) && (number_of_exclusions <= PARAMETER_DELTA_SBFS)) // Step 3 { X.add_element (worst); k--; number_of_exclusions++; J[k] = X.cost = Y.cost; worst = get_worst_element (&X); if (cost_function->has_reached_threshold ()) break; Y.add_element (worst); Y.cost = cost_function->cost(&Y); if (cost_function->has_reached_threshold ()) break; if (store_visited_subsets) list_of_visited_subsets->add_subset (&X); } } } // End while (((best = get_best_element (&X)) ... delete [] J; number_of_visited_subsets = cost_function->get_number_of_calls_of_cost_function (); clean_list_of_minima (max_size_of_minima_list); gettimeofday (&end_program, NULL); elapsed_time_of_the_algorithm = diff_us (end_program, begin_program); }
static void *worker(void *A_) { struct asill_s *A = (struct asill_s *) A_; while (A->running) { int transfered, ret; run_q(A); A->T = (get_reg_r(A, 0x30b2) & 0x7ff) * A->Tk + A->T0; if ((ret = libusb_bulk_transfer(A->h, 0x82, A->d, A->width * A->height * (A->fmt == ASILL_FMT_RAW16 ? 2 : 1) / (A->bin * A->bin), &transfered, 1000 + (A->exposure_us / 1000))) == 0) { struct timeval now; gettimeofday(&now, NULL); if (A->data && !A->data_ready) { memcpy(A->data, A->d, A->width * A->height * (A->fmt == ASILL_FMT_RAW16 ? 2 : 1) / (A->bin * A->bin)); A->data_ready = 1; } if (A->cb) { A->cb(A->d, A->width / A->bin, A->height / A->bin); } A->fps_n += 1; if (A->last_fps_comp.tv_sec == 0 && A->last_fps_comp.tv_usec == 0) gettimeofday(&A->last_fps_comp, NULL); else { int e; e = diff_us(A->last_fps_comp, now); if (e > 1000000) { float xframe = ((float) e) / A->fps_n; A->fps = 1000000.0 / xframe; A->last_fps_comp = now; A->fps_n = 0; } } if (A->save_path[0]) { struct timeval tv; char fname[MAX_PATH]; FILE *f; gettimeofday(&tv, NULL); snprintf(fname, MAX_PATH, "%s/%010lu_%06lu.hdr", A->save_path, tv.tv_sec, tv.tv_usec); save_hdr(A, fname, tv.tv_sec, tv.tv_usec); snprintf(fname, MAX_PATH, "%s/%010lu_%06lu.pgm", A->save_path, tv.tv_sec, tv.tv_usec); f = fopen(fname, "w"); if (f) { fprintf(f, "P%d\n%d %d\n%d\n", 5, A->width, A->height, A->fmt == ASILL_FMT_RAW16 ? 65535 : 255); fwrite(A->d, A->width * A->height * (A->fmt == ASILL_FMT_RAW16 ? 2 : 1), 1, f); fclose(f); } } } else { fprintf(stderr, "bulk transfer failed: %d\n", ret); if (0) { stop(A); init(A); } } } return NULL; }