void status_jobs( job_queue_type * queue , int num_jobs , job_type ** jobs , thread_pool_type * tp) { for (int i=0; i < num_jobs; i++) { job_type * job = jobs[i]; arg_pack_type * arg = arg_pack_alloc(); arg_pack_append_ptr( arg , job ); arg_pack_append_ptr( arg , queue ); thread_pool_add_job(tp , status_job__ , arg ); } thread_pool_add_job( tp , global_status , queue ); }
void matrix_inplace_matmul_mt2(matrix_type * A, const matrix_type * B , thread_pool_type * thread_pool){ int num_threads = thread_pool_get_max_running( thread_pool ); arg_pack_type ** arglist = util_malloc( num_threads * sizeof * arglist ); int it; thread_pool_restart( thread_pool ); { int rows = matrix_get_rows( A ) / num_threads; int rows_mod = matrix_get_rows( A ) % num_threads; int row_offset = 0; for (it = 0; it < num_threads; it++) { int row_size; arglist[it] = arg_pack_alloc(); row_size = rows; if (it < rows_mod) row_size += 1; arg_pack_append_int(arglist[it] , row_offset ); arg_pack_append_int(arglist[it] , row_size ); arg_pack_append_ptr(arglist[it] , A ); arg_pack_append_const_ptr(arglist[it] , B ); thread_pool_add_job( thread_pool , matrix_inplace_matmul_mt__ , arglist[it]); row_offset += row_size; } } thread_pool_join( thread_pool ); for (it = 0; it < num_threads; it++) arg_pack_free( arglist[it] ); free( arglist ); }
void enkf_main_initialize_from_scratch_with_bool_vector(enkf_main_type * enkf_main , const stringlist_type * param_list ,const bool_vector_type * iens_mask , init_mode_type init_mode) { int num_cpu = 4; int ens_size = enkf_main_get_ensemble_size( enkf_main ); thread_pool_type * tp = thread_pool_alloc( num_cpu , true ); arg_pack_type ** arg_list = util_calloc( ens_size , sizeof * arg_list ); int i; int iens; for (iens = 0; iens < ens_size; iens++) { arg_list[iens] = arg_pack_alloc(); if (bool_vector_safe_iget(iens_mask , iens)) { arg_pack_append_ptr( arg_list[iens] , enkf_main ); arg_pack_append_const_ptr( arg_list[iens] , param_list ); arg_pack_append_int( arg_list[iens] , iens ); arg_pack_append_int( arg_list[iens] , init_mode ); thread_pool_add_job( tp , enkf_main_initialize_from_scratch_mt , arg_list[iens]); } } thread_pool_join( tp ); for (i = 0; i < ens_size; i++) { arg_pack_free( arg_list[i] ); } free( arg_list ); thread_pool_free( tp ); }
static void job_queue_handle_EXIT( job_queue_type * queue , job_queue_node_type * node) { job_queue_change_node_status(queue , node , JOB_QUEUE_RUNNING_CALLBACK ); { arg_pack_type * arg_pack = arg_pack_alloc(); arg_pack_append_ptr( arg_pack , queue ); arg_pack_append_int( arg_pack , job_queue_node_get_queue_index(node)); thread_pool_add_job( queue->work_pool , job_queue_run_EXIT_callback , arg_pack ); } }
void ensemble_load_from_glob( ensemble_type * ensemble , const char * pattern , thread_pool_type * tp) { glob_t pglob; int i; glob( pattern , GLOB_NOSORT , NULL , &pglob ); for (i=0; i < pglob.gl_pathc; i++) { arg_pack_type * arg_pack = arg_pack_alloc( ); arg_pack_append_ptr( arg_pack , ensemble ); arg_pack_append_owned_ptr( arg_pack , util_alloc_string_copy( pglob.gl_pathv[i] ) , free ); thread_pool_add_job( tp , ensemble_add_case__ , arg_pack ); } globfree( &pglob ); }
void enkf_main_initialize_from_scratch(enkf_main_type * enkf_main , const stringlist_type * param_list , int iens1 , int iens2, init_mode_enum init_mode) { int num_cpu = 4; thread_pool_type * tp = thread_pool_alloc( num_cpu , true ); int ens_sub_size = (iens2 - iens1 + 1) / num_cpu; arg_pack_type ** arg_list = util_calloc( num_cpu , sizeof * arg_list ); int i; printf("Setting up ensemble members from %d to %d", iens1, iens2); if (init_mode == INIT_CONDITIONAL) { printf(" using conditional initialization (keep existing parameter values).\n"); } else if (init_mode == INIT_FORCE) { printf(" using forced initialization (initialize from scratch).\n"); } else if (init_mode == INIT_NONE) { printf(" not initializing at all.\n"); } fflush( stdout ); for (i = 0; i < num_cpu; i++) { arg_list[i] = arg_pack_alloc(); arg_pack_append_ptr( arg_list[i] , enkf_main ); arg_pack_append_const_ptr( arg_list[i] , param_list ); { int start_iens = i * ens_sub_size; int end_iens = start_iens + ens_sub_size; if (i == (num_cpu - 1)){ end_iens = iens2 + 1; /* Input is upper limit inclusive. */ if(ens_sub_size == 0) start_iens = iens1; /* Don't necessarily want to start from zero when ens_sub_size = 0*/ } arg_pack_append_int( arg_list[i] , start_iens ); arg_pack_append_int( arg_list[i] , end_iens ); } arg_pack_append_int( arg_list[i] , init_mode ); thread_pool_add_job( tp , enkf_main_initialize_from_scratch_mt , arg_list[i]); } thread_pool_join( tp ); for (i = 0; i < num_cpu; i++) arg_pack_free( arg_list[i] ); free( arg_list ); thread_pool_free( tp ); printf("Done setting up ensemble.\n"); }
void thread_test() { time_map_type * time_map = time_map_alloc( ); { int pool_size = 1000; thread_pool_type * tp = thread_pool_alloc( pool_size/2 , true ); thread_pool_add_job( tp , update_time_map , time_map ); thread_pool_join(tp); thread_pool_free(tp); } { int i; for (i=0; i < MAP_SIZE; i++) test_assert_true( time_map_iget( time_map , i ) == i ); } time_map_free( time_map ); }
int main(int argc, int argv) { char *args[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"}; thread_pool_t *thread_pool = thread_pool_init(1, 2); int i = 0, tag, value; for (i = 0; i < 30; i++) { do { tag = thread_pool_add_job(thread_pool, work, args[i]); if (tag == 1) { value = thread_pool_resize(thread_pool, thread_pool->thread_num * 2, thread_pool->queue_max_num * 2); if (value == -1) { printf("参数错误!\n"); exit(-1); } else if (value == -2) { printf("申请内存错误!\n"); exit(-1); } else if (value == -3) { printf("线程创建错误!\n"); exit(-1); } } }while (tag != 0); } sleep(2); thread_pool_destroy(thread_pool); return 0; }
// 服务器等待连接函数 bool xyftp_accept_client() { int listen_fd; int accept_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { xyftp_print_info(LOG_ERR, "Create Socket Error, What The F**k !?"); return false; } int optval = 1; if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&optval, sizeof(int)) < 0) { xyftp_print_info(LOG_ERR, "Set Socket SO_REUSEADDR Error!"); return false; } bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(config_global.ftp_port); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { xyftp_print_info(LOG_ERR, "Bind Scoket Error!"); return false; } if (listen(listen_fd, LISTEN_SIZE) < 0) { xyftp_print_info(LOG_ERR, "Listen Socket Error!"); return false; } socklen_t client_addr_len = sizeof(client_addr); while (true) { bzero(&client_addr, sizeof(client_addr)); accept_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &client_addr_len); if (accept_fd < 0) { xyftp_print_info(LOG_ERR, "Accept Socket Error!"); return false; } char info_buf[100]; sprintf(info_buf, "New Connection Create. Client IP : %s", inet_ntoa(client_addr.sin_addr)); xyftp_print_info(LOG_INFO, info_buf); // 传值,注意在线程函数内部读取参数值,而非解引用 int tag, thread_pool_want; do { // 非阻塞模式调用线程池接口 tag = thread_pool_add_job(thread_pool_global, xyftp_thread_job_entry, (void *)accept_fd, 0); if (tag == 1) { thread_pool_want = thread_pool_global->thread_num + config_global.thread_pool_add_size; if (thread_pool_want <= config_global.max_clients) { if (thread_pool_resize(thread_pool_global, thread_pool_want, thread_pool_want) != 0) { xyftp_print_info(LOG_ERR, "Thread Pool Resize Error!"); break; } xyftp_print_info(LOG_INFO, "Thread Pool Resized Succeed!"); } else { xyftp_print_info(LOG_ERR, "Too Many Client!"); close(accept_fd); break; } } else if (tag == -1) { xyftp_print_info(LOG_ERR, "Add Thread Pool Job Error!"); return false; } } while (tag != 0); } }
void test_runpath_list() { runpath_list_type * list = runpath_list_alloc(); test_assert_int_equal( runpath_list_size( list ) , 0 ); runpath_list_add( list , 3 , 0, "path" , "base"); runpath_list_add( list , 2 , 0, "path" , "base"); runpath_list_add( list , 1 , 0, "path" , "base"); runpath_list_add( list , 3 , 1, "path" , "base"); runpath_list_add( list , 2 , 1, "path" , "base"); runpath_list_add( list , 1 , 1, "path" , "base"); test_assert_int_equal( runpath_list_size( list ) , 6 ); test_assert_int_equal( runpath_list_iget_iens( list , 0 ) , 3 ); test_assert_int_equal( runpath_list_iget_iens( list , 2 ) , 1 ); test_assert_int_equal( runpath_list_iget_iter( list , 3 ) , 1 ); runpath_list_sort( list ); test_assert_int_equal( runpath_list_iget_iens( list , 0 ) , 1 ); test_assert_int_equal( runpath_list_iget_iens( list , 4 ) , 3 ); runpath_list_clear( list ); test_assert_int_equal( runpath_list_size( list ) , 0 ); test_assert_string_equal( runpath_list_get_line_fmt( list ) , RUNPATH_LIST_DEFAULT_LINE_FMT ); { const char * other_line = "%d %s %s"; runpath_list_set_line_fmt( list , other_line ); test_assert_string_equal( runpath_list_get_line_fmt( list ) , other_line ); } runpath_list_set_line_fmt( list , NULL ); test_assert_string_equal( runpath_list_get_line_fmt( list ) , RUNPATH_LIST_DEFAULT_LINE_FMT ); { const int block_size = 100; const int threads = 100; thread_pool_type * tp = thread_pool_alloc( threads , true ); int it; for (it = 0; it < threads; it++) { int iens_offset = it * block_size; arg_pack_type * arg_pack = arg_pack_alloc(); arg_pack_append_ptr( arg_pack , list ); arg_pack_append_int( arg_pack , iens_offset ); arg_pack_append_int( arg_pack , block_size ); thread_pool_add_job( tp , add_pathlist , arg_pack ); } thread_pool_join( tp ); test_assert_int_equal( runpath_list_size( list ) , block_size * threads ); runpath_list_sort( list ); { int iens; for (iens = 0; iens < block_size * threads; iens++) test_assert_int_equal( runpath_list_iget_iens( list , iens ) , iens ); } { test_work_area_type * work_area = test_work_area_alloc("enkf_runpath_list" ); const char *filename = "runpath_list.txt"; { FILE * stream = util_fopen( filename, "w"); runpath_list_fprintf( list , stream ); fclose( stream ); } { int file_iens; int file_iter; char file_path[256]; char file_base[256]; int iens; FILE * stream = util_fopen( filename, "r"); for (iens = 0; iens < threads * block_size; iens++) { int fscanf_return = fscanf( stream , "%d %s %s %d" , &file_iens , file_path , file_base, &file_iter); test_assert_int_equal(fscanf_return, 4 ); test_assert_int_equal( file_iens , iens ); test_assert_int_equal( file_iter , 0 ); } fclose( stream ); } test_work_area_free( work_area ); } } runpath_list_free( list ); }
void enkf_tui_run_manual_load__( void * arg ) { enkf_main_type * enkf_main = enkf_main_safe_cast( arg ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); const int last_report = -1; const int ens_size = enkf_main_get_ensemble_size( enkf_main ); int step1,step2; bool_vector_type * iactive = bool_vector_alloc( 0 , false ); run_mode_type run_mode = ENSEMBLE_EXPERIMENT; enkf_main_init_run(enkf_main , run_mode); /* This is ugly */ step1 = 0; step2 = last_report; /** Observe that for the summary data it will load all the available data anyway. */ { char * prompt = util_alloc_sprintf("Which realizations to load (Ex: 1,3-5) <Enter for all> [M to return to menu] : [ensemble size:%d] : " , ens_size); char * select_string; util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> "); select_string = util_alloc_stdin_line(); enkf_tui_util_sscanf_active_list( iactive , select_string , ens_size ); util_safe_free( select_string ); free( prompt ); } if (bool_vector_count_equal( iactive , true )) { int iens; arg_pack_type ** arg_list = util_calloc( ens_size , sizeof * arg_list ); thread_pool_type * tp = thread_pool_alloc( 4 , true ); /* num_cpu - HARD coded. */ for (iens = 0; iens < ens_size; iens++) { arg_pack_type * arg_pack = arg_pack_alloc(); arg_list[iens] = arg_pack; if (bool_vector_iget(iactive , iens)) { enkf_state_type * enkf_state = enkf_main_iget_state( enkf_main , iens ); arg_pack_append_ptr( arg_pack , enkf_state); /* 0: */ arg_pack_append_ptr( arg_pack , fs ); /* 1: */ arg_pack_append_int( arg_pack , step1 ); /* 2: This will be the load start parameter for the run_info struct. */ arg_pack_append_int( arg_pack , step1 ); /* 3: Step1 */ arg_pack_append_int( arg_pack , step2 ); /* 4: Step2 For summary data it will load the whole goddamn thing anyway.*/ arg_pack_append_bool( arg_pack , true ); /* 5: Interactive */ arg_pack_append_owned_ptr( arg_pack , stringlist_alloc_new() , stringlist_free__); /* 6: List of interactive mode messages. */ thread_pool_add_job( tp , enkf_state_load_from_forward_model_mt , arg_pack); } } thread_pool_join( tp ); thread_pool_free( tp ); printf("\n"); { qc_module_type * qc_module = enkf_main_get_qc_module( enkf_main ); runpath_list_type * runpath_list = qc_module_get_runpath_list( qc_module ); for (iens = 0; iens < ens_size; iens++) { if (bool_vector_iget(iactive , iens)) { const enkf_state_type * state = enkf_main_iget_state( enkf_main , iens ); runpath_list_add( runpath_list , iens , enkf_state_get_run_path( state ) , enkf_state_get_eclbase( state )); } } qc_module_export_runpath_list( qc_module ); } for (iens = 0; iens < ens_size; iens++) { if (bool_vector_iget(iactive , iens)) { stringlist_type * msg_list = arg_pack_iget_ptr( arg_list[iens] , 6 ); if (stringlist_get_size( msg_list )) enkf_tui_display_load_msg( iens , msg_list ); } } for (iens = 0; iens < ens_size; iens++) arg_pack_free( arg_list[iens]); free( arg_list ); } bool_vector_free( iactive ); }
int rest_handle_request(HTTP_SESSION *s, unsigned char **d, size_t *l) { REST_URI_HANDLER handler = NULL; if (memcmp(s->http_url, "/history/", 9) == 0) { char *ptr; unsigned int object_id = strtoul(&s->http_url[9], &ptr, 10); if (*ptr == '/') { unsigned int t_from = strtoul(ptr + 1, &ptr, 10); if (*ptr == '/') { unsigned int t_to = strtoul(ptr + 1, &ptr, 10); if ((memcmp(ptr, ".png", 4) == 0)||(memcmp(ptr, ".bin", 4) == 0)) { DB_OBJECT *object; object = api_db_get_object(object_id); if (object == NULL) { if (s->zero_init.keep_alive) { *d = response_fail_404_ka; *l = response_fail_404_ka_length; } else { *d = response_fail_404_close; *l = response_fail_404_close_length; } return 0; } REST_USER *user = rest_find_user(s); if ((user == NULL)||(!rest_check_access_to_object(user, object))) { if (s->zero_init.keep_alive) { *d = response_fail_401_ka; *l = response_fail_401_ka_length; } else { *d = response_fail_401_close; *l = response_fail_401_close_length; } } THREAD_POOL_JOB job; job.terminal_id = object_id; job.tFrom = t_from; job.tTo = t_to; job.session = s; job.code = (memcmp(ptr, ".bin", 4)) ? JOB_CODE_HISTORY_PNG : JOB_CODE_HISTORY_BIN; thread_pool_add_job(&job); return 1; } } } } if (memcmp(s->http_url, "/report/", 8) == 0) { char *ptr; unsigned int report_id = strtoul(&s->http_url[8], &ptr, 10); if (*ptr == '/') { unsigned int object_id = strtoul(ptr + 1, &ptr, 10); if (*ptr == '/') { unsigned int t_from = strtoul(ptr + 1, &ptr, 10); if (*ptr == '/') { unsigned int t_to = strtoul(ptr + 1, &ptr, 10); if (memcmp(ptr, ".json", 5) == 0) { DB_OBJECT *object; object = api_db_get_object(object_id); if ((object == NULL)||(report_id < 1)||(report_id > 16)) { if (s->zero_init.keep_alive) { *d = response_fail_404_ka; *l = response_fail_404_ka_length; } else { *d = response_fail_404_close; *l = response_fail_404_close_length; } return 0; } REST_USER *user = rest_find_user(s); if ((user == NULL)||(!rest_check_access_to_object(user, object))) { if (s->zero_init.keep_alive) { *d = response_fail_401_ka; *l = response_fail_401_ka_length; } else { *d = response_fail_401_close; *l = response_fail_401_close_length; } } if (t_from > t_to) { response_fail_with_message(*d, l, (unsigned char *)"Invalid argument", 16, s->zero_init.keep_alive > 0); return 0; } THREAD_POOL_JOB job; JKEY key; if ((jparse_parse(object->core_data, object->core_data_size, s->session_data, s->session_data_len, &key) == 0)&&(key.value_type == JPARSE_VALUE_TYPE_OBJECT)) { job.ignition = false; job.engine = false; job.move = false; job.ignition_source = 0; job.engine_source = 0; job.move_source = 0; job.lls_table_left.reserve(100); job.lls_table_right.reserve(100); job.lls_left_source = 0; job.lls_right_source = 0; bool A[8] = {false, false, false, false, false, false, false, false}; bool F[8] = {false, false, false, false, false, false, false, false}; bool D[8] = {false, false, false, false, false, false, false, false}; bool RS485 = false; bool RS232[2] = {false, false}; bool injector = false; JKEY *k = key.value.object_val.first_key; while (k) { if ((k->key_len == 4)&&(memcmp(k->key, "caps", 4) == 0)) { if (k->value_type == JPARSE_VALUE_TYPE_OBJECT) { JKEY *l = k->value.object_val.first_key; while (l) { if ((l->key_len == 8)&&(memcmp(l->key, "ignition", 8) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { job.ignition = true; } else if ((l->key_len == 8)&&(memcmp(l->key, "engine", 6) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { job.engine = true; } else if ((l->key_len == 4)&&(memcmp(l->key, "move", 4) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { job.move = true; } else if ((l->key_len == 5)&&(memcmp(l->key, "rs485", 5) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { RS485 = true; } else if ((l->key_len == 7)&&(memcmp(l->key, "rs232_1", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { RS232[0] = true; } else if ((l->key_len == 7)&&(memcmp(l->key, "rs232_2", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { RS232[1] = true; } else if ((l->key_len == 8)&&(memcmp(l->key, "injector", 8) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { injector = true; } else if (l->key_len == 7) { if ((memcmp(l->key, "analog1", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) A[0] = true; else if ((memcmp(l->key, "analog2", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) A[1] = true; else if ((memcmp(l->key, "analog3", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) A[2] = true; else if ((memcmp(l->key, "analog4", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) A[3] = true; else if ((memcmp(l->key, "analog5", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) A[4] = true; else if ((memcmp(l->key, "analog6", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) A[5] = true; else if ((memcmp(l->key, "analog7", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) A[6] = true; else if ((memcmp(l->key, "analog8", 7) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) A[7] = true; } else if (l->key_len == 10) { if ((memcmp(l->key, "frequency1", 10) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) F[0] = true; else if ((memcmp(l->key, "frequency2", 10) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) F[1] = true; else if ((memcmp(l->key, "frequency3", 10) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) F[2] = true; else if ((memcmp(l->key, "frequency4", 10) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) F[3] = true; else if ((memcmp(l->key, "frequency5", 10) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) F[4] = true; else if ((memcmp(l->key, "frequency6", 10) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) F[5] = true; else if ((memcmp(l->key, "frequency7", 10) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) F[6] = true; else if ((memcmp(l->key, "frequency8", 10) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) F[7] = true; } else if (l->key_len == 9) { if ((memcmp(l->key, "discrete1", 9) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) D[0] = true; else if ((memcmp(l->key, "discrete2", 9) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) D[1] = true; else if ((memcmp(l->key, "discrete3", 9) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) D[2] = true; else if ((memcmp(l->key, "discrete4", 9) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) D[3] = true; else if ((memcmp(l->key, "discrete5", 9) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) D[4] = true; else if ((memcmp(l->key, "discrete6", 9) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) D[5] = true; else if ((memcmp(l->key, "discrete7", 9) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) D[6] = true; else if ((memcmp(l->key, "discrete8", 9) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) D[7] = true; } l = l->next_key; } } } k = k->next_key; } k = key.value.object_val.first_key; while (k) { if ((k->key_len == 7)&&(memcmp(k->key, "sensors", 7) == 0)) { if (k->value_type == JPARSE_VALUE_TYPE_OBJECT) { JKEY *l = k->value.object_val.first_key; while (l) { char discrete[] = "discrete1_type"; for (int i = 1; i <= 8; i++) { discrete[8] = i + '0'; if ((l->key_len == 14)&&(memcmp(l->key, discrete, 14) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_NUMBER)) { if ((l->value.int_val == 1)&&(D[i - 1])) job.ignition_source = i; else if ((l->value.int_val == 2)&&(D[i - 1])) job.engine_source = i; else if ((l->value.int_val == 3)&&(D[i - 1])) job.move_source = i; } } if ((l->key_len == 15)&&(memcmp(l->key, "injector_factor", 15) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_NUMBER)) { job.injector_factor = l->value.int_val; } l = l->next_key; } } break; } k = k->next_key; } if (report_id == JOB_CODE_REPORT_FUEL) { job.filter_level = 2; job.fill_threshold = 25; job.drain_threshold = 10000; job.max_consumption = 10000; job.injector = injector; while (k) { if ((k->key_len == 7)&&(memcmp(k->key, "sensors", 7) == 0)) { if (k->value_type == JPARSE_VALUE_TYPE_OBJECT) { JKEY *l = k->value.object_val.first_key; while (l) { char analog[] = "analog1_type"; char frequency[] = "frequency1_type"; for (int i = 1; i <= 8; i++) { analog[6] = i + '0'; frequency[9] = i + '0'; if ((l->key_len == 12)&&(memcmp(l->key, analog, 12) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_NUMBER)) { if ((l->value.int_val == 2)&&(A[i - 1])) job.lls_left_source = 0x10 | i; else if ((l->value.int_val == 3)&&(A[i - 1])) job.lls_right_source = 0x10 | i; } else if ((l->key_len == 15)&&(memcmp(l->key, frequency, 15) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_NUMBER)) { if ((l->value.int_val == 2)&&(F[i - 1])) job.lls_left_source = i; else if ((l->value.int_val == 3)&&(F[i - 1])) job.lls_right_source = i; } } if ((RS485 == true)&&(l->key_len == 14)&&(memcmp(l->key, "rs485_lls_left", 14) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { job.lls_left_source = FUEL_SOURCE_RS485_1; } if ((RS485 == true)&&(l->key_len == 15)&&(memcmp(l->key, "rs485_lls_right", 15) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_TRUE)) { job.lls_right_source = FUEL_SOURCE_RS485_2; } l = l->next_key; } } } else if ((k->key_len == 4)&&(memcmp(k->key, "fuel", 4) == 0)) { if (k->value_type == JPARSE_VALUE_TYPE_OBJECT) { JKEY *l = k->value.object_val.first_key; while (l) { if ((l->key_len == 12)&&(memcmp(l->key, "filter_level", 12) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_NUMBER)) { job.filter_level = l->value.int_val; } else if ((l->key_len == 14)&&(memcmp(l->key, "fill_threshold", 14) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_NUMBER)) { job.fill_threshold = l->value.int_val; } else if ((l->key_len == 15)&&(memcmp(l->key, "drain_threshold", 15) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_NUMBER)) { job.drain_threshold = l->value.int_val; } else if ((l->key_len == 15)&&(memcmp(l->key, "max_consumption", 15) == 0)&&(l->value_type == JPARSE_VALUE_TYPE_NUMBER)) { job.max_consumption = l->value.int_val; } l = l->next_key; } } } else if ((k->key_len == 7)&&(memcmp(k->key, "llsleft", 7) == 0)) { if (k->value_type == JPARSE_VALUE_TYPE_OBJECT) { JKEY *l = k->value.object_val.first_key; while (l) { if ((l->key_len == 5)&&(memcmp(l->key, "table", 5) == 0)) { if (l->value_type == JPARSE_VALUE_TYPE_ARRAY) { JOBJECT *o = l->value.array_val.first_object; while (o) { FUEL_TABLE_RECORD ftr; ftr.fuel_value = -1; ftr.sensor_value = -1; JKEY *tk = o->first_key; while (tk) { if ((tk->key_len == 12)&&(memcmp(tk->key, "sensor_value", 12) == 0)) { if (tk->value_type == JPARSE_VALUE_TYPE_FLOAT) ftr.sensor_value = tk->value.float_val; else if (tk->value_type == JPARSE_VALUE_TYPE_NUMBER) ftr.sensor_value = (float)tk->value.int_val; } else if ((tk->key_len == 10)&&(memcmp(tk->key, "fuel_value", 10) == 0)) { if (tk->value_type == JPARSE_VALUE_TYPE_FLOAT) ftr.fuel_value = tk->value.float_val; else if (tk->value_type == JPARSE_VALUE_TYPE_NUMBER) ftr.fuel_value = (float)tk->value.int_val; } tk = tk->next_key; } if ((ftr.fuel_value != -1)&&(ftr.sensor_value != -1)) job.lls_table_left.push_back(ftr); o = o->next_object; } std::sort(job.lls_table_left.begin(), job.lls_table_left.end()); } break; } l = l->next_key; } } } else if ((k->key_len == 8)&&(memcmp(k->key, "llsright", 8) == 0)) { if (k->value_type == JPARSE_VALUE_TYPE_OBJECT) { JKEY *l = k->value.object_val.first_key; while (l) { if ((l->key_len == 5)&&(memcmp(l->key, "table", 5) == 0)) { if (l->value_type == JPARSE_VALUE_TYPE_ARRAY) { JOBJECT *o = l->value.array_val.first_object; while (o) { FUEL_TABLE_RECORD ftr; ftr.fuel_value = -1; ftr.sensor_value = -1; JKEY *tk = o->first_key; while (tk) { if ((tk->key_len == 12)&&(memcmp(tk->key, "sensor_value", 12) == 0)) { if (tk->value_type == JPARSE_VALUE_TYPE_FLOAT) ftr.sensor_value = tk->value.float_val; else if (tk->value_type == JPARSE_VALUE_TYPE_NUMBER) ftr.sensor_value = (float)tk->value.int_val; } else if ((tk->key_len == 10)&&(memcmp(tk->key, "fuel_value", 10) == 0)) { if (tk->value_type == JPARSE_VALUE_TYPE_FLOAT) ftr.fuel_value = tk->value.float_val; else if (tk->value_type == JPARSE_VALUE_TYPE_NUMBER) ftr.fuel_value = (float)tk->value.int_val; } tk = tk->next_key; } if ((ftr.fuel_value != -1)&&(ftr.sensor_value != -1)) job.lls_table_right.push_back(ftr); o = o->next_object; } std::sort(job.lls_table_right.begin(), job.lls_table_right.end()); } break; } l = l->next_key; } } } k = k->next_key; } job.terminal_id = object_id; job.tFrom = t_from; job.tTo = t_to; job.session = s; job.code = report_id; thread_pool_add_job(&job); return 1; } else { job.terminal_id = object_id; job.tFrom = t_from; job.tTo = t_to; job.session = s; job.code = report_id; thread_pool_add_job(&job); return 1; } } } } } } } if (memcmp(s->http_url, "/report.pdf", 11) == 0) { THREAD_POOL_JOB job; job.session = s; job.code = JOB_CODE_PDF; job.buffer = (unsigned char *)malloc(s->zero_init.body_len); if (job.buffer == NULL) { *d = response_fail_500_ka; *l = response_fail_500_ka_length; return 0; } job.buffer_size = s->zero_init.body_len; memcpy(job.buffer, s->http_body, s->zero_init.body_len); thread_pool_add_job(&job); return 1; } int i = rest_find_uri(s->http_url); if (s->zero_init.keep_alive) { if (i == -1) { *d = response_fail_404_ka; *l = response_fail_404_ka_length; return 0; } switch (s->parser.method) { case HTTP_GET: handler = rest_table[i].get; break; case HTTP_PUT: handler = rest_table[i].put; break; case HTTP_POST: handler = rest_table[i].post; break; case HTTP_DELETE: handler = rest_table[i].del; break; } if (handler == NULL) { *d = response_fail_405_ka; *l = response_fail_405_ka_length; return 0; } unsigned int id = rest_table[i].extract_id(s, s->http_url); if (id == UINT_MAX) { if (rest_table[i].extract_id == rest_get_self_id) { *d = response_fail_401_ka; *l = response_fail_401_ka_length; } else { *d = response_fail_404_ka; *l = response_fail_404_ka_length; } return 0; } int res = handler(s, id, d, l); switch (res) { case 0: break; default: case 500: *d = response_fail_500_ka; *l = response_fail_500_ka_length; break; case 401: *d = response_fail_401_ka; *l = response_fail_401_ka_length; break; case 404: *d = response_fail_404_ka; *l = response_fail_404_ka_length; break; case REST_SUCCESS: *d = response_success_ka; *l = response_success_ka_length; break; case REST_FAIL: *d = response_fail_ka; *l = response_fail_ka_length; break; } } return 0; }
int main(int argc , char ** argv) { install_SIGNALS(); if(argc > 1) { if(strcmp(argv[1], "-h") == 0) print_usage(__LINE__); } if(argc < 2) print_usage(__LINE__); else{ char ** input = &argv[1]; /* Skipping the name of the executable */ int input_length = argc - 1; int input_offset = 0; bool use_eclbase, fmt_file; const char * report_filen = "RUN_GRAVITY.out"; ecl_file_type ** restart_files; ecl_file_type * init_file; ecl_grid_type * ecl_grid; int model_phases; int file_phases; vector_type * grav_stations = vector_alloc_new(); /* Restart info */ restart_files = load_restart_info( (const char **) input , input_length , &input_offset , &use_eclbase , &fmt_file); /* INIT and GRID/EGRID files */ { char * grid_filename = NULL; char * init_filename = NULL; if (use_eclbase) { /* The first command line argument is interpreted as ECLBASE, and we search for grid and init files in cwd. */ init_filename = ecl_util_alloc_exfilename_anyfmt( NULL , input[0] , ECL_INIT_FILE , fmt_file , -1); grid_filename = ecl_util_alloc_exfilename_anyfmt( NULL , input[0] , ECL_EGRID_FILE , fmt_file , -1); if (grid_filename == NULL) grid_filename = ecl_util_alloc_exfilename_anyfmt( NULL , input[0] , ECL_GRID_FILE , fmt_file , -1); if ((init_filename == NULL) || (grid_filename == NULL)) /* Means we could not find them. */ util_exit("Could not find INIT or GRID|EGRID file \n"); } else { /* */ if ((input_length - input_offset) > 1) { init_filename = util_alloc_string_copy(input[input_offset]); grid_filename = util_alloc_string_copy(input[input_offset + 1]); input_offset += 2; } else print_usage(__LINE__); } init_file = ecl_file_open(init_filename ); ecl_grid = ecl_grid_alloc(grid_filename ); free( init_filename ); free( grid_filename ); } // Load the station_file if (input_length > input_offset) { char * station_file = input[input_offset]; if (util_file_exists(station_file)) load_stations( grav_stations , station_file); else util_exit("Can not find file:%s \n",station_file); } else print_usage(__LINE__); /** OK - now everything is loaded - check that all required keywords+++ are present. */ gravity_check_input(ecl_grid , init_file , restart_files[0] , restart_files[1] , &model_phases , &file_phases); /* OK - now it seems the provided files have all the information we need. Let us start using it. The main loop is run in parallell on four threads - most people have four cores these days. */ { int i; int num_threads = 4; thread_pool_type * tp = thread_pool_alloc( num_threads , true); arg_pack_type ** arg_list = util_calloc( num_threads , sizeof * arg_list); { int station_delta = vector_get_size( grav_stations ) / num_threads; for (i = 0; i < num_threads; i++) { int station1 = i * station_delta; int station2 = station1 + station_delta; if (i == num_threads) station2 = vector_get_size( grav_stations ); arg_list[i] = arg_pack_alloc( ); arg_pack_append_ptr( arg_list[i] , grav_stations ); arg_pack_append_ptr( arg_list[i] , ecl_grid); arg_pack_append_ptr( arg_list[i] , init_file ); arg_pack_append_ptr( arg_list[i] , restart_files); arg_pack_append_int( arg_list[i] , station1 ); arg_pack_append_int( arg_list[i] , station2 ); arg_pack_append_int( arg_list[i] , model_phases ); arg_pack_append_int( arg_list[i] , file_phases ); thread_pool_add_job( tp , gravity_response_mt , arg_list[i]); } } thread_pool_join( tp ); for (i = 0; i < num_threads; i++) arg_pack_free( arg_list[i] ); free( arg_list ); } { FILE * stream = util_fopen(report_filen , "w"); int station_nr; double total_chisq = 0; for(station_nr = 0; station_nr < vector_get_size( grav_stations ); station_nr++){ const grav_station_type * g_s = vector_iget_const(grav_stations, station_nr); fprintf(stream, "%f",g_s->grav_diff); printf ("DELTA_G %4s[%02d]: %12.6f %12.6f %12.6f %12.6f", g_s->name , station_nr, g_s->grav_diff, g_s->utm_x, g_s->utm_y, g_s->depth); if ( g_s->has_obs ) { double y = (g_s->grav_diff - g_s->obs_gdiff) / g_s->std_gdiff; double chi_sq = y * y; total_chisq += chi_sq; fprintf(stream , " %g",chi_sq); printf(" %g",chi_sq); } fprintf(stream , " \n"); printf("\n"); } if (total_chisq > 0) { printf("Total chisq misfit: %g \n", total_chisq); } fclose(stream); } vector_free( grav_stations ); ecl_grid_free(ecl_grid); ecl_file_close(restart_files[0]); ecl_file_close(restart_files[1]); free( restart_files ); ecl_file_close(init_file); } }