示例#1
0
文件: test_embed.c 项目: rieck/sally
/* 
 * A simple test for the binary embedding
 */
int test_embed_bin()
{
    int i, j, n, err = 0;
    string_t strs[10];

    input_config("lines");
    char *test_file = getenv("TEST_FILE");
    n = input_open(test_file);
    input_read(strs, n);

    test_printf("Testing binary embedding");
    config_set_string(&cfg, "features.vect_embed", "bin");
    config_set_string(&cfg, "features.vect_norm", "none");

    for (i = 0, err = 0; i < n; i++) {
        fvec_t *fv = fvec_extract(strs[i].str, strs[i].len);
        double n = 0;
        for (j = 0; j < fv->len; j++)
            n += fv->val[j];
        err += fabs(n - fv->len) > 1e-6;
        fvec_destroy(fv);
    }
    test_return(err, n);

    input_free(strs, n);
    input_close();
    return err;
}
示例#2
0
文件: test_embed.c 项目: rieck/sally
/* 
 * A simple test for the binary embedding
 */
int test_embed_tfidf()
{
    int i, j, n, err = 0;
    string_t strs[10];

    config_set_string(&cfg, "features.vect_norm", "none");
    config_set_string(&cfg, "features.tfidf_file", TEST_TFIDF);

    unlink(TEST_TFIDF);
    char *test_file = getenv("TEST_FILE");
    idf_create(test_file);
    test_printf("Testing TFIDF embedding");

    input_config("lines");
    n = input_open(test_file);
    input_read(strs, n);

    /* Compute IDF manually */
    config_set_string(&cfg, "features.vect_embed", "bin");
    fvec_t *w = fvec_zero();
    for (i = 0, err = 0; i < n; i++) {
        fvec_t *fv = fvec_extract(strs[i].str, strs[i].len);
        fvec_add(w, fv);
        fvec_destroy(fv);
    }
    fvec_invert(w);
    fvec_mul(w, n);
    fvec_log2(w);

    if (!idf_check(w)) {
        err++;
        test_error("(%d) internal idf values seem to be wrong", i);
    }

    /* Invert w for multiplying out IDFs */
    fvec_invert(w);

    config_set_string(&cfg, "features.vect_embed", "tfidf");
    for (i = 0, err = 0; i < n; i++) {
        fvec_t *fv = fvec_extract(strs[i].str, strs[i].len);
        fvec_times(fv, w);

        /* Check if rest tf */
        double d = 0;
        for (j = 0; j < fv->len; j++)
            d += fv->val[j];
        err += fabs(d - 1.0) > 1e-6;
        fvec_destroy(fv);
    }
    test_return(err, n);

    fvec_destroy(w);
    input_free(strs, n);
    input_close();

    idf_destroy();
    unlink(TEST_TFIDF);

    return err;
}
示例#3
0
文件: test_embed.c 项目: rieck/sally
/* 
 * A simple test for the l2 norm
 */
int test_norm_l2()
{
    int i, j, n, err = 0;
    string_t strs[10];

    input_config("lines");
    char *test_file = getenv("TEST_FILE");
    n = input_open(test_file);
    input_read(strs, n);

    test_printf("Testing L2 normalization");
    config_set_string(&cfg, "features.vect_norm", "l2");
    for (i = 0, err = 0; i < n; i++) {
        fvec_t *fv = fvec_extract(strs[i].str, strs[i].len);
        double n = 0;
        for (j = 0; j < fv->len; j++)
            n += fv->val[j] * fv->val[j];
        err += fabs(sqrt(n) - 1.0) > 1e-6;
        fvec_destroy(fv);
    }
    test_return(err, n);

    input_free(strs, n);
    input_close();
    return err;
}
示例#4
0
文件: sally.c 项目: yangke/sally
/**
 * Exit Sally tool. Close open files and free memory.
 */
static void sally_exit()
{
    int ehash;
    const char *cfg_str, *hash_file;

    info_msg(1, "Flushing. Closing input and output.");
    input_close();
    output_close();

    config_lookup_string(&cfg, "features.vect_embed", &cfg_str);
    if (!strcasecmp(cfg_str, "tfidf"))
        idf_destroy(input);

    config_lookup_string(&cfg, "input.stopword_file", &cfg_str);
    if (strlen(cfg_str) > 0)
        stopwords_destroy();

    config_lookup_string(&cfg, "features.hash_file", &hash_file);
    if (strlen(hash_file) > 0) {
        info_msg(1, "Saving explicit hash table to '%s'.", hash_file);    
        gzFile z = gzopen(hash_file, "w9");
        if (!z)
            error("Could not open hash file '%s'", hash_file);
        fhash_write(z);
        gzclose(z);
    }
    
    config_lookup_int(&cfg, "features.explicit_hash", &ehash);
    if (ehash || strlen(hash_file) > 0)
        fhash_destroy();

    /* Destroy configuration */
    config_destroy(&cfg);

}
示例#5
0
void read_coordinates(char *argv[])
{
  static FILE *fp;
  static char Species[20];
  static double tmp0,tmp1;
  static int i,j;

  Gxyz = (double**)malloc(sizeof(double*)*(atomnum+1)); 
  for (i=0; i<=atomnum; i++){
    Gxyz[i] = (double*)malloc(sizeof(double)*60); 
  }

  if ((fp = fopen(argv[2],"r")) != NULL){
    printf("Read the coordinates file (%s)\n",argv[2]);

    /****************************************************
                         open the file
    ****************************************************/

    input_open(argv[2]);

    /****************************************************
                         read data
    ****************************************************/

    if (fp=input_find("<Atoms.SpeciesAndCoordinates") ) {
      for (i=1; i<=atomnum; i++){
        fscanf(fp,"%i %s %lf %lf %lf %lf %lf",&j,&Species,
	       &Gxyz[i][1],&Gxyz[i][2],&Gxyz[i][3],&tmp0,&tmp1);

        if (i!=j){
          printf("Format error of the sequential number %i in <Atoms.SpeciesAndCoordinates\n",j);
          exit(0);
        }
      }

      if (!input_last("Atoms.SpeciesAndCoordinates>")) {
        /* format error */
        printf("Format error for Atoms.SpeciesAndCoordinates\n");
        exit(0);
      }
    }

    /****************************************************
                       input_close
    ****************************************************/

    input_close();
  }
  else {
    printf("Failure of reading the coordinates file (%s).\n",argv[2]);
  }

  for (i=1; i<=atomnum; i++){
    printf("i=%i  coodinates=%15.12f %15.12f %15.12f\n",
	   i,Gxyz[i][1],Gxyz[i][2],Gxyz[i][3]);
  }

}
示例#6
0
void close(){
	net_close();
	input_close();
	physics_close();
	console_close();
	test_logic_close();
	// the functions are smart enough to not close if they never initialized
}
示例#7
0
void post_main(void)
{
	Replay::close();
	game.close();
	Carets::close();
	
	Graphics::close();
	input_close();
	font_close();
	sound_close();
	tsc_close();
	textbox.Deinit();
}
示例#8
0
/*
 * Read a .def file and import oid to string mapping.
 * Mappings are inserted into a global structure containing list for each OID syntax type.
 */
int
snmp_import_file(struct snmp_toolinfo *tool, struct fname *file)
{
	int idx;

	snmp_import_init(&(file->cut));
	input_fopen(file->name);
	if ((idx = snmp_import(tool)) < 0)
	    warnx("Failed to read mappings from file %s", file->name);

	input_close();

	return (idx);
}
示例#9
0
文件: bossao.c 项目: tedkulp/bossogg
void bossao_stop (void)
{
   /*
   if (first == 0) {
      bossao_pause ();
   } else
   first = 0;
   */
   bossao_pause ();
   LOG ("about to clear");
   //thbuf_clear (thbuf);
   thbuf_static_clear (&thbuf);
   LOG ("cleared");
   input_close ();
   LOG ("closed");
}
示例#10
0
文件: nal.c 项目: walafc0/soclib
int32_t _test_nal(int32_t argc, int8_t *argv[])
{
  nal_unit unit;
  int32_t count;

  if (!input_open("../streams/in.264"))
    return 1;

  for (count = 1; get_next_nal_unit(&unit); ++count) {
    printf("%d: count=%d zero=%d ref_idc=%d type: %s\n", count,
	   unit.NumBytesInNALunit, unit.forbidden_zero_bit,
	   unit.nal_ref_idc, _str_nal_unit_type(unit.nal_unit_type));
  }

  input_close();
  return 0;
}
示例#11
0
static int
tgetc(void)
{
	int c;

	if (pbchar != -1) {
		c = pbchar;
		pbchar = -1;
		return (c);
	}

	for (;;) {
		if (input == NULL)
			return (EOF);

		if ((c = getc(input->fp)) != EOF)
			return (c);

		input_close();
	}
}
示例#12
0
void Show_DFT_DATA(char *argv[]) 
{
  FILE *fp,*fp0,*fp1,*fp2,*fp3;
  int Num_DatFiles,i,j,k,fp_OK;
  int Num_Atoms;
  int NGrid1_1,NGrid1_2,NGrid1_3;
  int NGrid2_1,NGrid2_2,NGrid2_3;
  double Utot1,Utot2,dU,dF;
  double gx,gy,gz,fx,fy,fz;
  double sum1,sum2;
  double time1,TotalTime;
  char SN[30][YOUSO10];
  char SB[30][YOUSO10];
  char SV[30][YOUSO10];
  char fname[YOUSO10];
  char fname0[YOUSO10];
  char fname1[YOUSO10];
  char fname2[YOUSO10];
  char fname_dat[YOUSO10];
  char fname_dat2[YOUSO10];
  char fname_out1[YOUSO10];
  char fname_out2[YOUSO10];
  char ftmp[YOUSO10];
  fname_type *fndat;
  int numprocs,myid;

  char *dir;
  char input_dir[300];
  char *output_file;
  DIR *dp;
  struct dirent *entry;

  MPI_Request request;
  MPI_Status  status;

  /* set up MPI */

  MPI_Comm_size(mpi_comm_level1,&numprocs);
  MPI_Comm_rank(mpi_comm_level1,&myid);

  sprintf(input_dir,"%s",argv[2]);

  printf("%s\n",input_dir);
  
  /* print the header */

  if (myid==Host_ID){

    /* set dir */

    dir = input_dir;

    /* count the number of dat files */

    if(( dp = opendir(dir) ) == NULL ){
      printf("could not find the directry '%s'\n",input_dir);
      MPI_Finalize();
      exit(0);
    }

    Num_DatFiles = 0;
    while((entry = readdir(dp)) != NULL){

      if ( strstr(entry->d_name,".dat")!=NULL ){ 
          
        Num_DatFiles++;
      }
    }
    closedir(dp);

    fndat = (fname_type*)malloc(sizeof(fname_type)*Num_DatFiles);

    /* store the name of dat files */

    if(( dp = opendir(dir) ) == NULL ){
      printf("could not find the directry '%s'\n",input_dir);
      MPI_Finalize();
      exit(0);
    }

    Num_DatFiles = 0;
    while((entry = readdir(dp)) != NULL){
 
      if ( strstr(entry->d_name,".dat")!=NULL ){ 

        sprintf(fndat[Num_DatFiles].fn,"%s/%s",input_dir,entry->d_name);  
        Num_DatFiles++;
      }
    }
    closedir(dp);

  } /* if (myid==Host_ID) */

  if (myid==Host_ID){
    printf(" %2d dat files are found in the directory '%s'.\n\n\n",Num_DatFiles,input_dir);
  }

  /***********************************************************
         start calculations
  ***********************************************************/

  for (i=0; i<Num_DatFiles; i++){

    sprintf(fname_dat,"%s",fndat[i].fn);

    input_open(fname_dat);
    input_int("Species.Number",&SpeciesNum,0);

    /* read Definition.of.Atomic.Species */

    if (fp=input_find("<Definition.of.Atomic.Species")) {

      for (k=0; k<SpeciesNum; k++){
        fscanf(fp,"%s %s %s",SN[k],SB[k],SV[k]);

        printf("i=%2d k=%2d %s %s\n",i,k,SB[k],SV[k]);

	/*
        SpeciesString2int(i);
	*/
      }

      if (! input_last("Definition.of.Atomic.Species>")) {
	MPI_Finalize();
	exit(0);
      }
    }

    input_close();
  }

  if (myid==Host_ID){
    free(fndat);
  }

  MPI_Barrier(mpi_comm_level1);
  MPI_Finalize();
  exit(0);
}
示例#13
0
int main(int argc, char *argv[])
{
bool inhibit_loadfade = false;
bool error = false;
bool freshstart;
	
	SetLogFilename("debug.txt");
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
	{
		staterr("ack, sdl_init failed: %s.", SDL_GetError());
		return 1;
	}
	atexit(SDL_Quit);
	
	// start up inputs first thing because settings_load may remap them
	input_init();
	
	// load settings, or at least get the defaults,
	// so we know the initial screen resolution.
	settings_load();
	
	if (Graphics::init(settings->resolution)) { staterr("Failed to initialize graphics."); return 1; }
	if (font_init()) { staterr("Failed to load font."); return 1; }
	
	//speed_test();
	//return 1;
	
	#ifdef CONFIG_DATA_EXTRACTOR
	if (!settings->files_extracted)
	{
		if (extract_main())
		{
			Graphics::close();
			font_close();
			return 0;
		}
		else
		{
			settings->files_extracted = true;
			settings_save();
		}
	}
	#endif
	
	if (check_data_exists())
	{
		return 1;
	}
	
	Graphics::ShowLoadingScreen();
	if (sound_init()) { fatal("Failed to initialize sound."); return 1; }
	if (trig_init()) { fatal("Failed trig module init."); return 1; }
	
	if (tsc_init()) { fatal("Failed to initialize script engine."); return 1; }
	if (textbox.Init()) { fatal("Failed to initialize textboxes."); return 1; }
	if (Carets::init()) { fatal("Failed to initialize carets."); return 1; }
	
	if (game.init()) return 1;
	game.setmode(GM_NORMAL);
	// set null stage just to have something to do while we go to intro
	game.switchstage.mapno = 0;
	
	//#define REPLAY
	#ifdef REPLAY
		game.switchstage.mapno = START_REPLAY;
		//Replay::set_ffwd(6000);
		//Replay::set_stopat(3500);
		game.switchstage.param = 1;
	#else
		//game.switchstage.mapno = LOAD_GAME;
		//game.pause(GP_OPTIONS);
		
		if (settings->skip_intro && file_exists(GetProfileName(settings->last_save_slot)))
			game.switchstage.mapno = LOAD_GAME;
		else
			game.setmode(GM_INTRO);
	#endif
	
	// for debug
	if (game.paused) { game.switchstage.mapno = 0; game.switchstage.eventonentry = 0; }
	if (game.switchstage.mapno == LOAD_GAME) inhibit_loadfade = true;
	
	game.running = true;
	freshstart = true;
	
	stat("Entering main loop...");
	#ifdef __SDLSHIM__
		set_console_visible(false);
	#endif
	
	//speed_test();
	//return 1;
	
	while(game.running)
	{
		// SSS/SPS persists across stage transitions until explicitly
		// stopped, or you die & reload. It seems a bit risky to me,
		// but that's the spec.
		if (game.switchstage.mapno >= MAPNO_SPECIALS)
		{
			StopLoopSounds();
		}
		
		// enter next stage, whatever it may be
		if (game.switchstage.mapno == LOAD_GAME || \
			game.switchstage.mapno == LOAD_GAME_FROM_MENU)
		{
			if (game.switchstage.mapno == LOAD_GAME_FROM_MENU)
				freshstart = true;
			
			stat("= Loading game =");
			if (game_load(settings->last_save_slot))
			{
				fatal("savefile error");
				goto ingame_error;
			}
			
			Replay::OnGameStarting();
			
			if (!inhibit_loadfade) fade.Start(FADE_IN, FADE_CENTER);
			else inhibit_loadfade = false;
		}
		else if (game.switchstage.mapno == START_REPLAY)
		{
			stat(">> beginning replay '%s'", GetReplayName(game.switchstage.param));
			
			StopScripts();
			if (Replay::begin_playback(GetReplayName(game.switchstage.param)))
			{
				fatal("error starting playback");
				goto ingame_error;
			}
		}
		else
		{
			if (game.switchstage.mapno == NEW_GAME || \
				game.switchstage.mapno == NEW_GAME_FROM_MENU)
			{
				bool show_intro = (game.switchstage.mapno == NEW_GAME_FROM_MENU);
				InitNewGame(show_intro);
			}
			
			// slide weapon bar on first intro to Start Point
			if (game.switchstage.mapno == STAGE_START_POINT && \
				game.switchstage.eventonentry == 91)
			{
				freshstart = true;
			}
			
			// switch maps
			if (load_stage(game.switchstage.mapno)) goto ingame_error;
			
			player->x = (game.switchstage.playerx * TILE_W) << CSF;
			player->y = (game.switchstage.playery * TILE_H) << CSF;
		}
		
		// start the level
		if (game.initlevel()) return 1;
		
		if (freshstart)
			weapon_introslide();
		
		gameloop();
		game.stageboss.OnMapExit();
		freshstart = false;
	}
	
shutdown: ;
	Replay::close();
	game.close();
	Carets::close();
	
	Graphics::close();
	input_close();
	font_close();
	sound_close();
	tsc_close();
	textbox.Deinit();
	return error;
	
ingame_error: ;
	stat("");
	stat(" ************************************************");
	stat(" * An in-game error occurred. Game shutting down.");
	stat(" ************************************************");
	error = true;
	goto shutdown;
}
示例#14
0
文件: esp.c 项目: certik/openmx
void read_input(char *file)
{
  static int i,j,spe,po;
  static int coordinates_unit;
  static int ct_AN;
  double r_vec[20],r_vec2[20];
  int i_vec[20],i_vec2[20];
  char *s_vec[20],Species[20];
  char buf[MAXBUF];
  FILE *fp;


  if (input_open(file)){

    /****************************************************
                 find dipole moment
    ****************************************************/

    if (fp=input_find("Dipole") ) {
      fscanf(fp,"%s",buf);
      fscanf(fp,"%s",buf);
      fscanf(fp,"%s %lf %lf %lf",buf,&Ref_DipMx,&Ref_DipMy,&Ref_DipMz);
    }

    input_close();
  }
  else {
    exit(0);
  }

  /****************************************************
              Definition of Atomic Species
  ****************************************************/

  /*
  input_int("Species.Number",&SpeciesNum,0);
  if (SpeciesNum<=0){
    printf("Species.Number may be wrong.\n");
    po++;
  }

  SpeName = (char**)malloc(sizeof(char*)*SpeciesNum);
  for (spe=0; spe<SpeciesNum; spe++){
    SpeName[spe] = (char*)malloc(sizeof(char)*YOUSO10);
  }  

  SpeBasis = (char**)malloc(sizeof(char*)*SpeciesNum);
  for (spe=0; spe<SpeciesNum; spe++){
    SpeBasis[spe] = (char*)malloc(sizeof(char)*YOUSO10);
  }  

  SpeVPS = (char**)malloc(sizeof(char*)*SpeciesNum);
  for (spe=0; spe<SpeciesNum; spe++){
    SpeVPS[spe] = (char*)malloc(sizeof(char)*YOUSO10);
  }  

  if (fp=input_find("<Definition.of.Atomic.Species")) {
    for (i=0; i<SpeciesNum; i++){
      fscanf(fp,"%s %s %s",SpeName[i],SpeBasis[i],SpeVPS[i]);
    }
    if (! input_last("Definition.of.Atomic.Species>")) {

      po++;

      printf("Format error for Definition.of.Atomic.Species\n");
      exit(0);
    }
  }
  */

  /****************************************************
                        atoms
  ****************************************************/

  /*
  input_int("Atoms.Number",&atomnum,0);
  s_vec[0]="Ang";  s_vec[1]="AU";
  i_vec[0]= 0;     i_vec[1]= 1;
  input_string2int("Atoms.SpeciesAndCoordinates.Unit",
                   &coordinates_unit,2,s_vec,i_vec);

  if (fp=input_find("<Atoms.SpeciesAndCoordinates") ) {

    for (i=1; i<=atomnum; i++){
      fgets(buf,MAXBUF,fp);

      sscanf(buf,"%i %s %lf %lf %lf",
             &j, &Species, &Gxyz[i][1],&Gxyz[i][2],&Gxyz[i][3]);

      WhatSpecies[i] = Species2int(Species);

      printf("i=%3d spe=%2d %15.12f %15.12f %15.12f\n",
              i,WhatSpecies[i],Gxyz[i][1],Gxyz[i][2],Gxyz[i][3]);

      if (i!=j){
	printf("Format error of the sequential number %i in <Atoms.SpeciesAndCoordinates\n",j);
	po++;
      }
    }

    ungetc('\n',fp);
    if (!input_last("Atoms.SpeciesAndCoordinates>")) {

      printf("Format error for Atoms.SpeciesAndCoordinates\n");
      po++;
    }

    if (coordinates_unit==0){
      for (i=1; i<=atomnum; i++){
	Gxyz[i][1] = Gxyz[i][1]/BohrR;
	Gxyz[i][2] = Gxyz[i][2]/BohrR;
	Gxyz[i][3] = Gxyz[i][3]/BohrR;
      }
    }
  }
  */


}
示例#15
0
int main(int argc, char** argv) 
{
	char *record;
	char *block;									//endiamesos xwros mnhmhs ston opoio ekxwroun ta block oi reducers//
	int i,j,fd;
	char buffer[56];								//pinakas ston opoion sxhmatizoume to katallhlo format gia ta pipenames//
	pid_t pid;
	int status;
	fd_set read_set, active_set;							//ta set me tous file descriptors pou xrhsimopoiei h select//
	BUFFERS *buffers;								//deikths sthn arxh twn buffers//
	
	struct sigaction sig_act;

	if (argc!=3)									//onoma ektelesimou, #Mappers, #Reducers//
	{
		printf("Usage:%s  #Mappers  #Reducers\n",argv[0]);
		return -1;
	}
	
	sig_act.sa_handler=handler1;     		    				//Signal Handler1 gia ton patera//
	sig_act.sa_flags=0;
											//prosthetw ta simata sto set tou patera.
	if ((sigemptyset(&sig_act.sa_mask ) == -1) || (sigaction(SIGINT,&sig_act,NULL) == -1 ) 
	|| (sigaction(SIGQUIT,&sig_act,NULL) == -1 ) || (sigaction(SIGTERM,&sig_act,NULL) == -1 ))
	{
	      perror("FAIL TO INSTAL  SIGNAL HANDLER FOR SIGUSR1");
	      return -1;
	}
	Mappers=atoi(argv[1]);
	Reducers=atoi(argv[2]);
	for(i=0;i<Mappers;i++)								//dhmiourgia twn pipes prin dhmiourgh8oun ta paidia//
	{
		for (j=0;j<Reducers;j++)
		{
			buffer[0]='\0';
			sprintf(buffer,".pipe.%d.%d",i+1,j+1);				
			if ( mkfifo(buffer,0666 )==-1)	
       			{
                        	perror("mkfifo");
				removepipes(Mappers,Reducers);
                        	return -1;
                	}
		}
	}
	mkdir("./output_folder",0700);					//dhmiourgoume ton output folder//
	for(i=0;i<Mappers+Reducers;i++)							//kanena paidi den exei dhmiourgh8ei akoma//							
		pids[i]=0;
	for(i=0;i<Mappers+Reducers;i++)
	{
 		pids[i]=pid=fork();
		if (pid<0)								//periptwsh apotyxias fork()//
        	{									//epeidh den 3eroume posa paidia exoun gennh8ei epityxws,ara posa prepei na//
        		perror("fork failed\n");					//skotwsoume,o pateras stelnei ena SIGINT ston eauto tou kai o handler tou patera//
               		kill(getpid(),SIGINT);						//eidopoiei ta gennhmena paidia na termatisoun//
			removepipes(Mappers,Reducers);
                        return -1;
        	}
		else if ( pid==0 )							//to paidi vgainei apo to loop ka8ws mono h Initial prepei na kanei fork()//
			break;
	}
	if (pid==0)									//an eimaste sto paidi//
	{
		sigset_t ign_signals;
		
		//prosthetw sta 3 simata sto set twn paidiwn kai meta ta mplokarw//
		if ( (sigemptyset(&ign_signals ) == -1) || (sigaddset(&ign_signals,SIGINT) == -1 ) || (sigaddset(&ign_signals,SIGQUIT) == -1 ) 
				|| (sigaddset(&ign_signals,SIGTERM) == -1 ))
		{
	      		kill(getppid(),SIGINT);
		}
		else
			sigprocmask(SIG_BLOCK,&ign_signals,NULL);

 		
		sig_act.sa_handler=handler2;     		    			 //Signal Handler2-o handler twn paidiwn//
		sig_act.sa_flags=0;
		//orizw ton handler na antistoixei sto SIGUSR1, pou stelnei o parent//
		if ((sigemptyset(&sig_act.sa_mask ) == -1) || (sigaction(SIGUSR1,&sig_act,NULL) == -1 ))
	      		kill(getppid(),SIGINT);
		for(i=0;i<Mappers+Reducers;i++)						//anazhtw ston pinaka me ta pids to 10 mhdeniko,pou ypodeiknyei th seira gennhshs//
			if (pids[i]==0)
				break;
		i++;									//auti einai i thesi mou, gia na 3exwrizw an eimai maper i reducer//
		if (i<=Mappers)								//kwdikas tou mapper//
		{
			MRIFILE* fp;
			buffers=calloc(Reducers,sizeof(struct BUFFERS));		//desmeuoume mnhmh gia tosous buffers osoi einai kai oi reducers//	
			for (j=0;j<Reducers && !error ;j++)
			{
				buffer[0]='\0';
				sprintf(buffer,".pipe.%d.%d",i,j+1);			//dinoume onoma sta pipe//
				if ( (fd=open(buffer,O_WRONLY))<0 ) 			//anoigei to pipe gia grapsimo//
       		                {
					if ( errno != EINTR )				//an yparxei la8os k den einai apo shma//
        	                        	kill(getppid(),SIGINT);			//steile to shma //
	       	                	perror("Mapper::open");				//alliws ginetai allo la8os sthn open//
					free(buffers);					//kai apodesmevoume tous porous tou mapper//
					return -1;
        	                }
				initialize(buffers, j, fd);				//arxikopoioume tous buffers//
			}
			DIR             *dip;
			struct dirent   *dit;
			if ( (dip = opendir("input_folder")) == NULL)			//anoigoume ton inputfolder//
       			{
				if ( errno != EINTR )                           	//an yparxei la8os k den einai apo shma//
                                	kill(getppid(),SIGINT);                 	//steile to shma //
                                perror("Mapper::opendir");                         	//alliws ginetai allo la8os sthn open//
        		}
			while ((dit = readdir(dip)) != NULL  &&  !error)		//oso exoume files gia diavasma//
        		{
                		if ( strncmp(dit->d_name,"file_",5) == 0)		//an oi prwtoi 5 xarakthres twn 2 autwn string einai idioi,dhladh an einai file_//
				{
					int filenumber;					//ari8mos arxeiou input//
					
					sscanf(dit->d_name,"file_%d",&filenumber);	//kanw retrieve ton ari8mo tou trexodos arxeiou//
					if ( filenumber%Mappers+1==i)			//apofasizoume me katakermatismo poios mapper 8a diavazei apo ka8e file//
					{	
						char filename[50];
						sprintf(filename,"./input_folder/%s",dit->d_name);
						fp=input_open(filename);			//anoigoume to file//
						while ( (record=input_next(fp))!= NULL )	//kai oso uparxoun eggrafes gia diavasma//
						{						//kaleitai h addrecord gia pros8hkh eggrafwn stous buffers//
							addrecord(buffers,record,partition(map(record).key,Reducers));	
						}
						input_close(fp);			//kleinoume to file//
					}
				}
			}
			closedir(dip);							//kleinoume kai ton katalogo//
			for (j=0; j<Reducers && error==0; j++)				
			{
				if ( buffers[j].curbyte != 4 ){
					if ( my_write(buffers[j].pipedesc,buffers[j].block)!= BLKSIZE )	//grafoume sto pipe oses eggrafes exoun 3emeinei//
        	                       		printf("error here\n");
				}
			}
			for (j=0;j<Reducers;j++)
                       	{
				close(buffers[j].pipedesc);				//kleinoume ta pipe afou exoume diavasei//
			}
			free(buffers);							//afou teleiwnoume me to diavasma apodesmevoume taous buffers//
		}
		else									//kwdikas tou reducer//
		{
			char **keys;
			char **values;
			char *tempkey;							//deikths sthn prwth 8esh tou pinaka twn keys//
			char **tempval;							//deikths sthn prwth 8esh tou pinaka twn values//
			int counter;
			int total_records=0;
			int memsize=100;
			int whoami;							//8esh mias diergasias ston pinaka twn paidiwn//
			whoami=i;
			char buffer[40];
			MROFILE *fp;

			sprintf(buffer,"./output_folder/file_%d",whoami-Mappers);	//ka8orismos twn files sto output folder gia ton ka8e reducer
                        fp=output_open(buffer);

			if (  (keys=calloc(memsize,sizeof(char*))) == NULL		//an exoume provlhma me th desmeush tou pinaka gia ta keys,h' ta values// 
			|| (values=calloc(memsize,sizeof(char*)))==NULL 		//h' to block ,tote//
			|| (block=calloc(BLKSIZE,sizeof(char)))==NULL )
			{
			 	kill(getppid(),SIGINT);					//stelnoume shma ston patera//
			}
			FD_ZERO(&read_set);						//mhdenizoume to set twn file descriptors twn pipe//
			for (j=0;j<Mappers;j++)
			{
				buffer[0]='\0';
				sprintf(buffer,".pipe.%d.%d",j+1,i-Mappers);	
				if ( (fd=open(buffer,O_RDONLY))<0 ) 			//anoigoume to pipe gia diavasma//
       		                {
					if ( errno != EINTR )				//an yparxei la8os kai den einai apo shma//
						kill(getppid(),SIGINT);			//tote stelnoume to shma//
        	                	perror("Reducer::open");
					break;
        	                }
				FD_SET(fd,&read_set);					//pros8etoume ton fd sto readset//
			}
			active_set=read_set;
			int closed=0;							//arxikopoioume to plh8os twn diavasmenwn fds//
			while ( error==0 && closed != Mappers)				//oso uparxoun fds sto active set kai den lavei shma//
			{
				read_set=active_set;
				int chosen;
				if ( (chosen=select(FD_SETSIZE,&read_set,NULL,NULL,NULL)) < 0) 		
				{
					if ( errno != EINTR ){
                                                kill(getppid(),SIGINT);
						error=1;
					}
					perror("select");				//alliws yparxei provlhma sth select//
					break;
				}
				else							//h select epistrefei ton katallhlo fd//
				{
					for (i = 0; i < FD_SETSIZE; i++)		//gia olous tous fds pou yparxoun mesa sto set//
					{
             					if (FD_ISSET (i, &read_set))		//an o i fd einai melos tou read set otan epistrepsei h select//
						{
							if ( (chosen=my_read(i,block))<0 )		//diavase apo ton i fd//
							{
								if ( errno != EINTR )
								{
                                         			       	kill(getppid(),SIGINT);
                                                			error=1;
                                        			}
								perror("read");
								break;
							}
							else if ( chosen==0 )			
							{
								FD_CLR (i, &active_set); 	//afairei ton i fd apo to active set//
								closed++;			//au3anetai kata ena o ari8mos twn fds pou diavasthkan//
								close(i);			//kleinei o i fd//
								if ( closed == Mappers )
									break;
							}
							else
								retrieve_records(block,&keys,&values,&total_records,&memsize);
						}
					}
				}
			}
			printf("%s\n",keys[10000]);
			printf("%d\n",total_records);
			if ( error==0 )								//an den exoume lavei shma//
			{
				sort(keys, values, total_records,compare);						
        	                tempkey=keys[0];						//o deikths tempkey deixnei sthn 1h 8esh tou pinaka twn keys//
                	        tempval=values;
                        	counter=1;
                	        for ( i=1; i<total_records; i++)
                        	{
                               		if ( compare(tempkey,keys[i]) == 0 )			//an ta kleidia se dyo diadoxikes 8eseis tou pinaka einai idia//
                                	        counter++;					//au3anetai kata 1 to plh8os twn eggrafwn me to idio kleidi//	
                       		        else							//an ta kleidia se dyo diadoxikes 8eseis tou pinaka einai diaforetika//
                                	{
						output_next(fp,reduce(tempkey,tempval,counter));//kaleitai h reduce kai grafoume to apotelesma sto output file.//
                         	                counter=1;					//epilegoume neo kleidi//
                                	        tempkey=keys[i];				//h kainouria 8esh tou tempkey//
                                        	tempval=values+i;				//h kainouria 8esh tou tempval//
                                	}
        	                }
			}
			output_close(fp);							//kleinoume to output file tou kathe reducer//
			if ( block != NULL )							//apodesmeush twn porwn tou reducer//
				free(block);
			if ( keys != NULL && values!= NULL )					//an den exei ginei la8os kata th desmeush//
			{									//apodesmeuoume olh th dunamika desmeumenh mnhmh//
				for(i=0;i<memsize;i++)						
				{
					if ( keys[i]!=NULL && values[i]!=NULL )
					{
						free(keys[i]);
						free(values[i]);
					}
				}
				free(keys);
				free(values);
			}
			for (i = 0; i < FD_SETSIZE; i++)					//kleinoume olous tous file descriptors pou vriskodai sto active set//
                              if (FD_ISSET (i, &active_set))   
					close(i);
		}	
	}
	else											//alliws an eisai ston patera,Initial process//
	{
		while ( (pid=r_wait(NULL))>0 )							//perimenei ta paidia tou na teleiwsoun// 
		{	
			fprintf(stderr,"Child %d finished\n",pid);
		}	
		removepipes(Mappers,Reducers);							//svhsimo twn pipes//
	}
	return 0;
	
}
示例#16
0
int 
main(int argc, char** argv) {


  int c, i, rd, j;
  long bytes_read = 0;
  int samples_per_tick;
  div_t rest;
  rdfun input_read;
  char* input = "alsa:default";

  while ( ( c = getopt(argc, argv, "vphN:o:g:k:r:c:lS:i:t:s") ) != -1) 
	switch (c) {
	case 'v': debug++; break;
	case 'p': print_freqs = 1; break;
	case 'h': use_harm_comp = 1; break;
	case 'N': N = atoi(optarg); break;
	case 'g': gain = atof(optarg); break;
	case 'o': 
	  if (strcmp(optarg, "seq") == 0 ) use_sequencer = 1;
	  else midi_filename =strdup(optarg); 
	  break;
	case 'k': hysteresis = atof(optarg); break;
	case 'r': ringsize = atoi(optarg); break;
	case 'c': midi_channel = atoi(optarg); break;
	case 'i': input = strdup(optarg); break;
	case 'S': SR = atoi(optarg); break;
	case 's': print_statistics = 1; break;
	case 't': threshold = atof(optarg); break;
	default: usage();
	}

  if ( debug > 0 )
	fprintf(stderr, 
			"ringbuffersize: %d ringbuffermask:0x%08x\n"
			"gain: %f\n",
			RINGBUFFERSIZE, RINGBUFFERMASK, gain);



  print_prologoue(N, SR);

  input_read = input_open(input);

  // allocate buffers

  buffer_f = (real_t**)malloc ( RINGBUFFERSIZE * sizeof(real_t*) );
  for ( j = 0; j < RINGBUFFERSIZE; j++ ) {
	buffer_f[j] = malloc ( N * sizeof(real_t) );
	for ( i = 0; i < N ; i ++ )
	  buffer_f[j][i] = 0.0;
  }


  // prepare midi file
  if (midi_filename != NULL ) {
	midi_file = midi_write_open(midi_filename);
	midi_write_header(midi_timeDivision, 1, midi_file);
	midi_write_track_header(midi_file);
  }

  if ( use_sequencer )
	midi_connect_sequencer();


  // prebuffer everthing
  precalculate();

  samples_per_tick = samples_per_midi_tick(SR, midi_bpm, midi_timeDivision);
  rest.rem = 0;

  // process data
  while ( (rd = input_read(buffer_f[current_buffer], N)) ) { 

	bytes_read += rd;

	rest = div(rest.rem + N, samples_per_tick);

	if (midi_file != NULL )
	  midi_write_increase_difftime(rest.quot, midi_file);

	absolute_time += N;

 	scan_freqs(); 

	// advance buffer:
	current_buffer = (current_buffer+1) & RINGBUFFERMASK;
  }



  for ( j = lo_note; j < hi_note; j++ ) {
	if ( act_freq[j] )
	  note_off(j, 0);
  }

  // free stuff
  for ( j = 0; j < RINGBUFFERSIZE; j++ ) free(buffer_f[j]);
  free(buffer_f);
  for (i = 0; i< NTONES; i++ ) free(cos_precalc[i]);

  // fixup midi
  if (midi_file != NULL ) {
	midi_write_track_end(midi_file);
	midi_write_close(midi_file);
  }


  if ( print_statistics ) {

	// octave style output of powers over frequency
	// first row: frequencies
	// second row: power for that freq
	fprintf(stderr, "freqs = [");
	for (j=lo_note;j<hi_note-1;j++) fprintf(stderr, "%.2f,", midi_note_to_hertz(j));
	fprintf(stderr, "%.2f", midi_note_to_hertz(j));
	fprintf(stderr, ";");
	for (j=lo_note;j<hi_note-1;j++) fprintf(stderr, "%.2f,", max_powers[j]);
	fprintf(stderr, "%.2f", max_powers[j]);
	fprintf(stderr, "];\n");
	
	fprintf(stderr, 
			"\n\n note_ons:%ld bytes_read:%ld playtime:%ld:%ld\n", 
			stats_note_ons, bytes_read, bytes_read/(SR*60), (bytes_read / SR)%60 );
  }


  input_close();

  print_epilogue();

  return 0;
}
示例#17
0
文件: Runtest.c 项目: rigarash/openmx
void Runtest(char *mode, int argc, char *argv[]) 
{
  FILE *fp,*fp0,*fp1,*fp2,*fp3;
  int Num_DatFiles,i,j,k,fp_OK;
  int Num_Atoms;
  int NGrid1_1,NGrid1_2,NGrid1_3;
  int NGrid2_1,NGrid2_2,NGrid2_3;
  double Utot1,Utot2,dU,dF;
  double Spread1,Spread2;
  double Omega1,Omega2;
  double gx,gy,gz,fx,fy,fz;
  double sum1,sum2;
  double time1,TotalTime;
  char fname[YOUSO10];
  char fname0[YOUSO10];
  char fname1[YOUSO10];
  char fname2[YOUSO10];
  char fname_dat[YOUSO10];
  char fname_dat2[YOUSO10];
  char fname_out1[YOUSO10];
  char fname_out2[YOUSO10];
  char ftmp[YOUSO10];
  fname_type *fndat;
  char operate[800];
  int numprocs,myid;

  char *dir;
  char *input_dir;
  char *output_file;
  DIR *dp;
  struct dirent *entry;

  MPI_Request request;
  MPI_Status  status;

  /* set up MPI */

  MPI_Comm_size(MPI_COMM_WORLD1, &numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD1, &myid);

  if (strcasecmp(mode,"S")==0){  
    input_dir = "input_example";
    output_file = "runtest.result";
  }
  else if (strcasecmp(mode,"L")==0){  
    input_dir = "large_example";
    output_file = "runtestL.result";
  }
  else if (strcasecmp(mode,"L2")==0){  
    input_dir = "large2_example";
    output_file = "runtestL2.result";
  }
  else if (strcasecmp(mode,"G")==0){  
    input_dir = "geoopt_example";
    output_file = "runtestG.result";
  }
  else if (strcasecmp(mode,"WF")==0){  
    input_dir = "wf_example";
    output_file = "runtestWF.result";
  }
  else if (strcasecmp(mode,"NEGF")==0){  
    input_dir = "negf_example";
    output_file = "runtestNEGF.result";
  }

  /* set Runtest_flag */

  Runtest_flag = 1;

  /* initialize TotalTime */

  TotalTime = 0.0;

  /* print the header */

  if (myid==Host_ID){

    printf("\n*******************************************************\n");  fflush(stdout);
    printf("*******************************************************\n");    fflush(stdout);
    printf(" Welcome to OpenMX   Ver. %s                           \n",Version_OpenMX); fflush(stdout);
    printf(" Copyright (C), 2002-2013, T.Ozaki                     \n");    fflush(stdout);
    printf(" OpenMX comes with ABSOLUTELY NO WARRANTY.             \n");    fflush(stdout);
    printf(" This is free software, and you are welcome to         \n");    fflush(stdout);
    printf(" redistribute it under the constitution of the GNU-GPL.\n");    fflush(stdout);
    printf("*******************************************************\n");    fflush(stdout);
    printf("*******************************************************\n\n\n");fflush(stdout);  

    printf("\n");
    printf(" OpenMX is now in the mode to check whether OpenMX runs normally\n"); fflush(stdout);
    printf(" on your machine or not by comparing the stored *.out and\n");        fflush(stdout);
    printf(" generated *.out \n"); fflush(stdout);
    printf("\n");fflush(stdout);

    /* set dir */

    dir = input_dir;

    /* count the number of dat files */

    if(( dp = opendir(dir) ) == NULL ){
      printf("could not find the directry '%s'\n",input_dir);
      MPI_Finalize();
      exit(0);
    }

    Num_DatFiles = 0;
    while((entry = readdir(dp)) != NULL){

      if ( strstr(entry->d_name,".dat")!=NULL ){ 
          
        Num_DatFiles++;
      }
    }
    closedir(dp);

    fndat = (fname_type*)malloc(sizeof(fname_type)*Num_DatFiles);

    /* store the name of dat files */

    if(( dp = opendir(dir) ) == NULL ){
      printf("could not find the directry '%s'\n",input_dir);
      MPI_Finalize();
      exit(0);
    }

    Num_DatFiles = 0;
    while((entry = readdir(dp)) != NULL){
 
      if ( strstr(entry->d_name,".dat")!=NULL ){ 

        sprintf(fndat[Num_DatFiles].fn,"%s/%s",input_dir,entry->d_name);  
        Num_DatFiles++;
      }
    }
    closedir(dp);

    /* sorting fndat */

    qsort(fndat, Num_DatFiles, sizeof(fname_type), stringcomp);  

    /*
    for (i=0; i<Num_DatFiles; i++){
      printf("i=%2d %s\n",i,fndat[i].fn);
    } 
    */

  } /* if (myid==Host_ID) */

  sprintf(fname2,"%s",output_file);

  if (myid==Host_ID){
    fp = fopen(fname2, "r");   
    if (fp!=NULL){
      fclose(fp); 
      sprintf(operate,"%s",fname2);
      remove(operate);
    }
  }

  if (myid==Host_ID){
    printf(" %2d dat files are found in the directory '%s'.\n\n\n",Num_DatFiles,input_dir);
  }

  MPI_Bcast(&Num_DatFiles, 1, MPI_INT, Host_ID, MPI_COMM_WORLD1);

  /***********************************************************
                      start calculations
  ***********************************************************/

  for (i=0; i<Num_DatFiles; i++){

    if (myid==Host_ID){
      sprintf(fname_dat,"%s",fndat[i].fn);
    }  

    MPI_Bcast(&fname_dat, YOUSO10, MPI_CHAR, Host_ID, MPI_COMM_WORLD1);

    /* run openmx */

    argv[1] = fname_dat;
    run_main(argc, argv, numprocs, myid); 

    /***********************************************************
          comparison between two files and save the result               
    ***********************************************************/

    if (myid==Host_ID){

      input_open(fname_dat);
      input_string("System.Name",fname_dat2,"default");
      input_close();

      /* compare two out files */

      sprintf(fname_out1,"%s.out",fname_dat2);
      sprintf(fname_out2,"%s/%s.out",input_dir,fname_dat2);

      /* generated file */

      input_open(fname_out1);
      input_double("Utot.",&Utot1,(double)0.0);

      /* for Wannier functions */

      if (strcasecmp(mode,"WF")==0){  
        input_double("Sum.of.Spreads.",&Spread1,(double)0.0);
        input_double("Total.Omega.=",&Omega1,(double)0.0);
      }

      input_int("Num.Grid1.",&NGrid1_1,(int)0);
      input_int("Num.Grid2.",&NGrid1_2,(int)0);
      input_int("Num.Grid3.",&NGrid1_3,(int)0);

      input_double("Elapsed.Time.",&time1,(double)0.0);

      TotalTime += time1;

      if (fp3=input_find("<coordinates.forces")) {
          
	fscanf(fp3,"%d",&Num_Atoms);

	sum1 = 0.0;
	for (j=1; j<=Num_Atoms; j++){  
	  fscanf(fp3,"%d %s %lf %lf %lf %lf %lf %lf",
		 &k,ftmp,&gx,&gy,&gz,&fx,&fy,&fz);
	  sum1 += fx + fy + fz;
	}

	if ( ! input_last("coordinates.forces>") ) {
	  printf("Format error for coordinates.forces\n");
	}
      }
      else {
	sum1 = 1000.0;
      }

      input_close();

      /* stored file */

      input_open(fname_out2);

      /* Utot */

      input_double("Utot.",&Utot2,(double)0.0);

      /* for Wannier functions */

      if (strcasecmp(mode,"WF")==0){  
        input_double("Sum.of.Spreads.",&Spread2,(double)0.0);
        input_double("Total.Omega.=",&Omega2,(double)0.0);
      }

      /* grids */

      input_int("Num.Grid1.",&NGrid2_1,(int)0);
      input_int("Num.Grid2.",&NGrid2_2,(int)0);
      input_int("Num.Grid3.",&NGrid2_3,(int)0);

      /* coordinates and forces */

      if (fp3=input_find("<coordinates.forces")) {
          
	fscanf(fp3,"%d",&Num_Atoms);

	sum2 = 0.0;
	for (j=1; j<=Num_Atoms; j++){  
	  fscanf(fp3,"%d %s %lf %lf %lf %lf %lf %lf",
		 &k,ftmp,&gx,&gy,&gz,&fx,&fy,&fz);
	  sum2 += fx + fy + fz;
	}

	if ( ! input_last("coordinates.forces>") ) {
	  /* format error */
	  printf("Format error for coordinates.forces\n");
	}
      }
      else {
	sum2 = 100.0;
      }

      input_close();

      dU = fabs(Utot1 - Utot2);
      dF = fabs(sum1 - sum2);

      /* write the result to a file, runtest.result */

      if ( (fp2 = fopen(fname2,"a")) != NULL ){

	if (  (NGrid1_1!=NGrid2_1)
	      || (NGrid1_2!=NGrid2_2)
	      || (NGrid1_3!=NGrid2_3) )
	  {
	    fprintf(fp2,"  Invalid comparison due to the different number of grids.\n");
	    fprintf(fp2,"  You may use a different radix for FFT.\n");
	  }

        if (strcasecmp(mode,"WF")==0){  

	  fprintf(fp2,"%4d  %-32.30s Elapsed time(s)=%8.2f  diff spread=%15.12f  diff Omega=%15.12f\n",
		  i+1,fname_dat,time1,fabs(Spread1-Spread2),fabs(Omega1-Omega2));
	}
        else{

	  fprintf(fp2,"%4d  %-32.30s Elapsed time(s)=%8.2f  diff Utot=%15.12f  diff Force=%15.12f\n",
		i+1,fname_dat,time1,dU,dF);
	}

	if (i==(Num_DatFiles-1)){
	  fprintf(fp2,"\n\nTotal elapsed time (s) %11.2f\n",TotalTime);
	}

	fclose(fp2);
      }
    }

  }

  /* tell us the end of calculation */
  if (myid==Host_ID){
    printf("\n\n\n\n");
    printf("The comparison can be found in a file '%s'.\n\n\n",output_file);
  }

  if (myid==Host_ID){
    free(fndat);
  }


  MPI_Barrier(MPI_COMM_WORLD1);
  MPI_Finalize();
  exit(0);
}