void save_galaxy_tree_reorder_on_disk(void)
{
  int i, idsource, idsave, dest, *id;
  struct GALAXY_OUTPUT galaxy_save, galaxy_source;
  struct mp_tree_data *mp;

  mp = (struct mp_tree_data *) mymalloc("mp", sizeof(struct mp_tree_data) * NGalTree);
  id = (int *) mymalloc("id", sizeof(int) * NGalTree);
  
  for(i = 0; i < NGalTree; i++)
    {
      mp[i].index = i;
      mp[i].key = GalTree[i].GalID;
    }
  
  qsort(mp, NGalTree, sizeof(struct mp_tree_data), save_galaxy_tree_mp_comp);
  
  for(i = 0; i < NGalTree; i++)
    id[mp[i].index] = i;

  for(i = 0; i < NGalTree; i++)
    {
      if(id[i] != i)
	{
	  myfseek(FdGalTree, (1 + TotGalCount + i) * sizeof(struct GALAXY_OUTPUT), SEEK_SET);
	  myfread(&galaxy_source, sizeof(struct GALAXY_OUTPUT), 1, FdGalTree);
	  idsource = id[i];
	  dest = id[i];

	  do
	    {
	      myfseek(FdGalTree, (1 + TotGalCount + dest) * sizeof(struct GALAXY_OUTPUT), SEEK_SET);
	      myfread(&galaxy_save, sizeof(struct GALAXY_OUTPUT), 1, FdGalTree);
	      idsave = id[dest];

	      myfseek(FdGalTree, (1 + TotGalCount + dest) * sizeof(struct GALAXY_OUTPUT), SEEK_SET);
	      myfwrite(&galaxy_source, sizeof(struct GALAXY_OUTPUT), 1, FdGalTree);
	      id[dest] = idsource;

	      if(dest == i)
		break;

	      galaxy_source = galaxy_save;
	      idsource = idsave;

	      dest = idsource;
	    }
	  while(1);
	}
    }

  myfree(id);
  myfree(mp);

  myfseek(FdGalTree, (1 + TotGalCount + NGalTree) * sizeof(struct GALAXY_OUTPUT), SEEK_SET);
}
Пример #2
0
int rlearn( void )
{

int size, pos;
int i;
tlearn cell;

if( Flag.learn == 0 || Learn.f == NULL ) return NOVALUE;

size = Learn.filesize/sizeof(tlearn);
pos = G[Counter].hashboard % size;

for( i=0; i<LREHASH; i++ )
{
	if( fseek( Learn.f, ((pos+i)%size)*sizeof(tlearn), SEEK_SET ) != 0 )
		return NOVALUE;
	myfread( &cell, sizeof(tlearn), Learn.f );

	if( cell.hashboard == 0 ) return NOVALUE;

	if( cell.hashboard == G[Counter].hashboard )
	{
		if( cell.depth >= Depth ) return cell.value;
		else return NOVALUE;
	}
}

return NOVALUE;

}
Пример #3
0
void wlearn( int depth, int value )
{

int size, pos;
int i;
int bdepth, bpos;
tlearn cell;

if( Flag.learn == 0 || Learn.f == NULL || depth <= 400 ) return;

size = Learn.filesize/sizeof(tlearn);
pos = G[Counter].hashboard % size;

if( fseek( Learn.f, pos*sizeof(tlearn), SEEK_SET ) != 0 ) return;
myfread( &cell, sizeof(tlearn), Learn.f );

bdepth = -1000;
bpos   = pos;

for( i=0; i<LREHASH; i++ )
{
	if( fseek( Learn.f, ((pos+i)%size)*sizeof(tlearn), SEEK_SET ) != 0 )
		return;
	myfread( &cell, sizeof(tlearn), Learn.f );

	if( cell.hashboard == 0 || cell.hashboard == G[Counter].hashboard )
	{ bpos = pos+i; bdepth = cell.depth; break; }

	if( cell.depth > bdepth )
	{ bdepth = cell.depth; bpos = pos+i; }
}

if( cell.hashboard == G[Counter].hashboard && cell.depth > depth )
	return;

if( fseek( Learn.f, (bpos%size)*sizeof(tlearn), SEEK_SET ) != 0 )
	return;

cell.hashboard = G[Counter].hashboard;
cell.depth = depth;
cell.value = value;

myfwrite( &cell, sizeof(tlearn), Learn.f );

}
Пример #4
0
/** 
 * Read binary with byte swap (assume file is Big Endian)
 * 
 * @param buf [out] data buffer
 * @param unitbyte [in] size of unit in bytes
 * @param unitnum [in] number of units to be read
 * @param fp [in] file pointer
 * 
 * @return TRUE if required number of units are fully read, FALSE if failed.
 */
static boolean
myread(void *buf, size_t unitbyte, int unitnum, FILE *fp)
{
  if (myfread(buf, unitbyte, unitnum, fp) < (size_t)unitnum) {
    return(FALSE);
  }
#ifndef WORDS_BIGENDIAN
  swap_bytes(buf, unitbyte, unitnum);
#endif
  return(TRUE);
}
Пример #5
0
//**************************************************************************
bool ipage_t::readPage( FILE *fp, pa_t tag, uint32 &totalread )
{
  uint64   pc;
  uint32   index;
  uint32   instr;
  int      numread;
  int      count = 0;

  // right shift the bits, so the offset can simply be masked on
  tag = tag << IPAGE_PAGE_BITS;

  // read the physical PC
  ASSERT ( sizeof(pa_t) == sizeof(uint64) );
  numread = myfread( &pc, sizeof(uint64), 1, fp );
  if (numread != 1) {
    ERROR_OUT("read fails: ipage_t readPage (%d)\n", numread);
    return (false);
  }
  m_header.m_physical_addr = pc;

  for (index = 0; index < IPAGE_MAX_INSTR; index ++) {

    numread = myfread( &instr, sizeof(uint32), 1, fp );
    if (numread != 1) {
      ERROR_OUT("read fails: ipage_t readPage (%d)\n", numread);
      return (false);
    }
    
    if (instr == 0) {
      m_instr[index] = NULL;
    } else {
      // decode and add this instruction to the page
      pa_t ppc = m_header.m_physical_addr | index;
      m_instr[index] = new static_inst_t( ppc, instr );    
      count++;
    }
  }

  totalread += count;
  return (true);
}
Пример #6
0
/** 
 * Read binary data from a file pointer, with byte swapping.
 * 
 * @param buf [out] buffer to store read data
 * @param unitbyte [in] size of a unit in bytes
 * @param unitnum [in] number of unit to read
 * @param fp [in] file pointer
 * 
 * @return TRUE if specified number of unit was successfully read, FALSE if failed.
 */
static boolean
myread(char *buf, size_t unitbyte, int unitnum, FILE *fp)
{
  size_t tmp;
  if ((tmp = myfread(buf, unitbyte, unitnum, fp)) < (size_t)unitnum) {
    jlog("Error: rdparam: failed to read %d bytes\n", unitbyte * unitnum);
    return(FALSE);
  }
  /* swap if necessary */
  if (needswap) swap_bytes(buf, unitbyte, unitnum);
  return(TRUE);
}
Пример #7
0
/** 
 * Binary read function with byte swap
 * 
 * @param fp [in] file pointer
 * @param buf [out] data buffer
 * @param unitbyte [in] unit size in bytes
 * @param unitnum [in] number of unit to read.
 */
static boolean
rdnfunc(FILE *fp, void *buf, size_t unitbyte, size_t unitnum)
{
  size_t tmp;
  if ((tmp = myfread(buf, unitbyte, unitnum, fp)) < unitnum) {
    jlog("Error: ngram_read_bin: failed to read %d bytes\n", unitbyte*unitnum);
    return FALSE;
  }
  if (need_swap) {
    if (unitbyte != 1) {
      swap_bytes(buf, unitbyte, unitnum);
    }
  }
  return TRUE;
}
Пример #8
0
/** 
 * Binary read function with byte swap
 * 
 * @param fp [in] file pointer
 * @param buf [out] data buffer
 * @param unitbyte [in] unit size in bytes
 * @param unitnum [in] number of unit to read.
 */
static boolean
rdnfunc(FILE *fp, void *buf, size_t unitbyte, size_t unitnum)
{
  size_t tmp;
  if ((tmp = myfread(buf, unitbyte, unitnum, fp)) < unitnum) {
    jlog("Error: ngram_read_bin: failed to read %d bytes\n", unitbyte*unitnum);
    return FALSE;
  }
//[email protected]
#ifndef ANDROID_CUSTOM
  if (need_swap) {
    if (unitbyte != 1) {
      swap_bytes(buf, unitbyte, unitnum);
    }
  }
//[email protected]
#endif
  return TRUE;
}
Пример #9
0
/** 
 * Binary read function with byte swap
 * 
 * @param fp [in] file pointer
 * @param buf [out] data buffer
 * @param unitbyte [in] unit size in bytes
 * @param unitnum [in] number of unit to read.
 */
static boolean
rdnfunc(FILE *fp, void *buf, size_t unitbyte, size_t unitnum)
{
  size_t tmp;
  if ((tmp = myfread(buf, unitbyte, unitnum, fp)) < unitnum) {
    jlog("Error: ngram_read_bin: failed to read %d bytes\n", unitbyte*unitnum);
    return FALSE;
  }

// modified by Kenta Kubo on May 1, 2016
#ifndef ANDROID_CUSTOM
  if (need_swap) {
    if (unitbyte != 1) {
      swap_bytes(buf, unitbyte, unitnum);
    }
  }
#endif

  return TRUE;
}
Пример #10
0
void get_xfrac_mesh()
{
  FILE* fp;
  char buf[1024],sbuf[1024];
  float redshift;
  int dummy;

#ifdef PARALLEL
  if(ThisTask == 0)
    {
#endif
      redshift = ZZ[MAXSNAPS-1];
      sprintf(buf, "%s/xfrac3d_%2.3f.bin", XfracDir, redshift);
      if((fp = fopen(buf,"r")) == NULL)
	{
	  char sbuf[1000];
	  printf("can't open file `%s'\n", buf);
	  // terminate(sbuf);
	  XfracMesh[0] = XfracNGrids;
	  XfracMesh[1] = XfracNGrids;
	  XfracMesh[2] = XfracNGrids;
	}
      else {
	fread(&dummy, 1, sizeof(int),fp);
	myfread(XfracMesh, 3, sizeof(int),fp);
	fread(&dummy, 1, sizeof(int),fp);
	fclose(fp);
      }
#ifdef PARALLEL
    }
  MPI_Barrier(MPI_COMM_WORLD);
  MPI_Bcast(XfracMesh, 3, MPI_INT, 0, MPI_COMM_WORLD);
  MPI_Barrier(MPI_COMM_WORLD);
  if(ThisTask == 0) printf("Task:%d => %d %d %d\n",ThisTask,XfracMesh[0],XfracMesh[1],XfracMesh[2]);
#endif

}
void save_galaxy_tree_finalize(int filenr, int tree)
{
  int i, p, num;
  struct GALAXY_OUTPUT galaxy_output;

  for(i = 0; i < NGalTree; i++)
    {
      GalTree[i].Done = 0;
      GalTree[i].LastProgGal = -1;
      GalTree[i].MainLeaf = -1;
      GalTree[i].TreeRoot = -1;
    }

  GalCount = 0;

  for(num = LastDarkMatterSnapShot; num >= 0; num--)
    {
      for(i = 0; i < NGalTree; i++)
	{
	  if(GalTree[i].SnapNum == num)
	    if(GalTree[i].Done == 0)
		walk_galaxy_tree(i);
	}
    }

  for(i = 0; i < NGalTree; i++)
    {
      p = GalTree[i].FirstProgGal;
      while(p >= 0)
	{
	  GalTree[p].DescendantGal = i;
	  p = GalTree[p].NextProgGal;
	}
    }

  for(i = 0; i < NGalTree; i++)
    {
      if(GalTree[i].FirstProgGal >= 0)
	GalTree[i].FirstProgGal = GalTree[GalTree[i].FirstProgGal].GalID;

      if(GalTree[i].LastProgGal >= 0)
	GalTree[i].LastProgGal = GalTree[GalTree[i].LastProgGal].GalID;

      if(GalTree[i].MainLeaf >= 0)
	GalTree[i].MainLeaf = GalTree[GalTree[i].MainLeaf].GalID;

      if(GalTree[i].TreeRoot >= 0)
	GalTree[i].TreeRoot = GalTree[GalTree[i].TreeRoot].GalID;

      if(GalTree[i].NextProgGal >= 0)
	GalTree[i].NextProgGal = GalTree[GalTree[i].NextProgGal].GalID;

      if(GalTree[i].DescendantGal >= 0)
	GalTree[i].DescendantGal = GalTree[GalTree[i].DescendantGal].GalID;

      if(GalTree[i].FOFCentralGal >= 0)
	GalTree[i].FOFCentralGal = GalTree[GalTree[i].FOFCentralGal].GalID;
    }

  // order GalTree by current order of storage in file (IndexStored)
  qsort(GalTree, NGalTree, sizeof(struct galaxy_tree_data), save_galaxy_tree_compare);
   

  /* Before, the header was a simple integer for number of galaxies. So, the
     code had to jump over an int (used to store the number of galaxies) and
     and over all the galaxies written so far */ 
  // for DB compatible output, pad the first line with the size of one struct.
 
  for(i = 0; i < NGalTree; i++)
    { 
      myfseek(FdGalTree, (1 + TotGalCount + i) * sizeof(struct GALAXY_OUTPUT), SEEK_SET);
      myfread(&galaxy_output, sizeof(struct GALAXY_OUTPUT), 1, FdGalTree);

      prepare_galaxy_tree_info_for_output(filenr, tree, &GalTree[i], &galaxy_output);

      myfseek(FdGalTree, (1 + TotGalCount + i) * sizeof(struct GALAXY_OUTPUT), SEEK_SET);
      myfwrite(&galaxy_output, sizeof(struct GALAXY_OUTPUT), 1, FdGalTree);

    }

  // GL: propose to only reorder file on disk based on input (or Makefile) parameter
  // if DB is fast in ordering, eg using SSDs, we could leave it out here.
  // BTW it is BETTER not to reorder galaxies if SFHBins are not also reordered,
  // if at least both are to be used in light cone post processing.
  // for easier to read
  save_galaxy_tree_reorder_on_disk();

  TotGalCount += NGalTree;

}
Пример #12
0
int bcreate( int argc, char ** argv )
{
  FILE * f;
  int c;

  while( (c=getopt(argc,argv,"b:p:c:v:g:w:d:l:u:")) != -1 )
  switch(c)
  {
	case 'b':
		if( sscanf( optarg, "%i", &Asize ) == 0 ) wusage();
		if( Asize < 1000 ) wusage();
	break;
	case 'p':
		if( sscanf( optarg, "%i", &MaxBookPly ) == 0 ) wusage();
		if( MaxBookPly < 2 ) wusage();
	break;
	case 'c':
		if( sscanf( optarg, "%i", &MaxComment ) == 0 ) wusage();
		if( MaxComment < 2 ) wusage();
	break;
	case 'v':
		if( sscanf( optarg, "%i", &MinValue ) == 0 ) wusage();
		if( MinValue < 0 ) wusage();
	break;
	case 'g':
		if( sscanf( optarg, "%i", &MinPercentage ) == 0 ) wusage();
		if( MinPercentage < 50 || MinPercentage > 100 ) wusage();
	break;
	case 'w':
		if( sscanf( optarg, "%i", &WIN ) == 0 ) wusage();
		if( WIN < 1 || WIN > 10 ) wusage();
	break;
	case 'd':
		if( sscanf( optarg, "%i", &DRA ) == 0 ) wusage();
		if( DRA < 0 || DRA > 10 ) wusage();
	break;
	case 'l':
		if( sscanf( optarg, "%i", &LOO ) == 0 ) wusage();
		if( LOO < 0 || LOO > 10 ) wusage();
	break;
	case 'u':
		if( sscanf( optarg, "%i", &UNK ) == 0 ) wusage();
		if( UNK < 0 || UNK > 10 ) wusage();
	break;
	default: wusage();
  }

  A=malloc(Asize*sizeof(tb));
  if( A == NULL )
  { printf("cannot alloc %lu bytes of memory\n",
    (unsigned long)(Asize*sizeof(tb))); exit(0);
  }

  {
    struct stat fs;
    if( ! stat("rbook.phalanx",&fs) )
	{ puts("rbook.phalanx exists, skipping stage 1"); goto stage_2; }
    else
	{ tb1 = fopen("rbook.phalanx", "w+"); }
  }

  setvbuf(stdout, (char*)NULL, _IONBF, 0);
  puts(  "creating opening book");
  puts(  "-----------------------------");
  puts(  "   games positions buffer%");
  puts(  "-----------------------------");
  setTimeout(printit, 5);
  while( findgame() )
  { parsegame(); games++; }
  printf("end of file, parsed %i positions in %i games\n",positions,games);
  if( games == 0 )
  { puts("opening book not created"); return 0; }
  compress();
  clearTimeout(void);

 stage_2:;

  if( ( f = fopen( SBOOK_FILE, "wb" ) ) == NULL )
  { printf("cannot create book file %s\n", SBOOK_FILE); return 1; }

 {
  int dots=0;
  struct stat fs;
  int fsize;
  tb M;
  tb1=fopen("rbook.phalanx","rb");
  myfread( &M, sizeof(tb), tb1 );

  stat("rbook.phalanx",&fs); fsize = fs.st_size/sizeof(tb);

  printf("writing book ");

  while(!feof(tb1))
  {
	int i;
	int minn;
	A[0]=M;
	I=0;

	do
	{
		int fpos=ftell(tb1)/sizeof(tb);
		if( fpos % (fsize/30) == 1 )
		{
		  if( dots==3 )
		  { printf("%i%%", (int)(((float)fpos)*100/fsize + 1) ); dots=0; }
		  else printf(".");
		  dots++;
		}
		I++; myfread( A+I, sizeof(tb), tb1 );
	}
	while( !feof(tb1) && M.hashboard==A[I].hashboard );

	M=A[I];

	/* sort them, better first */
	qsort( A, I, sizeof(tb), usortkey );

	/* more variability in frequently played lines of early openings */
	{ unsigned i=100, j=10, p=MinPercentage;
	  while( A[0].n > i && p>10 ) { p-=j; j--; i*=10; }
	  minn=A[0].n*p/100;
	}

	if( minn > A[0].n ) minn = A[0].n;

	if( minn < MinValue ) minn=MinValue;

	for( i=0; i!=I && A[i].n>=minn; i++ ) {}

/*
	printf("got %i of %i moves at hasboard of %08X, next HB %08X\n",
		i, I, A[0].hashboard, M.hashboard );
*/

	I=i;

	for(i=0;i!=I;i++)
	{
		unsigned short m = A[i].move;
		myfwrite( A+i, sizeof(unsigned), f );
		myfwrite( &m, sizeof(unsigned short), f );
	}
  }
  printf(" done\n");
 }

  return 0;
}
Пример #13
0
void load_group_catalogue_v2(HBTInt Nsnap,CATALOGUE *Cat,char *GrpPath)
{//old groupcat format
  FILE *fd;
  int i;
  char buf[1024];
  int TotNgroups,NFiles,dummy,Ngroups,Nids;
  int ByteOrder;
  
struct 
{
int *Len;
int *Offset;
IDatInt *PID; 
}ICat;

#ifdef SNAPLIST
Nsnap=snaplist[Nsnap];
#endif
  sprintf(buf, "%s/group_tab_%03d",GrpPath,(int)Nsnap);
  ByteOrder=check_grpcat_byteorder(buf);
  myfopen(fd,buf,"r");

  myfread(&Ngroups, sizeof(int), 1, fd);
  myfread(&Nids, sizeof(int), 1, fd);
  myfread(&TotNgroups, sizeof(int), 1, fd);
  myfread(&NFiles, sizeof(int), 1, fd);
  Cat->Ngroups=Ngroups;
  Cat->Nids=Nids;

  fprintf(logfile,"Snap="HBTIFMT"  Ngroups="HBTIFMT"  Nids="HBTIFMT"  TotNgroups=%d  NFiles=%d\n", Nsnap, Cat->Ngroups, Cat->Nids, TotNgroups, NFiles);

  ICat.Len= mymalloc(sizeof(int)*Cat->Ngroups);
  ICat.Offset=mymalloc(sizeof(int)*Cat->Ngroups);
  ICat.PID=mymalloc(sizeof(IDatInt)*Cat->Nids);
  Cat->ID2Halo=mymalloc(sizeof(HBTInt)*NP_DM);//consider move this out.............

  myfread(ICat.Len, sizeof(int), Cat->Ngroups, fd);
  myfread(ICat.Offset,sizeof(int), Cat->Ngroups, fd);
  fclose(fd);


  sprintf(buf, "%s/group_ids_%03d", GrpPath, (int)Nsnap);
  ByteOrder=check_grpcat_byteorder(buf);
  myfopen(fd,buf,"r");

  myfread(&dummy, sizeof(int), 1, fd);
  myfread(&dummy, sizeof(int), 1, fd);
  myfread(&dummy, sizeof(int), 1, fd);
  myfread(&dummy, sizeof(int), 1, fd);

  myfread(ICat.PID, sizeof(IDatInt), Cat->Nids, fd);
  
#ifndef HBT_INT8
	  Cat->Len=ICat.Len;
	  Cat->Offset=ICat.Offset;
#else
	  Cat->Len=mymalloc(sizeof(HBTInt)*Cat->Ngroups);
	  Cat->Offset=mymalloc(sizeof(HBTInt)*Cat->Ngroups);
	  for(i=0;i<Cat->Ngroups;i++)
	  {
		  Cat->Len[i]=ICat.Len[i];
		  Cat->Offset[i]=ICat.Offset[i];
	  }
	  myfree(ICat.Len);
	  myfree(ICat.Offset);
#endif
  
  #ifdef HBTPID_RANKSTYLE
  IDatInt *PIDs,*p;  
  PIDs=load_PIDs_Sorted();  
  Cat->PIDorIndex=mymalloc(sizeof(HBTInt)*Cat->Nids);
  for(i=0;i<Cat->Nids;i++)
  {	  
	p=bsearch(&(ICat.PID[i]),PIDs,NP_DM,sizeof(IDatInt),comp_IDatInt);
	Cat->PIDorIndex[i]=p-PIDs;
  }
  myfree(PIDs);
  myfree(ICat.PID);
  #else
#ifdef SAME_INTTYPE
	  Cat->PIDorIndex=ICat.PID;
#else
	  Cat->PIDorIndex=mymalloc(sizeof(HBTInt)*Cat->Nids);
	  for(i=0;i<Cat->Nids;i++)
	  Cat->PIDorIndex[i]=ICat.PID[i];
	  myfree(ICat.PID);
#endif
  #endif
  
  fclose(fd);
}
Пример #14
0
static int read_gadget_header(FILE *fp,IO_HEADER * h)
{//read the header part, assign header extensions, and do several consistency check
//return ByteOrder
	int dummy,dummy2,n,ns,ByteOrder;
	
	n=sizeof(IO_HEADER);
	ns=n;
	swap_Nbyte(&ns,1,sizeof(ns));
			
	fread(&dummy,sizeof(dummy),1,fp);
	
	if(dummy==n)
	 ByteOrder=0;
	else if(dummy==ns)
	 ByteOrder=1;
	else
	{
		fprintf(logfile,"endianness check failed for header\n file format not expected:%d;%d,%d\n",dummy,n,ns);
		fflush(logfile);
		exit(1);
	}
	
	dummy=n;

	myfread(h->npart,sizeof(int),6,fp);
	myfread(h->mass,sizeof(double),6,fp);
	myfread(&h->time,sizeof(double),1,fp);
	myfread(&h->redshift,sizeof(double),1,fp);
	myfread(&h->flag_sfr,sizeof(int),1,fp);
	myfread(&h->flag_feedback,sizeof(int),1,fp);
	myfread(h->npartTotal,sizeof(int),6,fp);
	myfread(&h->flag_cooling,sizeof(int),1,fp);
	myfread(&h->num_files,sizeof(int),1,fp);
	myfread(&h->BoxSize,sizeof(double),1,fp);
	myfread(&h->Omega0,sizeof(double),1,fp);
	myfread(&h->OmegaLambda,sizeof(double),1,fp);
	myfread(&h->HubbleParam,sizeof(double),1,fp);
	fseek(fp,n+sizeof(int),SEEK_SET);
	myfread(&dummy2,sizeof(dummy2),1,fp);
    if(dummy!=dummy2)
	{
		fprintf(logfile,"error!record brackets not match for header!\t%d,%d\n",dummy,dummy2);
		exit(1);
	} 
	
	/*extend and examine the header*/
	if(NFILES!=h->num_files)
	  {
		  fprintf(logfile,"error: number of snapfiles specified not the same as stored: %d,%d\n",
		  NFILES,h->num_files);
		  fflush(logfile);
//		  exit(1);
	  }
	  
	h->Hz=HUBBLE0 * sqrt(h->Omega0 / (h->time * h->time * h->time) 
			+ (1 - h->Omega0 - h->OmegaLambda) / (h->time * h->time)
			+ h->OmegaLambda);//Hubble param for the current catalogue;
	
	if(h->npartTotal[0])
	  {
	  if(0==h->mass[0])
	  h->mass[0]=MP_GAS;
	  else if(fabs(MP_GAS-h->mass[0])>0.01*MP_GAS)
	  {
		  fprintf(logfile,"error: GAS particle mass mismatch: %g,%g\n",MP_GAS,h->mass[0]);
		  fflush(logfile);
		  exit(1);
	  }
	  }
     if(0==h->mass[1])
	  h->mass[1]=MP_DM;
	  else if(fabs(MP_DM-h->mass[1])>0.01*MP_DM)
	  {
		  fprintf(logfile,"error: DM particle mass mismatch: %g,%g\n",MP_DM,h->mass[1]);
		  fflush(logfile);
		  exit(1);
	  }
	  
	#ifdef CONVERT_LENGTH_MPC_KPC
	  h->BoxSize*=1000.;
	#endif 
	
	int j;
	for(j=0, NumPart=0; j<6; j++)
	    NumPart+= h->npartTotal[j];	
	if(NP_SIM!=NumPart)
	{
		fprintf(logfile,"error:Total Number of Particles differ: %lld,%lld\n",(long long)NP_SIM,(long long)NumPart);
		fflush(logfile);
		exit(1);
	}
	if(NP_GAS!=h->npartTotal[0])
	{
		fprintf(logfile,"error:Number of Gas Particles differ: %lld,%u\n",(long long)NP_GAS,h->npartTotal[0]);
		fflush(logfile);
		exit(1);
	}
	if(NP_DM!=h->npartTotal[1])
	{
		fprintf(logfile,"error:Number of DM Particles differ: %lld,%u\n",(long long)NP_DM,h->npartTotal[1]);
		fflush(logfile);
		exit(1);
	}
	
	return ByteOrder;
	
}
Пример #15
0
void load_group_catalogue_millimill(HBTInt Nsnap,CATALOGUE *Cat,char *GrpPath)
{//milli-mellennium format
  FILE *fd;
  char buf[1024];
  int Ngroups,TotNgroups,Nids,NFiles;
  long long i,Nload,TotNids;
  int ByteOrder,grpfile_type;
  
struct 
{
int *Len;
int *Offset;
IDatInt *PID; 
}ICat;
  
  	#ifdef SNAPLIST
	Nsnap=snaplist[Nsnap];
	#endif

  Nload=0; TotNids=0;
  for(i=0;i<NFILES_GRP;i++)
  {
  grpfile_type=1;	
  sprintf(buf, "%s/groups_%03d/subhalo_tab_%03d.%d",GrpPath,(int)Nsnap,(int)Nsnap,(int)i);
  if(!try_readfile(buf))
  {
	  sprintf(buf, "%s/snapdir_%03d/group_tab_%03d.%d",GrpPath,(int)Nsnap,(int)Nsnap,(int)i);
	  grpfile_type=2;
  }
  if(1==NFILES_GRP)
  {
	if(!try_readfile(buf))
	{
		sprintf(buf, "%s/subhalo_tab_%03d",GrpPath,(int)Nsnap);
		grpfile_type=3;
	}
	if(!try_readfile(buf))
	{
	sprintf(buf, "%s/group_tab_%03d",GrpPath,(int)Nsnap);
	grpfile_type=4;
	}
  }
	
  ByteOrder=check_grpcat_byteorder(buf);
  	
  myfopen(fd,buf,"r");

  myfread(&Ngroups, sizeof(int), 1, fd);
  myfread(&Nids, sizeof(int), 1, fd);
  myfread(&TotNgroups, sizeof(int), 1, fd);
  Cat->Ngroups=TotNgroups;
  TotNids+=Nids;
  //~ myfread(&TotNids,sizeof(long long),1,fd);
  myfread(&NFiles, sizeof(int), 1, fd);
 
  if(1==grpfile_type||3==grpfile_type)
  {
  int Nsub,TotNsub;
  myfread(&Nsub,sizeof(int),1,fd);
  myfread(&TotNsub,sizeof(int),1,fd);
  }
  
  if(NFILES_GRP!=NFiles)
	  {
		  fprintf(logfile,"error: number of grpfiles specified not the same as stored: %d,%d\n for file %s\n",
		  NFILES_GRP,(int)NFiles,buf);
		  fflush(logfile);
		  exit(1);
	  }
 
  if(0==i)
  fprintf(logfile,"Snap="HBTIFMT"  Ngroups=%d  Nids=%d  TotNgroups=%d  NFiles=%d\n", Nsnap, Ngroups, Nids, (int)(Cat->Ngroups), NFiles);
  
  if(0==i)
  {
  ICat.Len= mymalloc(sizeof(int)*Cat->Ngroups);
  ICat.Offset=mymalloc(sizeof(int)*Cat->Ngroups);
  }
  
  myfread(ICat.Len+Nload, sizeof(int), Ngroups, fd);
  myfread(ICat.Offset+Nload, sizeof(int), Ngroups, fd);
  if(feof(fd))
  {
	fprintf(logfile,"error:End-of-File in %s\n",buf);
	fflush(logfile);exit(1);  
  }
  Nload+=Ngroups;
  fclose(fd);
  }
  #ifndef HBT_INT8
  if(TotNids>INT_MAX)
  {
	  printf("error: TotNids larger than HBTInt can hold! %lld\n",TotNids);
	  exit(1);
  }
  #endif
  Cat->Nids=TotNids;
  if(Nload!=Cat->Ngroups)
  {
	  fprintf(logfile,"error:Num groups loaded not match: %lld,"HBTIFMT"\n",Nload,Cat->Ngroups);
	  fflush(logfile);exit(1);
  }
  
  ICat.PID=mymalloc(sizeof(IDatInt)*Cat->Nids);
  Cat->ID2Halo=mymalloc(sizeof(HBTInt)*NP_DM);//consider move this out.............
  Nload=0;
  for(i=0;i<NFILES_GRP;i++)
  {
  switch(grpfile_type)
  {
  case 1:
	sprintf(buf, "%s/groups_%03d/subhalo_ids_%03d.%d",GrpPath,(int)Nsnap,(int)Nsnap,(int)i);
	break;
  case 2:
	sprintf(buf, "%s/snapdir_%03d/group_ids_%03d.%d",GrpPath,(int)Nsnap,(int)Nsnap,(int)i);
	break;
  case 3:
	sprintf(buf, "%s/subhalo_ids_%03d",GrpPath,(int)Nsnap);
	break;
  case 4:
	sprintf(buf, "%s/group_ids_%03d",GrpPath,(int)Nsnap);
  break;
  default:
	fprintf(logfile,"error: grpfile_type not assigned? %s\n",buf);
	exit(1);
  }
  ByteOrder=check_grpcat_byteorder(buf);
  myfopen(fd,buf,"r");

  myfread(&Ngroups, sizeof(int), 1, fd);
  myfread(&Nids, sizeof(int), 1, fd);
  myfread(&TotNgroups, sizeof(int), 1, fd);
  myfread(&NFiles, sizeof(int), 1, fd);

  myfread(ICat.PID+Nload, sizeof(IDatInt), Nids, fd);
  if(feof(fd))
  {
	fprintf(logfile,"error:End-of-File in %s\n",buf);
	fflush(logfile);exit(1);  
  }
  Nload+=Nids;
  fclose(fd);
	}
	
  if(Nload!=Cat->Nids)
  {
	  fprintf(logfile,"error:Num grpparticles loaded not match: %lld,%lld\n",Nload,(long long)Cat->Nids);
	  fflush(logfile);
	  exit(1);
  }
  
  #define ID_MASK 0x00000003FFFFFFFF
  //~ if(Cat->Nids>0)
  //~ {
  //~ printf("%0llx,%0llx\n%lld,%lld\n",ICat.PID[0],ICat.PID[0]&ID_MASK,ICat.PID[0],ICat.PID[0]&ID_MASK);
  //~ exit(1);
  //~ }
  for(i=0;i<Cat->Nids;i++)  //only the least significant 34bits are the actual ids
	  ICat.PID[i]=ICat.PID[i]&ID_MASK;
  
#ifndef HBT_INT8
	  Cat->Len=ICat.Len;
	  Cat->Offset=ICat.Offset;
#else
	  Cat->Len=mymalloc(sizeof(HBTInt)*Cat->Ngroups);
	  Cat->Offset=mymalloc(sizeof(HBTInt)*Cat->Ngroups);
	  for(i=0;i<Cat->Ngroups;i++)
	  {
		  Cat->Len[i]=ICat.Len[i];
		  Cat->Offset[i]=ICat.Offset[i];
	  }
	  myfree(ICat.Len);
	  myfree(ICat.Offset);
#endif

TotNids=0;
	for(i=0;i<Cat->Ngroups;i++)
	{
		Cat->Offset[i]=TotNids;
		TotNids+=Cat->Len[i];
	}
	if(TotNids!=Cat->Nids)
   {
	  fprintf(logfile,"error:Num grpparticles summed not match: %lld,%lld\n",TotNids,(long long)Cat->Nids);
	  fflush(logfile);
	  exit(1);
   }
  
  #ifdef HBTPID_RANKSTYLE
  IDatInt *PIDs,*p;  
  PIDs=load_PIDs_Sorted();
  Cat->PIDorIndex=mymalloc(sizeof(HBTInt)*Cat->Nids);
  for(i=0;i<Cat->Nids;i++)
  {	  
	p=bsearch(&(ICat.PID[i]),PIDs,NP_DM,sizeof(IDatInt),comp_IDatInt);
	Cat->PIDorIndex[i]=p-PIDs;
  }
  myfree(PIDs);
  myfree(ICat.PID);
  #else
#ifdef SAME_INTTYPE
	  Cat->PIDorIndex=ICat.PID;
#else
	  Cat->PIDorIndex=mymalloc(sizeof(HBTInt)*Cat->Nids);
	  for(i=0;i<Cat->Nids;i++)
	  Cat->PIDorIndex[i]=ICat.PID[i];
	  myfree(ICat.PID);
#endif 
  #endif
  
	for(i=1;i<Cat->Ngroups;i++)
	{
		if(Cat->Len[i]>Cat->Len[i-1])
		{
		fprintf(logfile,"warning: groups not sorted with mass\n");
		}
	}
	
}	
Пример #16
0
IGL_INLINE igl::opengl::gliGenericImage *
igl::opengl::gliReadTGA(FILE *fp, char *name, int /*hflip*/, int vflip)
{
  igl::opengl::TgaHeader tgaHeader;
  igl::opengl::TgaFooter tgaFooter;
  char horzrev, vertrev;
  int width, height, bpp;
  int start, end, dir;
  int i, j, k;
  int pelbytes, wbytes;
  GLenum format;
  int components;
  RLEstate rleRec;
  RLEstate *rleInfo;
  int rle;
  int index, colors, length;
  GLubyte *cmap, *pixels, *data;
  int (*myfread)(RLEstate *rleInfo, unsigned char*, size_t, size_t, FILE*);
  igl::opengl::gliGenericImage *genericImage;

  /* Check the footer. */
  if (fseek(fp, 0L - sizeof(tgaFooter), SEEK_END)
      || fread(&tgaFooter, sizeof(tgaFooter), 1, fp) != 1) {
    sprintf(error, "TGA: Cannot read footer from \"%s\"", name);
    if (_verbose) printf("%s\n", error);
    return NULL;
  }  

  /* Check the signature. */
  if (memcmp(tgaFooter.signature, TGA_SIGNATURE,
             sizeof(tgaFooter.signature)) == 0) {
    if (_verbose) printf("TGA: found New TGA\n");
  } else {
    if (_verbose) printf("TGA: found Original TGA\n");
  }

  if (fseek(fp, 0, SEEK_SET) ||
      fread(&tgaHeader, sizeof(tgaHeader), 1, fp) != 1) {
    sprintf(error, "TGA: Cannot read header from \"%s\"", name);
    if (_verbose) printf("%s\n", error);
    return NULL;
  }

  if (_verbose && tgaHeader.idLength) {
    char *idString = (char*) malloc(tgaHeader.idLength);
    
    if (fread(idString, tgaHeader.idLength, 1, fp) != 1) {
      sprintf(error, "TGA: Cannot read ID field in \"%s\"", name);
      printf("%s\n", error);
    } else {
      printf("TGA: ID field: \"%*s\"\n", tgaHeader.idLength, idString);
    }
    free(idString);
  } else {
    /* Skip the image ID field. */
    if (tgaHeader.idLength && fseek(fp, tgaHeader.idLength, SEEK_CUR)) {
      sprintf(error, "TGA: Cannot skip ID field in \"%s\"", name);
      if (_verbose) printf("%s\n", error);
      return NULL;
    }
  }
  
  /* Reassemble the multi-byte values correctly, regardless of
     host endianness. */
  width = (tgaHeader.widthHi << 8) | tgaHeader.widthLo;
  height = (tgaHeader.heightHi << 8) | tgaHeader.heightLo;
  bpp = tgaHeader.bpp;
  if (_verbose) {
    printf("TGA: width=%d, height=%d, bpp=%d\n", width, height, bpp);
  }

  horzrev = tgaHeader.descriptor & TGA_DESC_HORIZONTAL;
  vertrev = tgaHeader.descriptor & TGA_DESC_VERTICAL;
  //vertrev=0;

//   // JASON - we can force this stuff if we want
//   if( hflip )
//       horzrev = 1;
  if( vflip )
      vertrev = 1;

  if (_verbose && horzrev) printf("TGA: horizontal reversed\n");
  if (_verbose && vertrev) printf("TGA: vertical reversed\n");

  rle = 0;
  switch (tgaHeader.imageType) {
  case TGA_TYPE_MAPPED_RLE:
    rle = 1;
    if (_verbose) printf("TGA: run-length encoded\n");
  case TGA_TYPE_MAPPED:
    /* Test for alpha channel. */
    format = GL_COLOR_INDEX;
    components = 1;
    if (_verbose) {
      printf("TGA: %d bit indexed image (%d bit palette)\n",
        tgaHeader.colorMapSize, bpp);
    }
    break;

  case TGA_TYPE_GRAY_RLE:
    rle = 1;
    if (_verbose) printf("TGA: run-length encoded\n");
  case TGA_TYPE_GRAY:
    format = GL_LUMINANCE;
    components = 1;
    if (_verbose) printf("TGA: %d bit grayscale image\n", bpp);
    break;

  case TGA_TYPE_COLOR_RLE:
    rle = 1;
    if (_verbose) printf("TGA: run-length encoded\n");
  case TGA_TYPE_COLOR:
    /* Test for alpha channel. */
    if (bpp == 32) {
      format = GL_BGRA_EXT;
      components = 4;
      if (_verbose) {
        printf("TGA: %d bit color image with alpha channel\n", bpp);
      }
    } else {
      format = GL_BGR_EXT;
      components = 3;
      if (_verbose) printf("TGA: %d bit color image\n", bpp);
    }
    break;

  default:
    sprintf(error,
      "TGA: unrecognized image type %d\n", tgaHeader.imageType);
    if (_verbose) printf("%s\n", error);
    return NULL;
  }

  if ((format == GL_BGRA_EXT && bpp != 32) ||
      (format == GL_BGR_EXT && bpp != 24) ||
      ((format == GL_LUMINANCE || format == GL_COLOR_INDEX) && bpp != 8)) {
    /* FIXME: We haven't implemented bit-packed fields yet. */
    fprintf(stderr, "bpp %d, format %x\n", bpp, (unsigned int)format); 
    sprintf(error, "TGA: channel sizes other than 8 bits are unimplemented");
    if (_verbose) printf("%s\n", error);
    return NULL;
  }

  /* Check that we have a color map only when we need it. */
  if (format == GL_COLOR_INDEX) {
    if (tgaHeader.colorMapType != 1) {
      sprintf(error, "TGA: indexed image has invalid color map type %d\n",
        tgaHeader.colorMapType);
      if (_verbose) printf("%s\n", error);
      return NULL;
    }
  } else if (tgaHeader.colorMapType != 0) {
    sprintf(error, "TGA: non-indexed image has invalid color map type %d\n",
      tgaHeader.colorMapType);
    if (_verbose) printf("%s\n", error);
    return NULL;
  }

  if (tgaHeader.colorMapType == 1) {
    /* We need to read in the colormap. */
    index = (tgaHeader.colorMapIndexHi << 8) | tgaHeader.colorMapIndexLo;
    length = (tgaHeader.colorMapLengthHi << 8) | tgaHeader.colorMapLengthLo;

    if (_verbose) {
      printf("TGA: reading color map (%d + %d) * (%d / 8)\n",
        index, length, tgaHeader.colorMapSize);
    }
    if (length == 0) {
      sprintf(error, "TGA: invalid color map length %d", length);
      if (_verbose) printf("%s\n", error);
      return NULL;
    }
    if (tgaHeader.colorMapSize != 24) {
      /* We haven't implemented bit-packed fields yet. */
      sprintf(error, "TGA: channel sizes other than 8 bits are unimplemented");
      if (_verbose) printf("%s\n", error);
      return NULL;
    }

    pelbytes = tgaHeader.colorMapSize / 8;
    colors = length + index;
    cmap = (GLubyte*)malloc (colors * pelbytes);

    /* Zero the entries up to the beginning of the map. */
    memset(cmap, 0, index * pelbytes);

    /* Read in the rest of the colormap. */
    if (fread(cmap, pelbytes, length, fp) != (size_t) length) {
      sprintf(error, "TGA: error reading colormap (ftell == %ld)\n",
        ftell (fp));
      if (_verbose) printf("%s\n", error);
      return NULL;
    }

    if (pelbytes >= 3) {
      /* Rearrange the colors from BGR to RGB. */
      int tmp;
      for (j = index; j < length * pelbytes; j += pelbytes) {
        tmp = cmap[j];
        cmap[j] = cmap[j + 2];
        cmap[j + 2] = tmp;
      }
    }
  } else {
    colors = 0;
    cmap = NULL;
  }

  /* Allocate the data. */
  pelbytes = bpp / 8;
  pixels = (unsigned char *) malloc (width * height * pelbytes);

  if (rle) {
    rleRec.statebuf = 0;
    rleRec.statelen = 0;
    rleRec.laststate = 0;
    rleInfo = &rleRec;
    myfread = rle_fread;
  } else {
    rleInfo = NULL;
    myfread = std_fread;
  }

  wbytes = width * pelbytes;

  if (vertrev) {
    start = 0;
    end = height;
    dir = 1;
  } else {
    /* We need to reverse loading order of rows. */
    start = height-1;
    end = -1;
    dir = -1;
  }

  for (i = start; i != end; i += dir) {
    data = pixels + i*wbytes;

    /* Suck in the data one row at a time. */
    if (myfread(rleInfo, data, pelbytes, width, fp) != width) {
      /* Probably premature end of file. */
      if (_verbose) {
        printf ("TGA: error reading (ftell == %ld, width=%d)\n",
          ftell(fp), width);
      }
      return NULL;
    }  

    if (horzrev) {
      /* We need to mirror row horizontally. */
      for (j = 0; j < width/2; j++) {
        GLubyte tmp;

        for (k = 0; k < pelbytes; k++) {
          tmp = data[j*pelbytes+k];
          data[j*pelbytes+k] = data[(width-j-1)*pelbytes+k];
          data[(width-j-1)*pelbytes+k] = tmp;
        }
      }
    }
  }

  if (rle) {
    free(rleInfo->statebuf);
  }

  if (fgetc (fp) != EOF) {
    if (_verbose) printf ("TGA: too much input data, ignoring extra...\n");
  }

  genericImage = (igl::opengl::gliGenericImage*) malloc(sizeof(igl::opengl::gliGenericImage));
  genericImage->width = width;
  genericImage->height = height;
  genericImage->format = format;
  genericImage->components = components;
  genericImage->cmapEntries = colors;
  genericImage->cmapFormat = GL_BGR_EXT;  // XXX fix me
  genericImage->cmap = cmap;
  genericImage->pixels = pixels;

  return genericImage;
}
Пример #17
0
/* binary book */
int sbookmoves( int *moves, int *values, tmove *m, int n )
{
	int index, i;
	int64 first, last=0, middle=0;
	unsigned fkey, lkey;
	unsigned pos;
	long booksize; 	/* filesize in bytes */
	FILE *f;
	int hits=0;

	if( (f=Sbook.f) == NULL ) return -1;

	if( G[Counter].hashboard < Sbook.firstkey
	 || G[Counter].hashboard > Sbook.lastkey ) return -1;

	fkey = Sbook.firstkey; lkey = Sbook.lastkey;

	booksize = Sbook.filesize/6;

	first = 0; last = booksize-1;
	if( G[Counter].hashboard == Sbook.firstkey ) middle=0;
	else if( G[Counter].hashboard == Sbook.lastkey ) middle=booksize-1;
	else
	while( first < last )
	{
		/* middle = (first+last) / 2; */

		middle = (G[Counter].hashboard-fkey)*(last-first)
		       / (lkey-fkey) + first;

		if(middle==first) middle++; if(middle==last) middle--;
		if(middle==first)
		{
#ifdef SHOWREADS
			printf("notfoundinbook %i\n", hits );
#endif
			return -1;
		}

		if( fseek(f,middle*6,SEEK_SET)!=0 ) return -1;
		myfread( &pos, sizeof(unsigned), f ); hits++;

		if( pos < G[Counter].hashboard ) { first=middle; fkey=pos; }
		else if( pos == G[Counter].hashboard ) break;
		else { last=middle; lkey=pos; }
	}

/*
printf("[%i %i %i %i] [%08X %08X]\n",
        first,middle,last,booksize,pos,G[Counter].hashboard);
*/

#ifdef SHOWREADS
	printf("yesfoundinbook %i\n",hits);
#endif

	if( middle == 0 ) pos = G[Counter].hashboard;
	else
	do
	{
		middle --;
		if( fseek(f,middle*6,SEEK_SET)!=0 ) return -1;
		myfread( &pos, sizeof(unsigned), f );
	} while( middle>0 && pos==G[Counter].hashboard );

	if(pos!=G[Counter].hashboard)
	{ middle ++; pos = G[Counter].hashboard; }

	/*** Position found in book! ***/
	index=0;
	if( fseek(f,middle*6+4,SEEK_SET)!=0 ) return -1;
	while( middle<booksize && pos==G[Counter].hashboard )
	{
		unsigned short sm;

		myfread( &sm, sizeof(unsigned short), f );
		for( i=0; i!=n; i++ ) if( sm == smove(m+i) )
		{ moves[index]=i; values[index]=100; index++; }

		myfread( &pos, sizeof(unsigned), f );
	}

	return index;
}
Пример #18
0
int* getipt(char *data, int mode, int *pN)      
{
	int			 scale;
	FILE		*datafile;
	int			*tmparray;

	if (NULL != data)
		{
			datafile = fopen(data, "r");
			switch (mode)
				{
				case BIN_MODE:
					if (NULL == datafile)
						{
							fprintf(stderr, "Can't open file %s.\n", data);
							exit(1);
						}
					myfread(&scale, sizeof(int), 1, datafile, data);
				
					tmparray = (int*)malloc(sizeof(int) *	scale);
					if (NULL == tmparray)
						{
							fprintf(stderr, "Can't allocate enough space.");
							exit(2);
						}
					myfread(tmparray, sizeof(int), scale, datafile, data);
					break;

				case CHAR_MODE:
					if (NULL == datafile)
						{
							fprintf(stderr, "Can't open file %s.", data);
							exit(1);
						}
					myfscanf(datafile, "%d", &scale, 1, data);
					
					tmparray = (int*)malloc(sizeof(int) *	scale);
					if (NULL == tmparray)
						{
							fprintf(stderr, "Can't allocate enough space.");
							exit(2);
						}
					myfscanf(datafile, "%d", tmparray, scale, data);
					break;

				default:
					fprintf(stderr, "Please choose the right mode.\n");
					exit(6);

					break;
			}
		}
	else
		{
			data = "~/AlgorithmEx/RandomInput";
			datafile = fopen(data, "r");
			if (NULL == datafile)
				{
					fprintf(stderr, "Can't open file %s.\n", data);
					exit(1);
				}
			myfread(&scale, sizeof(int), 1, datafile, data);
				
			tmparray = (int*)malloc(sizeof(int) *	scale);
			if (NULL == tmparray)
				{
					fprintf(stderr, "Can't allocate enough space.");
					exit(2);
				}
			myfread(tmparray, sizeof(int), scale, datafile, data);
		}

	*pN = scale;
	return tmparray;
}
Пример #19
0
int dump_gen(int readwrite, long dump_cnt, int bintxt, int whichdump, MPI_Datatype datatype, char *fileprefix, char *fileformat, char *filesuffix, int (*headerfun) (int bintxt, FILE*headerptr),int (*setgetcontent) (int i, int j, int k, MPI_Datatype datatype, void*setbuf))
{
  int i = 0, j = 0, k = 0, l = 0, col = 0;
  FILE **fpp;
  char dfnam[MAXFILENAME];
  char dfnamreal[MAXFILENAME];
  char localfileformat[MAXFILENAME];
  void *jonio;
  void *writebuf;
  char truemyidtxt[MAXFILENAME];
  char filerw[MAXFILENAME];
  FILE *headerptr;
  int numfiles,coliter;
  void *setbuf;
  int sizeofdatatype;
  int romiocloopend;
  int headerbintxt;
  int dumpbintxt;
  fpos_t headerendpos;
  long headerbytesize;
  int binextension;


  ////////////
  //
  // setup file format for header and dump
  //
  ////////////

  if(readwrite==READFILE){
	strcpy(filerw,"rb");  //atch
	//if(bintxt==BINARYOUTPUT)
    //else strcpy(filerw,"rt");
  }
  else if(readwrite==WRITEFILE){
	strcpy(filerw,"wb");  //atch
    //if(bintxt==BINARYOUTPUT) strcpy(filerw,"w");
	//else strcpy(filerw,"wt");
  }

  if(bintxt==BINARYOUTPUT) headerbintxt=dumpbintxt=BINARYOUTPUT;
  else if(bintxt==TEXTOUTPUT) headerbintxt=dumpbintxt=TEXTOUTPUT;
  else if(bintxt==MIXEDOUTPUT){
    headerbintxt=TEXTOUTPUT;
    dumpbintxt=BINARYOUTPUT;
  }



  numcolumns=dnumcolumns[whichdump];
  docolsplit=DOCOLSPLIT[whichdump]; // docolsplit global var for now


  ////////////////////
  //
  // See if enough HD space
  //
  ////////////////////

  if(mpicombine){
    if(dumpbintxt==BINARYOUTPUT){
      if(myid==0) isenoughfreespace((unsigned long long)(sizeof(FTYPE))*(unsigned long long)numcolumns*(unsigned long long)(totalsize[1])*(unsigned long long)(totalsize[2])*(unsigned long long)(totalsize[3]) );
      else isenoughfreespace(0);
    }
    else{// text
      if(myid==0) isenoughfreespace((unsigned long long)(22)*(unsigned long long)numcolumns*(unsigned long long)(totalsize[1])*(unsigned long long)(totalsize[2])*(unsigned long long)(totalsize[3]) );
      else isenoughfreespace(0);
    }
  }
  else{
    if(dumpbintxt==BINARYOUTPUT){
      isenoughfreespace((unsigned long long)(sizeof(FTYPE))*(unsigned long long)numcolumns*(unsigned long long)(N1)*(unsigned long long)(N2)*(unsigned long long)(N3) );
    }
    else{// text
      isenoughfreespace((unsigned long long)(21)*(unsigned long long)numcolumns*(unsigned long long)(N1)*(unsigned long long)(N2)*(unsigned long long)(N3) );
    }
  }


  /////////////////////
  //
  // Allocate memory for setting up setbuf
  //
  //////////////////////

  sizeofdatatype=getsizeofdatatype(datatype);
  setbuf=malloc(numcolumns*sizeofdatatype);
  if(setbuf==NULL){
    dualfprintf(fail_file,"cannot allocate memory to setbuf in %s %s with numcolumns=%d and sizeofdatatype=%d\n",fileprefix,filesuffix,numcolumns,sizeofdatatype);
    myexit(1);
  }


  //  trifprintf("numcolumns=%d sizeofdatatype=%d setbuf=%d total=%d\n",numcolumns,sizeofdatatype,setbuf,numcolumns*sizeofdatatype);

  //////////////////////////////////
  //
  //  Set up DOCOLSPLIT for normal and ROMIO loop
  //
  ///////////////////////////////////



  if(docolsplit){
    numfiles=numcolumns;
    if(mpicombine&&USEMPI&&USEROMIO) romiocloopend=numfiles;
    else romiocloopend=1;
  }
  else{
    numfiles=1;
    romiocloopend=1;
  }


  //////////////////////////////////
  //
  //  Define file output and open it
  //
  ///////////////////////////////////

  // say whether .bin is allowed or not if binary
  if(fileprefix[0]=='i') binextension=0; // images don't need binary extension
  else binextension=1;


  // sometimes all CPUs need to know filename (e.g. ROMIO)
  // setup file suffix
  if((dumpbintxt==BINARYOUTPUT)&&(binextension)){
    if(USEMPI&&(mpicombine==0)&&(numprocs>1)) sprintf(truemyidtxt,".bin.%04d",myid);
    else strcpy(truemyidtxt,".bin");
  }
  else{
    if(USEMPI&&(mpicombine==0)&&(numprocs>1)) sprintf(truemyidtxt,".%04d",myid);
    else strcpy(truemyidtxt,"");
  }

  // setup filename
  if(dump_cnt>=0){
    strcpy(localfileformat,"%s");
    strcat(localfileformat,fileformat);
    strcat(localfileformat,"%s");
    strcat(localfileformat,"%s");
    sprintf(dfnam, localfileformat, fileprefix, dump_cnt, filesuffix, truemyidtxt);
  }
  else{ // then no file number wanted (i.e. for gdump())
    sprintf(dfnam, "%s%s%s", fileprefix, filesuffix, truemyidtxt);
  }



  ////////////////
  //
  // open files, or open files for header if mpicombine==1, which for mpicombine==1 gets reopened later by MPI routines
  //
  ///////////////

  if((USEMPI&&(myid==0)&&(mpicombine==1))||(mpicombine==0)){// for mpicombine==1 even with ROMIO, real filename and header not needed

    // only one CPU does header if mpicombine==1, header+dump done in all CPUs if mpicombine==0
    // create files for each column, or each column's header if mpicombine==1
    if((fpp=(FILE**)malloc(sizeof(FILE*)*numfiles))==NULL){
      dualfprintf(fail_file,"couldn't open fpp in dump()\n");
      myexit(2);
    }// now fpp[i] indexes a list of file pointers


    // setup each file corresponding to each column
    COLLOOP(coliter){
      if(docolsplit&&(numfiles>1)){
	sprintf(dfnamreal,"%s-col%04d",dfnam,coliter);
      }
      else strcpy(dfnamreal,dfnam);
      
      if ((fpp[coliter] = fopen(dfnamreal, filerw)) == NULL) {
	dualfprintf(fail_file, "error opening %s %s file\n",fileprefix,filesuffix);
	myexit(2);
      }
      //////////////////////////////////
      //
      //  read or write header: header is read/written in whatever style chosen to the top of each dump file created
      //
      ///////////////////////////////////
      headerfun(headerbintxt,fpp[coliter]); // outputs header to each column file (or just one file, or all CPU files, etc.)
      headerbytesize=ftell(fpp[coliter]);
    }
    
    // don't close if mpicombine==0, since used in a moment, else mpicombine==1 it's reopened by MPI routines
    if(USEMPI&&(myid==0)&&(mpicombine==1)) COLLOOP(coliter) fclose(fpp[coliter]); // will get reopened later by MPI routines


  }
  // need to broadcast the header size to other CPUs for ROMIO
#if(USEMPI&&USEROMIO)
  MPI_Bcast(&headerbytesize,1,MPI_LONG,0,MPI_COMM_WORLD);
#endif

    
  ///////////////////////////////////////////////////////////
  //
  // loop over columns for per-column buffer ROMIO dump
  //
  //
  ////////////////////////////////////////////////////////////


  ROMIOCOLLOOP(romiocoliter) { // only loop if mpicombine&&USEMPI&&USEROMIO&&docolsplit==1
    if(romiocloopend>1) trifprintf("romiocoliter=%d of romiocloopend=%d\n",romiocoliter,romiocloopend);


    
    // setup MPI buffer if mpicombine==1
    if( mpicombine == 0 ) { // then one file per CPU if USEMPI or just normal file writing on 1CPU
      writebuf=NULL;
    }
    else mpiio_init(dumpbintxt,sortedoutput, fpp, headerbytesize, readwrite, dfnam, numcolumns, datatype, &jonio, &writebuf);
    // if USEROMIO==1 then numcolumns interpreted properly for docolsplit


    if(readwrite==READFILE){
      //////////////////////////////////
      //
      // read DUMP 
      //
      //////////////////////////////////
      
      if (mpicombine == 1) {
#if(USEMPI)
	mpiio_seperate(binaryoutput,sortedoutput, STAGE1, numcolumns, datatype, fpp, jonio, writebuf);
#endif
      }
    }



    //////////////////
    //
    // DUMP LOOP
    //
    //////////////////



    if(readwrite==READFILE){
      BUFFERINIT0;
      DUMPGENLOOP {
	// buffer init starts the parallel index
	BUFFERINIT;
	// initialize to 0th column
	COLINIT;

	///////////////////////////////////////
	//
	// READFILE
	//
	//////////////////////
	if((mpicombine)&&(truempicombinetype==MPICOMBINEMINMEM)) mpiio_minmem(READFILE,whichdump,i,j,k,dumpbintxt,sortedoutput,numcolumns,datatype, fpp,jonio,writebuf);

	
	// read all at once
	myfread(dumpbintxt,datatype,setbuf,0,numcolumns,i,j,k,fpp,writebuf);
	
	// check
	if(nextbuf!=numcolumns){
	  dualfprintf(fail_file,"Number of columns (numcolumns=%d) isn't equal to number of columns/buffers attempted (nextbuf=%d)\n",numcolumns,nextbuf);
	  myexit(1);
	}
	
	// get the content of 1 row
	setgetcontent(i,j,k,datatype,setbuf);
	
	// check
	if(nextcol!=numcolumns){
	  dualfprintf(fail_file,"Number of columns (numcolumns=%d) isn't equal to number of columns attempted (nextcol=%d)\n",numcolumns,nextcol);
	  myexit(1);
	}
      }// end DUMPGENLOOP
    }// end readwrite==READFILE
    else if(readwrite==WRITEFILE){
Пример #20
0
int read_gadget_header(FILE *fp,IO_HEADER * h)
{   //read the header part, assign header extensions, and do several consistency check
//return ByteOrder
    int dummy,dummy2,n,ns,ByteOrder;

#define myfread(a,b,c,d) fread_swap(a,b,c,d,ByteOrder)

    n=sizeof(IO_HEADER);
    ns=n;
    swap_Nbyte((char *)&ns,1,sizeof(ns));

    fread(&dummy,sizeof(dummy),1,fp);

    /*=====determine byteorder======*/
    if(dummy==n)
        ByteOrder=0;
    else if(dummy==ns)
    {
        ByteOrder=1;
        printf(" --------------------------------------------------------\n");
        printf("|WARNING: Different Endianness detected! Doing ByteSwap..|\n");
        printf(" --------------------------------------------------------\n");
    }
    else
    {
        printf("header size not expected:%d;%d,%d\n",dummy,n,ns);
        exit(1);
    }

    dummy=n;

    myfread(h->npart,sizeof(int),6,fp);
    myfread(h->mass,sizeof(double),6,fp);
    myfread(&h->time,sizeof(double),1,fp);
    myfread(&h->redshift,sizeof(double),1,fp);
    myfread(&h->flag_sfr,sizeof(int),1,fp);
    myfread(&h->flag_feedback,sizeof(int),1,fp);
    myfread(h->npartTotal,sizeof(int),6,fp);
    myfread(&h->flag_cooling,sizeof(int),1,fp);
    myfread(&h->num_files,sizeof(int),1,fp);
    myfread(&h->BoxSize,sizeof(double),1,fp);
    myfread(&h->Omega0,sizeof(double),1,fp);
    myfread(&h->OmegaLambda,sizeof(double),1,fp);
    myfread(&h->HubbleParam,sizeof(double),1,fp);
    fseek(fp,n+sizeof(int),SEEK_SET);
    myfread(&dummy2,sizeof(dummy2),1,fp);
    if(dummy!=dummy2)
    {
        fprintf(stderr,"error!record brackets not match for header!\t%d,%d\n",dummy,dummy2);
        exit(1);
    }


    h->Hz=HUBBLE0 * sqrt(h->Omega0 / (h->time * h->time * h->time)
                         + (1 - h->Omega0 - h->OmegaLambda) / (h->time * h->time)
                         + h->OmegaLambda);//Hubble param for the current catalogue;

    return ByteOrder;

}
Пример #21
0
boolean
aptree_read(FILE *fp, APATNODE **root, BMALLOC_BASE **mroot, void *data, boolean (*load_data_func)(void **, void *, FILE *))
{
  int count_node, count_branch, count_data, maxbit;
  int *left, *right, *value;
  int num, did;
  boolean err;
  APATNODE *nodelist;
  APATNODE *node;
  int i;

  if (*root != NULL) {
    jlog("Error: aptree_read: root node != NULL!\n");
    return FALSE;
  }

  /* read header */
  if (myfread(&count_node, sizeof(int), 1, fp) < 1) {
    jlog("Error: aptree_read: fail to read header\n");
    return FALSE;
  }
  if (myfread(&count_data, sizeof(int), 1, fp) < 1) {
    jlog("Error: aptree_read: fail to read header\n");
    return FALSE;
  }
  jlog("Stat: aptree_read: %d nodes (%d branch + %d data)\n",
       count_node, count_node - count_data, count_data);
  /* prepare buffer */
  left = (int *)mymalloc(sizeof(int) * count_node);
  right = (int *)mymalloc(sizeof(int) * count_node);
  value = (int *)mymalloc(sizeof(int) * count_node);
  /* read data */
  if (myfread(left, sizeof(int), count_node, fp) < count_node) {
    jlog("Error: aptree_read: fail to read %d bytes\n", sizeof(int) * count_node);
    return FALSE;
  }
  if (myfread(right, sizeof(int), count_node, fp) < count_node) {
    jlog("Error: aptree_read: fail to read %d bytes\n", sizeof(int) * count_node);
    return FALSE;
  }
  if (myfread(value, sizeof(int), count_node, fp) < count_node) {
    jlog("Error: aptree_read: fail to read %d bytes\n", sizeof(int) * count_node);
    return FALSE;
  }
  /* allocate nodes */
  nodelist = (APATNODE *)mybmalloc2(sizeof(APATNODE) * count_node, mroot);
  for(i=0;i<count_node;i++) {
    node = &(nodelist[i]);
    if (left[i] == -1) {
      node->left0 = NULL;
    } else {
      node->left0 = &(nodelist[left[i]]);
    }
    if (right[i] == -1) {
      node->right1 = NULL;
    } else {
      node->right1 = &(nodelist[right[i]]);
    }
    if (left[i] == -1 && right[i] == -1) {
      /* load leaf data node */
      if ((*load_data_func)(&(node->value.data), data, fp) == FALSE) {
	jlog("Error: aptree_read: failed to load leaf data entity\n");
	return FALSE;
      }
    } else {
      /* set thres bit */
	node->value.thres_bit = value[i];
    }
  }
  /* set root node */
  *root = &(nodelist[0]);
  
  free(value);
  free(right);
  free(left);

  return TRUE;
}
Пример #22
0
void compress(void)
{
	int i1, i2;
	printit(0);
	clearTimeout(void);
	printf("\nsorting buffer ............ ");

	qsort( A, I, sizeof(tb), bsortkey );

	puts("done");
	printf("compressing buffer ........ ");
	i1=0; i2=1;
	while( i2 != I )
	{
		while( i2 != I
		    && A[i1].hashboard == A[i2].hashboard
		    && A[i1].move == A[i2].move )
		{
			A[i1].n += A[i2].n;
			i2++;
		}
		if( i2 != I )
		{ i1++; A[i1]=A[i2]; i2++; }
	}
	I=i1+1;
	A[I].n = 0;
	printf("done, buffer usage=%i (%i%%)\n",I,100*I/Asize);

	/** Now, take tb1 file and the memory buffer and join them to
        *** the tb2 (rbook.phalanx.tmp).  Rename tb2 to tb1.
	**/
	{
		int i=0;
		int done_m, done_f;
		tb mtb, ftb;

		printf("adding to file ............ ");
		tb1=fopen("rbook.phalanx","rb");
		tb2=fopen("rbook.phalanx.tmp","wb");
		rewind(tb1);

		mtb=A[0]; i=1; done_m=(i==I);
		myfread( &ftb, sizeof(tb), tb1 ); done_f=feof(tb1);

		while( !done_f || !done_m )
		{
			if( !done_m && !done_f && bsortkey(&mtb,&ftb)==0 )
			{
				mtb.n += ftb.n;
				myfwrite(&mtb,sizeof(tb),tb2);
				mtb=A[i]; i++; done_m=(i==I);
				myfread(&ftb,sizeof(tb),tb1); done_f=feof(tb1);
			}
			else
			if( !done_m && ( done_f || bsortkey(&mtb,&ftb)==-1 ) )
			{
				myfwrite(&mtb,sizeof(tb),tb2);
				mtb=A[i]; i++; done_m=(i==I);
			}
			else
			if( !done_f && ( done_m || bsortkey(&mtb,&ftb)==1 ) )
			{
				myfwrite(&ftb,sizeof(tb),tb2);
				myfread(&ftb,sizeof(tb),tb1); done_f=feof(tb1);
			}
		}

		/* good, it's there, close the two files, move newly
		 * joined to the first one and reopen */

		fclose(tb1); fclose(tb2);
		rename("rbook.phalanx.tmp","rbook.phalanx");
		printf("done\n");
	}

	I=0;

	setTimeout(printit, 5);
}
Пример #23
0
/* this routine loads particle data from Gadget's default
 * binary file format. (A snapshot may be distributed
 * into multiple files). use the bitwise loadflags to 
 * determine which part of the data to load. the last 3 bits
 * of loadflags encodes whether to load vel, pos and id.
 */
void load_particle_data_bypart(HBTInt Nsnap, char *SnapPath, unsigned char loadflags)
{
	unsigned char flag_id,flag_pos,flag_vel;
	int dummy,dummy2,ByteOrder;
	long int pre_len,tail_len;
	FILE *fp;
	char buf[1024];
	HBTInt i,j;
	HBTInt Nload=0;
	struct 
	{
	IDatInt *PID;	
	IDatReal (*Pos)[3];
	IDatReal (*Vel)[3];
	} IDat; //input particle data, temporary.
	
	/*loadflags encodes the flag to load id,pos,vel in its lowest,second lowest and third lowest bit */
	flag_id=get_bit(loadflags,0);
	flag_pos=get_bit(loadflags,1);
	flag_vel=get_bit(loadflags,2);
	
	IDat.PID=NULL;
	IDat.Pos=NULL;
	IDat.Vel=NULL;
	if(flag_id)
	IDat.PID=mymalloc(sizeof(IDatInt)*NP_DM);
	if(flag_pos)
	IDat.Pos=mymalloc(sizeof(IDatReal)*3*NP_DM);
	if(flag_vel)
	IDat.Vel=mymalloc(sizeof(IDatReal)*3*NP_DM);
	
	Pdat.Nsnap=Nsnap;
	#ifdef SNAPLIST
	Nsnap=snaplist[Nsnap];
	#endif

  for(i=0; i<NFILES; i++)
    {	
	sprintf(buf,"%s/snapdir_%03d/%s_%03d.%d",SnapPath,(int)Nsnap,SNAPFILE_BASE,(int)Nsnap,(int)i);
    if(1==NFILES)
	 if(!try_readfile(buf))	sprintf(buf,"%s/%s_%03d",SnapPath,SNAPFILE_BASE,(int)Nsnap); //try the other convention
	
	if(!try_readfile(buf))	sprintf(buf,"%s/%s_%03d.%d",SnapPath,SNAPFILE_BASE,(int)Nsnap,(int)i); //try the other convention
	if(!try_readfile(buf))	sprintf(buf,"%s/%d/%s.%d",SnapPath,(int)Nsnap,SNAPFILE_BASE,(int)i);//for BJL's RAMSES output
    
	  myfopen(fp,buf,"r");

      ByteOrder=read_gadget_header(fp,&header);
	  header.Nsnap=Nsnap;
	  
	  pre_len=header.npart[0]*sizeof(IDatReal)*3;
	  for(j=2,tail_len=0;j<6;j++)tail_len+=header.npart[j];
	  tail_len*=sizeof(IDatReal)*3;
      SKIP;
	  fseek(fp,pre_len,SEEK_CUR);//load only DM data;
	  if(flag_pos)
	  myfread(IDat.Pos+Nload,sizeof(IDatReal)*3,header.npart[1],fp);
	  else
	  fseek(fp,sizeof(IDatReal)*3*header.npart[1],SEEK_CUR);
	  fseek(fp,tail_len,SEEK_CUR);
      SKIP2;
	  CHECK;
	
      SKIP;
      fseek(fp,pre_len,SEEK_CUR);//load only DM data;
	  if(flag_vel)
	  myfread(IDat.Vel+Nload,sizeof(IDatReal)*3,header.npart[1],fp);
	  else
	  fseek(fp,sizeof(IDatReal)*3*header.npart[1],SEEK_CUR);
	  fseek(fp,tail_len,SEEK_CUR);
      SKIP2;
	  CHECK;
      
#ifndef NO_ID_RECORD //for Huiyuan's data.
	  pre_len=header.npart[0]*sizeof(IDatInt);
	  for(j=2,tail_len=0;j<6;j++)tail_len+=header.npart[j];
	  tail_len*=sizeof(IDatInt);
	  SKIP;
      fseek(fp,pre_len,SEEK_CUR);//load only DM data;
	  if(flag_id)
	  myfread(IDat.PID+Nload,sizeof(IDatInt),header.npart[1],fp);
	  else
	  fseek(fp,sizeof(IDatInt)*header.npart[1],SEEK_CUR);
	  fseek(fp,tail_len,SEEK_CUR);
      SKIP2;
	  CHECK;
#endif	  
	  
	  if(feof(fp))
	  {
		fprintf(logfile,"error:End-of-File in %s\n",buf);
		fflush(logfile);exit(1);  
	  }
	
	  Nload+=header.npart[1];
     
      fclose(fp);
  }
  if(NP_DM!=Nload)
  {
	  fprintf(logfile,"error: Number of loaded DM particles mismatch: %lld,%lld\n",(long long)NP_DM,(long long)Nload);
	  fflush(logfile);
	  exit(1);
  }

if(flag_id)
{
//now transfer to HBT's internal data  
  #ifdef HBTPID_RANKSTYLE
  struct io_ID2Ind *table;
  table=mymalloc(sizeof(struct io_ID2Ind)*NP_DM);
  for(i=0;i<NP_DM;i++)
  {
	  table[i].PID=IDat.PID[i];
	  table[i].PInd=i;
  }
  qsort(table,NP_DM,sizeof(struct io_ID2Ind),comp_PIDArr);
  Pdat.PID=mymalloc(sizeof(HBTInt)*NP_DM);
  for(i=0;i<NP_DM;i++)
	Pdat.PID[table[i].PInd]=i;  //now PID has been turned into particle ranks
	
  sprintf(buf,"%s/DM_PIDs_Sorted.dat",SUBCAT_DIR);
  if(!try_readfile(buf))//create the file if it does not exist
  {
	  IDatInt np;
	  myfopen(fp,buf,"w");
	  np=NP_DM;
	  fwrite(&np,sizeof(IDatInt),1,fp);
	  for(i=0;i<NP_DM;i++)
	  fwrite(&(table[i].PID),sizeof(IDatInt),1,fp);
	  fwrite(&np,sizeof(IDatInt),1,fp);
	  fclose(fp);
  }
	
  myfree(table);  
  myfree(IDat.PID);
  #else
#ifdef SAME_INTTYPE
  Pdat.PID=IDat.PID;
#else
  Pdat.PID=mymalloc(sizeof(HBTInt)*NP_DM);
  for(i=0;i<NP_DM;i++)
	Pdat.PID[i]=IDat.PID[i];
  myfree(IDat.PID);
#endif
  #endif
}
else
Pdat.PID=NULL;

if(flag_pos)
{
#ifdef SAME_REALTYPE
	  Pdat.Pos=IDat.Pos;
#else
	  Pdat.Pos=mymalloc(sizeof(HBTxyz)*NP_DM);
	  for(i=0;i<NP_DM;i++)
		for(j=0;j<3;j++)
			Pdat.Pos[i][j]=IDat.Pos[i][j];
	  myfree(IDat.Pos);
#endif
  	
  #ifdef CONVERT_LENGTH_MPC_KPC
  for(i=0;i<NP_DM;i++)
  for(j=0;j<3;j++)
  Pdat.Pos[i][j]*=1000.;
  #endif
  
  #ifdef PERIODIC_BDR
  for(i=0;i<NP_DM;i++)
  for(j=0;j<3;j++)
  Pdat.Pos[i][j]=position_modulus(Pdat.Pos[i][j]);
  #endif	
}
else
Pdat.Pos=NULL;

if(flag_vel)
{  
  #ifdef SAME_REALTYPE
	  Pdat.Vel=IDat.Vel;
#else
	  Pdat.Vel=mymalloc(sizeof(HBTxyz)*NP_DM);
	  for(i=0;i<NP_DM;i++)
		for(j=0;j<3;j++)
			Pdat.Vel[i][j]=IDat.Vel[i][j];
	  myfree(IDat.Vel);
#endif
}
else
Pdat.Vel=NULL;

}
Пример #24
0
Файл: tee.c Проект: 1heart/vim
void
main(int argc, char *argv[])
{
	int	append = 0;
	int	numfiles;
	int	opt;
	int	maxfiles;
	FILE	**filepointers;
	int	i;
	char	buf[BUFSIZ];
	int	n;
	int	optind = 1;

	for (i = 1; i < argc; i++)
	{
		if (argv[i][0] != '-')
			break;
		if (!strcmp(argv[i], "-a"))
			append++;
		else
			usage();
		optind++;
	}

	numfiles = argc - optind;

	if (numfiles == 0)
	{
		fprintf(stderr, "doesn't make much sense using tee without any file name arguments...\n");
		usage();
		exit(2);
	}

	maxfiles = sysconf(_SC_OPEN_MAX);	/* or fill in 10 or so */
	if (maxfiles < 0)
		maxfiles = 10;
	if (numfiles + 3 > maxfiles)	/* +3 accounts for stdin, out, err */
	{
		fprintf(stderr, "Sorry, there is a limit of max %d files.\n", maxfiles - 3);
		exit(1);
	}
	filepointers = calloc(numfiles, sizeof(FILE *));
	if (filepointers == NULL)
	{
		fprintf(stderr, "Error allocating memory for %d files\n", numfiles);
		exit(1);
	}
	for (i = 0; i < numfiles; i++)
	{
		filepointers[i] = fopen(argv[i+optind], append ? "ab" : "wb");
		if (filepointers[i] == NULL)
		{
			fprintf(stderr, "Can't open \"%s\"\n", argv[i+optind]);
			exit(1);
		}
	}
	setmode(fileno(stdin),  O_BINARY);
	fflush(stdout);	/* needed for _fsetmode(stdout) */
	setmode(fileno(stdout),  O_BINARY);

	while ((n = myfread(buf, sizeof(char), sizeof(buf), stdin)) > 0)
	{
		fwrite(buf, sizeof(char), n, stdout);
		fflush(stdout);
		for (i = 0; i < numfiles; i++)
		{
			if (filepointers[i] &&
			     fwrite(buf, sizeof(char), n, filepointers[i]) != n)
			{
				fprintf(stderr, "Error writing to file \"%s\"\n", argv[i+optind]);
				fclose(filepointers[i]);
				filepointers[i] = NULL;
			}
		}
	}
	for (i = 0; i < numfiles; i++)
	{
		if (filepointers[i])
			fclose(filepointers[i]);
	}

	exit(0);
}
Пример #25
0
void load_all_auxdata(int filenr)
{
  char buf[1000];
  FILE *fd;
  struct stat filestatus;
  int SnapShotInFileName, AuxNtotHalos, AuxTotIds;
  size_t bytes;



  //if def MCMC and PARALLEL only task 0 reads the representative treefile, then broadcasts
#ifdef PARALLEL
#ifdef MCMC
  if(ThisTask==0)
  {
  	printf("Task 0 reading aux data\n");
#endif
#endif
  SnapShotInFileName=LastDarkMatterSnapShot;

#ifdef MCMC
#ifdef MR_PLUS_MRII
  SnapShotInFileName=LastDarkMatterSnapShot_MRII;
#endif
#endif

#ifndef MRII
  sprintf(buf, "%s/treedata/treeaux_%03d.%d", SimulationDir, SnapShotInFileName, filenr);
#else
  sprintf(buf, "%s/treedata/treeaux_sf1_%03d.%d", SimulationDir, SnapShotInFileName, filenr);
#endif


  if(stat(buf, &filestatus) != 0)                  /* seems not to exist */
    {
      char sbuf[1000];
      sprintf(sbuf, "Can't open file `%s'\n", buf);
      terminate(sbuf);
    }

  if(!(fd = fopen(buf, "r")))
    {
      char sbuf[1000];
      sprintf(sbuf, "Can't open file `%s'\n", buf);
      terminate(sbuf);
    }

  bytes = filestatus.st_size;
  TreeAuxData = mymalloc("TreeAuxData", bytes);

  myfread(TreeAuxData, 1, bytes, fd);

  fclose(fd);

#ifdef PARALLEL
#ifdef MCMC
  } //end if ThisTask==0

  if(ThisTask==0)
  	printf("aux data read  by task %d\n", ThisTask);

  MPI_Barrier(MPI_COMM_WORLD);

  MPI_Bcast(&bytes, sizeof(size_t), MPI_BYTE, 0, MPI_COMM_WORLD);
  if(ThisTask>0)
  	TreeAuxData = mymalloc("TreeAuxData", bytes);

  if(ThisTask==0)
  	printf("broadcasting aux data\n", ThisTask);

  //MPI_BCast has a limit of 2Gb so everything needs to be passed in smaller chunks
  int ii, Nmessages=10000;
  long long  MsgSizeInBytes=10000000; //chunks of 10MsgSizeInBytes
  for(ii=0;ii<Nmessages;ii++)
  {
  	//if next chunk is outside of array size, just pass whats left and then exit the loop
  	if((ii+1)*MsgSizeInBytes>bytes)
  	{
  		MPI_Bcast(&TreeAuxData[ii*MsgSizeInBytes],bytes-ii*MsgSizeInBytes, MPI_BYTE, 0, MPI_COMM_WORLD);
  		break;
  	}
  	else
  		MPI_Bcast(&TreeAuxData[ii*MsgSizeInBytes],MsgSizeInBytes, MPI_BYTE, 0, MPI_COMM_WORLD);
  	//MPI_Bcast(&TreeAuxData[ii*bytes/Nmessages],bytes/Nmessages, MPI_BYTE, 0, MPI_COMM_WORLD);
  }

  if(ThisTask==0)
  	printf("done broadcasting aux data\n", ThisTask);

  MPI_Barrier(MPI_COMM_WORLD);
#endif
#endif

  int *header = TreeAuxData;
  NtotHalos = header[0];
  TotIds = header[1];
  Ntrees = header[2];
  TotSnaps = header[3];

  CountIDs_snaptree = header + 4 + 2 * TotSnaps;
  OffsetIDs_snaptree = CountIDs_snaptree +  TotSnaps * Ntrees;
  CountIDs_halo = OffsetIDs_snaptree + TotSnaps * Ntrees;
  OffsetIDs_halo = CountIDs_halo + NtotHalos;


}