예제 #1
0
파일: potfit.c 프로젝트: swishart/potfit
int main(int argc, char** argv)
{

  initialize_global_variables();

  int ret = init_mpi(&argc, &argv);

  if (ret != POTFIT_SUCCESS) {
    shutdown_mpi();
    return EXIT_FAILURE;
  }

  read_input_files(argc, argv);

  g_mpi.init_done = 1;

  ret = broadcast_params_mpi();

  switch (ret) {
    case POTFIT_ERROR_MPI_CLEAN_EXIT:
      shutdown_mpi();
      return EXIT_SUCCESS;
    case POTFIT_ERROR:
      shutdown_mpi();
      return EXIT_FAILURE;
  }

  g_calc.ndim = g_pot.opt_pot.idxlen;
  g_calc.ndimtot = g_pot.opt_pot.len;

  // main force vector, all forces, energies, stresses, etc. will be stored here
  g_calc.force = (double*)Malloc(g_calc.mdim * sizeof(double));

  // starting positions for the force vector
  set_force_vector_pointers();

#if defined(APOT)
#if defined(MPI)
  MPI_Bcast(g_pot.opt_pot.table, g_calc.ndimtot, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif  // MPI
  update_calc_table(g_pot.opt_pot.table, g_pot.calc_pot.table, 1);
#endif  // APOT

  if (g_mpi.myid > 0) {
    start_mpi_worker(g_calc.force);
  } else {
#if defined(MPI)
    if (g_mpi.num_cpus > g_config.nconf) {
      warning("You are using more CPUs than you have configurations!\n");
      warning("While this will not do any harm, you are wasting %d CPUs.\n",
              g_mpi.num_cpus - g_config.nconf);
    }
#endif  // MPI

    time_t start_time;
    time_t end_time;

    time(&start_time);

    if (g_param.opt && g_calc.ndim > 0) {
      run_optimization();
    } else if (g_calc.ndim == 0) {
      printf(
          "\nOptimization disabled due to 0 free parameters. Calculating "
          "errors.\n");
    } else {
      printf("\nOptimization disabled. Calculating errors.\n\n");
    }

    time(&end_time);

#if defined(APOT)
    double tot = calc_forces(g_pot.opt_pot.table, g_calc.force, 0);
#else
    double tot = calc_forces(g_pot.calc_pot.table, g_calc.force, 0);
#endif  // APOT

#if defined(UQ)

    return uncertainty_quantification(tot,g_files.sloppyfile);

#endif //UQ

      
    write_pot_table_potfit(g_files.endpot);
    return 0;
    {
      int format = -1;

      switch (g_pot.format_type) {
        case POTENTIAL_FORMAT_UNKNOWN:
          error(1, "Unknown potential format detected! (%s:%d)\n", __FILE__,
                __LINE__);
        case POTENTIAL_FORMAT_ANALYTIC:
          format = 0;
          break;
        case POTENTIAL_FORMAT_TABULATED_EQ_DIST:
          format = 3;
          break;
        case POTENTIAL_FORMAT_TABULATED_NON_EQ_DIST:
          format = 4;
          break;
      }

      printf("\nPotential in format %d written to file \t%s\n", format,
             g_files.endpot);
    }

    if (g_param.writeimd == 1)
      write_pot_table_imd(g_files.imdpot);

    if (g_param.plot == 1)
      write_plotpot_pair(&g_pot.calc_pot, g_files.plotfile);

    if (g_param.write_lammps == 1)
      write_pot_table_lammps();

// will not work with MPI
#if defined(PDIST) && !defined(MPI)
    write_pairdist(&g_pot.opt_pot, g_files.distfile);
#endif  // PDIST && !MPI

    // write the error files for forces, energies, stresses, ...
    write_errors(g_calc.force, tot);

    /* calculate total runtime */
    if (g_param.opt && g_mpi.myid == 0 && g_calc.ndim > 0) {
      printf("\nRuntime: %d hours, %d minutes and %d seconds.\n",
             (int)difftime(end_time, start_time) / 3600,
             ((int)difftime(end_time, start_time) % 3600) / 60,
             (int)difftime(end_time, start_time) % 60);
      printf("%d force calculations, each took %f seconds\n", g_calc.fcalls,
             (double)difftime(end_time, start_time) / g_calc.fcalls);
    }

#if defined(MPI)
    calc_forces(NULL, NULL, 1); /* go wake up other threads */
#endif                          // MPI
  }                             /* myid == 0 */

// do some cleanups before exiting

#if defined(MPI)
  // kill MPI
  shutdown_mpi();
#endif  // MPI

  free_allocated_memory();

  return 0;
}
예제 #2
0
/**
 * The main entry function.
 *
 * Command line arguments have to be in order:
 * - command (cyboi)
 * - abstraction (compound|operation)
 * - location (inline|file|ftp|http)
 * - model (a compound model or primitive operation, for example: exit or model.submodel)
 *
 * Usage:
 * cyboi compound|operation inline|file|ftp|http model.submodel
 *
 * Example 1 (starts up and right away shuts down the system):
 * cyboi operation inline exit
 *
 * Example 2 (calls the startup routine of some application):
 * cyboi compound file /application/logic/startup.cybol
 *
 * The main function follows a system lifecycle to start up,
 * run and shut down the CYBOI system, in the following order:
 * 1 initialize global variables
 * 2 create statics (state/ logic knowledge container etc.)
 * 3 create startup signal and add to signal memory
 * 4 run dynamics (signal waiting loop)
 * 5 destroy startup signal
 * 6 destroy statics (state/ logic knowledge container etc.)
 *
 * @param p0 the argument count (argc)
 * @param p1 the argument vector (argv)
 * @return the return value
 */
int main(int p0, char** p1) {

    // Return 1 to indicate an error, by default.
    int r = 1;

    //
    // Global variables.
    //

    // Initialize global variables.
    // CAUTION!
    // They have to be initialized before the command line parameter check below!
    // Otherwise, the logger may not be able to log possible error messages.
    initialize_global_variables();

    //
    // Testing.
    //

    // Call testing procedures.
    // Comment/ uncomment this as needed.
    // CAUTION!
    // This has to stand AFTER the initialization of the
    // global variables because these are used by the testing code.
//    test();
//    return 0;

    if (p1 != NULL_POINTER) {

        if (p0 == STARTUP_PARAMETERS_COUNT) {

            //
            // Knowledge container.
            //

            // The knowledge container and its count and size.
            void* k = NULL_POINTER;
            int kc = 0;
            int ks = 0;

            // Create knowledge container.
            create_compound((void*) &k, (void*) &ks);

            //
            // Internals container.
            //

            // The internals container and its count and size.
            void* i = NULL_POINTER;
            int ic = 0;
            int is = 0;

            // Create internals container.
//??            create_internals((void*) &i, (void*) &ic, (void*) &is);

            //
            // Signal container.
            //

            // The signal container and its count and size.
            void* s = NULL_POINTER;
            int sc = 0;
            int ss = 0;

            // Create signal container.
            create_signal_memory((void*) &s, (void*) &ss);

            //
            // Startup model.
            //

            // Initialize persistent part abstraction, location, model
            // and their counts and sizes.
            void* ppa = (void*) p1[ABSTRACTION_STARTUP_PARAMETER_INDEX];
            int ppac = strlen(p1[ABSTRACTION_STARTUP_PARAMETER_INDEX]);
            int ppas = ppac;
            void* ppl = (void*) p1[LOCATION_STARTUP_PARAMETER_INDEX];
            int pplc = strlen(p1[LOCATION_STARTUP_PARAMETER_INDEX]);
            int ppls = pplc;
            void* ppm = (void*) p1[MODEL_STARTUP_PARAMETER_INDEX];
            int ppmc = strlen(p1[MODEL_STARTUP_PARAMETER_INDEX]);
            int ppms = ppmc;

            // Initialize transient part abstraction, model
            // and their counts and sizes.
            // CAUTION! A transient location is not stored,
            // since that is only needed temporarily
            // for model loading.
            void* tpa = NULL_POINTER;
            int tpac = 0;
            int tpas = 0;
            void* tpm = NULL_POINTER;
            int tpmc = 0;
            int tpms = 0;

            // Create transient part abstraction, model
            // and their counts and sizes.
            interpret_model((void*) &tpa, (void*) &tpac, (void*) &tpas,
                (void*) &ppa, (void*) &ppac,
                (void*) &STRING_ABSTRACTION, (void*) &STRING_ABSTRACTION_COUNT);
            interpret_located_model((void*) &tpm, (void*) &tpmc, (void*) &tpms,
                (void*) &ppa, (void*) &ppac,
                (void*) &ppl, (void*) &pplc,
                (void*) &ppm, (void*) &ppmc);

            //
            // Startup signal.
            //

            // Add startup signal to signal memory.
            set_signal((void*) &s, (void*) &sc, (void*) &ss,
                (void*) &tpm, (void*) &tpmc,
                (void*) &NORMAL_PRIORITY,
                (void*) &tpa, (void*) &tpac);

            //
            // Waiting loop.
            //

            // The system is now started up and complete so that a loop
            // can be entered, waiting for signals (events/ interrupts)
            // which are stored/ found in the signal memory.
            // The loop is left as soon as its shutdown flag is set.
            wait((void*) &s, (void*) &sc, (void*) &ss,
                (void*) &k, (void*) &kc, (void*) &ks,
                (void*) &i, (void*) &ic, (void*) &is);

            //
            // Destruction.
            //

            //?? Do not forget to destroy startup abstraction etc. HERE!

            // Destroy startup model.
            destroy_model((void*) &tpm, (void*) &tpmc, (void*) &tpms,
                (void*) &NULL_POINTER, (void*) &NULL_POINTER, (void*) &NULL_POINTER,
                (void*) &tpa, (void*) &tpac,
                (void*) &tpa, (void*) &tpac,
                (void*) &tpm, (void*) &tpmc,
                (void*) &NULL_POINTER, (void*) &NULL_POINTER,
                (void*) &NULL_POINTER, (void*) &NULL_POINTER,
                (void*) &NULL_POINTER, (void*) &NULL_POINTER);

            // Destroy signal container.
            destroy_signal_memory((void*) &s, (void*) &ss);

            // Destroy internals container.
//??            destroy_internals((void*) &i, (void*) &ic, (void*) &is);

            // Destroy knowledge container.
            destroy_compound((void*) &k, (void*) &ks);

            log_message((void*) &INFO_LOG_LEVEL, (void*) &EXIT_CYBOI_NORMALLY_MESSAGE, (void*) &EXIT_CYBOI_NORMALLY_MESSAGE_COUNT);

            // Return 0 to indicate proper shutdown.
            r = 0;

        } else {

            log_message((void*) &ERROR_LOG_LEVEL, (void*) &COULD_NOT_EXECUTE_CYBOI_THE_COMMAND_LINE_ARGUMENT_NUMBER_IS_INCORRECT_MESSAGE, (void*) &COULD_NOT_EXECUTE_CYBOI_THE_COMMAND_LINE_ARGUMENT_NUMBER_IS_INCORRECT_MESSAGE_COUNT);
            log_message((void*) &INFO_LOG_LEVEL, (void*) &USAGE_MESSAGE, (void*) &USAGE_MESSAGE_COUNT);
        }

    } else {

        log_message((void*) &ERROR_LOG_LEVEL, (void*) &COULD_NOT_EXECUTE_CYBOI_THE_COMMAND_LINE_ARGUMENT_VECTOR_IS_NULL_MESSAGE, (void*) &COULD_NOT_EXECUTE_CYBOI_THE_COMMAND_LINE_ARGUMENT_VECTOR_IS_NULL_MESSAGE_COUNT);
    }

    return r;
}
예제 #3
0
int main(){
	int maxfd = 0;
//	int udp_socketfd = tcpudpchannel_init();
//	maxfd = udp_socketfd;
// Maybe check if there is a master server here
	initialize_global_variables();
	mutex_init();
	int tcp_socketfd = client_init();
	maxfd = tcp_socketfd > maxfd ? tcp_socketfd : maxfd;
	int button_socketfd = button_init();
	maxfd = button_socketfd > maxfd ? button_socketfd : maxfd;
	int elevator_control_socketfd = elevator_control_init();
	maxfd = elevator_control_socketfd > maxfd ? elevator_control_socketfd : maxfd;
	printf("MAIN: elevator control initiated\n");
	int floor_control_socketfd = floor_control_init();
	maxfd = floor_control_socketfd > maxfd ? floor_control_socketfd : maxfd;
	printf("MAIN: floor control initiated\n");
	printf("MAIN: global variables initiated\n");

	fd_set socket_set;
	FD_ZERO(&socket_set);
	struct timeval timeout;
	char recv_buffer[1024];
	
	while(1){
//	  printf("MAIN: making socket set for tcp:%d\n", tcp_socketfd);
		timeout.tv_sec = 3*60;
		timeout.tv_usec = 0;
//        FD_SET(udp_socketfd ,&socket_set);
 	  FD_SET(tcp_socketfd ,&socket_set);
//		printf("MAIN: making socket set for button:%d\n", button_socketfd);
    FD_SET(button_socketfd ,&socket_set);
//		printf("MAIN: making socket set for elevator control:%d\n", elevator_control_socketfd);
    FD_SET(elevator_control_socketfd ,&socket_set);
//		printf("MAIN: making socket set for floor_control:%d\n", floor_control_socketfd);
	  FD_SET(floor_control_socketfd ,&socket_set);
//		printf("MAIN: waiting for select\n");
		select(maxfd + 1, &socket_set, NULL, NULL, &timeout);
		printf("MAIN: select initiated\n");
		for( int i = 0; i <= maxfd; i ++){
			if(FD_ISSET(i,&socket_set)){
//				if(i == udp_socketfd){
//					recv(i,recv_buffer,sizeof(recv_buffer),0);
//				}
				if(i == tcp_socketfd){
					recv(i,recv_buffer,sizeof(recv_buffer),0);
					printf("MAIN: recieved message from master: %s\n",recv_buffer);
					send(floor_control_socketfd,recv_buffer,sizeof(recv_buffer),0);
				}
				else if(i == button_socketfd){
     			   recv(i,recv_buffer,sizeof(recv_buffer),0);
					send(tcp_socketfd,recv_buffer,sizeof(recv_buffer),0);
//					printf("MAIN: sent message: %s\n",recv_buffer);
					if(recv_buffer[0] == 'c'){
						send(floor_control_socketfd,recv_buffer,sizeof(recv_buffer),0);
					}
				}
				else if(i == elevator_control_socketfd){
					recv(i,recv_buffer,sizeof(recv_buffer),0);
					printf("MAIN: recieved message from elevator_control: %s\n",recv_buffer);
					send(tcp_socketfd,recv_buffer,sizeof(recv_buffer),0);
					if(recv_buffer[0] == 'r'){  
						printf("MAIN: sent message to floor_control: %s\n",recv_buffer);
						send(floor_control_socketfd,recv_buffer,sizeof(recv_buffer),0); 
					}
				}
				else if(i == floor_control_socketfd){
					recv(i,recv_buffer,sizeof(recv_buffer),0);
					send(tcp_socketfd,recv_buffer,sizeof(recv_buffer),0);
					printf("MAIN: sent message to master: %s\n",recv_buffer);
          }
			}
		}
	}
    return 0;
}