int loop (void) { on_start (); if (binlog_enabled) { double t = get_double_time (); logprintf ("replay log start\n"); replay_log (); logprintf ("replay log end in %lf seconds\n", get_double_time () - t); write_binlog (); #ifdef USE_LUA lua_binlog_end (); #endif } else { read_auth_file (); } update_prompt (); assert (DC_list[dc_working_num]); if (!DC_working || !DC_working->auth_key_id) { // if (auth_state == 0) { DC_working = DC_list[dc_working_num]; assert (!DC_working->auth_key_id); dc_authorize (DC_working); assert (DC_working->auth_key_id); auth_state = 100; write_auth_file (); } if (verbosity) { logprintf ("Requesting info about DC...\n"); } do_help_get_config (); net_loop (0, mcs); if (verbosity) { logprintf ("DC_info: %d new DC got\n", new_dc_num); } int i; for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) { dc_authorize (DC_list[i]); assert (DC_list[i]->auth_key_id); write_auth_file (); } if (auth_state == 100 || !(DC_working->has_auth)) { registeringStarted(); start_registering(); } registeringFinished(); return connect_to_server(); }
static void test_random_throughtput() { const int payload = 2000000; { // rand no parallel const double start_time = get_double_time(); for (int kk=0; kk<payload; kk++) { rand(); } const double end_time = get_double_time(); std::cout << "rand no parallel " << static_cast<int>(1e-3*payload/(end_time-start_time)) << "krand/s " << clock_it(end_time-start_time) << std::endl; } #if defined(OPENMP_FOUND) { // rand parallel const double start_time = get_double_time(); #pragma omp parallel for default(none) for (int kk=0; kk<payload; kk++) { rand(); } const double end_time = get_double_time(); std::cout << "rand parallel " << static_cast<int>(1e-3*payload/(end_time-start_time)) << "krand/s " << clock_it(end_time-start_time) << std::endl; } #endif { // boost::mt no parallel Rng gen; gen.seed(rand()); const double start_time = get_double_time(); for (int kk=0; kk<payload; kk++) { gen(); } const double end_time = get_double_time(); std::cout << "boost::mt no parallel " << static_cast<int>(1e-3*payload/(end_time-start_time)) << "krand/s " << clock_it(end_time-start_time) << std::endl; } #if defined(OPENMP_FOUND) { // boost::mt parallel Rng gen_common; gen_common.seed(rand()); const double start_time = get_double_time(); Rng gen_thread; #pragma omp parallel default(none) shared(gen_common, std::cout) private(gen_thread) { #pragma omp critical { gen_thread.seed(gen_common); //std::cout << "** " << gen_thread() << " " << omp_get_thread_num() << std::endl; } #pragma omp for schedule(static, 10000) for (int kk=0; kk<payload; kk++) { gen_thread(); } } const double end_time = get_double_time(); std::cout << "boost::mt parallel " << static_cast<int>(1e-3*payload/(end_time-start_time)) << "krand/s " << clock_it(end_time-start_time) << std::endl; } #endif }
Game play_game(const Options& options, Rng& rng) { static const boost::regex re_url("^(?:http://)?([^/]+)(/.*)$"); HTTPConnection connection(options.server_name); connection.proxy = options.proxy; const PTree& initial_json = connection.get_initial_state_json(options); //std::cout << initial_json; boost::match_results<std::string::const_iterator> what; if (!regex_search(initial_json.get<std::string>("playUrl"), what, re_url)) throw std::runtime_error("can't parse play url"); const std::string play_server_name(what[1].first, what[1].second); const std::string play_end_point(what[2].first, what[2].second); const std::string& view_url = initial_json.get<std::string>("viewUrl"); std::cout << "view game at " << view_url << std::endl; double start_time = get_double_time(); if (options.collect_map) { // collect maps const Tiles tiles = get_tiles(initial_json.get_child("game.board")); const HashedPair<Tiles> hashed_tiles(tiles); std::stringstream ss; ss << "map_" << std::hex << hashed_tiles.hash << std::dec << ".txt"; std::cout << "saving " << ss.str() << std::endl; std::ofstream handle(ss.str().c_str()); handle << hashed_tiles.value; handle.close(); } Game game(initial_json); #if defined(BOTUCT) || defined(BOTMULTI) Bot bot(game, options.uct_constant, options.max_mc_depth, rng); #elif defined(BOTRANDOM) Bot bot(game, rng); #else Bot bot(game); #endif #if defined(REPORTING) Reports reports; #endif while (!game.is_finished()) { OmpFlag continue_flag(true); std::cout << std::endl; std::cout << "======================================== " << clock_it(get_double_time() - start_time) << std::endl; game.status(std::cout); std::cout << "++++++++++++++++++++++++++++++++++++++++ " << clock_it(get_double_time() - start_time) << std::endl; #if defined(REPORTING) Report report_aa = bot.crunch_it_baby(game, continue_flag, start_time, TURN_DURATION); report_aa.type = 1; reports.push_back(report_aa); #else bot.crunch_it_baby(game, continue_flag, start_time, TURN_DURATION); #endif const Direction direction = bot.get_move(game); std::cout << "bot direction " << direction << std::endl; bot.advance_game(game, direction); //game.status(std::cout); std::cout << "---------------------------------------- " << clock_it(get_double_time() - start_time) << std::endl; std::cout << "view game at " << view_url << std::endl; PTree new_json; double request_start_time; double request_end_time; #if defined(OPENMP_FOUND) #pragma omp parallel sections default(shared) shared(new_json, request_end_time, request_start_time, continue_flag, play_end_point, play_server_name, start_time, bot, game) { #pragma omp section #endif { request_start_time = get_double_time(); new_json = connection.get_new_state_json(play_end_point, direction); request_end_time = get_double_time(); continue_flag.reset(); } #if defined(OPENMP_FOUND) #pragma omp section { #if defined(REPORTING) Report report_bb = bot.crunch_it_baby(game, continue_flag, start_time, 4); report_bb.type = 2; reports.push_back(report_bb); #else bot.crunch_it_baby(game, continue_flag, start_time, 4); #endif } } #endif game.state.update(new_json); game.update(new_json); std::cout << "request took " << clock_it(request_end_time-request_start_time) << std::endl; //game.status(std::cout); std::cout << "======================================== " << clock_it(get_double_time() - start_time) << std::endl; start_time = get_double_time(); } assert( game.is_finished() ); #if defined(REPORTING) save_report_file(reports, "report.txt"); #endif return game; }
int loop (void) { on_start (); if (binlog_enabled) { double t = get_double_time (); logprintf ("replay log start\n"); replay_log (); logprintf ("replay log end in %lf seconds\n", get_double_time () - t); write_binlog (); #ifdef USE_LUA lua_binlog_end (); #endif } else { read_auth_file (); } update_prompt (); assert (DC_list[dc_working_num]); if (!DC_working || !DC_working->auth_key_id) { // if (auth_state == 0) { DC_working = DC_list[dc_working_num]; assert (!DC_working->auth_key_id); dc_authorize (DC_working); assert (DC_working->auth_key_id); auth_state = 100; write_auth_file (); } if (verbosity) { logprintf ("Requesting info about DC...\n"); } do_help_get_config (); net_loop (0, mcs); if (verbosity) { logprintf ("DC_info: %d new DC got\n", new_dc_num); } int i; for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) { dc_authorize (DC_list[i]); assert (DC_list[i]->auth_key_id); write_auth_file (); } if (auth_state == 100 || !(DC_working->has_auth)) { if (!default_username) { size_t size = 0; char *user = 0; if (!user) { printf ("Telephone number (with '+' sign): "); if (net_getline (&user, &size) == -1) { perror ("getline()"); exit (EXIT_FAILURE); } set_default_username (user); } } int res = do_auth_check_phone (default_username); assert (res >= 0); logprintf ("%s\n", res > 0 ? "phone registered" : "phone not registered"); if (res > 0 && !register_mode) { do_send_code (default_username); char *code = 0; size_t size = 0; printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): "); while (1) { if (net_getline (&code, &size) == -1) { perror ("getline()"); exit (EXIT_FAILURE); } if (!strcmp (code, "call")) { printf ("You typed \"call\", switching to phone system.\n"); do_phone_call (default_username); printf ("Calling you! Code: "); continue; } if (do_send_code_result (code) >= 0) { break; } printf ("Invalid code. Try again: "); tfree_str (code); } auth_state = 300; } else { printf ("User is not registered. Do you want to register? [Y/n] "); char *code; size_t size; if (net_getline (&code, &size) == -1) { perror ("getline()"); exit (EXIT_FAILURE); } if (!*code || *code == 'y' || *code == 'Y') { printf ("Ok, starting registartion.\n"); } else { printf ("Then try again\n"); exit (EXIT_SUCCESS); } char *first_name; printf ("First name: "); if (net_getline (&first_name, &size) == -1) { perror ("getline()"); exit (EXIT_FAILURE); } char *last_name; printf ("Last name: "); if (net_getline (&last_name, &size) == -1) { perror ("getline()"); exit (EXIT_FAILURE); } int dc_num = do_get_nearest_dc (); assert (dc_num >= 0 && dc_num <= MAX_DC_NUM && DC_list[dc_num]); dc_working_num = dc_num; DC_working = DC_list[dc_working_num]; do_send_code (default_username); printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): "); while (1) { if (net_getline (&code, &size) == -1) { perror ("getline()"); exit (EXIT_FAILURE); } if (!strcmp (code, "call")) { printf ("You typed \"call\", switching to phone system.\n"); do_phone_call (default_username); printf ("Calling you! Code: "); continue; } if (do_send_code_result_auth (code, first_name, last_name) >= 0) { break; } printf ("Invalid code. Try again: "); tfree_str (code); } auth_state = 300; } } for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) { do_export_auth (i); do_import_auth (i); bl_do_dc_signed (i); write_auth_file (); } write_auth_file (); fflush (stdout); fflush (stderr); read_state_file (); read_secret_chat_file (); set_interface_callbacks (); do_get_difference (); net_loop (0, dgot); #ifdef USE_LUA lua_diff_end (); #endif send_all_unsent (); do_get_dialog_list (); if (wait_dialog_list) { dialog_list_got = 0; net_loop (0, dlgot); } return main_loop (); }
static AwnGraphSinglePoint awn_CPUicon_get_load(AwnCPUicon *self) { guint i; glibtop_cpu cpu; AwnCPUiconPrivate *priv; float total, used; gdouble load; AwnGraphSinglePoint point; gdouble new_time; priv = AWN_CPUICON_GET_PRIVATE (self); new_time = get_double_time (); glibtop_get_cpu(&cpu); #undef NOW #undef LAST #define NOW (priv->times[priv->now]) #define LAST (priv->times[priv->now ^ 1]) if (priv->num_cpus == 1) { NOW[0][CPU_TOTAL] = cpu.total; NOW[0][CPU_USED] = cpu.user + cpu.nice + cpu.sys; } else { for (i = 0; i < priv->num_cpus; i++) { NOW[i][CPU_TOTAL] = cpu.xcpu_total[i]; NOW[i][CPU_USED] = cpu.xcpu_user[i] + cpu.xcpu_nice[i] + cpu.xcpu_sys[i]; } } load = total = used = 0.0; for (i = 0; i < priv->num_cpus; i++) { total = total + NOW[i][CPU_TOTAL] - LAST[i][CPU_TOTAL]; used = used + NOW[i][CPU_USED] - LAST[i][CPU_USED]; } load = used / MAX(total, (float)priv->num_cpus * 1.0f); point.value = load * 100.0; if (point.value>100.0) { point.value = 100.0; } point.points = (new_time - priv->prev_time) * 1000.0 / get_conf_value_int (G_OBJECT(self), "update-timeout"); priv->prev_time = new_time; // toggle the buffer index. priv->now ^= 1; #undef NOW #undef LAST return point; }
static void awn_CPUicon_constructed (GObject *object) { /*FIXME*/ AwnCPUiconPrivate * priv; glibtop_cpu cpu; int i = 0; AwnApplet * applet; g_assert (G_OBJECT_CLASS ( awn_CPUicon_parent_class) ); if (G_OBJECT_CLASS ( awn_CPUicon_parent_class)->constructed) { G_OBJECT_CLASS ( awn_CPUicon_parent_class)->constructed(object); } g_object_get (object, "applet",&applet, NULL); g_assert (applet); g_assert (AWN_IS_APPLET (applet)); priv = AWN_CPUICON_GET_PRIVATE (object); /* this will choose add_seconds in a relatively conservative manner. Note that the timer is assumed to be incorrect and time elapsed is actually measured accurately when the timer fires. Area graph can be informed that the measurement contains a partial point and it will average things out. */ priv->dialog = awn_cpu_dialog_new_with_applet(GTK_WIDGET(object),applet); gtk_window_set_title (GTK_WINDOW (priv->dialog),_("CPU")); g_signal_connect(object, "button-press-event", G_CALLBACK(_awn_cpu_icon_clicked), priv->dialog); priv->num_cpus = 0; priv->prev_time = get_double_time(); glibtop_get_cpu(&cpu); while (i < GLIBTOP_NCPU && cpu.xcpu_total[i] != 0) { priv->num_cpus++; i++; } priv->now = 0; connect_notify (object, "graph-type", G_CALLBACK (_graph_type_change),applet); connect_notify (object, "update-timeout", G_CALLBACK (_update_timeout_change),object); set_timeout (AWN_CPUICON(object)); priv->text_overlay = AWN_OVERLAY(awn_overlay_text_new()); g_object_set (priv->text_overlay, "align", AWN_OVERLAY_ALIGN_RIGHT, "gravity", GDK_GRAVITY_SOUTH, "x-adj", 0.3, "y-adj", 0.0, "text", "0.0", NULL); awn_overlayable_add_overlay (AWN_OVERLAYABLE(object), priv->text_overlay); do_bridge ( applet,object, "icon","update_timeout","update-timeout"); }