int main(int argc, char *argv[]) { if(argc != 3) { fprintf(stderr, "Usage: %s start, end \n", argv[0]); exit(EXIT_FAILURE); } double starttime, endtime; starttime = MPI_Wtime(); char* endptr; double start = strtod(argv[1], &endptr); double end = strtod(argv[2], &endptr); int provided = 0; MPI_Init(&argc, &argv); MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN); //MPI_Errhandler newErrorHandler; //MPI_Comm_create_errhandler(error_handler, &newErrorHandler); //MPI_Comm_set_errhandler(MPI_COMM_WORLD, newErrorHandler); int childrenQuantity, procNumber; MPI_Comm_rank(MPI_COMM_WORLD, &procNumber); MPI_Comm_size(MPI_COMM_WORLD, &childrenQuantity); double globalResult = 0; if(childrenQuantity == 1) { Range distance = Range_ctor(start, end, step); globalResult = calc_integral(&distance); MPI_Finalize(); endtime = MPI_Wtime(); //printf("result = %lg, time = %lg\n", globalResult, endtime-starttime); return 0; } if(procNumber == 0) { double localResult[2]; int distanceQuantity = 30; Range distancesQuery[distanceQuantity]; //Make distance Query double part = (end - start)/distanceQuantity; for(int i = 0; i < distanceQuantity; i++) { distancesQuery[i] = Range_ctor(start + i * part, start + (i + 1) * part, step); } //Send distance to child int currentPoint = 0; Range distance; for(int i = 1; i < childrenQuantity; i++) { MPI_Send(&distancesQuery[currentPoint].start, 1,\ MPI_DOUBLE, i, 0, MPI_COMM_WORLD); MPI_Send(&distancesQuery[currentPoint].end, 1, \ MPI_DOUBLE, i, 0, MPI_COMM_WORLD); currentPoint++; } //Get answer and send more! MPI_Status status; int recieveMessagesNumbers = 0; while(recieveMessagesNumbers < distanceQuantity - 1) { try { MPI_Recv(&localResult, 2, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status); recieveMessagesNumbers ++; globalResult += localResult[0]; //delete recieve message from query for(int i = 0; i < distanceQuantity; i++) { if(distancesQuery[i].start == localResult[1]) { distancesQuery[i].step = -1; break; } } while(true) { if(distancesQuery[currentPoint].step > 0) { MPI_Send(&distancesQuery[currentPoint].start, 1, MPI_DOUBLE, status.MPI_SOURCE, 0, MPI_COMM_WORLD); MPI_Send(&distancesQuery[currentPoint].end, 1, MPI_DOUBLE, status.MPI_SOURCE, 0, MPI_COMM_WORLD); currentPoint = (currentPoint + 1) % distanceQuantity; break; } currentPoint = (currentPoint + 1) % distanceQuantity; } } catch(...) { printf("Hello"); } } //end all threads: endtime = MPI_Wtime(); printf("result = %lg, time = %lg", globalResult, endtime-starttime); double closeThread[2]; closeThread[0] = 0; closeThread[1] = -1; for(int i = 1; i < childrenQuantity; i++) { MPI_Send(&closeThread[0], 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD); MPI_Send(&closeThread[1], 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD); } MPI_Finalize(); return 0; } create_threads(childrenQuantity, start, end, procNumber); MPI_Finalize(); //double result = calc_integral(childrenQuantity, start, end); //printf("result = %lg", result); return 0; }
thread_pool_t *get_thread_pool(){; if (pool == NULL) return create_threads(); return pool; }
int main ( int argc, char *argv[] ) { pwr_tStatus sts; int event; plc_sProcess *pp; uid_t ruid; struct passwd *pwd; /* struct rlimit rlim; int i; */ /* Set core dump file size limit to infinite */ /* rlim.rlim_cur = RLIM_INFINITY; rlim.rlim_max = RLIM_INFINITY; sts = setrlimit(RLIMIT_CORE, &rlim); printf("%d\n", sts); i = 1/0; printf("%d\n", i); */ pp = init_process(); qcom_WaitAnd(&sts, &pp->eventQ, &qcom_cQini, ini_mEvent_newPlcInit, qcom_cTmoEternal); init_plc(pp); create_threads(pp); init_threads(pp); /* Once threads has set their priority don't run as root */ #if 0 ruid = getuid(); if (ruid == 0) { pwd = getpwnam("pwrp"); if (pwd != NULL) { setreuid(pwd->pw_uid, pwd->pw_uid); } } else setreuid(ruid, ruid); #endif qcom_SignalOr(&sts, &qcom_cQini, ini_mEvent_newPlcInitDone); qcom_WaitAnd(&sts, &pp->eventQ, &qcom_cQini, ini_mEvent_newPlcStart, qcom_cTmoEternal); // proc_SetPriority(pp->PlcProcess->Prio); set_values(pp); start_threads(pp); run_threads(pp); time_Uptime(&sts, &pp->PlcProcess->StartTime, NULL); qcom_SignalOr(&sts, &qcom_cQini, ini_mEvent_newPlcStartDone); #if 0 /* Force the backup to take care initialized backup objects. */ bck_ForceBackup(NULL); #endif errh_SetStatus( PWR__SRUN); qcom_WaitOr(&sts, &pp->eventQ, &qcom_cQini, ini_mEvent_terminate | ini_mEvent_oldPlcStop, qcom_cTmoEternal, &event); switch ( event) { case ini_mEvent_terminate: errh_SetStatus( PWR__SRVTERM); stop_threads(pp); clean_all(pp); nmps_delete_lock( &sts); break; case ini_mEvent_oldPlcStop: errh_SetStatus( PWR__SRVTERM); time_Uptime(&sts, &pp->PlcProcess->StopTime, NULL); stop_threads(pp); save_values(pp); qcom_SignalOr(&sts, &qcom_cQini, ini_mEvent_oldPlcStopDone); #if defined OS_ELN sts = proc_SetPriority(31); #endif clean_all(pp); break; default: ; } exit(0); }
int main(int argc, char* argv[]) { if(argc < 4) { printf("Usage:%s etc_fn server_id log_fn\n", argv[0]); return -1; } //命令行参数,依次为: 配置文件路径,server_id,日志文件路径名 const char* pszEtcFn = argv[1]; uint16_t nServerId = SERVER_LOG; const char* pszLogPath = argv[3]; signal(SIGPIPE, SIG_IGN); //curl_global_init(CURL_GLOBAL_ALL); CDebug::Init(); InitLib(); //printf("%d\n", mysql_thread_safe() ); { MYSQL* dummy = mysql_init(NULL); mysql_close(dummy); } g_logger_mutex = new pthread_mutex_t; if(!g_pluto_recvlist.InitMutex() || !g_pluto_sendlist.InitMutex() || !g_worldOther.InitMutex() || pthread_mutex_init(g_logger_mutex, NULL) != 0 ) { printf("pthead_mutext_t init error:%d,%s\n", errno, strerror(errno)); return -1; } g_logger.SetLogPath(pszLogPath); CWorldOther& world = g_worldOther; int nRet = world.init(pszEtcFn); if(nRet != 0) { printf("CWorldLog init error:%d\n", nRet); return nRet; } COtherServer s; s.SetMailboxId(nServerId); s.SetWorld(&world); world.SetServer(&s); vector<pthread_t> pid_list; int ret = create_threads(pid_list); if ( 0 != ret) { return ret; } signal(SIGALRM, SigHandler); uint16_t unPort = world.GetServerPort(nServerId); ////初始化线程池以及相关的多线程的检查 //InitThreadPool(); s.Service("", unPort); //处理完了所有的包后再关闭线程池 //DestroyThreadPool(); for(size_t i = 0; i < pid_list.size(); ++i) { if(pthread_join(pid_list[i], NULL) != 0) { return -3; } } }
int main() { create_threads(); }
/** @details -# verify that all jobs in the checkpoint are present in the current simulation -# Clear all current job information in scheduler -# Clear all current sim_object information in scheduler -# Add the sim_objects in checkpoint to the scheduler -# Call post_checkpoint to delete memory for checkpoint */ int Trick::Executive::restart() { unsigned int ii ; std::multimap<std::string, Trick::JobData *>::iterator it ; std::pair<std::multimap<std::string, Trick::JobData *>::iterator, std::multimap<std::string, Trick::JobData *>::iterator> ret ; std::vector <Trick::SimObject *>::iterator sit ; std::vector <Trick::JobData *>::iterator jit ; std::multimap<std::string, Trick::JobData *> all_jobs_from_checkpointed_objects ; /* Save the current job index position of the input processor queue. This queue position is special because of the possibility of restarting from a checkpoint read in from the input file. When we restart from a checkpoint we reset the queues. If we reset the input_processor queue we'll call the input_processor again... and we can end up in recursive hell. Save the current position and replace the queue position after the queues have been rebuilt. This ensures that we don't call the input file twice, or more! */ unsigned int current_ip_index = input_processor_queue.get_curr_index() ; /* clear all current job information in scheduler. These will be repopulated when we call add_job_to_queue */ clear_scheduled_queues() ; all_jobs.clear() ; all_jobs_vector.clear() ; all_tagged_jobs.clear() ; /* clear all sim_object information in scheduler */ num_sim_objects = 0 ; sim_objects.clear() ; /* restore the list of sim_objects from the checkpoint. */ restore_stl(sim_objects , std::string("trick_sys") , std::string("sim_objects")) ; /* Create a temporary all_jobs map to use to restore job data from all_jobs_for_checkpoint */ for ( sit = sim_objects.begin() ; sit != sim_objects.end() ; sit++ ) { for ( jit = (*sit)->jobs.begin() ; jit != (*sit)->jobs.end() ; jit++ ) { /* Dynamically created sim_objects, like data_recording objects, require that some of the job class data be repopulated before we start copying job data back into the sim. */ (*jit)->sim_object_id = (*sit)->id ; (*jit)->parent_object = (*sit) ; (*jit)->frame_id = (*jit)->sim_object_id + ((double)(*jit)->id / 100.0); std::string so_name_and_period = (*sit)->name + "." ; if ( strncmp( (*jit)->name.c_str() , so_name_and_period.c_str(), so_name_and_period.length())) { (*jit)->name = (*sit)->name + "." + (*jit)->name ; } all_jobs_from_checkpointed_objects.insert(std::pair<std::string, JobData *>((*jit)->name,(*jit))) ; } } /* verify that all jobs in the checkpoint are present in the current simulation */ for ( ii = 0 ; ii < num_all_jobs ; ii++ ) { ret = all_jobs_from_checkpointed_objects.equal_range(all_jobs_for_checkpoint[ii].name) ; if ( ret.first == all_jobs_from_checkpointed_objects.end() ) { message_publish(MSG_ERROR, "Could not find job %s\n", all_jobs_for_checkpoint[ii].name.c_str()) ; exec_terminate_with_return( -1 , __FILE__ , __LINE__ , "Job in checkpoint file does not exist in current sim." ) ; } else { for ( it = ret.first ; it != ret.second ; it++ ) { // The JobData::id and JobData::sim_object_id together uniquely identify a job. if (( it->second->id == all_jobs_for_checkpoint[ii].id ) && ( it->second->sim_object_id == all_jobs_for_checkpoint[ii].sim_object_id )) { // copy the job information from the checkpoint back to the job. it->second->copy_from_checkpoint(&all_jobs_for_checkpoint[ii]) ; } } } } /* restore the executive sim_objects vector from the checkpoint and add back all of the jobs to the schedulers */ for ( sit = sim_objects.begin() ; sit != sim_objects.end() ; sit++ ) { add_jobs_to_queue(*sit, true) ; for ( ii = 0 ; ii < other_schedulers.size() ; ii++ ) { other_schedulers[ii]->add_sim_object(*sit) ; } } num_sim_objects = sim_objects.size() ; // The queues have been rebuilt, restore the current position of the input processor queue. input_processor_queue.set_curr_index(current_ip_index) ; // set the restart_called flag to true. This flag is checked during Executive::init. If // we have restarted from a checkpoint, we will be skipping initialization jobs. restart_called = true ; /* Delete temporary memory used to restore jobs */ TMM_delete_var_a(all_jobs_for_checkpoint) ; all_jobs_for_checkpoint = NULL ; // restore the vector of freeze times restore_stl(freeze_times , std::string("trick_sys") , std::string("freeze_times")) ; /* Set the main thread current time to the simulation time tics value, used with Executive::get_sim_time() */ threads[0]->curr_time_tics = time_tics ; // if we restarted from scratch without calling init jobs, need to create child threads create_threads() ; // make sure asynchronous must finish jobs restart at correct time for (ii = 1; ii < threads.size() ; ii++) { Threads * curr_thread = threads[ii] ; if ( (curr_thread->process_type == PROCESS_TYPE_AMF_CHILD) || (curr_thread->process_type == PROCESS_TYPE_ASYNC_CHILD) ) { if ( curr_thread->amf_cycle_tics > 0 and time_tics % curr_thread->amf_cycle_tics ) { curr_thread->amf_next_tics = time_tics + curr_thread->amf_cycle_tics - (time_tics % curr_thread->amf_cycle_tics) ; } else { curr_thread->amf_next_tics = time_tics ; } } } // If we are in freeze, reset the freeze_scheduled queue, we'll restart freeze time at 0 if ( mode == Freeze ) { init_freeze_scheduled() ; } return(0) ; }