i32 g_error_log_add(char *error_name, u32 error_code, i32 error_result) { pthread_mutex_lock(&g_error_log_mutex); i32 res = 0; #if ERROR_LOG_ENABLE == 1 FILE *g_error_log_fd; g_error_log_fd = fopen(g_error_log_file_name,"a+"); if (g_error_log_fd == NULL) res = -1; else { fprintf(g_error_log_fd,"%s > %f > %u %i\n", error_name, get_ms_time(), error_code, error_result); fclose(g_error_log_fd); #if ERROR_LOG_PRINT_ENABLE printf("ERROR : %s > %f > %u %i\n", error_name, get_ms_time(), error_code, error_result); #endif res = 0; } #endif pthread_mutex_unlock(&g_error_log_mutex); return res; }
static void flv_video(struct serializer *s, struct encoder_packet *packet, bool is_header) { int64_t offset = packet->pts - packet->dts; int32_t time_ms = get_ms_time(packet, packet->dts); if (!packet->data || !packet->size) return; s_w8(s, RTMP_PACKET_TYPE_VIDEO); #ifdef DEBUG_TIMESTAMPS blog(LOG_DEBUG, "Video: %lu", time_ms); #endif s_wb24(s, (uint32_t)packet->size + 5); s_wb24(s, time_ms); s_w8(s, (time_ms >> 24) & 0x7F); s_wb24(s, 0); /* these are the 5 extra bytes mentioned above */ s_w8(s, packet->keyframe ? 0x17 : 0x27); s_w8(s, is_header ? 0 : 1); s_wb24(s, get_ms_time(packet, offset)); s_write(s, packet->data, packet->size); /* write tag size (starting byte doesnt count) */ s_wb32(s, (uint32_t)serializer_get_pos(s) + 4 - 1); }
int main(int argc, const char** argv) { double alfa = 2; double beta = 0.7; size_t batches[] = { 100, 1000, 10000, 1000000 }; size_t num_batches = sizeof(batches) / sizeof(size_t); double* cpu_rands = NULL; double* gpu_rands = NULL; double start, end, tdiff; size_t i, batch_size; StableDist *dist = stable_create(alfa, beta, 1, 0, 0); if (!dist) { fprintf(stderr, "StableDist creation failure. Aborting.\n"); return 1; } if (stable_activate_gpu(dist)) { fprintf(stderr, "Couldn't initialize GPU.\n"); return 1; } stable_set_absTOL(1e-20); stable_set_relTOL(1.2e-10); stable_rnd_seed(dist, time(NULL)); fprintf(stderr, "Count\tCPU-ms\tGPU-ms\n"); for (i = 0; i < num_batches; i++) { batch_size = batches[i]; cpu_rands = calloc(batch_size, sizeof(double)); gpu_rands = calloc(batch_size, sizeof(double)); start = get_ms_time(); stable_rnd(dist, cpu_rands, batch_size); end = get_ms_time(); tdiff = end - start; fprintf(stderr, "%zu\t%.3lf\t", batch_size, tdiff); start = get_ms_time(); stable_rnd_gpu(dist, gpu_rands, batch_size); end = get_ms_time(); tdiff = end - start; fprintf(stderr, "%.3lf\n", tdiff); } for (i = 0; i < batch_size; i++) printf("%.6lf\n", gpu_rands[i]); stable_free(dist); return 0; }
CBaseBot::CBaseBot() { agent_interface.id = cfg_get_unique_id() + rand(); agent_interface.param = 0; agent_interface.value = 0; agent_interface.color_intensity = 1.0; agent_interface.time_stamp = get_ms_time(); agent_interface.type = AGENT_TYPE_BOT; agent_interface.type_behaviour = ROBOT_TYPE_BASE; agent_interface.type_interaction = AGENT_TYPE_INTERACTION_WEAK; agent_interface.size = AGENT_BOT_SIZE*3.0; agent_interface.request = AGENT_REQUEST_NULL; agent_interface.x = 0.1*rnd_()*POSITION_MAX_X; //*x_max; agent_interface.y = 0.1*rnd_()*POSITION_MAX_Y; //*y_max; agent_interface.z = 0.0*rnd_()*0.99;//*z_max; agent_interface.roll = 0.0; agent_interface.pitch = 0.0; agent_interface.yaw = 0.0; agent_interface.dt = cfg_get_dt(); agent_interface.action_type = ACTION_TYPE_NULL; dx = 0.0; dy = 0.0; dz = 0.0; dyaw = 0.0; client = new CClient(); }
static void flv_audio(struct serializer *s, struct encoder_packet *packet, bool is_header) { int32_t time_ms = get_ms_time(packet, packet->dts); if (!packet->data || !packet->size) return; s_w8(s, RTMP_PACKET_TYPE_AUDIO); #ifdef DEBUG_TIMESTAMPS blog(LOG_DEBUG, "Audio: %lu", time_ms); #endif s_wb24(s, (uint32_t)packet->size + 2); s_wb24(s, time_ms); s_w8(s, (time_ms >> 24) & 0x7F); s_wb24(s, 0); /* these are the two extra bytes mentioned above */ s_w8(s, 0xaf); s_w8(s, is_header ? 0 : 1); s_write(s, packet->data, packet->size); /* write tag size (starting byte doesnt count) */ s_wb32(s, (uint32_t)serializer_get_pos(s) + 4 - 1); }
void CRobotVisualisation::refresh(struct sRobotData robot_data) { u32 j; i32 idx = -1; for (j = 0; j < robots.size(); j++) { if (robots[j].robot_id == robot_data.robot_id) { idx = j; break; } } if (idx == -1) for (j = 0; j < robots.size(); j++) if (robots[j].robot_id == 0) { idx = j; break; } if (idx != -1) robots[idx] = robot_data; else g_debug_log_add((char*)"robot visualisation",(char*)"no space for more robots"); //forget old robots for (j = 0; j < robots.size(); j++) { if ((robots[j].time_stamp + time_out_deadline) < get_ms_time()) robots[j].robot_id = 0; } }
i32 CServer::main_remote() { debug_log_add((char*)"server main start"); int portno = cfg_get_port(); server_listen_fd = 0; struct sockaddr_in serv_addr; server_listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_listen_fd < 0) return -3; memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(portno); if (bind(server_listen_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) return -4; if (listen(server_listen_fd, 10) < 0) return -5; struct sRobot rx_data, tx_data; u32 ResultDataSize = sizeof(struct sRobot); u32 run = 1; while (run) { this->time = get_ms_time(); struct sockaddr_in client; int c = sizeof(struct sockaddr_in); int connfd = accept(server_listen_fd, (struct sockaddr *)&client, (socklen_t*)&c); if (connfd < 0) return -6; if (read(connfd, (u8*)(&rx_data), ResultDataSize) < 0) exit(-8); process_data(&rx_data, &tx_data); if (write(connfd, (u8*)(&tx_data), ResultDataSize) < 0) exit(-9); close(connfd); robots_refresh(); } return 0; }
cell AMX_NATIVE_CALL Natives::GetTimerIntervalLeft(AMX *amx, cell *params) { if (params[0] < 4) { return 0; } int id = params[1]; if (is_valid_timer(id)) { return timers[id]->next - get_ms_time(); } return 0; }
static int write_packet(struct flv_output *stream, struct encoder_packet *packet, bool is_header) { uint8_t *data; size_t size; int ret = 0; stream->last_packet_ts = get_ms_time(packet, packet->dts); flv_packet_mux(packet, &data, &size, is_header); fwrite(data, 1, size, stream->file); bfree(data); obs_free_encoder_packet(packet); return ret; }
CRobotVisualisation::CRobotVisualisation(u32 enable_opengl) { u32 j; terminal_width = 128; terminal_height = 32; time_out_deadline = 1000.0*60.0; //one minute cm_size = 10.0*100.0/81.0; /* screen_width = 1920; screen_height = 1080; */ screen_width = 640; screen_height = screen_width*9.0/16.0; u32 robots_max_count = 1024; /* for (j = 0; j < 200; j++) printf("\n"); clear(); */ struct sRobotData robot; robot.robot_id = 0; robot.type = 0; robot.time_stamp = get_ms_time(); for (j = 0; j < robots_max_count; j++) robots.push_back(robot); if (enable_opengl) { visualise_init(); } client = new CClient(); }
void CBaseBot::process() { agent_interface.action[0] = dx; agent_interface.action[1] = dy; agent_interface.action[2] = dz; agent_interface.action[5] = dyaw; agent_interface.time_stamp = get_ms_time(); u32 id1 = agent_interface.id; i32 res = client->process(&agent_interface); u32 id2 = agent_interface.id; if (res < 0) { g_error_log_add((char*)"connection error", 10, res); } }
i32 CRobotVisualisation::read_from_server() { struct sAgentInterface agent_interface; agent_interface.id = 0; agent_interface.param = 0; agent_interface.time_stamp = get_ms_time(); agent_interface.type = AGENT_TYPE_VISUALISATION; agent_interface.type_behaviour = AGENT_TYPE_BEHAVIOUR_NULL; agent_interface.type_interaction = AGENT_TYPE_INTERACTION_NULL; agent_interface.x = 0.0; agent_interface.y = 0.0; agent_interface.z = 0.0; agent_interface.roll = 0.0; agent_interface.pitch = 0.0; agent_interface.yaw = 0.0; agent_interface.size = 1.0; agent_interface.dt = cfg_get_dt(); agent_interface.action_type = ACTION_TYPE_NULL; agent_interface.param = agent_idx; i32 res = client->process(&agent_interface); if (res == 0) agent_idx = (agent_idx+1)%agent_interface.action_type; if ( (res == 0) && (agent_interface.type != AGENT_TYPE_VISUALISATION) ) { agent_interface.id = agent_interface.param; agent_group->update_agent(&agent_interface, NULL); } return res; }
i32 CServer::main_local() { u32 run = 1; while (run) { this->time = get_ms_time(); if (this->time > this->time_refresh) { u32 j; for (j = 0; j < c_robots.size(); j++) c_robots[j]->process(&robots[j]); } if (this->time > (this->time_refresh + dt)) debug_log_add((char*)"real time error"); robots_refresh(); if (getch() == 27) run = 0; } u32 i; for (i = 0; i < ROBOT_TYPE_COUNT; i++) { char file_name[256]; sprintf(file_name, "collective_brain_%i", i); this->collective_brain[i]->save_to_file(file_name); } return 0; }
int main(int argc, char *argv[]) { double alfa, beta, sigma, mu_0; double alfa_init = 0, beta_init = 0, sigma_init = 0, mu_0_init = 0; double *data; int i = 1, iexp, N, Nexp; int seed; double acc_pdev; double total_duration, start, end; struct fittest tests[] = { //{ stable_fit_mle, 0, "MLE" }, //{ stable_fit_mle2d, 0, "M2D"}, //{ stable_fit_koutrouvelis, 0, "KTR"}, { stable_fit_mle, 1, "MLE" }, { stable_fit_mle2d, 1, "M2D"}, { stable_fit_koutrouvelis, 1, "KTR"}, { stable_fit_grid, 1, "GRD" }, { stable_fit_grid, 0, "GRD" } }; struct fittest *test; size_t num_tests = sizeof tests / sizeof(struct fittest); struct fitresult* results; struct fitresult* result; results = calloc(num_tests, sizeof(struct fitresult)); StableDist *dist = NULL; alfa = 1.5; beta = 0.75; sigma = 5.0; mu_0 = 15.0; N = 400; Nexp = 10; seed = -1; printf("Parameters for the random data generated:\n"); printf("α\t%lf\n", alfa); printf("β\t%lf\n", beta); printf("σ\t%lf\n", sigma); printf("μ\t%lf\n", mu_0); printf("Size\t%d\n", N); printf("\nWill perform %d experiments for each fitter.\n\n", Nexp); if ((dist = stable_create(alfa, beta, sigma, mu_0, 0)) == NULL) { printf("Error when creating the distribution"); exit(1); } stable_set_THREADS(1); stable_set_absTOL(1e-16); stable_set_relTOL(1e-8); stable_set_FLOG("errlog.txt"); if (seed < 0) stable_rnd_seed(dist, time(NULL)); else stable_rnd_seed(dist, seed); /* Random sample generation */ data = (double *)malloc(N * sizeof(double)); stable_rnd(dist, data, N); printf("Fitter\tms/fit\t\tα\t\tβ\t\tμ\t\tσ\n"); for (i = 0; i < num_tests; i++) { test = tests + i; result = results + i; total_duration = 0; for (iexp = 0; iexp < Nexp; iexp++) { stable_fit_init(dist, data, N, NULL, NULL); add_initial_estimations(alfa); add_initial_estimations(beta); add_initial_estimations(sigma); add_initial_estimations(mu_0); if (test->gpu_enabled) stable_activate_gpu(dist); else stable_deactivate_gpu(dist); dist->parallel_gridfit = test->gpu_enabled; // Temporary. start = get_ms_time(); test->func(dist, data, N); end = get_ms_time(); add_avg_err(alfa); add_avg_err(beta); add_avg_err(sigma); add_avg_err(mu_0); result->ms_duration += end - start; } calc_avg_err(alfa); calc_avg_err(beta); calc_avg_err(sigma); calc_avg_err(mu_0); result->ms_duration /= Nexp; printf("%s", test->name); if (test->gpu_enabled) printf("_GPU"); printf("\t%lf\t%.2lf ± %.2lf\t%.2lf ± %.2lf\t%.2lf ± %.2lf\t%.2lf ± %.2lf\n", result->ms_duration, result->alfa, result->alfa_err, result->beta, result->beta_err, result->sigma, result->sigma_err, result->mu_0, result->mu_0_err); } alfa_init /= Nexp * num_tests; beta_init /= Nexp * num_tests; sigma_init /= Nexp * num_tests; mu_0_init /= Nexp * num_tests; printf("\n\nInitial estimations: \n"); printf("α = %lf\nβ = %.2lf\nμ = %.2lf\nσ = %.2lf\n", alfa_init, beta_init, sigma_init, mu_0_init); printf("\n\nComparison of actual vs. expected results:\n"); printf("Fitter\tα error\t\tβ error\t\tμ error\t\tσ error\t\tAverage %% error\n"); for (i = 0; i < num_tests; i++) { result = results + i; test = tests + i; acc_pdev = 0; printf("%s", test->name); if (test->gpu_enabled) printf("_GPU"); print_deviation(alfa); print_deviation(beta); print_deviation(mu_0); print_deviation(sigma); printf("\t%.1lf %%\n", acc_pdev / 4); } free(data); free(results); stable_free(dist); fclose(stable_get_FLOG()); return 0; }
cell AMX_NATIVE_CALL Natives::GetTickCount(AMX *amx, cell *params) { return (int) ((get_ms_time() - start_time) % MAX_INT); }
int main(int argc, char *argv[]) { double alfa, beta, sigma, mu_0; double *data; int i = 1, iexp, N, Nexp; int seed; char testname[100]; size_t test_count = 0; double total_duration, start, end; struct fittest tests[] = { //{ stable_fit_mle, 0, "MLE" }, //{ stable_fit_mle2d, 0, "M2D"}, //{ stable_fit_koutrouvelis, 0, "KTR"}, //{ stable_fit_koutrouvelis, 1, "KTR"}, //{ stable_fit_mle, 1, "MLE" }, // { stable_fit_mle2d, 1, "M2D"}, { stable_fit_grid, 1, "GRD" }, // { stable_fit_grid, 0, "GRD" } }; struct fittest *test; size_t num_tests = sizeof tests / sizeof(struct fittest); StableDist *dist = NULL; alfa = 1.5; beta = 0.75; sigma = 5.0; mu_0 = 15.0; N = 1000; Nexp = 20; seed = -1; install_stop_handlers(); if ((dist = stable_create(alfa, beta, sigma, mu_0, 0)) == NULL) { printf("Error when creating the distribution"); exit(1); } stable_set_THREADS(1); stable_set_absTOL(1e-16); stable_set_relTOL(1e-8); if (seed < 0) stable_rnd_seed(dist, time(NULL)); else stable_rnd_seed(dist, seed); /* Random sample generation */ data = (double *) malloc(Nexp * N * sizeof(double)); for (i = 0; i < num_tests; i++) { test = tests + i; total_duration = 0; char out_fname[100]; char* gpu_marker; if (test->gpu_enabled) gpu_marker = "_GPU"; else gpu_marker = ""; snprintf(out_fname, 100, "%s%s.dat", test->name, gpu_marker); snprintf(testname, 100, "%s%s", test->name, gpu_marker); FILE* out = fopen(out_fname, "w"); if (!out) { perror("fopen"); return 1; } printf("Estimation evaluation for %s...\n", testname); if (test->gpu_enabled) stable_activate_gpu(dist); else stable_deactivate_gpu(dist); for (alfa = ALFA_START; alfa <= ALFA_END + 2 * DBL_EPSILON; alfa += ALPHA_INCR) { for (beta = BETA_START; beta <= BETA_END + 2 * DBL_EPSILON; beta += BETA_INCR) { mu_0 = 0; sigma = 1; for (mu_0 = MU_START; mu_0 <= MU_END + 2 * DBL_EPSILON; mu_0 += MU_INCR) { for (sigma = SIGMA_START; sigma <= SIGMA_END + 2 * DBL_EPSILON; sigma += SIGMA_INCR) { double alfa_est = 0, beta_est = 0, mu_0_est = 0, sigma_est = 0; double alfa_est_err = 0, beta_est_err = 0, mu_0_est_err = 0, sigma_est_err = 0; stable_setparams(dist, alfa, beta, sigma, mu_0, 0); printf("Testing %s %.2lf/%.2lf/%.2lf/%.2lf\n", testname, alfa, beta, mu_0, sigma); stable_rnd(dist, data, N * Nexp); double ms_duration = 0; dist->parallel_gridfit = test->gpu_enabled; // Temporary. for (iexp = 0; iexp < Nexp; iexp++) { if (stable_fit_init(dist, data + iexp * N, N, NULL, NULL) != 0) { printf("Warning: couldn't init distribution\n"); continue; } printf("Eval %d... ", iexp); fflush(stdout); start = get_ms_time(); test->func(dist, data + iexp * N, N); end = get_ms_time(); add_avg_err(alfa); add_avg_err(beta); add_avg_err(sigma); add_avg_err(mu_0); ms_duration += end - start; printf("Done\n"); fflush(stdout); } calc_avg_err(alfa); calc_avg_err(beta); calc_avg_err(sigma); calc_avg_err(mu_0); ms_duration /= Nexp; fprintf(out, "%lf %lf %lf %lf %lf ", alfa, beta, mu_0, sigma, ms_duration); fprintf(out, "%lf %lf %lf %lf %lf %lf %lf %lf\n", alfa_est, alfa_est_err, beta_est, beta_est_err, sigma_est, sigma_est_err, mu_0_est, mu_0_est_err); fflush(out); fflush(stdout); test_count++; } } } } fclose(out); printf("Eval finished: %zu sample points.\n", test_count); test_count = 0; } printf("Done\n"); free(data); stable_free(dist); return 0; }
void CServer::robots_refresh() { u32 j, i; std::vector<u32> robots_erase_list; struct sRobot robot; /*check for refresh time*/ if (this->time > this->time_refresh) { #ifdef VISUALISATION_IN_SERVER /* for (j = 0; j < robots.size(); j++) visualisation_update(robots[j]); */ visualisation_update_all(&robots); #endif this->time_refresh = this->dt + get_ms_time(); for (j = 0; j < robots.size(); j++) { switch (robots[j].request) { case REQUEST_ROBOT_RESPAWN: respawn(&robots[j]); update_sensors(j); break; case REQUEST_ROBOT_DELETE: robots_erase_list.push_back(j); break; case REQUEST_ROBOT_ADD_RED_PHEROMONE: robot.type = ROBOT_TYPE_RED_PHEROMONE; robot.parameter_int = 0.0; robot.parameter_f = 0.0; robot.reward = 0.0; for (i = 0; i < ROBOT_SPACE_DIMENSION; i++) robot.position[i] = robots[j].position[i]; if (robots[j].sensors[ROBOT_SENSOR_RED_PHEROMONE_DISTANCE_IDX] > robots[j].colision_distance) add_new_robot(robot); break; case REQUEST_ROBOT_ADD_GREEN_PHEROMONE: robot.type = ROBOT_TYPE_GREEN_PHEROMONE; robot.parameter_int = 0.0; robot.parameter_f = 0.0; robot.reward = 0.0; for (i = 0; i < ROBOT_SPACE_DIMENSION; i++) robot.position[i] = robots[j].position[i]; if (robots[j].sensors[ROBOT_SENSOR_GREEN_PHEROMONE_DISTANCE_IDX] > robots[j].colision_distance) add_new_robot(robot); break; case REQUEST_ROBOT_ADD_BLUE_PHEROMONE: robot.type = ROBOT_TYPE_BLUE_PHEROMONE; robot.parameter_int = 0.0; robot.parameter_f = 0.0; robot.reward = 0.0; for (i = 0; i < ROBOT_SPACE_DIMENSION; i++) robot.position[i] = robots[j].position[i]; if (robots[j].sensors[ROBOT_SENSOR_BLUE_PHEROMONE_DISTANCE_IDX] > robots[j].colision_distance) add_new_robot(robot); break; case REQUEST_ROBOT_ADD_PATH: robot.type = ROBOT_TYPE_PATH; robot.parameter_int = 0.0; robot.parameter_f = 0.0; robot.reward = 0.0; for (i = 0; i < ROBOT_SPACE_DIMENSION; i++) robot.position[i] = robots[j].position[i]; if (robots[j].sensors[ROBOT_SENSOR_BLUE_PHEROMONE_DISTANCE_IDX] > robots[j].colision_distance) add_new_robot(robot); break; default: break; } if (robots[j].type&ROBOT_MOVEABLE_FLAG) { if (robots[j].type == ROBOT_TYPE_RED_ROBOT) red_score+= robots[j].fitness; if (robots[j].type == ROBOT_TYPE_GREEN_ROBOT) green_score+= robots[j].fitness; if (robots[j].type == ROBOT_TYPE_BLUE_ROBOT) blue_score+= robots[j].fitness; update_position(j); update_sensors(j); } robots[j].request = REQUEST_NULL; } } sprintf(text, "red %6.2f , green %6.2f , blue %6.2f", red_score, green_score, blue_score); for (j = 0; j < robots_erase_list.size(); j++) { u32 idx = robots_erase_list[j]; robots.erase(robots.begin() + idx); delete c_robots[idx]; c_robots.erase(c_robots.begin() + idx); } }
int main(int argc, char *argv[]) { double alfa, beta, sigma, mu_0; double *data; int N, Nexp; int seed; double total_duration, start, end; double *pdf; double ms_duration; StableDist *dist = NULL; alfa = 1.5; beta = 0.75; sigma = 5.0; mu_0 = 15.0; N = 400; Nexp = 1; seed = -1; printf("Parameters for the random data generated:\n"); printf("α\t%lf\n", alfa); printf("β\t%lf\n", beta); printf("σ\t%lf\n", sigma); printf("μ\t%lf\n", mu_0); printf("Size\t%d\n", N); printf("\nWill perform %d experiments.\n\n", Nexp); if ((dist = stable_create(alfa, beta, sigma, mu_0, 0)) == NULL) { printf("Error when creating the distribution"); exit(1); } stable_set_THREADS(1); stable_set_absTOL(1e-16); stable_set_relTOL(1e-8); stable_set_FLOG("errlog.txt"); if (seed < 0) stable_rnd_seed(dist, time(NULL)); else stable_rnd_seed(dist, seed); /* Random sample generation */ data = (double *)malloc(N * sizeof(double)); pdf = (double *) calloc(N, sizeof(double)); stable_rnd(dist, data, N); total_duration = 0; stable_fit_init(dist, data, N, NULL, NULL); stable_activate_gpu(dist); start = get_ms_time(); stable_fit_grid(dist, data, N); end = get_ms_time(); ms_duration = end - start; printf("time = %lf\nα = %lf\nβ = %.2lf\nμ = %.2lf\nσ = %.2lf\n", ms_duration, dist->alfa, dist->beta, dist->sigma, dist->mu_0); free(data); stable_free(dist); fclose(stable_get_FLOG()); return 0; }
//from参数与get_ms_time配合使用,打印程序开销时间,深绿色 int64 prt_cost_time(char * str, int64 from) { int64 now = get_ms_time(); printf("[0;32m""%s %lld ms\n""[0;39m", str, now - from); return now; }