コード例 #1
0
ファイル: wiicursor.cpp プロジェクト: abom/linux-whiteboard
void WiiCursor::right_click_thread_func() {
    WiiCursorThreadData& data = m_thread_data; // Readability

    // Sets up the timer
    delta_t_t last_time = 0;
    get_delta_t(last_time);

    // Sets up itself
    data.moved = 0;
    data.waited = 0;

    // Readability
    WiiEvents& wii_events = data.events;
    WiiEventData& wii_event_data = data.event_data;
    while ( data.all_running() ) {
	if ( data.right_click() ) {
		wii_events.right_button_down(wii_event_data);
	    break;
	}
	else data.waited += get_delta_t(last_time);

	if ( data.click_and_drag() ) {
		wii_events.begin_click_and_drag(wii_event_data);
	    break;
	}
	else {
	    // WARNING: A locking mechanism is needed to avoid this hack
	    // data.m_ir.x should never be invalid
	    // m_ir must not be read and updated at the same time in this function and in process()
	    if (data.ir.x != INVALID_IR_POS)
		data.moved = squared_distance(data.ir, data.ir_on_mouse_down);
	}
    }
}
コード例 #2
0
static double
get_velocity_delta_t()
{
	static double previous_t = 0.0;

	return (get_delta_t(&previous_t));
}
コード例 #3
0
static double
get_steering_delta_t()
{
	static double previous_t = 0.0;

	return (get_delta_t(&previous_t));
}
コード例 #4
0
ファイル: desc-heap.c プロジェクト: mono/heap-prof
static void
check_point (MonoProfiler* p, char* reason)
{
	int i;
	
	printf ("Checkpoint at %d for %s\n", get_delta_t (p), reason);
	
	for (i = 0; i < p->klass_table->len; i ++) {
		if (p->type_live_data [i] > (.01 * p->total_live_bytes))
			printf ("   %s : %d\n", g_ptr_array_index (p->klass_table, i), p->type_live_data [i]);
	}
}
コード例 #5
0
ファイル: desc-heap.c プロジェクト: mono/heap-prof
static void
prof_heap_resize (MonoProfiler *p, gint64 new_size)
{
	printf ("heap resized to %lld @ %d\n", new_size, get_delta_t (p));
	check_point (p, "heap-resize");
}
コード例 #6
0
ファイル: main.c プロジェクト: abocquet/Projet-PPE
int main(int argc, const char * argv[]) {
	
	setup_log();
	
	double destination[3] = {0, 5, 3} ;
	
	double vitesse[3] = {0} ;
	double position[3] = {0} ;
	
	double propulsion_verticale = 0, propulsion_horizontale = 0 ;
	
	int  i = 1;
	
	do {
		double delta_t = get_delta_t(); ;
		
		double acceleration[3];
		double deplacement[3] = { 0 };
		
		acquerir_acceleration(acceleration);
		
		double v0[3];
		matrix_col_copy(vitesse, v0);
		
		// vitesse = acceleration * ∆t + v0
			matrix_k(acceleration, delta_t, acceleration);
			matrix_col_sum(acceleration, v0, vitesse);
		
		// vitesse = 1/2 . acceleration * ∆t^2 + v0 . ∆t
			matrix_k(acceleration, delta_t, acceleration);
			matrix_k(acceleration, 0.5, acceleration);
		
			matrix_k(v0, delta_t, v0);
		
			matrix_col_sum(deplacement, acceleration, deplacement);
			matrix_col_sum(deplacement, v0, deplacement);
		
		//On calcul la nouvelle position du dirigeable
			double θ = acquerir_angle_nord() / (2 * PI) ; //conversion degré/radians
		
			double matrice_passage[3][3] = {
				{ cos(θ), sin(θ),  0 },
				{ -sin(θ), cos(θ), 0 },
				{	0	 ,	0	 , 1 }
			};
		
			matrix_product(deplacement, matrice_passage, deplacement); //on calcul les coordonées de d dans (i,j)
			matrix_col_sum(deplacement, position, position); // on ajoute le déplacement à la position du dirigeable
		
		// et on agit en fonction
		
			//on inverse la matrice de passage afin d'aller de (i, j) vers (k, l)
			matrice_passage[0][1] *= -1 ;
			matrice_passage[1][0] *= -1 ;
		
			//les coordonées de la destination, dans le référentiel du ballon
			double dest[3] ;
			matrix_col_copy(destination, dest) ;
			matrix_col_diff(dest, position, dest);
			matrix_product(dest, matrice_passage, dest);
		
			char tourner = 0 ;
			double angle_destination = atan(dest[0]/dest[1]);
			double distance_destination = sqrt(pow(dest[0], 2) + pow(dest[1], 2));
		
			// Dans le plan (x, y)
			if(distance_destination > 0.1){
				if(dest[1] > 0 && isClose(angle_destination, 0, PI/16)){
					if(vitesse[1] > .1 && acceleration[1] > 0){
						propulsion_horizontale -= .1 ;
					}
						
					if(vitesse[1] < .1){
						propulsion_horizontale += .1 ;
					}
				}
				else {
					propulsion_horizontale = .2 ;
					
					if(dest[0] < 0){
						tourner = -1 ;
					}
					else if(dest[0] > 0){
						tourner = 1 ;
					}
				}
			}
			else {
				if (vitesse[1] > 0.01) { //on ralenti à l'approche de l'objectif
					propulsion_horizontale = -7 ;
				}
				else {
					propulsion_horizontale = 0 ;
				}
			}
		
		
			// on adapte la propulsion verticale
			if(vitesse[2] > 0.1){
				propulsion_verticale-- ;
				if(vitesse[2] > 1){
					propulsion_verticale -= 10;
				}
			}
			else if(vitesse[2] < - 0.1){
				propulsion_verticale++ ;
			
				if(vitesse[2] < -1){
					propulsion_verticale += 10;
				}
			}
			else if(position[2] < destination[2]){
				propulsion_verticale++;
			}
			else if (position[2] > destination[2]){
				propulsion_verticale-- ;
			}
	
		
			//On réajuste ensuite les demandes moteur
				if(propulsion_verticale > 100){
					propulsion_verticale = 100 ;
				}
				else if(propulsion_verticale < 1){
					propulsion_verticale = 1 ;
				}
		
				if(propulsion_horizontale > 100){
					propulsion_horizontale = 100 ;
				}
				else if(propulsion_horizontale < -100){
					propulsion_horizontale = -100 ;
				}
		
			double norme = sqrt(pow(propulsion_verticale,2) + pow(propulsion_horizontale,2)) ;
		
			// si on demande une trop forte poussée aux moteurs, on réajuste la demande
			// en donnant la priorité à la poussée verticale
			if(norme > 100){
				norme = 100 ;
				int signe = propulsion_horizontale < 0 ? -1 : 1 ;
				propulsion_horizontale = signe * sqrt(pow(norme, 2) - pow(propulsion_verticale,2)) ;
				
			}
		
			double angle_moteur_gauche = atan(propulsion_horizontale / propulsion_verticale) ;
			double angle_moteur_droit = angle_moteur_gauche ;
		
			if(tourner != 0){
				angle_moteur_gauche *= tourner ;
				angle_moteur_droit *= -tourner ;
			}
		
			adapter_propulsion(norme, angle_moteur_gauche, angle_moteur_droit);
		
		if(i % 5 == 0){ //On complète le log toutes les 5 périodes
			add_log(acceleration, vitesse, position, dest, propulsion_verticale, propulsion_horizontale, angle_moteur_gauche, angle_moteur_droit, θ);
		}
		
	} while(i++ < 1200) ;
		
	matrix_col_print(position);
	close_log();
	
    return 0;
}
コード例 #7
0
ファイル: Bonnie.c プロジェクト: B-Rich/afterburner
main(
  int    argc,
  char * argv[])
{
  int    buf[Chunk / IntSize];
  int    bufindex;
  int    chars[256];
  int    child;
  char * dir;
  int    html = 0;
  int    fd;
  double first_start;
  double last_stop;
  int    lseek_count = 0;
  char * machine;
  char   name[Chunk];
  int    next;
  int    seek_control[2];
  int    seek_feedback[2];
  char   seek_tickets[Seeks + SeekProcCount];
  double seeker_report[3];
  off_t  size;
  FILE * stream;
  off_t  words;

  fd = -1;
  basetime = (int) time((time_t *) NULL);
  size = 100;
  dir = ".";
  machine = "";

  /* pick apart args */
  for (next = 1; next < argc; next++)
    if (strcmp(argv[next], "-d") == 0)
      dir = argv[++next];
    else if (strcmp(argv[next], "-s") == 0)
      size = atol(argv[++next]);
    else if (strcmp(argv[next], "-m") == 0)
      machine = argv[++next];
    else if (strcmp(argv[next], "-html") == 0)
      html = 1;
    else
      usage();

  if (size < 1)
    usage();

  /* sanity check - 32-bit machines can't handle more than 2047 Mb */
  if (sizeof(off_t) <= 4 && size > 2047)
  {
    fprintf(stderr, "File too large for 32-bit machine, sorry\n");
    exit(1);
  }

  sprintf(name, "%s/Bonnie.%d", dir, getpid());

  /* size is in meg, rounded down to multiple of Chunk */
  size *= (1024 * 1024);
  size = Chunk * (size / Chunk);
  fprintf(stderr, "File '%s', size: %ld\n", name, size);

  /* Fill up a file, writing it a char at a time with the stdio putc() call */
  fprintf(stderr, "Writing with putc()...");
  newfile(name, &fd, &stream, 1);
  timestamp();
  for (words = 0; words < size; words++)
    if (putc(words & 0x7f, stream) == EOF)
      io_error("putc");
  
  /*
   * note that we always close the file before measuring time, in an
   *  effort to force as much of the I/O out as we can
   */
  if (fclose(stream) == -1)
    io_error("fclose after putc");
  get_delta_t(Putc);
  fprintf(stderr, "done\n");

  /* Now read & rewrite it using block I/O.  Dirty one word in each block */
  newfile(name, &fd, &stream, 0);
  if (lseek(fd, (off_t) 0, 0) == (off_t) -1)
    io_error("lseek(2) before rewrite");
  fprintf(stderr, "Rewriting...");
  timestamp();
  bufindex = 0;
  if ((words = read(fd, (char *) buf, Chunk)) == -1)
    io_error("rewrite read");
  while (words == Chunk)
  { /* while we can read a block */
    if (bufindex == Chunk / IntSize)
      bufindex = 0;
    buf[bufindex++]++;
    if (lseek(fd, (off_t) -words, 1) == -1)
      io_error("relative lseek(2)");
    if (write(fd, (char *) buf, words) == -1)
      io_error("re write(2)");
    if ((words = read(fd, (char *) buf, Chunk)) == -1)
      io_error("rwrite read");
  } /* while we can read a block */
  if (close(fd) == -1)
    io_error("close after rewrite");
  get_delta_t(ReWrite);
  fprintf(stderr, "done\n");

  /* Write the whole file from scratch, again, with block I/O */
  newfile(name, &fd, &stream, 1);
  fprintf(stderr, "Writing intelligently...");
  for (words = 0; words < Chunk / IntSize; words++)
    buf[words] = 0;
  timestamp();
  for (words = bufindex = 0; words < (size / Chunk); words++)
  { /* for each word */
    if (bufindex == (Chunk / IntSize))
      bufindex = 0;
    buf[bufindex++]++;
    if (write(fd, (char *) buf, Chunk) == -1)
      io_error("write(2)");
  } /* for each word */
  if (close(fd) == -1)
    io_error("close after fast write");
  get_delta_t(FastWrite);
  fprintf(stderr, "done\n");

  /* read them all back with getc() */
  newfile(name, &fd, &stream, 0);
  for (words = 0; words < 256; words++)
    chars[words] = 0;
  fprintf(stderr, "Reading with getc()...");
  timestamp();
  for (words = 0; words < size; words++)
  { /* for each byte */
    if ((next = getc(stream)) == EOF)
      io_error("getc(3)");

    /* just to fool optimizers */
    chars[next]++;
  } /* for each byte */
  if (fclose(stream) == -1)
    io_error("fclose after getc");
  get_delta_t(Getc);
  fprintf(stderr, "done\n");

  /* use the frequency count */
  for (words = 0; words < 256; words++)
    sprintf((char *) buf, "%d", chars[words]);

  /* Now suck it in, Chunk at a time, as fast as we can */
  newfile(name, &fd, &stream, 0);
  if (lseek(fd, (off_t) 0, 0) == -1)
    io_error("lseek before read");
  fprintf(stderr, "Reading intelligently...");
  timestamp();
  do
  { /* per block */
    if ((words = read(fd, (char *) buf, Chunk)) == -1)
      io_error("read(2)");
    chars[buf[abs(buf[0]) % (Chunk / IntSize)] & 0x7f]++;
  } /* per block */
  while (words);
  if (close(fd) == -1)
    io_error("close after read");
  get_delta_t(FastRead);
  fprintf(stderr, "done\n");

  /* use the frequency count */
  for (words = 0; words < 256; words++)
    sprintf((char *) buf, "%d", chars[words]);

  /*
   * Now test random seeks; first, set up for communicating with children.
   * The object of the game is to do "Seeks" lseek() calls as quickly
   *  as possible.  So we'll farm them out among SeekProcCount processes.
   *  We'll control them by writing 1-byte tickets down a pipe which
   *  the children all read.  We write "Seeks" bytes with val 1, whichever
   *  child happens to get them does it and the right number of seeks get
   *  done.
   * The idea is that since the write() of the tickets is probably
   *  atomic, the parent process likely won't get scheduled while the
   *  children are seeking away.  If you draw a picture of the likely
   *  timelines for three children, it seems likely that the seeks will
   *  overlap very nicely with the process scheduling with the effect
   *  that there will *always* be a seek() outstanding on the file.
   * Question: should the file be opened *before* the fork, so that
   *  all the children are lseeking on the same underlying file object?
   */
  if (pipe(seek_feedback) == -1 || pipe(seek_control) == -1)
    io_error("pipe");
  for (next = 0; next < Seeks; next++)
    seek_tickets[next] = 1;
  for ( ; next < (Seeks + SeekProcCount); next++)
    seek_tickets[next] = 0;

  /* launch some parallel seek processes */
  for (next = 0; next < SeekProcCount; next++)
  { /* for each seek proc */
    if ((child = fork()) == -1)
      io_error("fork");
    else if (child == 0)
    { /* child process */

      /* set up and wait for the go-ahead */
      close(seek_feedback[0]);
      close(seek_control[1]);
      newfile(name, &fd, &stream, 0);
      srandom(getpid());
      fprintf(stderr, "Seeker %d...", next + 1);

      /* wait for the go-ahead */
      if (read(seek_control[0], seek_tickets, 1) != 1)
	io_error("read ticket");
      timestamp();
      seeker_report[StartTime] = time_so_far();

      /* loop until we read a 0 ticket back from our parent */
      while(seek_tickets[0])
      { /* until Mom says stop */
        doseek((long) (random() % (size / Chunk)), fd,
	  ((lseek_count++ % UpdateSeek) == 0));
	if (read(seek_control[0], seek_tickets, 1) != 1)
	  io_error("read ticket");
      } /* until Mom says stop */
      if (close(fd) == -1)
        io_error("close after seek");

      /* report to parent */
      get_delta_t(Lseek);
      seeker_report[EndTime] = time_so_far();
      seeker_report[CPU] = delta[(int) Lseek][CPU];
      if (write(seek_feedback[1], seeker_report, sizeof(seeker_report))
          != sizeof(seeker_report))
        io_error("pipe write");
      exit(0);
    } /* child process */
  } /* for each seek proc */

  /*
   * Back in the parent; in an effort to ensure the children get an even
   *  start, wait a few seconds for them to get scheduled, open their
   *  files & so on.
   */
  close(seek_feedback[1]);
  close(seek_control[0]);
  sleep(5);
  fprintf(stderr, "start 'em...");
  if (write(seek_control[1], seek_tickets, sizeof(seek_tickets)) 
      != sizeof(seek_tickets))
    io_error("write tickets");
  
  /* read back from children */
  for (next = 0; next < SeekProcCount; next++)
  { /* for each child */
    if (read(seek_feedback[0], (char *) seeker_report, sizeof(seeker_report))
        != sizeof(seeker_report))
      io_error("pipe read");

    /*
     * each child writes back its CPU, start & end times.  The elapsed time 
     *  to do all the seeks is the time the first child started until the 
     *  time the last child stopped
     */
    delta[(int) Lseek][CPU] += seeker_report[CPU];
    if (next == 0)
    { /* first time */
      first_start = seeker_report[StartTime];
      last_stop = seeker_report[EndTime];
    } /* first time */
    else
    { /* not first time */
      first_start = (first_start < seeker_report[StartTime]) ?
	first_start : seeker_report[StartTime]; 
      last_stop = (last_stop > seeker_report[EndTime]) ?
	last_stop : seeker_report[EndTime]; 
    } /* not first time */
    if (wait(&child) == -1)
      io_error("wait");
    fprintf(stderr, "done...");
  } /* for each child */
  fprintf(stderr, "\n");
  delta[(int) Lseek][Elapsed] = last_stop - first_start;

  if (html)
    write_html(machine, size);
  else
    report(machine, size);
  unlink(name);
}
コード例 #8
0
ファイル: fringestop.c プロジェクト: gsbuser/code
int main(int argc, char* argv[]) {


  //  double PNT_RA, PNT_DEC;
  FILE *fp;
  static int fdin, fdout, fdonegate, fdoutac, fdoutvar, rank;
  char fname[80],fntimes[80],fnin[80];
  ANTCOORDS coords[NPOD];
  int i,ii,jj, re, is, bi, iepoch, t_index;
  int fstart=0, nf=(NF/NNODE), tstart=0;  
  double TJD[NEPOCH_READ], GST[NEPOCH_READ];
  double RA_pt[NEPOCH_READ], Dec_pt[NEPOCH_READ];
  char fn[80],fnrebin[80],fnout[80],fnonegate[80], fntmp[80];
  static float fcross[NGATE][NCORR][FFTLEN/NNODE/2][2];
  static float fcross_cum[NGATE][NCORR+NPAD][FFTLEN/NNODE/2][2];
  static float cross[NGATE][NCROSS];
  static double cross0[NGATE][NCROSS];
  static int count_cross0[NGATE][NCROSS];
  static float cross_onegate[ONEGATE][NCROSS];
  static float fcross_onegate[ONEGATE][NCORR][FFTLEN/NNODE/2][2];
  static float fcross_tmp[NCORR*FFTLEN/NNODE];
  static float fcross_var[NCORR*FFTLENOUT/NNODE/2];
  ssize_t size;
  int ierr=0;
  char lmap[NFOLD][FFTLEN/16/NNODE];
  time_t current_time;

  struct timeval4
  {
    int tv_sec;
    int tv_usec;
  };

  //ierr=feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );
  //printf("ierr=%d exception=%d\n",ierr,fegetexcept ());

  MPI_Init(NULL,NULL);
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  MPI_Comm_size(MPI_COMM_WORLD,&size);
  if (rank == 0) {
    printf("rank=%d,size=%d NEPOCH=%d OBS_EPOCH=%f\n",rank,size,NEPOCH,OBS_EPOCH);
    if (NNODE != size) exit(-1);
    if (NGATE*(NCORR+NPAD)%NNODE!=0) {
      fprintf(stderr,"error: nnode must divide NGATE*NCORR\n");
      exit(-1);
    }
    if (argc != 4) {
      printf("argc=%d ",argc);
      for (ii=0;ii<argc;ii++) printf("argv=%s ",argv[ii]);
      fprintf(stderr,"usage: %s corrfile.in corrfile.out fsall.rebin.out\n",argv[0]);
      exit(-1);
    }
  }
  omp_set_num_threads(8);

  sprintf(fn,"%s.node%%d",argv[1]);
  sprintf(fnin,fn,rank);
  fdin=open(fnin,O_RDONLY);
  if (fdin<0) {
    fprintf(stderr,"rank=%d fdin=%s\n",rank, fn);
    fflush(stderr);
    perror("open corrin.dat");
    exit(-1);
  }

  /* determine output files */
  sprintf(fn,argv[2]);
#ifdef WRITELOCAL
  char *fnbase="%s.node%d.fsall";  
  sprintf(fnout,fnbase,fn,rank);
  fdout=open(fnout,O_TRUNC|O_CREAT|O_WRONLY|O_NONBLOCK,S_IRUSR|S_IWUSR|S_IROTH);
  if (fdout<0) {printf("file %s ",fnout); fflush(stdout); perror("open corrout.dat");}
#else
  char *fnbase="%s.fsall.rebin";
  if (rank == 0) {
  sprintf(fnout,fnbase,argv[3]);
  fdout=open(fnout,O_TRUNC|O_CREAT|O_WRONLY|O_NONBLOCK,S_IRUSR|S_IWUSR|S_IROTH);
  if (fdout<0) {printf("file %s ",fnout); fflush(stdout); perror("open corrout.dat");}
  fnbase="%s.var.rebin";  
  sprintf(fnout,fnbase,argv[3]);
  fdoutvar=open(fnout,O_TRUNC|O_CREAT|O_WRONLY|O_NONBLOCK,S_IRUSR|S_IWUSR|S_IROTH);
  if (fdoutvar<0) {printf("file %s ",fnout); fflush(stdout); perror("open corrout.dat");}
  }
#endif


  char *fnbase2="%s.node%d.fs0";
  sprintf(fnonegate,fnbase2,fn,rank);

  fdonegate=open(fnonegate,O_TRUNC|O_CREAT|O_WRONLY|O_NONBLOCK,S_IRUSR|S_IWUSR|S_IROTH);
  if (fdonegate<0) {printf("file %s ",fnonegate); fflush(stdout); perror("open corrout.dat");}

#ifdef FOLDSIN
  fnbase="%s.node%d.acfold";
  sprintf(fnout,fnbase,fn,rank);   
  fdoutac=open(fnout,O_TRUNC|O_CREAT|O_WRONLY|O_NONBLOCK,S_IRUSR|S_IWUSR|S_IROTH);
  if (fdoutac<0) {printf("file %s ",fnout); fflush(stdout); perror("open corrout.dat");}
#endif


  sprintf(fntimes,"%s/times.dat",basename(fn));
  if (rank==0) printf("fntimes=%s\n",fntimes);
  get_tjd_gst(fntimes, NEPOCH_READ, TJD, GST);
  get_antenna_coords("/mnt/code/gsbuser/EoR/Fringestop_gates64/coords64_feb17_2011.dat", coords, NPOD);

  if (rank==0) printf("infile=%s outfile=%s timefile=%s\n",fnin,fnout,fntimes);

  for (iepoch=0;iepoch<NEPOCH_READ;iepoch++) {
    RA_pt[iepoch] = PNT_RA;
    Dec_pt[iepoch] = PNT_DEC;
  }

  for(ii=0;ii<NGATE;ii++) for (jj=0;jj<NCROSS;jj++) cross[ii][jj]=0;
  for (jj=0;jj<NCROSS;jj++) cross_onegate[0][jj]=0;
  for(is=0;is<NGATE;is++) for (jj=0;jj<NCORR+NPAD;jj++) for (ii=0;ii<FFTLEN/NNODE/2;ii++)   for (re=0;re<2;re++) fcross_cum[is][jj][ii][re]=0;
  for (i=0;i<NCORR*FFTLENOUT/NNODE/2;i++) fcross_var[i] = 0;
  
  
  /*frequency subset of this node */
  fstart=nf*(rank);


   struct CorrHeader head;

   if (read(fdin,&head,sizeof(head)) != sizeof(head)) perror("fringestop: read");
   if (rank == 0 ) {
     if (head.nfold != NFOLD) {
       printf("nfold from file=%d != NFOLD\n",head.nfold);
       exit(-1);
     }
     if (NFOLD != NGATE) printf("using FOLDSIN\n");
   }
   lseek(fdin,0,SEEK_SET);

#if 0
  
  /*subtract the bias */

  for (jj=0;jj<NGATE;jj++) for(ii=0;ii<NCROSS;ii++) cross0[jj][ii]=count_cross0[jj][ii]=0;

  for (iepoch=(tstart);iepoch<(NEPOCH);iepoch++) {

    static char buf[NFOLD][NCROSS];
    float fmax[NCORR];
    int i1;
    
    readbuf2(fdin,buf,fmax,lmap);
    for (jj=0;jj<NGATE;jj++) {
#pragma omp parallel for default(none) private(ii) shared(lmap,fmax,cross0,jj,buf,iepoch,count_cross0)
      for (i1=0;i1<FFTLEN/16/NNODE;i1++) 
	if (lmap[jj][i1]) 
	  for(ii=i1*(NCROSS/FFTLEN)*NNODE*16;ii<(i1+1)*(NCROSS/FFTLEN)*NNODE*16;ii++)  {
	    double tmp=DEBIAS2(buf[jj][ii])*fmax[(ii/16)%NCORR];
	    tmp=(buf[jj][ii]>0)?tmp:-tmp;
	    cross0[jj][ii]+=tmp;//   /(NEPOCH);
	    count_cross0[jj][ii]++;
	  }
    }
  }

  printf("rank %d done bias\n",rank);
  lseek(fdin,0,SEEK_SET);

#endif


  //printf("skipping debias\n");

  /* each timestamp */
  for(iepoch=(tstart);iepoch<(NEPOCH);iepoch++) {
    
    int i1;
    float fmax[NCORR];
    char buf[NFOLD][NCROSS];
    t_index=iepoch+EPOCH_START;

    readbuf2(fdin,buf,fmax,lmap);

#ifdef FOLDSIN
    for (jj=NGATE;jj<NFOLD;jj++){
#pragma omp parallel for default(none) private(ii) shared(lmap,fmax,cross,cross_onegate,jj,buf,cross0) 
      for (i1=0;i1<FFTLEN/16/NNODE;i1++)
	if (lmap[jj][i1])
	  for(ii=i1*(NCROSS/FFTLEN)*NNODE*16;ii<(i1+1)*(NCROSS/FFTLEN)*NNODE*16;ii++)  {
	    int cross_tmp=0;
	    cross_tmp=DEBIAS2(buf[jj][ii])*fmax[(ii/16)%NCORR];
	    cross_tmp=(buf[jj][ii]>0)?cross_tmp:-cross_tmp;
	    cross[jj-NGATE][ii]=cross_tmp;
	  }
    }
    shuffle((NFOLD-NGATE), NCORR, FFTLEN, NPOD, NNODE, fcross,cross);
    dump1byte(fdoutac,fcross,(NFOLD-NGATE)*NCROSS*ONEGATE);
#endif

    for (jj=0;jj<NGATE;jj++){
#pragma omp parallel for default(none) private(ii) shared(lmap,fmax,cross,cross_onegate,jj,buf,cross0,count_cross0) 
      for (i1=0;i1<FFTLEN/16/NNODE;i1++) 
	if (lmap[jj][i1]) 
	  for(ii=i1*(NCROSS/FFTLEN)*NNODE*16;ii<(i1+1)*(NCROSS/FFTLEN)*NNODE*16;ii++)  {
	    int cross_tmp=0;
	    cross_tmp=DEBIAS2(buf[jj][ii])*fmax[(ii/16)%NCORR];
	    cross_tmp=(buf[jj][ii]>0)?cross_tmp:-cross_tmp;
	    /* no de-bias anymore */
	    //	    cross_tmp-=cross0[jj][ii]/count_cross0[jj][ii];
	    cross[jj][ii]=cross_tmp;
	    cross_onegate[0][ii]+=cross_tmp;
	  }
    }
    
    /* convert data from int to float and do transpose to the regular order as in fcross */
    shuffle(NGATE, NCORR, FFTLEN, NPOD, NNODE, fcross,cross);
    shuffle(ONEGATE, NCORR, FFTLEN, NPOD, NNODE, fcross_onegate,cross_onegate);


    /* write out the onegate data in 1 byte*/
    dump1byte(fdonegate,fcross_onegate,NCROSS*ONEGATE);

    /* compute noise on rebinned grid */
    if (iepoch%2==0) {
    	for(i=0;i<NCORR*FFTLEN/NNODE;i++) fcross_tmp[i]=((float *)fcross_onegate)[i];
    } else {
      for (i=0;i<NCORR*FFTLEN/NNODE;i++)
    		fcross_var[i*FFTLENOUT/FFTLEN/2] += SQR(fcross_tmp[i]-((float *)fcross_onegate)[i]);
    }


    //printf("starting fringestop\n");

#if 1
    
    /* fringestop NGATE */
    /* loop over baselines */
#pragma omp parallel for default(none) shared(t_index,coords,fstart,nf,TJD,GST,RA_pt,Dec_pt,fcross,fcross_cum,lmap)
    for(bi=0;bi<NPOD;bi++) {
      int df_index,bindex,igate,bj,i,j;
      double data[NGATE][2*nf];
      double dt, dt_2pi, freq;
      double csphase[2], cs_dphase[2], datacorrect[2], thisdata[2];
      double suppression, dGST_fringe, half_fringe_angle;

      /* auto correlation */
#if 0
      for (igate=0;igate<NGATE;igate++)
	for(df_index=(F_CUTOFF); df_index<(nf-F_CUTOFF); df_index++)
	  for (i=0;i<2;i++)
	    fcross[igate][bindex][df_index][i]=0;
#endif


      for(bj=bi;bj<NPOD;bj++) {
        double csphasetable[nf][2];

	bindex=NPOD*bi-bi*(bi+1)/2+bj;

	/* time delay */
	dt = get_delta_t(RA_pt[t_index]-GST[t_index], Dec_pt[t_index], bi, bj,
			 coords);
	dt_2pi = dt * 2. * M_PI;

	freq = CORR_FREQ0 + CORR_DFREQ*(fstart+F_CUTOFF);
	csphase[0]   = cos(freq*dt_2pi);
	csphase[1]   = sin(freq*dt_2pi);
	cs_dphase[0] = cos(CORR_DFREQ*dt_2pi);
	cs_dphase[1] = sin(CORR_DFREQ*dt_2pi);

#if 1
	dGST_fringe = 0.5*CORR_NBLOCK*CORR_LENGTH*EARTH_OMEGA;
	half_fringe_angle = M_PI*freq*(get_delta_t(PNT_RA-GST[t_index]
						   +dGST_fringe,
						   PNT_DEC,bi, bj, coords)
				       - get_delta_t(PNT_RA-GST[t_index]
						     -dGST_fringe,
						     PNT_DEC, bi, bj, coords));
	suppression = (1.-fabs(dt/CORR_LENGTH)) * SINC(half_fringe_angle);
	csphase[0] /= suppression;
	csphase[1] /= suppression;
#endif

	for(df_index=(F_CUTOFF); df_index<(nf-F_CUTOFF); df_index++) {
	  double temp[2];
	  csphasetable[df_index][0]=csphase[0];
	  csphasetable[df_index][1]=csphase[1];
	  temp[0]=temp[1]=0;
	  C_COPY(temp,csphase);
	  CWFLOP(csphase, temp, cs_dphase);
        }
        /*NGATES*/

	for (igate=0;igate<NGATE;igate++) {
	  for(df_index=(F_CUTOFF); df_index<(nf-F_CUTOFF); df_index++) {
	    if (lmap[igate][df_index/8]){
	      datacorrect[0]=0.;
	      datacorrect[1]=0.;
	      thisdata[0] = fcross[igate][bindex][df_index][0];
	      thisdata[1] = fcross[igate][bindex][df_index][1];
	      CAFLOP(datacorrect, thisdata, csphasetable[df_index]);
	      for (i=0;i<2;i++)
		fcross_cum[igate][bindex][df_index][i]+=datacorrect[i];
	    }
	  }
	}
      }
    }
#endif
    if ((iepoch+1) % NTBIN == 0 ) {
      float mpi_cross[NNODE][NGATE*(NCORR+NPAD)/NNODE][FFTLEN/NNODE];
      float obuf_han_ave[NGATE*(NCORR+NPAD)/NNODE][FFTLENOUT/2][2];
      int i;

      //printf("starting MPI_ALLtoall, NGATE=%d, NPAD=%d, rank=%d\n",NGATE, NPAD,rank);
      ierr=MPI_Alltoall(fcross_cum,NGATE*(NCORR+NPAD)*FFTLEN/NNODE/NNODE,MPI_FLOAT,mpi_cross,NGATE*(NCORR+NPAD)*FFTLEN/NNODE/NNODE,MPI_FLOAT,MPI_COMM_WORLD);
      //	printf("rank %d done mpi alltoall, ierr=%d\n",rank, ierr);
#pragma omp parallel default(none) shared(obuf_han_ave,mpi_cross,rank) private(i)
      {
	/* reorder the matrix so that all frequencies are together */
#pragma omp for 
      for (i=0;i<NGATE*(NCORR+NPAD)/NNODE;i++) {
        float buf[FFTLEN];
	typedef float cfloat[FFTLEN/FFTLENOUT][2];
        cfloat *cbuf;
	int inode,ifreq;
	int j,k,l;
	cbuf=(cfloat *)buf;
	for (j=0;j<FFTLENOUT/2;j++)
	  obuf_han_ave[i][j][0]=obuf_han_ave[i][j][1]=0;
	for (inode=0;inode<NNODE;inode++)
	  for (ifreq=0;ifreq<FFTLEN/NNODE;ifreq++){
	    buf[ifreq+inode*FFTLEN/NNODE]=mpi_cross[inode][i][ifreq];
	  }
      /*initialize obuf_han_ave */
      /* do hanning smoothing here, then do frequency rebin */
	for (k=0;k<FFTLENOUT/2;k++)
	  for (j=0;j<(FFTLEN/FFTLENOUT);j++)
	    for (l=0;l<2;l++)
	    obuf_han_ave[i][k][l]+=(cbuf[k][(k==0)?MAX(0,j-1):j-1][l]*0.25+
				 cbuf[k][j][l]*0.5+
				 cbuf[k][(k==FFTLENOUT/2-1)?MIN(j+1,FFTLEN/FFTLENOUT-1):j+1][l]*0.25)/(FFTLEN/FFTLENOUT);
      }     
      }
#ifdef WRITELOCAL
      /* write out the onegate data in 1 byte*/
      dump1byte(fdout,obuf_han_ave,(NCORR+NPAD)*NGATE*FFTLENOUT/NNODE);
      //      size=write(fdout,obuf,sizeof(float)*(NCORR+NPAD)*NGATE*FFTLENOUT/NNODE);
#else
      static float obufall[NGATE][(NCORR+NPAD)*FFTLENOUT];
      ierr=MPI_Gather(obuf_han_ave,NGATE*(NCORR+NPAD)*FFTLENOUT/NNODE,MPI_FLOAT,obufall,NGATE*(NCORR+NPAD)*FFTLENOUT/NNODE,MPI_FLOAT,FSALLNODE,MPI_COMM_WORLD);
      if (ierr != 0) fprintf(stderr,"mpi_gather: ierr=%d\n",ierr);
      if (rank == FSALLNODE)
	for (i=0;i<NGATE;i++)
	  dump1bytefsall(fdout,obufall[i], NCORR*FFTLENOUT);
	  //  if (write(fdout,obufall[i],sizeof(float)*NCORR*FFTLENOUT)<sizeof(float)*NCORR*FFTLENOUT) perror("write corr");
/* write noise variance */
      static float obufvar[NNODE][NCORR*FFTLENOUT/NNODE/2];
      ierr=MPI_Gather(fcross_var,NCORR*FFTLENOUT/2/NNODE,MPI_FLOAT,obufvar,NCORR*FFTLENOUT/2/NNODE,MPI_FLOAT,FSALLNODE,MPI_COMM_WORLD);
      if (ierr != 0) fprintf(stderr,"mpi_gather: ierr=%d\n",ierr);
      if (rank == FSALLNODE)
      if (write(fdoutvar,obufvar,sizeof(float)*NCORR*FFTLENOUT/2)!=sizeof(float)*NCORR*FFTLENOUT/2) perror("write corr");
      MPI_Barrier(MPI_COMM_WORLD);
      for (i=0;i<NCORR*FFTLENOUT/NNODE/2;i++) fcross_var[i] = 0;
#endif
      
      //      size=write(fdout,fcross_cum,sizeof(float)*NCROSS*NGATE);
      // if (size<sizeof(float)*NCROSS*NGATE) perror("write corr");
      /* write out the onegate data in 1 byte*/
#if  0
      dump2byte(fdout,fcross_cum,NCROSS*NGATE);
#endif

      for(is=0;is<NGATE;is++) for (jj=0;jj<NCORR;jj++) for (ii=0;ii<FFTLEN/NNODE/2;ii++)
	for (re=0;re<2;re++) fcross_cum[is][jj][ii][re]=0;
      if ( (rank == 0) && ( (iepoch+1) % 64 == 0) ) {
	current_time=time(NULL);
	printf("done epoch=%d at %s",iepoch,ctime(&current_time));
      }
    }
  }
#ifdef WRITELOCAL
  size=write(fdout,cross0,sizeof(double)*NCROSS*NGATE);
#else
  static double mpi_cross0[NNODE][NGATE][NCROSS];
  ierr=MPI_Gather(cross0,NGATE*NCROSS,MPI_DOUBLE,mpi_cross0,NGATE*NCROSS,MPI_DOUBLE,0,MPI_COMM_WORLD);
  if (ierr != 0) fprintf(stderr,"mpi_gather: ierr=%d\n",ierr);
  if (rank == 0)
    if (write(fdout,mpi_cross0,sizeof(double)*NGATE*NCROSS*NNODE)<sizeof(double)*NGATE*NCROSS*NNODE) perror("write corr0");
#endif

  close(fdonegate);
  close(fdout);
  
  MPI_Finalize();

  return(0);

}